-
-
Notifications
You must be signed in to change notification settings - Fork 503
Expand file tree
/
Copy pathhelpers.js
More file actions
183 lines (158 loc) · 4.21 KB
/
helpers.js
File metadata and controls
183 lines (158 loc) · 4.21 KB
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
const { readdirSync } = require("node:fs");
const path = require("node:path");
const webpack = require("webpack");
const BundleAnalyzerPlugin = require("../src/BundleAnalyzerPlugin");
/* global it */
/**
* @template T
* @typedef {() => T} FunctionReturning
*/
/**
* @template T
* @param {FunctionReturning<T>} fn memorized function
* @returns {FunctionReturning<T>} new function
*/
const memoize = (fn) => {
let cache = false;
/** @type {T | undefined} */
let result;
return () => {
if (cache) {
return /** @type {T} */ (result);
}
result = fn();
cache = true;
// Allow to clean up memory for fn
// and all dependent resources
/** @type {FunctionReturning<T> | undefined} */
(fn) = undefined;
return /** @type {T} */ (result);
};
};
const getAvailableWebpackVersions = memoize(() =>
readdirSync(path.resolve(__dirname, "./webpack-versions"), {
withFileTypes: true,
})
.filter((entry) => entry.isDirectory())
.map((dir) => dir.name),
);
function wait(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
async function webpackCompile(config, version) {
if (version === undefined || version === null) {
throw new Error("Webpack version is not specified");
}
if (!getAvailableWebpackVersions().includes(version)) {
throw new Error(
`Webpack version "${version}" is not available for testing`,
);
}
let webpack;
try {
webpack = require(`./webpack-versions/${version}/node_modules/webpack`);
} catch (err) {
throw new Error(
`Error requiring Webpack ${version}:\n${err}\n\n` +
'Try running "npm run install-test-webpack-versions".',
{ cause: err },
);
}
await new Promise((resolve, reject) => {
webpack(config, (err, stats) => {
if (err) {
return reject(err);
}
if (stats.hasErrors()) {
return reject(stats.toJson({ source: false }).errors);
}
resolve();
});
});
// Waiting for the next tick (for analyzer report to be generated)
await wait(1);
}
function makeWebpackConfig(opts = {}) {
opts = {
...opts,
minify: false,
multipleChunks: false,
analyzerOpts: {
analyzerMode: "static",
openAnalyzer: false,
logLevel: "error",
...opts.analyzerOpts,
},
};
return {
context: __dirname,
mode: "development",
entry: {
bundle: "./src",
},
output: {
path: path.resolve(__dirname, "./output"),
filename: "[name].js",
},
optimization: {
runtimeChunk: {
name: "manifest",
},
},
plugins: ((plugins) => {
plugins.push(new BundleAnalyzerPlugin(opts.analyzerOpts));
if (opts.minify) {
plugins.push(
new webpack.optimize.UglifyJsPlugin({
comments: false,
mangle: true,
compress: {
warnings: false,
// eslint-disable-next-line camelcase
negate_iife: false,
},
}),
);
}
return plugins;
})([]),
};
}
function forEachWebpackVersion(versions, cb) {
const availableVersions = getAvailableWebpackVersions();
if (typeof versions === "function") {
cb = versions;
versions = availableVersions;
} else {
const notFoundVersions = versions.filter(
(version) => !availableVersions.includes(version),
);
if (notFoundVersions.length) {
throw new Error(
`These Webpack versions are not currently available for testing: ${notFoundVersions.join(", ")}\n` +
'You need to install them manually into "test/webpack-versions" directory.',
);
}
}
for (const version of versions) {
// eslint-disable-next-line func-style
const itFn = function itFn(testDescription, ...args) {
return it.call(this, `${testDescription} (Webpack ${version})`, ...args);
};
itFn.only = function only(testDescription, ...args) {
return it.only.call(
this,
`${testDescription} (Webpack ${version})`,
...args,
);
};
cb({
it: itFn,
version,
webpackCompile: (config) => webpackCompile(config, version),
});
}
}
module.exports = { forEachWebpackVersion, makeWebpackConfig, webpackCompile };