-
Notifications
You must be signed in to change notification settings - Fork 8
/
webpack.config.js
92 lines (88 loc) · 2.58 KB
/
webpack.config.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
import '@babel/polyfill';
import CircularJSON from 'circular-json';
import _ from 'lodash';
import fs from 'fs-extra';
import log, { setLevel } from '@reactant/core/log';
import path from 'path';
import pkgDir from 'pkg-dir';
import resolve from '@reactant/core/resolve';
import { createWebpackConfig } from '@reactant/cli/webpack';
import { rebuildConfig } from '@reactant/cli/config';
const rootPath = pkgDir.sync(process.cwd());
const mergeConfiguration = require(require.resolve('merge-configuration', {
paths: [path.resolve(rootPath, 'node_modules/@reactant/web')]
})).default;
let debug = false;
if (_.includes(process.argv, '--verbose')) setLevel('verbose');
if (_.includes(process.argv, '--debug')) {
setLevel('debug');
debug = true;
}
module.exports = webpackConfig => {
const { config, platform } = rebuildConfig({
options: { platform: 'web', debug }
});
const { paths, babel, options } = config;
webpackConfig = createWebpackConfig(config, { webpackConfig, platform });
webpackConfig.resolve.extensions.unshift('.web.js');
webpackConfig.externals = {
...webpackConfig.externals,
child_process: {},
deasync: {},
fs: {},
winston: {}
};
webpackConfig = replaceBabelRule(webpackConfig, {
test: /\.(js|jsx|mjs)$/,
include: [
path.resolve(rootPath, paths.src),
path.resolve(rootPath, paths.stories),
...getModuleIncludes(['react-navigation', 'static-container'])
],
loader: resolve('babel-loader', __dirname),
options: babel
});
webpackConfig = mergeConfiguration(
webpackConfig,
config.storybook,
{},
config
);
if (options.debug) {
fs.mkdirsSync(path.resolve(rootPath, paths.debug));
fs.writeFileSync(
path.resolve(
path.resolve(rootPath, paths.debug),
'webpack.storybook.json'
),
CircularJSON.stringify(webpackConfig, null, 2)
);
}
log.debug('webpackConfig', webpackConfig);
return webpackConfig;
};
function getModuleIncludes(modules) {
const includes = [];
modules.forEach(module => {
try {
const modulePath = pkgDir.sync(
require.resolve(path.resolve(rootPath, 'node_modules', module))
);
includes.push(modulePath);
} catch (err) {}
});
return includes;
}
function replaceBabelRule(webpackConfig, rule) {
const babelRule = _.find(webpackConfig.module.rules, rule => {
return !!_.find(
_.isArray(rule.use) ? rule.use : [],
rule => rule.loader.indexOf('babel-loader') > -1
);
});
_.each(_.keys(babelRule), key => {
delete babelRule[key];
});
_.assign(babelRule, rule);
return webpackConfig;
}