This repository has been archived by the owner on Aug 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
webpack.config.js
78 lines (71 loc) · 1.83 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
/* eslint-env node */
const _ = require('lodash');
const path = require('path');
const webpack = require('webpack');
const merge = require('webpack-merge');
const externals = require('webpack-node-externals');
const PeerDepsExternalsPlugin = require('peer-deps-externals-webpack-plugin');
const { DefinePlugin, IgnorePlugin } = webpack;
function stringify(data) {
return _.reduce(data, (config, value, key) => ({
...config, [key]: JSON.stringify(value),
}), {});
}
const baseConfig = environment => ({
entry: path.join(__dirname, 'src/index.js'),
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: [{
loader: 'babel-loader',
options: {
cacheDirectory: true,
rootMode: 'upward',
},
}],
}],
},
plugins: [
new DefinePlugin(stringify({
'process.env.NODE_ENV': environment,
})),
],
});
const testConfig = {
mode: 'none',
target: 'node',
devtool: 'inline-cheap-module-source-map',
externals: [externals()],
plugins: [
new IgnorePlugin(/react\/addons/),
new IgnorePlugin(/react\/lib\/ReactContext/),
new IgnorePlugin(/react\/lib\/ExecutionEnvironment/),
],
};
const commonJSConfig = {
mode: 'production',
output: {
filename: 'index.js',
libraryTarget: 'commonjs',
path: path.resolve(__dirname, 'dist/cjs'),
},
plugins: [
new PeerDepsExternalsPlugin(),
],
};
module.exports = (env) => {
// eslint-disable-next-line no-console
console.log(`Running Webpack build for '${env}'`);
switch (env) {
case 'test':
return merge.smart(baseConfig('test'), testConfig);
case 'cjs':
return merge.smart(baseConfig('production'), commonJSConfig);
default:
throw new Error(`Invalid Webpack environment: '${env}'`);
}
};