Skip to content
This repository was archived by the owner on Feb 7, 2022. It is now read-only.

Commit 2c50d31

Browse files
Oleg Andreyevefegurkan
authored andcommitted
Enable stats presets as string
- Inject Stats.presetToOptions to parse { stats: String } and gather options object. - Write unit test.
1 parent 86914e3 commit 2c50d31

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

src/__tests__/webpackWorker.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@ const watch = jest.fn().mockReturnValue({ close });
44

55
jest.setMock('webpack', () => ({ run, watch })); // try to get rid of this.
66
jest.mock('../watchModeIPC');
7+
jest.mock('webpack/lib/Stats');
78

89
let webpackWorker;
910
let promiseMock;
1011
let webpackMock;
12+
let webpackStatsMock;
1113
let notifyIPCWatchCompileDone;
1214

1315
describe('webpackWorker', () => {
1416
beforeEach(() => {
1517
promiseMock = require('bluebird');
1618
webpackMock = require('webpack');
19+
webpackStatsMock = require('webpack/lib/Stats');
1720
webpackWorker = require('../webpackWorker.js');
1821
notifyIPCWatchCompileDone = require('../watchModeIPC').notifyIPCWatchCompileDone;
1922
jest.doMock('testConfig', () => ({ webpack: 'config' }), { virtual: true });
2023
jest.resetModules();
2124
jest.clearAllMocks();
25+
process.removeAllListeners();
2226
});
2327

2428
describe('arguments', () => {
@@ -268,6 +272,28 @@ describe('webpackWorker', () => {
268272
finishedCallback(null, statsObj);
269273
expect(statsObj.toString.mock.calls).toMatchSnapshot();
270274
});
275+
276+
it('should translate stats string to object', () => {
277+
jest.spyOn(console, 'log');
278+
let presetToOptions = jest.spyOn(webpackStatsMock, 'presetToOptions');
279+
280+
const doneCallback = jest.fn();
281+
282+
webpackWorker('testConfig', {
283+
stats: true,
284+
modulesSort: 'name',
285+
chunksSort: 'size',
286+
assetsSort: 'name',
287+
exclude: ['file'],
288+
colors: true
289+
}, 0, 1, doneCallback);
290+
291+
expect(promiseMock.resolve.mock.calls[0][0]).toEqual({ webpack: 'config' });
292+
const thenCb = promiseMock.then.mock.calls[0][0];
293+
thenCb({ webpack: 'config', name: 'testApp', 'stats': 'verbose' });
294+
295+
expect(presetToOptions).toHaveBeenCalled();
296+
})
271297
});
272298
});
273299

src/webpackWorker.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
var Promise = require('bluebird'),
22
chalk = require('chalk'),
33
loadConfigurationFile = require('./loadConfigurationFile').default,
4-
notifyIPCWatchCompileDone = require('./watchModeIPC').notifyIPCWatchCompileDone;
4+
notifyIPCWatchCompileDone = require('./watchModeIPC').notifyIPCWatchCompileDone,
5+
presetToOptions = require('webpack/lib/Stats').presetToOptions;
56
/**
67
* Choose the most correct version of webpack, prefer locally installed version,
78
* fallback to the own dependency if there's none.
@@ -28,7 +29,12 @@ function getAppName(webpackConfig) {
2829
}
2930

3031
function getOutputOptions(webpackConfig, options) {
31-
var outputOptions = Object.create(webpackConfig.stats || {});
32+
var stats = webpackConfig.stats;
33+
// @see https://webpack.js.org/configuration/stats/
34+
if (typeof stats === 'string') {
35+
stats = presetToOptions(stats);
36+
}
37+
var outputOptions = Object.create(stats || {});
3238
if(typeof options.modulesSort !== 'undefined') {
3339
outputOptions.modulesSort = options.modulesSort;
3440
}

0 commit comments

Comments
 (0)