@@ -19,8 +19,8 @@ module.exports = class Inliner {
1919 this . isPackage = fs . existsSync ( path . join ( root , "packages" , pkg ) ) ;
2020 this . isLib = fs . existsSync ( path . join ( root , "lib" , pkg ) ) ;
2121 this . isClient = ! this . isPackage && ! this . isLib ;
22- this . submodules = [ "core" , "nested-clients" ] ;
23- this . hasSubmodules = this . submodules . includes ( pkg ) ;
22+ this . submodulePackages = [ "core" , "nested-clients" ] ;
23+ this . hasSubmodules = this . submodulePackages . includes ( pkg ) ;
2424 this . reExportStubs = false ;
2525 this . subfolder = this . isPackage ? "packages" : this . isLib ? "lib" : "clients" ;
2626 this . verbose = process . env . DEBUG || process . argv . includes ( "--debug" ) ;
@@ -210,6 +210,14 @@ module.exports = class Inliner {
210210 ...buildOptions ,
211211 entryPoints : [ path . join ( root , this . subfolder , this . package , "src" , "submodules" , submodule , "index.ts" ) ] ,
212212 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+ ] ,
213221 } ) ;
214222 }
215223 }
@@ -302,14 +310,29 @@ module.exports = class Inliner {
302310 }
303311 return contents ;
304312 } ;
313+ if ( this . verbose ) {
314+ console . log ( "Fixing imports for main file" , path . dirname ( this . outfile ) ) ;
315+ }
305316 this . indexContents = fixImportsForFile ( this . indexContents ) ;
306317 fs . writeFileSync ( this . outfile , this . indexContents , "utf-8" ) ;
307318 if ( this . hasSubmodules ) {
308319 const submodules = fs . readdirSync ( path . join ( path . dirname ( this . outfile ) , "submodules" ) ) ;
309320 for ( const submodule of submodules ) {
310321 const submoduleIndexPath = path . join ( path . dirname ( this . outfile ) , "submodules" , submodule , "index.js" ) ;
311322 const submoduleIndexContents = fs . readFileSync ( submoduleIndexPath , "utf-8" ) ;
312- fs . writeFileSync ( submoduleIndexPath , fixImportsForFile ( submoduleIndexContents , `/submodules/${ submodule } ` ) ) ;
323+ if ( this . verbose ) {
324+ console . log ( "Fixing imports for submodule file" , path . dirname ( submoduleIndexPath ) ) ;
325+ }
326+ fs . writeFileSync (
327+ submoduleIndexPath ,
328+ fixImportsForFile ( submoduleIndexContents , new RegExp ( `/submodules/(${ submodules . join ( "|" ) } )` , "g" ) )
329+ ) ;
330+ try {
331+ require ( submoduleIndexPath ) ;
332+ } catch ( e ) {
333+ console . error ( `File ${ submoduleIndexPath } has import errors.` ) ;
334+ throw e ;
335+ }
313336 }
314337 }
315338 return this ;
@@ -446,6 +469,27 @@ module.exports = class Inliner {
446469 . join ( "\n" ) ;
447470 fs . writeFileSync ( path . join ( __dirname , "tmp" , this . package + ".mjs" ) , tmpFileContents , "utf-8" ) ;
448471 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+
449493 if ( this . verbose ) {
450494 console . log ( "ESM compatibility verified." ) ;
451495 }
0 commit comments