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