@@ -19,8 +19,8 @@ module.exports = class Inliner {
19
19
this . isPackage = fs . existsSync ( path . join ( root , "packages" , pkg ) ) ;
20
20
this . isLib = fs . existsSync ( path . join ( root , "lib" , pkg ) ) ;
21
21
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 ) ;
24
24
this . reExportStubs = false ;
25
25
this . subfolder = this . isPackage ? "packages" : this . isLib ? "lib" : "clients" ;
26
26
this . verbose = process . env . DEBUG || process . argv . includes ( "--debug" ) ;
@@ -210,6 +210,14 @@ module.exports = class Inliner {
210
210
...buildOptions ,
211
211
entryPoints : [ path . join ( root , this . subfolder , this . package , "src" , "submodules" , submodule , "index.ts" ) ] ,
212
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
+ ] ,
213
221
} ) ;
214
222
}
215
223
}
@@ -302,14 +310,29 @@ module.exports = class Inliner {
302
310
}
303
311
return contents ;
304
312
} ;
313
+ if ( this . verbose ) {
314
+ console . log ( "Fixing imports for main file" , path . dirname ( this . outfile ) ) ;
315
+ }
305
316
this . indexContents = fixImportsForFile ( this . indexContents ) ;
306
317
fs . writeFileSync ( this . outfile , this . indexContents , "utf-8" ) ;
307
318
if ( this . hasSubmodules ) {
308
319
const submodules = fs . readdirSync ( path . join ( path . dirname ( this . outfile ) , "submodules" ) ) ;
309
320
for ( const submodule of submodules ) {
310
321
const submoduleIndexPath = path . join ( path . dirname ( this . outfile ) , "submodules" , submodule , "index.js" ) ;
311
322
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
+ }
313
336
}
314
337
}
315
338
return this ;
@@ -446,6 +469,27 @@ module.exports = class Inliner {
446
469
. join ( "\n" ) ;
447
470
fs . writeFileSync ( path . join ( __dirname , "tmp" , this . package + ".mjs" ) , tmpFileContents , "utf-8" ) ;
448
471
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
+
449
493
if ( this . verbose ) {
450
494
console . log ( "ESM compatibility verified." ) ;
451
495
}
0 commit comments