-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathwebpack.config.js
161 lines (146 loc) · 4.78 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
const webpack = require('webpack');
const path = require('path');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const Clean = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ChunkManifestPlugin = require('chunk-manifest-webpack-plugin');
const ScriptExtHtmlWebpackPlugin = require('script-ext-html-webpack-plugin');
const WebpackChunkHash = require('webpack-chunk-hash');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
function addVendorSplitting(plugins) {
return [
new webpack.optimize.CommonsChunkPlugin({
name: 'vendor',
minChunks(module) {
return module.context && module.context.indexOf('node_modules') >= 0;
}
}),
new webpack.optimize.CommonsChunkPlugin({
name: 'manifest',
minChunks: Infinity
}),
new webpack.HashedModuleIdsPlugin(),
new WebpackChunkHash(),
new ChunkManifestPlugin({
filename: 'chunk-manifest.json',
manifestVariable: 'webpackManifest',
inlineManifest: true
}),
...plugins,
new ScriptExtHtmlWebpackPlugin({
inline: 'manifest'
})
];
}
function addProductionPlugins(plugins) {
// https://webpack.js.org/guides/production-build/
return [
...plugins,
new webpack.optimize.UglifyJsPlugin({
beautify: false,
mangle: true,
comments: false
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
];
}
module.exports = (env) => {
const SRC_PATH = 'src';
const SRC_ABSOLUTE_PATH = path.join(__dirname, SRC_PATH);
const INDEX_HTML_TEMPLATE_ABSOLUTE_PATH = path.join(SRC_ABSOLUTE_PATH, 'index.html');
const DIST_PATH = 'dist';
const DIST_ABSOLUTE_PATH = path.join(__dirname, DIST_PATH);
// from documentation: Don’t use [chunkhash] in development since this will increase compilation time
// https://webpack.js.org/guides/caching/
const FILE_PATTERN_DEVELOPMENT = '[name]';
const FILE_PATTERN_PRODUCTION = '[name]-[chunkhash]';
let applicationBundleFilename = `${FILE_PATTERN_DEVELOPMENT}.js`;
let cssBundleFilename = `${FILE_PATTERN_DEVELOPMENT}.css`;
const IS_PRODUCTION = env && env.production;
const IS_ANALYZE = env && env.analyze;
if (IS_PRODUCTION) {
applicationBundleFilename = `${FILE_PATTERN_PRODUCTION}.js`;
cssBundleFilename = `${FILE_PATTERN_PRODUCTION}.css`;
}
let plugins = [
new ExtractTextPlugin({
filename: cssBundleFilename,
disable: false,
allChunks: true
}),
new Clean([DIST_PATH]),
new HtmlWebpackPlugin({
template: INDEX_HTML_TEMPLATE_ABSOLUTE_PATH
})
];
if (IS_ANALYZE) {
plugins = [
new BundleAnalyzerPlugin(),
...plugins
];
}
if (IS_PRODUCTION) {
// don't use it in development to save time on recompile
plugins = addVendorSplitting(plugins);
plugins = addProductionPlugins(plugins);
}
return {
context: SRC_ABSOLUTE_PATH,
entry: './entry',
output: {
path: DIST_ABSOLUTE_PATH,
filename: applicationBundleFilename
},
module: {
// loaders are loaded from bottom to top
rules: [{
test: /\.js$/,
include: SRC_ABSOLUTE_PATH, // other paths are ignored
use: [{
loader: 'babel-loader'
}, {
// ESLint should be before any transpiling tools.
// Or use preLoaders section to check source files, not modified by other loaders (like babel-loader)
loader: 'eslint-loader',
options: {
// treat errors like warnings to not fail the build in development iframe mode
// (http://localhost:8080/webpack-dev-server/)
emitWarning: true
}
}]
}, {
test: /\.scss$/,
include: SRC_ABSOLUTE_PATH, // other paths are ignored
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: `css-loader${IS_PRODUCTION ? '?minimize' : ''}!sass-loader`
})
}, {
test: /\.(jpe?g|png|gif|svg)$/,
include: SRC_ABSOLUTE_PATH, // other paths are ignored
use: 'file-loader'
}]
},
plugins,
// specific settings for webpack-dev-server, see https://webpack.js.org/configuration/dev-server/
devServer: {
// https://github.com/webpack/webpack-dev-server/issues/143
// https://github.com/brikis98/docker-osx-dev
// watchOptions: {
// poll: true,
// },
contentBase: DIST_PATH,
host: '0.0.0.0',
// proxy requests to the backend
// TODO: this setting doesn't work with 'historyApiFallback: true'
// proxy: {
// '*': 'http://localhost'
// },
// this setting is needed to support react-router
// TODO: this setting doesn't work with 'proxy'
historyApiFallback: true
}
};
};