@@ -2,7 +2,9 @@ const fs = require("fs");
22const path = require ( "path" ) ;
33const { spawnProcess } = require ( "./../utils/spawn-process" ) ;
44const 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
79const 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 ( / .j s $ / , "" )
158+ const variantExternalsForRollup = this . variantExternals . map ( ( variant ) =>
159+ path . basename ( variant ) . replace ( / .j s $ / , "" )
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 ( / \. t s $ / , "" ) ;
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 ) {
0 commit comments