Skip to content

Commit f65aa6b

Browse files
committed
feat: use rollup for cjs distributions
1 parent 58c7732 commit f65aa6b

File tree

9 files changed

+508
-57
lines changed

9 files changed

+508
-57
lines changed

.changeset/sour-seahorses-wash.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
"@smithy/service-client-documentation-generator": patch
3+
"@smithy/experimental-identity-and-auth": patch
4+
"@smithy/core": patch
5+
"@smithy/abort-controller": patch
6+
"@smithy/chunked-blob-reader": patch
7+
"@smithy/chunked-blob-reader-native": patch
8+
"@smithy/config-resolver": patch
9+
"@smithy/credential-provider-imds": patch
10+
"@smithy/eventstream-codec": patch
11+
"@smithy/eventstream-serde-browser": patch
12+
"@smithy/eventstream-serde-config-resolver": patch
13+
"@smithy/eventstream-serde-node": patch
14+
"@smithy/eventstream-serde-universal": patch
15+
"@smithy/fetch-http-handler": patch
16+
"@smithy/hash-blob-browser": patch
17+
"@smithy/hash-node": patch
18+
"@smithy/hash-stream-node": patch
19+
"@smithy/invalid-dependency": patch
20+
"@smithy/is-array-buffer": patch
21+
"@smithy/md5-js": patch
22+
"@smithy/middleware-apply-body-checksum": patch
23+
"@smithy/middleware-compression": patch
24+
"@smithy/middleware-content-length": patch
25+
"@smithy/middleware-endpoint": patch
26+
"@smithy/middleware-retry": patch
27+
"@smithy/middleware-serde": patch
28+
"@smithy/middleware-stack": patch
29+
"@smithy/node-config-provider": patch
30+
"@smithy/node-http-handler": patch
31+
"@smithy/property-provider": patch
32+
"@smithy/protocol-http": patch
33+
"@smithy/querystring-builder": patch
34+
"@smithy/querystring-parser": patch
35+
"@smithy/service-error-classification": patch
36+
"@smithy/shared-ini-file-loader": patch
37+
"@smithy/signature-v4": patch
38+
"@smithy/signature-v4a": patch
39+
"@smithy/smithy-client": patch
40+
"@smithy/types": patch
41+
"@smithy/url-parser": patch
42+
"@smithy/util-base64": patch
43+
"@smithy/util-body-length-browser": patch
44+
"@smithy/util-body-length-node": patch
45+
"@smithy/util-buffer-from": patch
46+
"@smithy/util-config-provider": patch
47+
"@smithy/util-defaults-mode-browser": patch
48+
"@smithy/util-defaults-mode-node": patch
49+
"@smithy/util-endpoints": patch
50+
"@smithy/util-hex-encoding": patch
51+
"@smithy/util-middleware": patch
52+
"@smithy/util-retry": patch
53+
"@smithy/util-stream": patch
54+
"@smithy/util-stream-browser": patch
55+
"@smithy/util-stream-node": patch
56+
"@smithy/util-uri-escape": patch
57+
"@smithy/util-utf8": patch
58+
"@smithy/util-waiter": patch
59+
"@smithy/uuid": patch
60+
---
61+
62+
update dist-cjs generation to use rollup

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636
},
3737
"devDependencies": {
3838
"@microsoft/api-extractor": "7.52.7",
39+
"@rollup/plugin-node-resolve": "^16.0.1",
40+
"@rollup/plugin-typescript": "^12.1.4",
3941
"@tsconfig/recommended": "1.0.2",
4042
"@types/jest": "28.1.3",
4143
"@types/jsdom": "20.0.1",
@@ -51,6 +53,7 @@
5153
"jest": "29.7.0",
5254
"prettier": "3.2.5",
5355
"puppeteer": "^19.2.0",
56+
"rollup": "^4.52.2",
5457
"ts-jest": "29.1.2",
5558
"turbo": "2.3.3",
5659
"typescript": "~5.8.3",

packages/core/src/util-identity-and-auth/memoizeIdentityProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import type { Identity, IdentityProvider } from "@smithy/types";
33
/**
44
* @internal
55
*/
6-
export const createIsIdentityExpiredFunction = (expirationMs: number) => (identity: Identity) =>
7-
doesIdentityRequireRefresh(identity) && identity.expiration!.getTime() - Date.now() < expirationMs;
6+
export const createIsIdentityExpiredFunction = (expirationMs: number) =>
7+
function isIdentityExpired(identity: Identity) {
8+
return doesIdentityRequireRefresh(identity) && identity.expiration!.getTime() - Date.now() < expirationMs;
9+
};
810

911
/**
1012
* @internal

packages/experimental-identity-and-auth/src/memoizeIdentityProvider.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import type { Identity, IdentityProvider } from "@smithy/types";
33
/**
44
* @internal
55
*/
6-
export const createIsIdentityExpiredFunction = (expirationMs: number) => (identity: Identity) =>
7-
doesIdentityRequireRefresh(identity) && identity.expiration!.getTime() - Date.now() < expirationMs;
6+
export const createIsIdentityExpiredFunction = (expirationMs: number) =>
7+
function isIdentityExpired(identity: Identity) {
8+
return doesIdentityRequireRefresh(identity) && identity.expiration!.getTime() - Date.now() < expirationMs;
9+
};
810

911
/**
1012
* @internal

packages/service-client-documentation-generator/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"version": "4.1.0",
44
"scripts": {
55
"build": "concurrently 'yarn:build:cjs' 'yarn:build:es' 'yarn:build:types && yarn build:types:downlevel'",
6-
"build:cjs": "node ../../scripts/inline service-client-documentation-generator",
6+
"build:cjs": "yarn g:tsc -p tsconfig.cjs.json",
77
"build:es": "yarn g:tsc -p tsconfig.es.json",
88
"build:types": "yarn g:tsc -p tsconfig.types.json",
99
"build:types:downlevel": "rimraf dist-types/ts3.4 && downlevel-dts dist-types dist-types/ts3.4",

scripts/compilation/Inliner.js

Lines changed: 98 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ const fs = require("fs");
22
const path = require("path");
33
const { spawnProcess } = require("./../utils/spawn-process");
44
const walk = require("./../utils/walk");
5-
const esbuild = require("esbuild");
5+
const rollup = require("rollup");
6+
const { nodeResolve } = require("@rollup/plugin-node-resolve");
7+
const typescript = require("@rollup/plugin-typescript");
68

79
const root = path.join(__dirname, "..", "..");
810

@@ -18,7 +20,6 @@ module.exports = class Inliner {
1820
this.platform = "node";
1921
this.isPackage = fs.existsSync(path.join(root, "packages", pkg));
2022
this.isLib = fs.existsSync(path.join(root, "lib", pkg));
21-
this.isClient = !this.isPackage && !this.isLib;
2223
this.isCore = pkg === "core";
2324
this.reExportStubs = false;
2425
this.subfolder = this.isPackage ? "packages" : this.isLib ? "lib" : "clients";
@@ -154,28 +155,68 @@ module.exports = class Inliner {
154155
return this;
155156
}
156157

157-
this.variantExternalsForEsBuild = this.variantExternals.map(
158-
(variant) => "*/" + path.basename(variant).replace(/.js$/, "")
158+
const variantExternalsForRollup = this.variantExternals.map((variant) =>
159+
path.basename(variant).replace(/.js$/, "")
159160
);
160161

161-
const buildOptions = {
162-
platform: this.platform,
163-
target: ["node18"],
164-
bundle: true,
165-
format: "cjs",
166-
mainFields: ["main"],
167-
allowOverwrite: true,
168-
entryPoints: [path.join(root, this.subfolder, this.package, "src", "index.ts")],
169-
supported: {
170-
"dynamic-import": false,
162+
const entryPoint = path.join(root, this.subfolder, this.package, "src", "index.ts");
163+
164+
const inputOptions = {
165+
input: entryPoint,
166+
plugins: [
167+
nodeResolve(),
168+
typescript({
169+
compilerOptions: {
170+
importHelpers: true,
171+
noEmitHelpers: false,
172+
module: "esnext",
173+
target: "es2022",
174+
noCheck: true,
175+
removeComments: true,
176+
},
177+
}),
178+
],
179+
external: (id) => {
180+
const relative = !!id.match(/^\.?\.?\//);
181+
if (!relative) {
182+
this.verbose && console.log("EXTERN (pkg)", id);
183+
return true;
184+
}
185+
186+
const local = id.includes(`/packages/`) && id.includes(`/dist-es/`);
187+
if (local) {
188+
this.verbose && console.log("EXTERN (local)", id);
189+
return true;
190+
}
191+
192+
if (id === entryPoint) {
193+
this.verbose && console.log("INTERN (entry point)", id);
194+
return false;
195+
}
196+
197+
for (const file of variantExternalsForRollup) {
198+
const idWithoutExtension = id.replace(/\.ts$/, "");
199+
if (idWithoutExtension.endsWith(file)) {
200+
this.verbose && console.log("EXTERN (variant)", id);
201+
return true;
202+
}
203+
}
204+
205+
this.verbose && console.log("INTERN (invariant)", id);
206+
return false;
171207
},
172-
outfile: this.outfile,
173-
keepNames: true,
174-
packages: "external",
175-
external: ["@smithy/*", "@aws-sdk/*", "node_modules/*", ...this.variantExternalsForEsBuild],
176208
};
177209

178-
await esbuild.build(buildOptions);
210+
const outputOptions = {
211+
file: this.outfile,
212+
format: "cjs",
213+
exports: "named",
214+
preserveModules: false,
215+
};
216+
217+
const bundle = await rollup.rollup(inputOptions);
218+
await bundle.write(outputOptions);
219+
await bundle.close();
179220

180221
if (this.isCore) {
181222
const submodules = fs.readdirSync(path.join(root, this.subfolder, this.package, "src", "submodules"));
@@ -189,12 +230,18 @@ module.exports = class Inliner {
189230
) {
190231
continue;
191232
}
192-
await esbuild.build({
193-
...buildOptions,
194-
keepNames: false,
195-
entryPoints: [path.join(root, this.subfolder, this.package, "src", "submodules", submodule, "index.ts")],
196-
outfile: path.join(root, this.subfolder, this.package, "dist-cjs", "submodules", submodule, "index.js"),
233+
234+
const submoduleBundle = await rollup.rollup({
235+
...inputOptions,
236+
input: path.join(root, this.subfolder, this.package, "src", "submodules", submodule, "index.ts"),
197237
});
238+
239+
await submoduleBundle.write({
240+
...outputOptions,
241+
file: path.join(root, this.subfolder, this.package, "dist-cjs", "submodules", submodule, "index.js"),
242+
});
243+
244+
await submoduleBundle.close();
198245
}
199246
}
200247

@@ -398,10 +445,33 @@ module.exports = class Inliner {
398445
}
399446

400447
// check ESM compat.
401-
const tmpFileContents = this.canonicalExports
402-
.filter((sym) => !sym.includes(":"))
403-
.map((sym) => `import { ${sym} } from "${this.pkgJson.name}";`)
404-
.join("\n");
448+
const tmpFileContents =
449+
`import assert from "node:assert";\n` +
450+
this.canonicalExports
451+
.filter((sym) => !sym.includes(":"))
452+
.map((sym) => {
453+
if (
454+
[
455+
"getDefaultClientConfiguration", // renamed as an alias
456+
"generateIdempotencyToken", // sometimes called v4
457+
"expectInt", // aliased to expectLong
458+
"handleFloat", // aliased to limitedParseDouble
459+
"limitedParseFloat", // aliased to limitedParseDouble
460+
"strictParseFloat", // aliased to strictParseDouble
461+
"strictParseInt", // aliased to strictParseLong
462+
].includes(sym)
463+
) {
464+
return `import { ${sym} } from "${this.pkgJson.name}";`;
465+
}
466+
return `import { ${sym} } from "${this.pkgJson.name}";
467+
if (typeof ${sym} === "function") {
468+
if (${sym}.name !== "${sym}") {
469+
throw new Error(${sym}.name + " does not equal expected ${sym}.")
470+
}
471+
}
472+
`;
473+
})
474+
.join("\n");
405475
fs.writeFileSync(path.join(__dirname, "tmp", this.package + ".mjs"), tmpFileContents, "utf-8");
406476
await spawnProcess("node", [path.join(__dirname, "tmp", this.package + ".mjs")]);
407477
if (this.verbose) {

tsconfig.cjs.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"importHelpers": true,
55
"module": "commonjs",
66
"noEmitHelpers": false,
7-
"target": "ES2018",
7+
"target": "es2022",
88
"noCheck": true
99
}
1010
}

tsconfig.es.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"module": "ESNext",
66
"moduleResolution": "bundler",
77
"noEmitHelpers": false,
8-
"target": "ES2020",
8+
"target": "es2022",
99
"noCheck": true
1010
}
1111
}

0 commit comments

Comments
 (0)