@@ -183,15 +183,29 @@ module.exports = class Inliner {
183183 if ( this . hasSubmodules ) {
184184 const submodules = fs . readdirSync ( path . join ( root , this . subfolder , this . package , "src" , "submodules" ) ) ;
185185 for ( const submodule of submodules ) {
186- fs . rmSync ( path . join ( path . join ( root , this . subfolder , this . package , "dist-cjs" , "submodules" , submodule ) ) , {
187- recursive : true ,
188- force : true ,
189- } ) ;
190186 if (
191187 ! fs . lstatSync ( path . join ( root , this . subfolder , this . package , "src" , "submodules" , submodule ) ) . isDirectory ( )
192188 ) {
193189 continue ;
194190 }
191+
192+ // remove invariant files.
193+ for await ( const file of walk (
194+ path . join ( root , this . subfolder , this . package , "dist-cjs" , "submodules" , submodule )
195+ ) ) {
196+ const stat = fs . lstatSync ( file ) ;
197+ if ( this . variantExternals . find ( ( ext ) => file . endsWith ( ext ) ) ) {
198+ continue ;
199+ }
200+ if ( stat . isDirectory ( ) ) {
201+ if ( fs . readdirSync ( file ) . length === 0 ) {
202+ fs . rmdirSync ( file ) ;
203+ }
204+ } else {
205+ fs . rmSync ( file ) ;
206+ }
207+ }
208+
195209 await esbuild . build ( {
196210 ...buildOptions ,
197211 entryPoints : [ path . join ( root , this . subfolder , this . package , "src" , "submodules" , submodule , "index.ts" ) ] ,
@@ -272,21 +286,32 @@ module.exports = class Inliner {
272286 return this ;
273287 }
274288 this . indexContents = fs . readFileSync ( this . outfile , "utf-8" ) ;
275- for ( const variant of Object . keys ( this . variantMap ) ) {
276- const basename = path . basename ( variant ) . replace ( / .j s $ / , "" ) ;
277- const dirname = path . dirname ( variant ) ;
289+ const fixImportsForFile = ( contents , remove = "" ) => {
290+ for ( const variant of Object . keys ( this . variantMap ) ) {
291+ const basename = path . basename ( variant ) . replace ( / .j s $ / , "" ) ;
292+ const dirname = path . dirname ( variant ) ;
278293
279- const find = new RegExp ( `require\\("\\.(.*?)/${ basename } "\\)` , "g" ) ;
280- const replace = `require("./${ dirname } /${ basename } ")` ;
294+ const find = new RegExp ( `require\\("\\.(.*?)/${ basename } "\\)` , "g" ) ;
295+ const replace = `require("./${ dirname } /${ basename } ")` . replace ( remove , "" ) ;
281296
282- this . indexContents = this . indexContents . replace ( find , replace ) ;
297+ contents = contents . replace ( find , replace ) ;
283298
284- if ( this . verbose ) {
285- console . log ( "Replacing" , find , "with" , replace ) ;
299+ if ( this . verbose ) {
300+ console . log ( "Replacing" , find , "with" , replace , "removed=" , remove ) ;
301+ }
286302 }
287- }
288-
303+ return contents ;
304+ } ;
305+ this . indexContents = fixImportsForFile ( this . indexContents ) ;
289306 fs . writeFileSync ( this . outfile , this . indexContents , "utf-8" ) ;
307+ if ( this . hasSubmodules ) {
308+ const submodules = fs . readdirSync ( path . join ( path . dirname ( this . outfile ) , "submodules" ) ) ;
309+ for ( const submodule of submodules ) {
310+ const submoduleIndexPath = path . join ( path . dirname ( this . outfile ) , "submodules" , submodule , "index.js" ) ;
311+ const submoduleIndexContents = fs . readFileSync ( submoduleIndexPath , "utf-8" ) ;
312+ fs . writeFileSync ( submoduleIndexPath , fixImportsForFile ( submoduleIndexContents , `/submodules/${ submodule } ` ) ) ;
313+ }
314+ }
290315 return this ;
291316 }
292317
@@ -376,19 +401,34 @@ module.exports = class Inliner {
376401 . map ( ( variant ) => path . basename ( variant ) . replace ( / .j s $ / , "" ) )
377402 ) ;
378403
379- for ( const line of this . indexContents . split ( "\n" ) ) {
380- // we expect to see a line with require() and the variant external in it
381- if ( line . includes ( "require(" ) ) {
382- const checkOrder = [ ...externalsToCheck ] . sort ( ) . reverse ( ) ;
383- for ( const external of checkOrder ) {
384- if ( line . includes ( external ) ) {
385- if ( this . verbose ) {
386- console . log ( "Inline index confirmed require() for variant external:" , external ) ;
404+ const inspect = ( contents ) => {
405+ for ( const line of contents . split ( "\n" ) ) {
406+ // we expect to see a line with require() and the variant external in it
407+ if ( line . includes ( "require(" ) ) {
408+ const checkOrder = [ ...externalsToCheck ] . sort ( ) . reverse ( ) ;
409+ for ( const external of checkOrder ) {
410+ if ( line . includes ( external ) ) {
411+ if ( this . verbose ) {
412+ console . log ( "Inline index confirmed require() for variant external:" , external ) ;
413+ }
414+ externalsToCheck . delete ( external ) ;
387415 }
388- externalsToCheck . delete ( external ) ;
389416 }
390417 }
391418 }
419+ } ;
420+
421+ inspect ( this . indexContents ) ;
422+
423+ if ( this . hasSubmodules ) {
424+ const submodules = fs . readdirSync ( path . join ( path . dirname ( this . outfile ) , "submodules" ) ) ;
425+ for ( const submodule of submodules ) {
426+ const submoduleIndexContents = fs . readFileSync (
427+ path . join ( path . dirname ( this . outfile ) , "submodules" , submodule , "index.js" ) ,
428+ "utf-8"
429+ ) ;
430+ inspect ( submoduleIndexContents ) ;
431+ }
392432 }
393433
394434 if ( externalsToCheck . size ) {
0 commit comments