-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
ZeroConfigGroup.js
45 lines (39 loc) · 1.43 KB
/
ZeroConfigGroup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
const GroupHelper = require('../utils/GroupHelper');
/**
* ZeroConfigGroup creates a zero configuration based on the environment
*/
class ZeroConfigGroup extends GroupHelper {
constructor(options) {
super(options);
}
/**
* It determines the mode to pass to webpack compiler
* @returns {string} The mode
*/
getEnvFromOptionsAndMode() {
if (process.env.NODE_ENV && (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'development')) {
return process.env.NODE_ENV;
} else if (this.args.prod) {
return 'production';
} else if (this.args.dev) {
return 'development';
}
return 'production';
}
resolveZeroConfig() {
const defaultConfigType = this.getEnvFromOptionsAndMode();
const defaultConfig = require(`./${defaultConfigType}-config`)();
const isEntryObject = defaultConfig.entry && defaultConfig.entry instanceof Object;
const isOutputDefined = defaultConfig.output && defaultConfig.output.filename;
const isConflictingOutput = isEntryObject && isOutputDefined && defaultConfig.output.filename === 'bundle.js';
if (isConflictingOutput) {
defaultConfig.output.filename = '[name].bundle.js';
}
this.opts.options = defaultConfig;
}
run() {
this.resolveZeroConfig();
return this.opts;
}
}
module.exports = ZeroConfigGroup;