-
Notifications
You must be signed in to change notification settings - Fork 0
/
webpack.common-config.js
107 lines (96 loc) · 3.1 KB
/
webpack.common-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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* eslint-disable */
const webpack = require('webpack');
const path = require('path');
const camelCase = require('lodash.camelcase');
const mergeWith = require('lodash.mergewith');
const Dashboard = require('webpack-dashboard');
const DashboardPlugin = require('webpack-dashboard/plugin');
const OpenBrowserPlugin = require('open-browser-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const pkg = require(path.join(process.cwd(), 'package.json'));
const nodeModulesPath = path.resolve(__dirname, './node_modules');
const modulesPath = path.resolve(__dirname, './src');
const resolveModulesPath = [modulesPath, nodeModulesPath];
function setloaders() {
return [{
test: /\.js?$/,
use: ['babel-loader'],
exclude: [nodeModulesPath, path.resolve(__dirname,'./src/lib/built-in-class-shim.js')]
}];
}
function setPlugins(isProd, nodeEnv) {
const plugins = [
new webpack.DefinePlugin({
'process.env': { NODE_ENV: JSON.stringify(nodeEnv) }
}),
];
if (!isProd) {
const dashboard = new Dashboard();
plugins.push(
new webpack.HotModuleReplacementPlugin(),
new DashboardPlugin(dashboard.setData),
new OpenBrowserPlugin({
url: 'http://localhost:1818/demo/'
})
);
}
return plugins;
}
function arrayMerge(objValue, srcValue) {
if (typeof objValue !== 'undefined' && Array.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
exports.update = function(inheritedConfig, newConfig) {
return mergeWith(inheritedConfig, newConfig, arrayMerge);
}
exports.inheritedConfig = function(env) {
const nodeEnv = env.NODE_ENV;
const isProd = (nodeEnv === 'production');
const minify = !!env.min;
process.env.NODE_ENV = nodeEnv;
const config = {
devtool: isProd ? 'source-map' : 'cheap-module-source-map',
mode: nodeEnv,
optimization: {
minimizer: (minify) ? [
new UglifyJsPlugin({
uglifyOptions: {
compress: { inline: 1 },
parallel: true
}
})
] : []
},
performance: {
hints: isProd ? 'warning' : false
},
entry: {
[minify ? `${pkg.name}.min` : pkg.name] : './src/index'
},
output: {
path: path.join(__dirname, 'dist'),
filename: '[name].js',
publicPath: 'dist/',
library: camelCase(pkg.name),
libraryTarget: 'umd',
umdNamedDefine: true
},
module: {
rules: setloaders(isProd)
},
resolve: {
modules: resolveModulesPath
},
devServer: {
host: '0.0.0.0',
port: 1818,
hot: true,
quiet: true,
historyApiFallback: true,
contentBase: './'
},
plugins: setPlugins(isProd, nodeEnv)
};
return config;
}