Skip to content

Commit fa9d5c2

Browse files
committed
feat: support array-configs
1 parent ae3ec9b commit fa9d5c2

File tree

9 files changed

+125
-70
lines changed

9 files changed

+125
-70
lines changed

.eslintignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ test/**/cliEntry.js
1111
test/**/foo.js
1212
test/**/**/**/bin/
1313
test/**/**/**/binary/
14+
test/**/dist/
15+
test/**/**/dist/
16+
test/**/**/**/dist/
1417
test/binCases/config-location/webpack-babel-config/bin/es6.js
1518
packages/**/*.js
1619
packages/utils/validate-identifier.ts
1720

1821
# TODO removeme
1922

20-
lib
23+
lib
24+
testy

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ test/**/bin/
2828
test/**/**/bin/
2929
test/**/**/**/bin/
3030
test/config/**/binary/**
31+
test/**/dist
32+
test/**/**/dist
33+
test/**/**/**/dist
3134
# lerna log
3235
lerna-debug.log
3336

lib/bootstrap.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,9 @@ async function runCLI(cli, commandIsUsed) {
6868
const newArgKeys = Object.keys(argsMap).filter(arg => !keysToDelete.includes(argsMap[arg].pos));
6969
process.argv = newArgKeys;
7070
args = cmdArgs(core, { stopAtFirstUnknown: false, partial: true });
71-
const result = await cli.run(args, core);
72-
if (!result) {
73-
return;
74-
}
71+
72+
await cli.run(args, core);
73+
console.log('\n')
7574
process.cliLogger.warn(`duplicate flags found, defaulting to last set value`);
7675
}
7776
else {

lib/groups/config.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,7 @@ class ConfigGroup extends GroupHelper {
101101
configOptions = resolvedConfigurationFiles;
102102
}
103103
if (configOptions.length > 0) {
104-
const merge = require('webpack-merge');
105-
this.opts['options'] = configOptions.reduce( (prev, curr) => {
106-
return merge(prev, curr);
107-
}, {});
104+
this.opts['options'] = configOptions;
108105
}
109106
else if(typeof configOptions === 'function') {
110107
const newOptions = await configOptions();

lib/utils/compiler.js

Lines changed: 67 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,72 @@ const fs = require('fs');
33
const chalk = require('chalk');
44
const Table = require('cli-table3');
55

6+
7+
function setUpHookForCompiler(compiler, outputOptions, options) {
8+
compiler.hooks.beforeRun.tap('webpackProgress', compilation => {
9+
if (outputOptions.progress) {
10+
let tmp_msg;
11+
const defaultProgressPluginHandler = (percent, msg, addInfo) => {
12+
percent = Math.floor(percent * 100);
13+
if (percent === 100) {
14+
msg = 'Compilation completed';
15+
}
16+
17+
if (msg && tmp_msg != msg) {
18+
process.cliLogger.info(percent + '% ' + msg);
19+
}
20+
tmp_msg = msg;
21+
};
22+
if (!progressPluginExists) {
23+
new ProgressPlugin(defaultProgressPluginHandler).apply(compiler);
24+
} else {
25+
if (!progressPluginExists.handler) {
26+
options.plugins = options.plugins.filter(e => e !== progressPluginExists);
27+
Object.keys(progressPluginExists).map(opt => {
28+
ProgressPlugin.defaultOptions[opt] = progressPluginExists[opt];
29+
});
30+
new ProgressPlugin(defaultProgressPluginHandler).apply(compiler);
31+
} else {
32+
progressPluginExists.apply(compiler);
33+
}
34+
}
35+
}
36+
});
37+
38+
if (outputOptions.infoVerbosity === 'verbose') {
39+
if (outputOptions.watch) {
40+
compiler.hooks.watchRun.tap('WebpackInfo', compilation => {
41+
const compilationName = compilation.name ? compilation.name : '';
42+
process.cliLogger.info('Compilation ' + compilationName + ' starting…');
43+
});
44+
} else {
45+
compiler.hooks.beforeRun.tap('WebpackInfo', compilation => {
46+
const compilationName = compilation.name ? compilation.name : '';
47+
process.cliLogger.info('Compilation ' + compilationName + ' starting…');
48+
});
49+
}
50+
compiler.hooks.done.tap('WebpackInfo', compilation => {
51+
const compilationName = compilation.name ? compilation.name : '';
52+
process.cliLogger.info('Compilation ' + compilationName + ' finished');
53+
});
54+
}
55+
}
56+
657
function showEmojiConditionally() {
758
return (process.stdout.isTTY && process.platform === 'darwin')
859
}
960

1061
function generateOutput(outputOptions, stats, statsErrors) {
1162
const statsObj = stats.toJson(outputOptions);
63+
if(statsObj.children && statsObj.children.length) {
64+
statsObj.children.forEach(child => {
65+
generateOutputForSingleCompilation(child, statsErrors);
66+
});
67+
return;
68+
}
69+
generateOutputForSingleCompilation(statsObj, statsErrors);
70+
}
71+
function generateOutputForSingleCompilation(statsObj, statsErrors) {
1272
const { assets, entrypoints, time, builtAt, warnings, outputPath } = statsObj;
1373

1474
const visibleEmojies = showEmojiConditionally() ? ['✅', '🌏', '⚒️ ', '⏱ ', '📂']: new Array(5).fill('');
@@ -140,54 +200,14 @@ async function webpackInstance(opts, shouldUseMem) {
140200
const interactive = require('./interactive');
141201
return interactive(options, outputOptions, processingErrors, shouldUseMem);
142202
}
143-
144-
compiler.hooks.beforeRun.tap('webpackProgress', compilation => {
145-
if (outputOptions.progress) {
146-
let tmp_msg;
147-
const defaultProgressPluginHandler = (percent, msg, addInfo) => {
148-
percent = Math.floor(percent * 100);
149-
if (percent === 100) {
150-
msg = 'Compilation completed';
151-
}
152-
153-
if (msg && tmp_msg != msg) {
154-
process.cliLogger.info(percent + '% ' + msg);
155-
}
156-
tmp_msg = msg;
157-
};
158-
if (!progressPluginExists) {
159-
new ProgressPlugin(defaultProgressPluginHandler).apply(compiler);
160-
} else {
161-
if (!progressPluginExists.handler) {
162-
options.plugins = options.plugins.filter(e => e !== progressPluginExists);
163-
Object.keys(progressPluginExists).map(opt => {
164-
ProgressPlugin.defaultOptions[opt] = progressPluginExists[opt];
165-
});
166-
new ProgressPlugin(defaultProgressPluginHandler).apply(compiler);
167-
} else {
168-
progressPluginExists.apply(compiler);
169-
}
170-
}
171-
}
172-
});
173-
174-
if (outputOptions.infoVerbosity === 'verbose') {
175-
if (outputOptions.watch) {
176-
compiler.hooks.watchRun.tap('WebpackInfo', compilation => {
177-
const compilationName = compilation.name ? compilation.name : '';
178-
process.cliLogger.info('Compilation ' + compilationName + ' starting…');
179-
});
180-
} else {
181-
compiler.hooks.beforeRun.tap('WebpackInfo', compilation => {
182-
const compilationName = compilation.name ? compilation.name : '';
183-
process.cliLogger.info('Compilation ' + compilationName + ' starting…');
184-
});
185-
}
186-
compiler.hooks.done.tap('WebpackInfo', compilation => {
187-
const compilationName = compilation.name ? compilation.name : '';
188-
process.cliLogger.info('Compilation ' + compilationName + ' finished');
189-
});
203+
if(compiler.compilers) {
204+
compiler.compilers.forEach((comp, idx) => {
205+
setUpHookForCompiler(comp, outputOptions, options[idx]);
206+
})
207+
} else {
208+
setUpHookForCompiler(compiler, outputOptions, options);
190209
}
210+
191211
if (outputOptions.watch) {
192212
const watchOptions = outputOptions.watchOptions || {};
193213
if (watchOptions.stdin) {

lib/utils/zero-config.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
module.exports = function(groupResult, isDevMode) {
1+
function mergeObjectOrArrayConfig(defaults, config) {
2+
const merge = require('webpack-merge');
3+
if(Array.isArray(config)) {
4+
return config.map(arrayObject => merge(defaults, arrayObject));
5+
}
6+
return merge(defaults, config);
7+
}
8+
9+
module.exports = function(webpackObject, isDevMode) {
210
if (!isDevMode) {
311
const prodConfig = require('./prod-config');
4-
groupResult.options = require('webpack-merge')(prodConfig, groupResult.options);
12+
webpackObject.options = mergeObjectOrArrayConfig(prodConfig, webpackObject.options);
513
} else {
614
const devConfig = require('./dev-config');
7-
groupResult.options = require('webpack-merge')(devConfig, groupResult.options);
15+
webpackObject.options = mergeObjectOrArrayConfig(devConfig, webpackObject.options);
816
}
9-
return groupResult;
17+
return webpackObject;
1018
};

lib/webpack-cli.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class WebpackCLI extends GroupHelper {
3333

3434
checkDefaults(groupResult) {
3535
const { options } = groupResult;
36+
// TODO: check defaults for array configs as well
3637
if (options.entry && options.entry === './index.js') {
3738
const normalizedEntry = resolve(options.entry);
3839
if (!existsSync(normalizedEntry)) {
@@ -97,9 +98,23 @@ class WebpackCLI extends GroupHelper {
9798
}
9899

99100
groupResult.outputOptions = Object.assign(groupObject.outputOptions, e.outputOptions);
100-
101+
if(Array.isArray(e.options)) {
102+
const defaultArrayOptions = e.options.map(arrayConfigObject => {
103+
return webpackMerge(groupResult.options, arrayConfigObject);
104+
})
105+
groupResult.options = defaultArrayOptions;
106+
return;
107+
}
108+
if(Array.isArray(groupResult.options)) {
109+
const defaultArrayOptions = groupResult.options.map(arrayConfigObject => {
110+
return webpackMerge(e.options, arrayConfigObject);
111+
})
112+
groupResult.options = defaultArrayOptions;
113+
return;
114+
}
101115
groupResult.options = webpackMerge(groupResult.options, e.options);
102116
});
117+
// TODO: Arrays needs to be searched for this first then default back to cli
103118
const isDevMode = groupResult.outputOptions['dev'];
104119
const wrappedConfig = require('./utils/zero-config')(groupResult, isDevMode);
105120
return wrappedConfig;

test/config/type/array/array-config.test.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,15 @@ describe('array configuration', () => {
77
it('is able to understand a configuration file in array format', done => {
88
const { stdout } = run(__dirname, ['-c', resolve(__dirname, 'webpack.config.js')], false);
99
const summary = extractSummary(stdout);
10-
const outputDir = 'type/array/binary';
10+
const outputDir = 'type/array/dist';
1111
const outDirectoryFromCompiler = summary['Output Directory'].split(sep);
1212
const outDirToMatch = outDirectoryFromCompiler.slice(outDirectoryFromCompiler.length - 3, outDirectoryFromCompiler.length).join('/');
1313
expect(outDirToMatch).toContain(outputDir);
14-
stat(resolve(__dirname, './binary/chunk_norris.js'), (err, stats) => {
14+
stat(resolve(__dirname, './dist/dist-commonjs.js'), (err, stats) => {
15+
expect(err).toBe(null);
16+
expect(stats.isFile()).toBe(true);
17+
});
18+
stat(resolve(__dirname, './dist/dist-amd.js'), (err, stats) => {
1519
expect(err).toBe(null);
1620
expect(stats.isFile()).toBe(true);
1721
done();
Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
module.exports = [
22
{
3-
entry: './a',
43
output: {
5-
path: __dirname + '/binary',
6-
filename: 'coconut.js',
4+
filename: './dist-amd.js',
5+
libraryTarget: 'amd',
76
},
7+
name: 'amd',
8+
entry: './a.js',
9+
mode: 'production',
10+
devtool: 'eval-module',
811
},
912
{
1013
output: {
11-
chunkFilename: 'chunk_norris.js',
12-
},
13-
optimization: {
14-
runtimeChunk: true,
14+
filename: './dist-commonjs.js',
15+
libraryTarget: 'commonjs',
1516
},
17+
name: 'commonjs',
18+
entry: './a.js',
19+
mode: 'production',
20+
target: 'node',
1621
},
1722
];

0 commit comments

Comments
 (0)