Skip to content

Commit 7312c1b

Browse files
authored
test(bundlers): add e2e test for bundler tree-shaking (#7245)
1 parent faadcc4 commit 7312c1b

File tree

9 files changed

+884
-165
lines changed

9 files changed

+884
-165
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ test-endpoints:
4646
test-e2e: bundles
4747
yarn g:vitest run -c vitest.config.e2e.ts --retry=4
4848
yarn g:vitest run -c vitest.config.browser.e2e.ts --retry=4
49+
make test-bundlers
50+
51+
test-bundlers:
52+
(cd ./tests/bundlers && make build test)
4953

5054
build-s3-browser-bundle:
5155
node ./clients/client-s3/test/browser-build/esbuild

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@
111111
"turbo": "2.5.3",
112112
"typescript": "~5.8.3",
113113
"verdaccio": "5.25.0",
114+
"vite": "7.0.6",
114115
"vitest": "2.1.9",
115-
"webpack": "5.76.0",
116+
"webpack": "5.101.0",
116117
"webpack-cli": "4.10.0",
117118
"yargs": "17.5.1"
118119
},

tests/bundlers/Makefile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
.PHONY: test build vite webpack
2+
3+
# asserts that bundles contain expected content.
4+
test:
5+
node test.spec.mjs
6+
7+
# create bundles
8+
build:
9+
make vite webpack
10+
11+
vite:
12+
npx vite build
13+
14+
webpack:
15+
npx webpack

tests/bundlers/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"dependencies": {},
3+
"type": "module"
4+
}

tests/bundlers/source.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { GetObjectCommand, S3Client } from "@aws-sdk/client-s3";
2+
3+
export { GetObjectCommand, S3Client };

tests/bundlers/test.spec.mjs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import assert from "node:assert";
2+
3+
import fs from "node:fs";
4+
import path from "node:path";
5+
import { fileURLToPath } from "node:url";
6+
7+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
8+
9+
const webpackDist = {
10+
name: "webpack",
11+
content: fs.readFileSync(path.join(__dirname, "dist", "webpack-dist.js"), "utf-8"),
12+
};
13+
const viteDist = {
14+
name: "vite",
15+
content: fs.readFileSync(path.join(__dirname, "dist", "vite-dist.js"), "utf-8"),
16+
};
17+
18+
for (const { content: fileContents, name } of [webpackDist, viteDist]) {
19+
console.log(name);
20+
21+
const contentSize = fileContents.replaceAll(/\s+/g, "").length;
22+
const callsToClassBuilder = fileContents.match(/\.classBuilder\(\)/g);
23+
const runtimeConfig = fileContents.match(/runtime: "browser"/);
24+
25+
try {
26+
assert(contentSize < 1_000_000);
27+
console.info(`✅ content size is under 1M char.`);
28+
} catch (e) {
29+
throw new Error("Content size should be less than 1M characters.");
30+
}
31+
32+
try {
33+
assert(callsToClassBuilder.length <= 2); // only GetObject and CreateSession should be present.
34+
console.info(`✅ two commands bundled (tree shaken).`);
35+
} catch (e) {
36+
throw new Error("there should only be 2 calls to the Command classBuilder. Tree-shaking failure?");
37+
}
38+
39+
try {
40+
assert(runtimeConfig.length > 0); // browser runtimeConfig should be loaded.
41+
console.info(`✅ runtimeConfig is browser.`);
42+
} catch (e) {
43+
throw new Error("the browser runtimeConfig should be present in the bundle.");
44+
}
45+
}

tests/bundlers/vite.config.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { defineConfig } from "vite";
2+
import * as path from "node:path";
3+
4+
export default defineConfig({
5+
build: {
6+
lib: {
7+
entry: path.join(__dirname, "source.ts"),
8+
name: "dist",
9+
// the proper extensions will be added
10+
fileName: "vite-dist",
11+
},
12+
rollupOptions: {
13+
// make sure to externalize deps that shouldn't be bundled
14+
// into your library
15+
external: [],
16+
output: {
17+
// Provide global variables to use in the UMD build
18+
// for externalized deps
19+
globals: {},
20+
},
21+
},
22+
minify: false,
23+
terserOptions: {
24+
mangle: false,
25+
},
26+
},
27+
});

tests/bundlers/webpack.config.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import path from "node:path";
2+
import { fileURLToPath } from "node:url";
3+
4+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
5+
6+
export default {
7+
mode: "production",
8+
entry: "./source.ts",
9+
output: {
10+
path: path.resolve(__dirname, "dist"),
11+
filename: "webpack-dist.js",
12+
library: "dist",
13+
},
14+
optimization: {
15+
minimize: false,
16+
},
17+
};

0 commit comments

Comments
 (0)