@@ -2,7 +2,10 @@ 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" ) ;
8+ const json = require ( "@rollup/plugin-json" ) ;
69
710const root = path . join ( __dirname , ".." , ".." ) ;
811
@@ -143,6 +146,8 @@ module.exports = class Inliner {
143146 }
144147 }
145148
149+ this . variantExternals = [ ...new Set ( this . variantExternals ) ] ;
150+
146151 return this ;
147152 }
148153
@@ -155,30 +160,67 @@ module.exports = class Inliner {
155160 return this ;
156161 }
157162
158- this . variantExternalsForEsBuild = this . variantExternals . map (
159- ( variant ) => "*/" + path . basename ( variant ) . replace ( / .j s $ / , "" )
160- ) ;
163+ const variantExternalsForRollup = this . variantExternals . map ( ( variant ) => variant . replace ( / .j s $ / , "" ) ) ;
164+
165+ const entryPoint = path . join ( root , this . subfolder , this . package , "src" , "index.ts" ) ;
166+
167+ const inputOptions = ( externals ) => ( {
168+ input : entryPoint ,
169+ plugins : [
170+ nodeResolve ( ) ,
171+ json ( ) ,
172+ typescript ( {
173+ compilerOptions : {
174+ importHelpers : true ,
175+ noEmitHelpers : false ,
176+ module : "esnext" ,
177+ target : "es2022" ,
178+ noCheck : true ,
179+ removeComments : true ,
180+ } ,
181+ } ) ,
182+ ] ,
183+ external : ( id ) => {
184+ const relative = ! ! id . match ( / ^ \. ? \. ? \/ / ) ;
185+ if ( ! relative ) {
186+ this . verbose && console . log ( "EXTERN (pkg)" , id ) ;
187+ return true ;
188+ }
161189
162- const buildOptions = {
163- platform : this . platform ,
164- target : [ "node18" ] ,
165- bundle : true ,
166- format : "cjs" ,
167- mainFields : [ "main" ] ,
168- allowOverwrite : true ,
169- entryPoints : [ path . join ( root , this . subfolder , this . package , "src" , "index.ts" ) ] ,
170- supported : {
171- "dynamic-import" : false ,
190+ const local = id . includes ( `/packages/` ) && id . includes ( `/dist-es/` ) ;
191+ if ( local ) {
192+ this . verbose && console . log ( "EXTERN (local)" , id ) ;
193+ return true ;
194+ }
195+
196+ if ( id === entryPoint ) {
197+ this . verbose && console . log ( "INTERN (entry point)" , id ) ;
198+ return false ;
199+ }
200+
201+ for ( const file of externals ) {
202+ const idWithoutExtension = id . replace ( / \. t s $ / , "" ) ;
203+ if ( idWithoutExtension . endsWith ( path . basename ( file ) ) ) {
204+ this . verbose && console . log ( "EXTERN (variant)" , id ) ;
205+ return true ;
206+ }
207+ }
208+
209+ this . verbose && console . log ( "INTERN (invariant)" , id ) ;
210+ return false ;
172211 } ,
173- outfile : this . outfile ,
174- keepNames : true ,
175- packages : "external" ,
176- external : [ "@smithy/*" , "@aws-sdk/*" , "node_modules/*" , ...this . variantExternalsForEsBuild ] ,
212+ } ) ;
213+
214+ const outputOptions = {
215+ file : this . outfile ,
216+ format : "cjs" ,
217+ exports : "named" ,
218+ preserveModules : false ,
177219 } ;
178220
179- if ( ! this . hasSubmodules ) {
180- await esbuild . build ( buildOptions ) ;
181- }
221+ const bundle = await rollup . rollup ( inputOptions ( variantExternalsForRollup ) ) ;
222+ await bundle . write ( outputOptions ) ;
223+ await bundle . close ( ) ;
182224
183225 if ( this . hasSubmodules ) {
184226 const submodules = fs . readdirSync ( path . join ( root , this . subfolder , this . package , "src" , "submodules" ) ) ;
@@ -206,19 +248,39 @@ module.exports = class Inliner {
206248 }
207249 }
208250
209- await esbuild . build ( {
210- ...buildOptions ,
211- entryPoints : [ path . join ( root , this . subfolder , this . package , "src" , "submodules" , submodule , "index.ts" ) ] ,
212- outfile : path . join ( root , this . subfolder , this . package , "dist-cjs" , "submodules" , submodule , "index.js" ) ,
213- external : [
214- "@smithy/*" ,
215- "@aws-sdk/*" ,
216- "node_modules/*" ,
217- ...this . variantExternals
218- . filter ( ( variant ) => variant . includes ( `submodules/${ submodule } ` ) )
219- . map ( ( variant ) => "*/" + path . basename ( variant ) . replace ( / .j s $ / , "" ) ) ,
220- ] ,
251+ // remove remaining empty directories.
252+ const submoduleFolder = path . join ( root , this . subfolder , this . package , "dist-cjs" , "submodules" , submodule ) ;
253+ function rmdirEmpty ( dir ) {
254+ for ( const entry of fs . readdirSync ( dir ) ) {
255+ const fullPath = path . join ( dir , entry ) ;
256+ if ( fs . lstatSync ( fullPath ) . isDirectory ( ) ) {
257+ if ( fs . readdirSync ( fullPath ) . length ) {
258+ rmdirEmpty ( fullPath ) ;
259+ } else {
260+ fs . rmdirSync ( fullPath ) ;
261+ }
262+ }
263+ }
264+ }
265+ rmdirEmpty ( submoduleFolder ) ;
266+
267+ const submoduleVariants = variantExternalsForRollup . filter ( ( external ) =>
268+ external . includes ( `submodules/${ submodule } ` )
269+ ) ;
270+
271+ const submoduleOptions = inputOptions ( submoduleVariants ) ;
272+
273+ const submoduleBundle = await rollup . rollup ( {
274+ ...submoduleOptions ,
275+ input : path . join ( root , this . subfolder , this . package , "src" , "submodules" , submodule , "index.ts" ) ,
276+ } ) ;
277+
278+ await submoduleBundle . write ( {
279+ ...outputOptions ,
280+ file : path . join ( root , this . subfolder , this . package , "dist-cjs" , "submodules" , submodule , "index.js" ) ,
221281 } ) ;
282+
283+ await submoduleBundle . close ( ) ;
222284 }
223285 }
224286
@@ -463,33 +525,37 @@ module.exports = class Inliner {
463525 }
464526
465527 // check ESM compat.
466- const tmpFileContents = this . canonicalExports
467- . filter ( ( sym ) => ! sym . includes ( ":" ) )
468- . map ( ( sym ) => `import { ${ sym } } from "${ this . pkgJson . name } ";` )
469- . join ( "\n" ) ;
528+ const tmpFileContents =
529+ `import assert from "node:assert";
530+
531+ const namingExceptions = [
532+ "paginateOperation" // name for all paginators.
533+ ];
534+ ` +
535+ this . canonicalExports
536+ . filter ( ( sym ) => ! sym . includes ( ":" ) )
537+ . map ( ( sym ) => {
538+ if (
539+ [
540+ "AWSSDKSigV4Signer" , // deprecated alias for AwsSdkSigV4Signer
541+ "resolveAWSSDKSigV4Config" , // deprecated alias for resolveAwsSdkSigV4Config
542+ "__Client" , // base Client in SDK clients
543+ "$Command" , // base Command in SDK clients
544+ ] . includes ( sym )
545+ ) {
546+ return `import { ${ sym } } from "${ this . pkgJson . name } ";` ;
547+ }
548+ return `import { ${ sym } } from "${ this . pkgJson . name } ";
549+ if (typeof ${ sym } === "function") {
550+ if (${ sym } .name !== "${ sym } " && !namingExceptions.includes(${ sym } .name)) {
551+ throw new Error(${ sym } .name + " does not equal expected ${ sym } .")
552+ }
553+ }
554+ ` ;
555+ } )
556+ . join ( "\n" ) ;
470557 fs . writeFileSync ( path . join ( __dirname , "tmp" , this . package + ".mjs" ) , tmpFileContents , "utf-8" ) ;
471558 await spawnProcess ( "node" , [ path . join ( __dirname , "tmp" , this . package + ".mjs" ) ] ) ;
472-
473- if ( this . hasSubmodules ) {
474- const submodules = fs . readdirSync ( path . join ( root , this . subfolder , this . package , "src" , "submodules" ) ) ;
475- for ( const submodule of submodules ) {
476- const canonicalExports = Object . keys (
477- require ( path . join ( root , this . subfolder , this . package , "dist-cjs" , "submodules" , submodule , "index.js" ) )
478- ) ;
479- const tmpFileContents = canonicalExports
480- . filter ( ( sym ) => ! sym . includes ( ":" ) )
481- . map ( ( sym ) => `import { ${ sym } } from "${ this . pkgJson . name } /${ submodule } ";` )
482- . join ( "\n" ) ;
483- const tmpFilePath = path . join ( __dirname , "tmp" , this . package + "_" + submodule + ".mjs" ) ;
484- fs . writeFileSync ( tmpFilePath , tmpFileContents , "utf-8" ) ;
485- await spawnProcess ( "node" , [ tmpFilePath ] ) ;
486- fs . rmSync ( tmpFilePath ) ;
487- if ( this . verbose ) {
488- console . log ( "ESM compatibility verified for submodule" , submodule ) ;
489- }
490- }
491- }
492-
493559 if ( this . verbose ) {
494560 console . log ( "ESM compatibility verified." ) ;
495561 }
0 commit comments