2
2
BuildAdapter ,
3
3
BuildAdapterOptions ,
4
4
BuildResult ,
5
+ EntryPoint ,
6
+ logger ,
5
7
} from '@softarc/native-federation/build' ;
6
8
import * as esbuild from 'esbuild' ;
7
9
import { rollup } from 'rollup' ;
@@ -25,10 +27,18 @@ export type ReplacementConfig = {
25
27
file : string ;
26
28
} ;
27
29
30
+ type EntryPointWithMeta = EntryPoint & {
31
+ meta : {
32
+ isPkg : boolean ;
33
+ originalFileName : string ;
34
+ } ;
35
+ } ;
36
+
28
37
export interface EsBuildAdapterConfig {
29
38
plugins : esbuild . Plugin [ ] ;
30
39
fileReplacements ?: Record < string , string | ReplacementConfig > ;
31
40
skipRollup ?: boolean ;
41
+ /** Identify packages for which compensating missing named exports */
32
42
compensateExports ?: RegExp [ ] ;
33
43
loader ?: { [ ext : string ] : esbuild . Loader } ;
34
44
}
@@ -43,11 +53,17 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
43
53
44
54
// TODO: Do we need to prepare packages anymore as esbuild has evolved?
45
55
46
- for ( const entryPoint of entryPoints ) {
56
+ const preparedEntryPoints = entryPoints as EntryPointWithMeta [ ] ;
57
+ for ( const entryPoint of preparedEntryPoints ) {
47
58
const isPkg = entryPoint . fileName . includes ( 'node_modules' ) ;
48
59
const pkgName = isPkg ? inferePkgName ( entryPoint . fileName ) : '' ;
49
60
const tmpFolder = `node_modules/.tmp/${ pkgName } ` ;
50
61
62
+ entryPoint . meta = {
63
+ originalFileName : entryPoint . fileName ,
64
+ isPkg,
65
+ } ;
66
+
51
67
if ( isPkg ) {
52
68
await prepareNodePackage (
53
69
entryPoint . fileName ,
@@ -56,13 +72,12 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
56
72
config ,
57
73
! ! options . dev
58
74
) ;
59
-
60
75
entryPoint . fileName = tmpFolder ;
61
76
}
62
77
}
63
78
64
79
const ctx = await esbuild . context ( {
65
- entryPoints : entryPoints . map ( ( ep ) => ( {
80
+ entryPoints : preparedEntryPoints . map ( ( ep ) => ( {
66
81
in : ep . fileName ,
67
82
out : path . parse ( ep . outName ) . name ,
68
83
} ) ) ,
@@ -82,16 +97,18 @@ export function createEsBuildAdapter(config: EsBuildAdapterConfig) {
82
97
const result = await ctx . rebuild ( ) ;
83
98
const writtenFiles = writeResult ( result , outdir ) ;
84
99
ctx . dispose ( ) ;
100
+ preparedEntryPoints . forEach ( ( entryPoint ) => {
101
+ const { meta, fileName, outName } = entryPoint ;
102
+ const normEntryPoint = meta . originalFileName . replace ( / \\ / g, '/' ) ;
103
+ if (
104
+ meta . isPkg &&
105
+ config ?. compensateExports ?. find ( ( regExp ) => regExp . exec ( normEntryPoint ) )
106
+ ) {
107
+ logger . verbose ( 'compensate exports for ' + meta . originalFileName ) ;
108
+ compensateExports ( fileName , path . join ( outdir , outName ) ) ;
109
+ }
110
+ } ) ;
85
111
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
- // }
95
112
} ;
96
113
}
97
114
0 commit comments