-
Notifications
You must be signed in to change notification settings - Fork 6
/
bundle-size.test.ts
69 lines (63 loc) · 2.18 KB
/
bundle-size.test.ts
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
/**
* Integration tests, verifying that even with a dead-code minifier, the plugin still
* results in significantly smaller outputs when lodash imports are inline into the
* output bundle, as would be required for browser bundles.
*
* These tests can take some time to run, due to additional processing overhead:
* multiple third-party rollup plugins are required to create the bundle.
*/
import { rollup } from "rollup";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import terser from "@rollup/plugin-terser";
import commonjs from "@rollup/plugin-commonjs";
import { optimizeLodashImports } from "../src";
const STANDARD_AND_FP = `${__dirname}/fixtures/standard-and-fp.js`;
const wrapperRollup = async (
input: string,
enableLodashOptimization: boolean,
enableTerser: boolean,
) => {
// nodeResolve + commonjs = baked in lodash
const plugins = [nodeResolve(), commonjs()];
if (enableLodashOptimization) {
plugins.push(optimizeLodashImports({ exclude: /node_modules/ }));
}
if (enableTerser) {
plugins.push(terser());
}
const bundle = await rollup({
input,
plugins,
});
const { output } = await bundle.generate({ format: "cjs", validate: true });
return output[0].code;
};
describe("output size is reduced for bundled lodash", () => {
test.each<[boolean]>([[false], [true]])(
"enableTerser: %p",
async (enableTerser) => {
expect.assertions(2);
const [unoptimized, optimized] = await Promise.all(
[false, true].map((enableLodashOptimization) =>
wrapperRollup(
STANDARD_AND_FP,
enableLodashOptimization,
enableTerser,
),
),
);
const improvementPercentage =
(unoptimized.length - optimized.length) / unoptimized.length;
console.log(
`Terser: ${enableTerser ? "yes" : "no"}\nOptimized: ${
optimized.length
}\nUnoptimized: ${unoptimized.length}\nSize reduction: ${Math.round(
improvementPercentage * 100,
)}%`,
);
// we expect over a 50% improvement
expect(unoptimized.length).toBeGreaterThan(optimized.length);
expect(improvementPercentage).toBeGreaterThan(0.5);
},
);
});