@@ -2,7 +2,9 @@ const fs = require("fs");
2
2
const path = require ( "path" ) ;
3
3
const { spawnProcess } = require ( "./../utils/spawn-process" ) ;
4
4
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" ) ;
6
8
7
9
const root = path . join ( __dirname , ".." , ".." ) ;
8
10
@@ -18,7 +20,6 @@ module.exports = class Inliner {
18
20
this . platform = "node" ;
19
21
this . isPackage = fs . existsSync ( path . join ( root , "packages" , pkg ) ) ;
20
22
this . isLib = fs . existsSync ( path . join ( root , "lib" , pkg ) ) ;
21
- this . isClient = ! this . isPackage && ! this . isLib ;
22
23
this . isCore = pkg === "core" ;
23
24
this . reExportStubs = false ;
24
25
this . subfolder = this . isPackage ? "packages" : this . isLib ? "lib" : "clients" ;
@@ -154,28 +155,68 @@ module.exports = class Inliner {
154
155
return this ;
155
156
}
156
157
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 $ / , "" )
159
160
) ;
160
161
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 ;
171
207
} ,
172
- outfile : this . outfile ,
173
- keepNames : true ,
174
- packages : "external" ,
175
- external : [ "@smithy/*" , "@aws-sdk/*" , "node_modules/*" , ...this . variantExternalsForEsBuild ] ,
176
208
} ;
177
209
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 ( ) ;
179
220
180
221
if ( this . isCore ) {
181
222
const submodules = fs . readdirSync ( path . join ( root , this . subfolder , this . package , "src" , "submodules" ) ) ;
@@ -189,12 +230,18 @@ module.exports = class Inliner {
189
230
) {
190
231
continue ;
191
232
}
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" ) ,
197
237
} ) ;
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 ( ) ;
198
245
}
199
246
}
200
247
@@ -398,10 +445,33 @@ module.exports = class Inliner {
398
445
}
399
446
400
447
// 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" ) ;
405
475
fs . writeFileSync ( path . join ( __dirname , "tmp" , this . package + ".mjs" ) , tmpFileContents , "utf-8" ) ;
406
476
await spawnProcess ( "node" , [ path . join ( __dirname , "tmp" , this . package + ".mjs" ) ] ) ;
407
477
if ( this . verbose ) {
0 commit comments