-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathwebpack.config.js
238 lines (217 loc) · 5.79 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 path = require('path')
const glob = require('glob')
const webpack = require('webpack')
const merge = require('webpack-merge')
const HtmlPlugin = require('html-webpack-plugin')
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
const StylelintPlugin = require('stylelint-webpack-plugin')
const ManifestPlugin = require('webpack-manifest-plugin')
const CleanPlugin = require('clean-webpack-plugin')
const { StatsWriterPlugin } = require('webpack-stats-plugin')
const parts = require('./webpack.parts')
const lintJSOptions = {
emitWarning: true,
// Fail only on errors
failOnWarning: false,
failOnError: true,
// Toggle autofix
fix: true,
cache: true,
formatter: require('eslint-friendly-formatter')
}
/*
To move all assets to some static folder
getPaths({ staticDir: 'some-name' })
To rename asset build folder
getPaths({ js: 'some-name' })
To move assets to the root build folder
getPaths({ css: '' })
Defaults values:
sourceDir - 'app',
buildDir - 'build',
staticDir - '',
images - 'images',
fonts - 'fonts',
css - 'styles',
js - 'scripts'
*/
const paths = getPaths()
const lintStylesOptions = {
context: path.resolve(__dirname, `${paths.app}/styles`),
syntax: 'scss',
emitErrors: false
// fix: true,
}
const cssPreprocessorLoader = { loader: 'fast-sass-loader' }
const commonConfig = merge([
{
context: paths.app,
resolve: {
unsafeCache: true,
symlinks: false
},
entry: `${paths.app}/scripts`,
output: {
path: paths.build,
publicPath: parts.publicPath
},
stats: {
warningsFilter: warning => warning.includes('entrypoint size limit'),
children: false,
modules: false
},
plugins: [
new HtmlPlugin({
template: './index.pug'
}),
new FriendlyErrorsPlugin(),
new StylelintPlugin(lintStylesOptions)
],
module: {
noParse: /\.min\.js/
}
},
parts.loadPug(),
parts.lintJS({ include: paths.app, options: lintJSOptions }),
parts.loadFonts({
include: paths.app,
options: {
name: `${paths.fonts}/[name].[hash:8].[ext]`
}
})
])
const productionConfig = merge([
{
mode: 'production',
optimization: {
splitChunks: {
chunks: 'all'
},
runtimeChunk: 'single'
},
output: {
chunkFilename: `${paths.js}/[name].[chunkhash:8].js`,
filename: `${paths.js}/[name].[chunkhash:8].js`
},
performance: {
hints: 'warning', // 'error' or false are valid too
maxEntrypointSize: 100000, // in bytes
maxAssetSize: 450000 // in bytes
},
plugins: [
new StatsWriterPlugin({ fields: null, filename: '../stats.json' }),
new webpack.HashedModuleIdsPlugin(),
new ManifestPlugin(),
new CleanPlugin()
]
},
parts.minifyJS({
terserOptions: {
parse: {
// we want terser to parse ecma 8 code. However, we don't want it
// to apply any minfication steps that turns valid ecma 5 code
// into invalid ecma 5 code. This is why the 'compress' and 'output'
// sections only apply transformations that are ecma 5 safe
// https://github.com/facebook/create-react-app/pull/4234
ecma: 8
},
compress: {
ecma: 5,
warnings: false,
// Disabled because of an issue with Uglify breaking seemingly valid code:
// https://github.com/facebook/create-react-app/issues/2376
// Pending further investigation:
// https://github.com/mishoo/UglifyJS2/issues/2011
comparisons: false
},
mangle: {
safari10: true
},
output: {
ecma: 5,
comments: false,
// Turned on because emoji and regex is not minified properly using default
// https://github.com/facebook/create-react-app/issues/2488
ascii_only: true
}
},
// Use multi-process parallel running to improve the build speed
// Default number of concurrent runs: os.cpus().length - 1
parallel: true,
// Enable file caching
cache: true
}),
parts.loadJS({
include: paths.app,
options: {
cacheDirectory: true
}
}),
parts.extractCSS({
include: paths.app,
use: [parts.autoprefix(), cssPreprocessorLoader],
options: {
filename: `${paths.css}/[name].[contenthash:8].css`,
chunkFilename: `${paths.css}/[id].[contenthash:8].css`
}
}),
parts.purifyCSS({
paths: glob.sync(`${paths.app}/**/*.+(pug|js)`, { nodir: true }),
styleExtensions: ['.css', '.scss']
}),
parts.minifyCSS({
options: {
discardComments: {
removeAll: true
}
}
}),
parts.loadImages({
include: paths.app,
options: {
limit: 15000,
name: `${paths.images}/[name].[hash:8].[ext]`
}
}),
// should go after loading images
parts.optimizeImages()
])
const developmentConfig = merge([
{
mode: 'development'
},
parts.devServer({
host: process.env.HOST,
port: process.env.PORT
}),
parts.loadCSS({ include: paths.app, use: [cssPreprocessorLoader] }),
parts.loadImages({ include: paths.app }),
parts.loadJS({ include: paths.app })
])
module.exports = env => {
process.env.NODE_ENV = env
return merge(
commonConfig,
env === 'production' ? productionConfig : developmentConfig
)
}
function getPaths ({
sourceDir = 'app',
buildDir = 'build',
staticDir = '',
images = 'images',
fonts = 'fonts',
js = 'scripts',
css = 'styles'
} = {}) {
const assets = { images, fonts, js, css }
return Object.keys(assets).reduce((obj, assetName) => {
const assetPath = assets[assetName]
obj[assetName] = !staticDir ? assetPath : `${staticDir}/${assetPath}`
return obj
}, {
app: path.join(__dirname, sourceDir),
build: path.join(__dirname, buildDir),
staticDir
})
}