-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathwebpack.config.js
204 lines (189 loc) · 6.23 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
"use strict";
/**
* Webpack configuration
*/
const fs = require("fs");
const path = require("path");
const { StatsWriterPlugin } = require("../../../index");
const INDENT = 2;
const STAT_RESET = Object.freeze({
// webpack5+ needs explicit declaration.
errors: true,
warnings: true,
// fallback for new stuff added after v3
all: false,
// explicitly turn off older fields
// (webpack <= v2.7.0 does not support "all")
// See: https://webpack.js.org/configuration/stats/
performance: false,
hash: false,
version: false,
timings: false,
entrypoints: false,
chunks: false,
chunkModules: false,
cached: false,
cachedAssets: false,
children: false,
moduleTrace: false,
assets: false,
modules: false,
publicPath: false
});
const MODE = process.env.MODE || "development";
const OUTPUT_PATH = path.join(__dirname, process.env.OUTPUT_PATH || "build");
// webpack5 has deprecated `hash`.
const VERS = parseInt(process.env.VERS || "", 10);
const HASH_KEY = VERS >= 5 ? "fullhash" : "hash"; // eslint-disable-line no-magic-numbers
// webpack5 returns array even if single item
const normAssets = ({ assetsByChunkName }) => {
Object.entries(assetsByChunkName).forEach(([key, val]) => {
assetsByChunkName[key] = Array.isArray(val) && val.length === 1 ? val[0] : val;
});
return assetsByChunkName;
};
const normData = (data) => Object.keys(data)
.sort()
.reduce((m, k) => Object.assign(m, { [k]: data[k] }), {});
module.exports = {
mode: MODE,
context: __dirname,
entry: {
main: "../../src/main.js"
},
output: {
path: OUTPUT_PATH,
publicPath: "/website-o-doom/",
filename: `[${HASH_KEY}].[name].js`
},
plugins: [
// Try various defaults and options.
new StatsWriterPlugin(),
new StatsWriterPlugin({}),
new StatsWriterPlugin({
filename: "stats-transform.json",
fields: null,
transform({ assetsByChunkName }) {
return JSON.stringify(normAssets({ assetsByChunkName }), null, INDENT);
}
}),
new StatsWriterPlugin({
filename: "stats-transform.md",
fields: null,
transform({ assetsByChunkName }) {
return Object.entries(normAssets({ assetsByChunkName })).reduce(
(memo, [key, val]) => `${memo}${key} | ${val}\n`,
"Name | Asset\n:--- | :----\n"
);
}
}),
new StatsWriterPlugin({
filename: "stats-transform-custom-obj.json",
transform({ assetsByChunkName }) {
return JSON.stringify({
main: normAssets({ assetsByChunkName }).main
}, null, INDENT);
}
}),
new StatsWriterPlugin({
filename: "stats-custom.json"
}),
// Relative paths work, but absolute paths do not currently.
new StatsWriterPlugin({
filename: "../build2/stats-custom2.json"
}),
// Dynamic filename.
new StatsWriterPlugin({
filename: () => "stats-dynamic.json"
}),
// Promise transform
new StatsWriterPlugin({
filename: "stats-transform-promise.json",
transform({ assetsByChunkName }) {
return Promise.resolve()
// Force async.
// eslint-disable-next-line promise/avoid-new
.then(() => new Promise((resolve) => { process.nextTick(resolve); }))
.then(() => JSON.stringify({
main: normAssets({ assetsByChunkName }).main
}, null, INDENT));
}
}),
// Custom stats
new StatsWriterPlugin({
filename: "stats-custom-stats.json",
stats: Object.assign({}, STAT_RESET, {
assets: true
}),
transform(data) {
data = normData(data);
// webpack >= v3 adds this field unconditionally, so remove it
delete data.filteredAssets;
// webpack >= 5 normalization (remove new extra entries without name).
if (data.assets) {
data.assets = data.assets.filter(({ name }) => name);
}
return JSON.stringify(data, null, INDENT);
}
}),
// Regression test: Missing `stats` option fields that should be default enabled.
// https://github.com/FormidableLabs/webpack-stats-plugin/issues/44
new StatsWriterPlugin({
filename: "stats-custom-stats-fields.json",
fields: ["errors", "warnings", "assets", "hash", "publicPath", "namedChunkGroups"]
}),
new StatsWriterPlugin({
filename: "stats-override-tostring-opt.json",
stats: Object.assign({}, STAT_RESET, {
// chunks are normally omitted due to second argument of .toJson()
chunks: true
}),
transform(data) {
data = normData(data);
// normalize subset of chunk metadata across all versions of webpack
data.chunks = data.chunks.map((chunk) => [
"rendered",
"initial",
"entry",
"size",
"names"
].reduce((obj, key) => {
obj[key] = chunk[key];
return obj;
}, {}));
return JSON.stringify(data, null, INDENT);
}
}),
new StatsWriterPlugin({
filename: "stats-should-not-exist.json",
emit: false
}),
new StatsWriterPlugin({
emit: false,
async transform(data, context) {
// eslint-disable-next-line global-require
const { JsonStreamStringify } = require("json-stream-stringify");
// Use same build directory as webpack.
const outputPath = context.compiler.options.output.path;
const filePath = path.join(outputPath, "stats-from-write-stream.json");
// Webpack is going to emit / create intermediate directories _after_ this plugin runs,
// so do a a mkdir -p before starting our streams.
await fs.promises.mkdir(outputPath, { recursive: true });
// Create and kick off the streams.
const jsonStream = new JsonStreamStringify(data, undefined, INDENT);
const writeStream = fs.createWriteStream(filePath);
return new Promise((resolve, reject) => { // eslint-disable-line promise/avoid-new
jsonStream.pipe(writeStream);
jsonStream.on("end", () => resolve());
jsonStream.on("error", (err) => reject(new Error(`error converting stream - ${err}`)));
});
}
}),
new StatsWriterPlugin({
filename: "stats.js",
transform() {
return "/*eslint-disable*/\nconsole.log(\"hello world\");\n";
}
})
]
};