This repository has been archived by the owner on Sep 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.js
107 lines (95 loc) · 2.94 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/* eslint no-console: off */
const path = require("path");
const webpack = require("webpack");
const CompressionPlugin = require('compression-webpack-plugin');
const utils = require('./server/utils');
/* Webpack config generator */
const generateConfig = ({devMode=false, analyzeBundle=false}) => {
utils.verbose(`Generating webpack config. devMode: ${devMode}`);
/* which directories should be parsed by babel and other loaders? */
const directoriesToTransform = [path.join(__dirname, 'src')];
/* plugins */
/* plugin: inject strings into the client-accessible process.env */
const pluginProcessEnvData = new webpack.DefinePlugin({
"process.env": {
NODE_ENV: devMode ? JSON.stringify("development") : JSON.stringify("production")
}
});
/* plugin: gzip everything - https://github.com/webpack-contrib/compression-webpack-plugin */
const pluginCompress = new CompressionPlugin({
filename: "[path].gz[query]",
algorithm: "gzip",
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8
});
const plugins = devMode ? [
new webpack.HotModuleReplacementPlugin(),
pluginProcessEnvData,
new webpack.NoEmitOnErrorsPlugin()
] : [
pluginProcessEnvData,
new webpack.optimize.AggressiveMergingPlugin(), // merge chunks - https://github.com/webpack/docs/wiki/list-of-plugins#aggressivemergingplugin
pluginCompress
];
if (analyzeBundle) {
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin; // eslint-disable-line
plugins.push(new BundleAnalyzerPlugin());
}
const entry = [
"babel-polyfill",
"./src/index"
];
if (devMode) {
entry.splice(1, 0, "webpack-hot-middleware/client");
}
/* Where do we want the output to be saved?
* For development we use the (virtual) "devel" directory
* Else we must choose to save it in the CWD or the source
*/
const outputPath = devMode ?
path.resolve(__dirname, "devel") : // development: use the (virtual) "devel" directory
path.resolve(__dirname, "dist");
utils.verbose(`Webpack writing output to: ${outputPath}`);
const config = {
mode: devMode ? 'development' : 'production',
context: __dirname,
entry,
output: {
path: outputPath,
filename: "bundle.js",
publicPath: "/dist/"
},
node: {
fs: 'empty'
},
plugins,
optimization: {
minimize: !devMode
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
include: directoriesToTransform,
options: {
cwd: path.resolve(__dirname)
}
},
{
test: /\.css$/,
use: ["style-loader", "css-loader"]
},
{
test: /\.(gif|png|jpe?g|svg)$/i,
use: "file-loader",
include: directoriesToTransform
}
]
}
};
config.devtool = 'cheap-module-source-map';
return config;
};
module.exports = {default: generateConfig};