22 BuildAdapter ,
33 BuildAdapterOptions ,
44 BuildResult ,
5+ EntryPoint ,
6+ logger ,
57} from '@softarc/native-federation/build' ;
68import * as esbuild from 'esbuild' ;
79import { rollup } from 'rollup' ;
@@ -25,10 +27,18 @@ export type ReplacementConfig = {
2527 file : string ;
2628} ;
2729
30+ type EntryPointWithMeta = EntryPoint & {
31+ meta : {
32+ isPkg : boolean ;
33+ originalFileName : string ;
34+ } ;
35+ } ;
36+
2837export interface EsBuildAdapterConfig {
2938 plugins : esbuild . Plugin [ ] ;
3039 fileReplacements ?: Record < string , string | ReplacementConfig > ;
3140 skipRollup ?: boolean ;
41+ /** Identify packages for which compensating missing named exports */
3242 compensateExports ?: RegExp [ ] ;
3343 loader ?: { [ ext : string ] : esbuild . Loader } ;
3444}
@@ -43,7 +53,8 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
4353
4454 // TODO: Do we need to prepare packages anymore as esbuild has evolved?
4555
46- for ( const entryPoint of entryPoints ) {
56+ const preparedEntryPoints = entryPoints as EntryPointWithMeta [ ] ;
57+ for ( const entryPoint of preparedEntryPoints ) {
4758 const isPkg = entryPoint . fileName . includes ( 'node_modules' ) ;
4859 const pkgName = isPkg ? inferePkgName ( entryPoint . fileName ) : '' ;
4960 const tmpFolder = `node_modules/.tmp/${ pkgName } ` ;
@@ -56,13 +67,16 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
5667 config ,
5768 ! ! options . dev
5869 ) ;
59-
70+ entryPoint . meta = {
71+ originalFileName : entryPoint . fileName ,
72+ isPkg,
73+ } ;
6074 entryPoint . fileName = tmpFolder ;
6175 }
6276 }
6377
6478 const ctx = await esbuild . context ( {
65- entryPoints : entryPoints . map ( ( ep ) => ( {
79+ entryPoints : preparedEntryPoints . map ( ( ep ) => ( {
6680 in : ep . fileName ,
6781 out : path . parse ( ep . outName ) . name ,
6882 } ) ) ,
@@ -82,16 +96,18 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
8296 const result = await ctx . rebuild ( ) ;
8397 const writtenFiles = writeResult ( result , outdir ) ;
8498 ctx . dispose ( ) ;
99+ preparedEntryPoints . forEach ( ( entryPoint ) => {
100+ const { meta, fileName, outName } = entryPoint ;
101+ const normEntryPoint = meta . originalFileName . replace ( / \\ / g, '/' ) ;
102+ if (
103+ meta . isPkg &&
104+ config ?. compensateExports ?. find ( ( regExp ) => regExp . exec ( normEntryPoint ) )
105+ ) {
106+ logger . verbose ( 'compensate exports for ' + meta . originalFileName ) ;
107+ compensateExports ( fileName , path . join ( outdir , outName ) ) ;
108+ }
109+ } ) ;
85110 return writtenFiles . map ( ( fileName ) => ( { fileName } ) ) ;
86-
87- // const normEntryPoint = entryPoint.replace(/\\/g, '/');
88- // if (
89- // isPkg &&
90- // config?.compensateExports?.find((regExp) => regExp.exec(normEntryPoint))
91- // ) {
92- // logger.verbose('compensate exports for ' + tmpFolder);
93- // compensateExports(tmpFolder, outfile);
94- // }
95111 } ;
96112}
97113
0 commit comments