/
WebpackClientConfig.js
135 lines (122 loc) · 4.25 KB
/
WebpackClientConfig.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
import fs from 'fs';
import webpack from 'webpack';
import AssetsPlugin from 'assets-webpack-plugin';
// var CompressionPlugin = require("compression-webpack-plugin");
import WebpackConfig from './WebpackConfig';
import UglifyJsPlugin from 'uglifyjs-webpack-plugin';
import WebpackAssetsManifest from 'webpack-assets-manifest';
import { BundleAnalyzerPlugin } from 'webpack-bundle-analyzer';
const OptimizeJsPlugin = require('optimize-js-plugin');
export default class WebpackClientConfig extends WebpackConfig {
name = 'client';
getTarget() {
return 'web';
}
getEntry() {
return {
client: './client.js',
};
}
getGlobals() {
return {
...super.getGlobals(),
__SERVER__: false,
__CLIENT__: true,
__WIDGET__: false,
};
}
getPreConfig() {
return {
...super.getPreConfig(),
devtool: this.clientDevtool || 'cheap-module-eval-source-map' || 'cheap-eval-source-map' || 'eval',
};
}
getOutput() {
return {
...super.getOutput(),
filename: this.isDebug() ? '[name].js?[chunkhash]' : '[name].[chunkhash].js',
chunkFilename: this.isDebug() ? 'module_[name].js?[chunkhash]' : 'module_[name].[chunkhash].js',
};
}
getPlugins() {
return [
...super.getPlugins(),
// Emit a file with assets paths
// https://github.com/sporto/assets-webpack-plugin#options
new AssetsPlugin({
path: this.resolvePath('build'),
filename: 'assets.js',
processOutput: x => `module.exports = ${JSON.stringify(x)};`,
}),
new WebpackAssetsManifest({
output: this.resolvePath('build/asset-manifest.json'),
publicPath: true,
writeToDisk: true,
customize: ({ key, value }) => {
// You can prevent adding items to the manifest by returning false.
if (key.toLowerCase().endsWith('.map')) return false;
return { key, value };
},
done: (manifest, stats) => {
// Write chunk-manifest.json.json
// const chunkFileName = this.resolvePath('build/asset-manifest.json');
const chunkFileName = this.resolvePath('build/chunk-manifest.json');
try {
const fileFilter = file => !file.endsWith('.map');
const addPath = file => manifest.getPublicPath(file);
const chunkFiles = stats.compilation.chunkGroups.reduce((acc, c) => {
acc[c.name] = [
...(acc[c.name] || []),
...c.chunks.reduce(
(files, cc) => [
...files,
...cc.files.filter(fileFilter).map(addPath),
],
[],
),
];
return acc;
}, Object.create(null));
fs.writeFileSync(chunkFileName, JSON.stringify(chunkFiles, null, 2));
} catch (err) {
console.error(`ERROR: Cannot write ${chunkFileName}: `, err);
if (!isDebug) process.exit(1);
}
},
}),
// Assign the module and chunk ids by occurrence count
// Consistent ordering of modules required if using any hashing ([hash] or [chunkhash])
// https://webpack.github.io/docs/list-of-plugins.html#occurrenceorderplugin
// new webpack.optimize.OccurrenceOrderPlugin(true),
...this.isDebug() ? [] : [
// new UglifyJsPlugin({
// parallel: true,
// uglifyOptions: {
// ie8: true,
// compress: {
// warnings: this.isVerbose(),
// unused: true,
// dead_code: true,
// }
// }
// }),
// new OptimizeJsPlugin({
// sourceMap: false,
// }),
//
// new CompressionPlugin({
// asset: "[path].gz[query]",
// algorithm: "gzip",
// test: /\.js$|\.html$/,
// threshold: 10240,
// minRatio: 0.8
// })
// A plugin for a more aggressive chunk merging strategy
// https://webpack.github.io/docs/list-of-plugins.html#aggressivemergingplugin
// new webpack.optimize.AggressiveMergingPlugin(),
// new BundleAnalyzerPlugin(),
...(this.isAnalyze && this.isAnalyze() ? [new BundleAnalyzerPlugin()] : []),
],
];
}
}