-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebpack.config.js
293 lines (273 loc) · 7.66 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* eslint-env node, browser: false */
const webpack = require('webpack');
const env = process.env.NODE_ENV || 'development';
const path = require('path');
const fs = require('fs');
const merge = require('webpack-merge');
process.traceDeprecation = false;
// webpack plugins and related utils
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
const WebpackBuildNotifierPlugin = require('webpack-build-notifier');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const autoprefixer = require('autoprefixer');
// Make sure any symlinks in the project folder are resolved:
// https://github.com/facebookincubator/create-react-app/issues/637
const appDirectory = fs.realpathSync(process.cwd());
const buildDirectory = path.resolve(appDirectory, 'build');
const examplesDirectory = path.resolve(appDirectory, 'examples');
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);
const examples = fs.readdirSync(examplesDirectory)
.filter(n => /\.js$/.test(n))
.map(n => n.substr(0, n.length - 3));
// configuration
const eslintConfig = require('./.eslintrc.js');
const port = '9900';
const host = 'localhost';
const browsers = [
'>1%',
'last 4 versions',
'Firefox ESR',
'not ie < 12'
];
function buildEntryPoint(entryPoint) {
const e = [entryPoint];
if (env === 'development') {
e.push(`webpack-dev-server/client?http://${host}:${port}/`);
}
return e;
}
const entry = {};
examples.forEach(name => {
entry['example-' + name] = buildEntryPoint(path.resolve(examplesDirectory, name + '.js'));
});
const plugins = [
new CaseSensitivePathsPlugin(),
new CleanWebpackPlugin()
];
const config = {
entry,
devtool: 'source-map',
output: {
path: buildDirectory,
filename: '[name].bundle-[hash].js',
pathinfo: env !== 'production'
},
module: {
rules: [
// preLoaders
{
test: /\.js$/,
exclude: /node_modules/,
enforce: 'pre',
loader: 'eslint-loader',
options: Object.assign({}, eslintConfig, {
formatter: eslintFormatter,
failOnHint: env === 'production',
emitWarning: true
})
},
{
oneOf: [
// 'url' loader works like 'file' loader except that it embeds assets
// smaller than specified limit in bytes as data URLs to avoid requests.
// A missing `test` is equivalent to a match.
{
test: [/\.gif$/, /\.jpe?g$/, /\.png$/],
loader: require.resolve('url-loader'),
options: {
limit: 10000,
name: 'examples/assets/[name].[hash:8].[ext]'
}
},
// Process JS with babel
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
babelrc: false,
presets: [
[
'@babel/env',
{
exclude: [
'transform-regenerator',
'transform-async-to-generator'
],
targets: {
browsers
},
useBuiltIns: false,
modules: false
}
]
],
plugins: [
'@babel/plugin-proposal-class-properties',
['@babel/plugin-proposal-object-rest-spread', { useBuiltIns: true }],
['@babel/plugin-transform-runtime', {
helpers: false,
regenerator: true
}],
'@babel/plugin-syntax-dynamic-import',
'module:fast-async'
// todo: for tests
// https://github.com/facebookincubator/create-react-app/blob/master/packages/babel-preset-react-app/index.js#L72
],
cacheDirectory: true
}
},
// 'postcss' loader applies autoprefixer to our CSS.
// 'css' loader resolves paths in CSS and adds assets as dependencies.
// 'style' loader turns CSS into JS modules that inject <style> tags.
// In production, we use a plugin to extract that CSS to a file, but
// in development 'style' loader enables hot editing of CSS.
{
test: /\.css$/,
use: [
require.resolve('style-loader'),
{
loader: require.resolve('css-loader'),
options: {
importLoaders: 1
}
},
{
loader: require.resolve('postcss-loader'),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: 'postcss',
plugins: () => [
require('postcss-flexbugs-fixes'),
autoprefixer({
browsers,
flexbox: 'no-2009'
})
]
}
}
]
},
{
test: /\.html$/,
loader: 'raw-loader'
},
// 'file' loader makes sure those assets get served by WebpackDevServer.
// When you `import` an asset, you get its (virtual) filename.
// In production, they would get copied to the `build` folder.
// This loader doesn't use a 'test' so it will catch all modules
// that fall through the other loaders.
{
// Exclude `js` files to keep 'css' loader working as it injects
// its runtime that would otherwise processed through 'file' loader.
// Also exclude `html` and `json` extensions so they get processed
// by webpacks internal loaders.
exclude: [/\.(js|mjs)$/, /\.html$/, /\.json$/],
loader: require.resolve('file-loader'),
options: {
name: 'examples/assets/[name].[hash:8].[ext]'
}
}
]
}/*,
{
test: /\.js$/,
include: [
path.resolve(__dirname, 'src/workers')
],
loader: 'worker-loader'
}*/
]
},
resolve: {
// root: path.resolve('./src'),
extensions: ['.js']
},
plugins,
optimization: {
namedModules: true,
splitChunks: {
minChunks: 2
}
}
};
const devConfig = {
devtool: 'cheap-module-source-map',
mode: 'development',
output: {
// workaround for https://github.com/facebookincubator/create-react-app/issues/2407
sourceMapFilename: '[file].map',
publicPath: `http://${host}:${port}/`
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('development'),
DEBUG: true
}),
new WebpackBuildNotifierPlugin({
title: path.basename(__dirname),
suppressSuccess: true
}),
// new webpack.HotModuleReplacementPlugin(),
new WatchMissingNodeModulesPlugin(resolveApp('node_modules'))
].concat(examples.map(name => {
return new HtmlWebpackPlugin({
inject: true,
template: path.resolve(examplesDirectory, 'template.html'),
chunks: ['common', 'example-' + name],
// filename: `./examples/${name}/index.html`
filename: `./examples/${name}.html`
});
})),
devServer: {
// hot: true,
progress: true,
inline: true,
// contentBase: './public',
stats: {
all: false,
colors: true,
errors: true,
warnings: true
},
port,
host
}
};
const distConfig = {
output: {
filename: 'index-[chunkhash].js'
},
mode: 'production',
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify('production'),
DEBUG: false
}),
new HtmlWebpackPlugin({
inject: true,
template: __dirname + '/public/index.html',
minify: {
removeComments: true,
removeCommentsFromCDATA: true,
removeCDATASectionsFromCDATA: true,
collapseWhitespace: true,
collapseBooleanAttributes: true,
removeAttributeQuotes: true,
removeRedundantAttributes: true,
useShortDoctype: true,
removeEmptyAttributes: true,
removeScriptTypeAttributes: true,
// lint: true,
caseSensitive: true,
minifyJS: true,
minifyCSS: true
}
})
]
};
module.exports = merge.smart(config, env === 'production' ? distConfig : devConfig);