-
-
Notifications
You must be signed in to change notification settings - Fork 594
/
ConfigGroup.js
189 lines (166 loc) · 6.4 KB
/
ConfigGroup.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const { existsSync } = require('fs');
const { resolve, sep, dirname, parse } = require('path');
const webpackMerge = require('webpack-merge');
const { extensions, jsVariants } = require('interpret');
const GroupHelper = require('../utils/GroupHelper');
const rechoir = require('rechoir');
const MergeError = require('../utils/errors/MergeError');
// Order defines the priority, in increasing order
// example - config file lookup will be in order of .webpack/webpack.config.development.js -> webpack.config.development.js -> webpack.config.js
const DEFAULT_CONFIG_LOC = [
'webpack.config',
'webpack.config.dev',
'webpack.config.development',
'webpack.config.prod',
'webpack.config.production',
'.webpack/webpack.config',
'.webpack/webpack.config.none',
'.webpack/webpack.config.dev',
'.webpack/webpack.config.development',
'.webpack/webpack.config.prod',
'.webpack/webpack.config.production',
'.webpack/webpackfile',
];
const modeAlias = {
production: 'prod',
development: 'dev',
};
const getDefaultConfigFiles = () => {
return DEFAULT_CONFIG_LOC.map((filename) => {
return Object.keys(extensions).map((ext) => {
return {
path: resolve(filename + ext),
ext: ext,
module: extensions[ext],
};
});
}).reduce((a, i) => a.concat(i), []);
};
const getConfigInfoFromFileName = (filename) => {
const fileMetaData = parse(filename);
return Object.keys(extensions)
.filter((ext) => ext.includes(fileMetaData.ext))
.filter((ext) => fileMetaData.base.substr(fileMetaData.base.length - ext.length) === ext)
.map((ext) => {
return {
path: resolve(filename),
ext: ext,
module: extensions[ext],
};
})
.filter((e) => existsSync(e.path));
};
class ConfigGroup extends GroupHelper {
constructor(options) {
super(options);
}
requireLoader(extension, path) {
rechoir.prepare(extensions, path, process.cwd());
}
requireConfig(configModule) {
const extension = Object.keys(jsVariants).find((t) => configModule.ext.endsWith(t));
if (extension) {
this.requireLoader(extension, configModule.path);
}
let config = require(configModule.path);
if (config.default) {
config = config.default;
}
return {
content: config,
path: configModule.path,
};
}
async finalize(moduleObj) {
const newOptionsObject = {
outputOptions: {},
options: {},
processingMessageBuffer: [],
};
if (!moduleObj) {
return newOptionsObject;
}
const configPath = moduleObj.path;
const configOptions = moduleObj.content;
if (typeof configOptions === 'function') {
// when config is a function, pass the env from args to the config function
const newOptions = configOptions(this.args.env);
// When config function returns a promise, resolve it, if not it's resolved by default
newOptionsObject['options'] = await Promise.resolve(newOptions);
} else {
if (Array.isArray(configOptions) && !configOptions.length) {
newOptionsObject['options'] = {};
return newOptionsObject;
}
newOptionsObject['options'] = configOptions;
}
if (configOptions && configPath.includes('.webpack')) {
const currentPath = configPath;
const parentContext = dirname(currentPath).split(sep).slice(0, -1).join(sep);
if (Array.isArray(configOptions)) {
configOptions.forEach((config) => {
config.context = config.context || parentContext;
});
} else {
configOptions.context = configOptions.context || parentContext;
}
newOptionsObject['options'] = configOptions;
}
return newOptionsObject;
}
async resolveConfigFiles() {
const { config, mode } = this.args;
if (config) {
const configPath = resolve(process.cwd(), config);
const configFiles = getConfigInfoFromFileName(configPath);
if (!configFiles.length) {
this.opts.processingMessageBuffer.push({
lvl: 'warn',
msg: `Configuration ${config} not found in ${configPath}`,
});
return;
}
const foundConfig = configFiles[0];
const resolvedConfig = this.requireConfig(foundConfig);
this.opts = await this.finalize(resolvedConfig);
return;
}
const defaultConfigFiles = getDefaultConfigFiles();
const tmpConfigFiles = defaultConfigFiles.filter((file) => {
return existsSync(file.path);
});
const configFiles = tmpConfigFiles.map(this.requireConfig.bind(this));
if (configFiles.length) {
const defaultConfig = configFiles.find((p) => p.path.includes(mode) || p.path.includes(modeAlias[mode]));
if (defaultConfig) {
this.opts = await this.finalize(defaultConfig);
return;
}
const foundConfig = configFiles.pop();
this.opts = await this.finalize(foundConfig);
return;
}
}
async resolveConfigMerging() {
// eslint-disable-next-line no-prototype-builtins
if (Object.keys(this.args).some((arg) => arg === 'merge')) {
const { merge } = this.args;
// try resolving merge config
const newConfigPath = this.resolveFilePath(merge);
if (!newConfigPath) {
throw new MergeError("The supplied merge config doesn't exist.");
}
const configFiles = getConfigInfoFromFileName(newConfigPath);
const foundConfig = configFiles[0];
const resolvedConfig = this.requireConfig(foundConfig);
const newConfigurationsObject = await this.finalize(resolvedConfig);
this.opts['options'] = webpackMerge(this.opts['options'], newConfigurationsObject.options);
}
}
async run() {
await this.resolveConfigFiles();
await this.resolveConfigMerging();
return this.opts;
}
}
module.exports = ConfigGroup;