|
1 | 1 | const { readdirSync } = require("fs"); |
2 | 2 | const webpack = require("webpack"); |
3 | | -const memoize = require("lodash.memoize"); |
4 | | -const partial = require("lodash.partial"); |
5 | | -const merge = require("lodash.merge"); |
6 | 3 |
|
7 | 4 | global.webpackCompile = webpackCompile; |
8 | 5 | global.makeWebpackConfig = makeWebpackConfig; |
9 | 6 | global.forEachWebpackVersion = forEachWebpackVersion; |
10 | 7 |
|
11 | 8 | const BundleAnalyzerPlugin = require("../lib/BundleAnalyzerPlugin"); |
12 | 9 |
|
| 10 | +/** |
| 11 | + * @template T |
| 12 | + * @typedef {() => T} FunctionReturning |
| 13 | + */ |
| 14 | + |
| 15 | +/** |
| 16 | + * @template T |
| 17 | + * @param {FunctionReturning<T>} fn memorized function |
| 18 | + * @returns {FunctionReturning<T>} new function |
| 19 | + */ |
| 20 | +const memoize = (fn) => { |
| 21 | + let cache = false; |
| 22 | + /** @type {T | undefined} */ |
| 23 | + let result; |
| 24 | + return () => { |
| 25 | + if (cache) { |
| 26 | + return /** @type {T} */ (result); |
| 27 | + } |
| 28 | + |
| 29 | + result = fn(); |
| 30 | + cache = true; |
| 31 | + // Allow to clean up memory for fn |
| 32 | + // and all dependent resources |
| 33 | + /** @type {FunctionReturning<T> | undefined} */ |
| 34 | + (fn) = undefined; |
| 35 | + return /** @type {T} */ (result); |
| 36 | + }; |
| 37 | +}; |
| 38 | + |
13 | 39 | const getAvailableWebpackVersions = memoize(() => |
14 | 40 | readdirSync(`${__dirname}/webpack-versions`, { withFileTypes: true }) |
15 | 41 | .filter((entry) => entry.isDirectory()) |
@@ -51,7 +77,7 @@ function forEachWebpackVersion(versions, cb) { |
51 | 77 | cb({ |
52 | 78 | it: itFn, |
53 | 79 | version, |
54 | | - webpackCompile: partial(webpackCompile, partial.placeholder, version), |
| 80 | + webpackCompile: (config) => webpackCompile(config, version), |
55 | 81 | }); |
56 | 82 | } |
57 | 83 | } |
@@ -95,19 +121,18 @@ async function webpackCompile(config, version) { |
95 | 121 | await wait(1); |
96 | 122 | } |
97 | 123 |
|
98 | | -function makeWebpackConfig(opts) { |
99 | | - opts = merge( |
100 | | - { |
101 | | - analyzerOpts: { |
102 | | - analyzerMode: "static", |
103 | | - openAnalyzer: false, |
104 | | - logLevel: "error", |
105 | | - }, |
106 | | - minify: false, |
107 | | - multipleChunks: false, |
| 124 | +function makeWebpackConfig(opts = {}) { |
| 125 | + opts = { |
| 126 | + ...opts, |
| 127 | + minify: false, |
| 128 | + multipleChunks: false, |
| 129 | + analyzerOpts: { |
| 130 | + analyzerMode: "static", |
| 131 | + openAnalyzer: false, |
| 132 | + logLevel: "error", |
| 133 | + ...(opts.analyzerOpts || {}), |
108 | 134 | }, |
109 | | - opts, |
110 | | - ); |
| 135 | + }; |
111 | 136 |
|
112 | 137 | return { |
113 | 138 | context: __dirname, |
|
0 commit comments