-
Notifications
You must be signed in to change notification settings - Fork 7
/
webpack.config.js
238 lines (216 loc) · 7.29 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
const webpack = require('webpack');
const {join} = 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;
const BIN_FILE_TYPES = require('./bin-file-types');
function getVendorSplittingPlugins(defaultPlugins) {
// https://webpack.js.org/guides/caching/
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
}),
...defaultPlugins,
new ScriptExtHtmlWebpackPlugin({
inline: 'manifest'
})
];
}
function getProductionPlugins(defaultPlugins, isAnalyze) {
// don't use it in development to save time on recompile
// https://webpack.js.org/guides/production-build/
let plugins = [
...getVendorSplittingPlugins(defaultPlugins),
// looks buggy, doesn't update [chunkhash].
// TODO: uncomment when migrate to webpack 3
// this https://github.com/webpack/webpack/issues/5184 is supposed to fix the problem. But it looks it doesn't.
// // https://medium.com/webpack/webpack-3-official-release-15fd2dd8f07b:
// new webpack.optimize.ModuleConcatenationPlugin(),
new webpack.optimize.UglifyJsPlugin({
beautify: false,
mangle: true,
comments: false
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production')
})
];
if (isAnalyze) {
plugins = [
...plugins,
new BundleAnalyzerPlugin()
];
}
return plugins;
}
function getDevelopmentPlugins(defaultPlugins) {
return defaultPlugins;
}
function getPlugins(defaultPlugins, isProduction, isAnalyze) {
return isProduction ?
getProductionPlugins(defaultPlugins, isAnalyze) :
getDevelopmentPlugins(defaultPlugins);
}
module.exports = (env) => {
const SRC_PATH = 'src';
const SRC_ABSOLUTE_PATH = join(__dirname, SRC_PATH);
const INDEX_HTML_TEMPLATE_ABSOLUTE_PATH = join(SRC_ABSOLUTE_PATH, 'index.html');
const DIST_ROOT = __dirname;
const DIST_PATH = 'dist';
const DIST_ABSOLUTE_PATH = join(DIST_ROOT, 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`;
}
const DEFAULT_PLUGINS = [
new ExtractTextPlugin({
filename: cssBundleFilename,
disable: false,
allChunks: true
}),
new Clean([DIST_PATH], {
root: DIST_ROOT
}),
new HtmlWebpackPlugin({
template: INDEX_HTML_TEMPLATE_ABSOLUTE_PATH
})
];
const CSS_MODULES_CLASS_NAME_TEMPLATE = '[local]-[hash:base64:5]';
const CSS_MODULES_CONFIG = {
modules: true,
sourceMap: false,
importLoaders: 1,
localIdentName: CSS_MODULES_CLASS_NAME_TEMPLATE
};
const plugins = getPlugins(DEFAULT_PLUGINS, IS_PRODUCTION, IS_ANALYZE);
return {
context: SRC_ABSOLUTE_PATH,
entry: './entry',
output: {
path: DIST_ABSOLUTE_PATH,
publicPath: '/', // uncomment this to load the bundle from site root (useful with react-router)
filename: applicationBundleFilename
},
resolve: {
modules: [join(__dirname, 'src'), 'node_modules']
},
module: {
// loaders are loaded from bottom to top
rules: [{
test: /\.js$/,
include: SRC_ABSOLUTE_PATH, // other paths are ignored
use: [{
loader: 'babel-loader',
query: {
plugins: [[
// this plugin is to be able to write styles in React components like styleName='button'
// instead of className={styles.button}. The first variant is more convenient for markupers.
'react-css-modules', {
context: SRC_ABSOLUTE_PATH,
generateScopedName: CSS_MODULES_CLASS_NAME_TEMPLATE
}
]]
}
}, {
// 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: /\.css$/,
include: SRC_ABSOLUTE_PATH, // other paths are ignored
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: {
loader: 'css-loader',
options: {
...CSS_MODULES_CONFIG,
minimize: IS_PRODUCTION
}
}
})
}, {
test: /\.scss$/,
include: SRC_ABSOLUTE_PATH, // other paths are ignored
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
minimize: IS_PRODUCTION
}
}, 'sass-loader']
})
}, {
test: /\.sass$/,
include: SRC_ABSOLUTE_PATH, // other paths are ignored
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: [{
loader: 'css-loader',
options: {
...CSS_MODULES_CONFIG,
minimize: IS_PRODUCTION
}
}, 'sass-loader']
})
}, {
test: new RegExp(`\\.(${BIN_FILE_TYPES})$`),
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: {
clientLogLevel: 'error',
// 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',
// disableHostCheck: true,
// 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
}
};
};