-
Notifications
You must be signed in to change notification settings - Fork 21
/
webpackHelpers.ts
41 lines (35 loc) · 1.16 KB
/
webpackHelpers.ts
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
import path from 'path';
import interpret from 'interpret';
import rechoir from 'rechoir';
import findup from 'findup-sync';
import webpack from 'webpack';
export const defaultWebpackConfigPath = (cwd: string) => {
const extensions = Object.keys(interpret.extensions);
const defaultConfigFileNames = ['webpack.config', 'webpackfile'];
const configFileRegExp = `(${defaultConfigFileNames.join(
'|',
)})(${extensions.join('|')})`;
const configPath =
findup(configFileRegExp, {
cwd,
}) || path.join(cwd, 'webpack.config.js');
return configPath;
};
export const webpackConfig = (cwd: string, configPath?: string) => {
const resolvedConfigPath = (() => {
if (!configPath) {
return defaultWebpackConfigPath(cwd);
}
if (path.isAbsolute(configPath)) {
return path.resolve(configPath);
}
return path.resolve(cwd, configPath);
})();
// register module loaders
rechoir.prepare(interpret.extensions, resolvedConfigPath);
const config: {
default: webpack.Configuration;
// eslint-disable-next-line global-require, import/no-dynamic-require
} = require(resolvedConfigPath);
return config.default || config;
};