-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebpack.config.babel.js
377 lines (352 loc) · 12.6 KB
/
webpack.config.babel.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import webpack from 'webpack';
import ExtractTextPlugin from 'extract-text-webpack-plugin';
import HtmlWebpackPlugin from 'html-webpack-plugin';
import ScriptExtHtmlWebpackPlugin from 'script-ext-html-webpack-plugin';
import autoprefixer from 'autoprefixer';
import CopyWebpackPlugin from 'copy-webpack-plugin';
import OfflinePlugin from 'offline-plugin';
import path from 'path';
import CleanPlugin from 'clean-webpack-plugin';
import ProgressBarPlugin from 'progress-bar-webpack-plugin';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
import Dotenv from 'dotenv-webpack';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
const ENV = process.env.NODE_ENV || "development";
const GEN_ANALITICS = process.env.GEN_ANALITICS;
const CSS_MAPS = ENV!=='production';
// TODO:
// 1. preload/prefetch is not working with webpack
// 2. preload js
// 3. preload css
// the path(s) that should be cleaned
const pathsToClean = [
'build'
];
// the clean options to use
const cleanOptions = {
root: __dirname,
verbose: true,
dry: false
};
var vendors = [
'preact',
'preact-redux',
'date-fns/format',
'date-fns/is_equal',
'date-fns/is_after',
'date-fns/difference_in_days',
'date-fns/difference_in_hours',
'date-fns/each_day',
'date-fns/last_day_of_week',
'date-fns/start_of_week',
'date-fns/get_day',
'date-fns/get_days_in_month',
'babel-polyfill',
];
module.exports = {
context: path.resolve(__dirname, "src"),
entry: {
app: path.resolve(__dirname, './src/index.js'),
// app: [],
vendors: [
'preact',
'preact-redux',
'babel-polyfill',
]
},
output: {
path: path.resolve(__dirname, "build"),
publicPath: '/',
filename: '[name].' + (ENV === 'production' ? '[chunkhash]' : '[hash]') + '.js',
chunkFilename: '[name]-[chunkhash].js',
},
performance: {
hints: (ENV === 'production' ? false : "warning")
},
resolve: {
extensions: ['.jsx', '.js', '.json', '.less'],
modules: [
path.resolve(__dirname, "src/style/vendor"),
path.resolve(__dirname, "src"),
path.resolve(__dirname, "node_modules"),
'node_modules'
],
alias: {
components: path.resolve(__dirname, "src/components"), // used for tests
style: path.resolve(__dirname, "src/style"),
'react': 'preact-compat',
'react-dom': 'preact-compat',
'react-redux': 'preact-redux'
}
},
optimization: {
splitChunks: {
cacheGroups: {
vendor_rest: {
test: (module, count) => {
var context = module.context;
return context && context.indexOf('node_modules') >= 0;
},
name: 'vendor_rest',
chunks: "all",
enforce: true
}
}
},
minimizer: ENV==='production' ? [
// we specify a custom UglifyJsPlugin here to get source maps in production
new UglifyJsPlugin({
cache: true,
parallel: true,
uglifyOptions: {
compress: {
unsafe_comps: true,
properties: true,
keep_fargs: false,
pure_getters: true,
collapse_vars: true,
warnings: false,
sequences: true,
dead_code: true,
drop_debugger: true,
comparisons: true,
conditionals: true,
evaluate: true,
booleans: true,
loops: true,
unused: true,
hoist_funs: true,
if_return: true,
join_vars: true,
drop_console: true
},
output: {
comments: false
},
},
})
]: []
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: path.resolve(__dirname, 'src'),
enforce: 'pre',
use: 'source-map-loader'
},
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: 'babel-loader'
},
{
// Transform our own .(less|css) files with PostCSS and CSS-modules
test: /\.(less|css)$/,
include: [path.resolve(__dirname, 'src/components')],
use: [
ENV==='production' ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: {
javascriptEnabled: true,
modules: true,
sourceMap: CSS_MAPS,
importLoaders: 1,
minimize: {
reduceInitial: false
}
}
},
{
loader: `postcss-loader`,
options: {
javascriptEnabled: true,
sourceMap: CSS_MAPS,
plugins: () => {
autoprefixer({ browsers: [ 'last 2 versions' ] });
}
}
},
{
loader: 'less-loader',
options: { javascriptEnabled: true, sourceMap: CSS_MAPS }
}
]
},
{
test: /\.(less|css)$/,
exclude: [path.resolve(__dirname, 'src/components')],
use: [
ENV==='production' ? MiniCssExtractPlugin.loader : 'style-loader',
{
loader: 'css-loader',
options: { javascriptEnabled: true,
sourceMap: CSS_MAPS,
importLoaders: 1,
minimize: {
reduceInitial: false
}
}
},
{
loader: `postcss-loader`,
options: {
javascriptEnabled: true,
sourceMap: CSS_MAPS,
plugins: () => {
autoprefixer({ browsers: [ 'last 2 versions' ] });
}
}
},
{
loader: 'less-loader',
options: { javascriptEnabled: true, sourceMap: CSS_MAPS }
}
]
},
{
type: 'javascript/auto',
test: /\.json$/,
use: 'json-loader'
},
{
test: /\.(xml|html|txt|md)$/,
use: 'raw-loader'
},
{
test: /\.(svg|woff2?|ttf|eot|jpe?g|png|gif)(\?.*)?$/i,
use: ENV==='production' ? 'file-loader' : 'url-loader'
}
]
},
plugins: ([
new Dotenv({
path: './.env',
}),
new ProgressBarPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new webpack.NormalModuleReplacementPlugin(/(.*)-ENV_TARGET(\.*)/, function(resource) {
resource.request = resource.request.replace(/-ENV_TARGET/, `-${ENV}`);
}),
new MiniCssExtractPlugin({
// Options similar to the same options in webpackOptions.output
// both options are optional
filename: "[name].css",
chunkFilename: "[id].css"
}),
new webpack.DefinePlugin({
'process.env' : {
NODE_ENV: JSON.stringify(ENV),
GOOGLE_ANALYTICS_ID: JSON.stringify(process.env.GOOGLE_ANALYTICS_ID),
APP_ENV: JSON.stringify(process.env.APP_ENV),
LOCAL_STORAGE_KEY: JSON.stringify(process.env.LOCAL_STORAGE_KEY),
APP_VERSION: JSON.stringify(process.env.APP_VERSION),
BASE_URL: JSON.stringify(process.env.BASE_URL),
}
}),
new HtmlWebpackPlugin({
template: '!!ejs-compiled-loader!./src/index.ejs',
minify: {
collapseWhitespace: true,
removeComments: true
},
favicon: './favicon.ico',
}),
new CopyWebpackPlugin([
{ from: './manifest.json', to: './' },
{ from: './favicon.ico', to: './' },
{ from: './assets/icons', to: './' },
]),
]).concat(ENV==='production' ? [
new CleanPlugin(pathsToClean, cleanOptions),
new ScriptExtHtmlWebpackPlugin({
defaultAttribute: 'async'
}),
// This plugin looks for similar chunks and files
// and merges them for better caching by the user
new webpack.optimize.OccurrenceOrderPlugin(),
// new webpack.optimize.MinChunkSizePlugin({
// //uncomment once we'll have to many requests on start
// // minChunkSize: 51200, // ~50kb
// }),
new OfflinePlugin({
relativePaths: false,
AppCache: false,
excludes: ['_redirects'],
ServiceWorker: {
events: true,
minify: false,
},
cacheMaps: [
{
match: /.*/,
to: '/',
requestTypes: ['navigate']
}
],
publicPath: '/'
})
] : []
).concat(GEN_ANALITICS ? [
new BundleAnalyzerPlugin({
// Can be `server`, `static` or `disabled`.
// In `server` mode analyzer will start HTTP server to show bundle report.
// In `static` mode single HTML file with bundle report will be generated.
// In `disabled` mode you can use this plugin to just generate Webpack Stats JSON file by setting `generateStatsFile` to `true`.
analyzerMode: 'static',
// Host that will be used in `server` mode to start HTTP server.
analyzerHost: 'localhost',
// Port that will be used in `server` mode to start HTTP server.
analyzerPort: 8888,
// Path to bundle report file that will be generated in `static` mode.
// Relative to bundles output directory.
reportFilename: 'report.html',
// Module sizes to show in report by default.
// Should be one of `stat`, `parsed` or `gzip`.
// See "Definitions" section for more information.
defaultSizes: 'start',
// Automatically open report in default browser
openAnalyzer: true,
// If `true`, Webpack Stats JSON file will be generated in bundles output directory
generateStatsFile: false,
// Name of Webpack Stats JSON file that will be generated if `generateStatsFile` is `true`.
// Relative to bundles output directory.
statsFilename: 'stats.json',
// Options for `stats.toJson()` method.
// For example you can exclude sources of your modules from stats file with `source: false` option.
// See more options here: https://github.com/webpack/webpack/blob/webpack-1/lib/Stats.js#L21
statsOptions: null,
// Log level. Can be 'info', 'warn', 'error' or 'silent'.
logLevel: 'info'
})
]:[]),
stats: { colors: true },
node: {
global: true,
process: false,
Buffer: false,
__filename: false,
__dirname: false,
setImmediate: false
},
// devtool: ENV==='production' ? 'source-map' : 'cheap-module-eval-source-map',
devServer: {
port: process.env.PORT || 5000,
host: 'localhost',
publicPath: '/',
contentBase: './src',
historyApiFallback: true,
open: true,
openPage: '',
disableHostCheck: true,
proxy: {
// OPTIONAL: proxy configuration:
// '/optional-prefix/**': { // path pattern to rewrite
// target: 'http://target-host.com',
// pathRewrite: path => path.replace(/^\/[^\/]+\//, '') // strip first path segment
// }
}
}
};