1
- import { BuildAdapter } from '@softarc/native-federation/build' ;
1
+ import { BuildAdapter , MappedPath } from '@softarc/native-federation/build' ;
2
2
import * as esbuild from 'esbuild' ;
3
3
import { createCompilerPlugin } from '@angular-devkit/build-angular/src/builders/browser-esbuild/compiler-plugin' ;
4
4
import { createSharedMappingsPlugin } from './shared-mappings-plugin' ;
5
- import { prepareNodePackage } from './prepare-node-package' ;
5
+ import { prepareNodePackage as runRollup } from './prepare-node-package' ;
6
6
7
- const SKIP_PACKAGE_PREPARATION = [ '@angular' , '@ngrx' , 'rxjs' , 'zone.js' ] ;
7
+ import * as fs from 'fs' ;
8
+ import * as path from 'path' ;
9
+
10
+ import { PluginItem , transformAsync } from '@babel/core' ;
11
+
12
+ // const SKIP_PACKAGE_PREPARATION = ['@angular', '@ngrx', 'rxjs', 'zone.js'];
8
13
9
14
export const AngularEsBuildAdapter : BuildAdapter = async ( options ) => {
10
- const { entryPoint, tsConfigPath, external, outfile, mappedPaths, packageName, esm } = options ;
15
+ const {
16
+ entryPoint,
17
+ tsConfigPath,
18
+ external,
19
+ outfile,
20
+ mappedPaths,
21
+ packageName,
22
+ esm,
23
+ kind,
24
+ } = options ;
25
+
26
+ // const pNameOrEmpty = packageName ?? '';
27
+
28
+ // const preparePackage = entryPoint.includes("node_modules")
29
+ // && !(SKIP_PACKAGE_PREPARATION.find(p => pNameOrEmpty?.split('/')[0] === p)
30
+ // || esm);
31
+
32
+ // const pkgName = preparePackage ? inferePkgName(entryPoint) : "";
33
+ // const tmpFolder = `node_modules/.tmp/native-federation/${pkgName}`;
34
+
35
+ if ( kind === 'shared-package' ) {
36
+ await runRollup ( entryPoint , external , outfile ) ;
37
+ } else {
38
+ await runEsbuild (
39
+ entryPoint ,
40
+ external ,
41
+ outfile ,
42
+ tsConfigPath ,
43
+ mappedPaths ,
44
+ // kind === 'shared-package' ? [] : null,
45
+ // kind === 'shared-package' ? path.dirname(entryPoint) : undefined,
46
+ ) ;
47
+ }
11
48
12
- const pNameOrEmpty = packageName ?? '' ;
49
+ if ( kind === 'shared-package' && fs . existsSync ( outfile ) ) {
50
+ await link ( outfile ) ;
51
+ }
52
+ } ;
53
+
54
+ async function link ( outfile : string ) {
55
+ console . log ( 'linking shared package' ) ;
56
+ const code = fs . readFileSync ( outfile , 'utf-8' ) ;
57
+
58
+ try {
59
+ const linkerEsm = await loadEsmModule < { default : PluginItem ; } > (
60
+ '@angular/compiler-cli/linker/babel'
61
+ ) ;
62
+
63
+ const linker = linkerEsm . default ;
13
64
14
- const preparePackage = entryPoint . includes ( "node_modules" )
15
- && ! ( SKIP_PACKAGE_PREPARATION . find ( p => pNameOrEmpty ?. split ( '/' ) [ 0 ] === p )
16
- || esm ) ;
65
+ const result = await transformAsync ( code , {
66
+ filename : outfile ,
67
+ // inputSourceMap: (useInputSourcemap ? undefined : false) as undefined,
68
+ // sourceMaps: pluginOptions.sourcemap ? 'inline' : false,
69
+ compact : false ,
70
+ configFile : false ,
71
+ babelrc : false ,
72
+ browserslistConfigFile : false ,
73
+ plugins : [ linker ] ,
74
+ } ) ;
17
75
18
- const pkgName = preparePackage ? inferePkgName ( entryPoint ) : "" ;
19
- const tmpFolder = `node_modules/.tmp/native-federation/${ pkgName } ` ;
76
+ fs . writeFileSync ( outfile , result . code , 'utf-8' ) ;
77
+ } catch ( e ) {
78
+ console . error ( 'error linking' , e ) ;
20
79
21
- if ( preparePackage ) {
22
- await prepareNodePackage ( entryPoint , external , tmpFolder ) ;
80
+ if ( fs . existsSync ( `${ outfile } .error` ) ) {
81
+ fs . unlinkSync ( `${ outfile } .error` ) ;
82
+ }
83
+ fs . renameSync ( outfile , `${ outfile } .error` ) ;
23
84
}
85
+ }
24
86
87
+ async function runEsbuild (
88
+ entryPoint : string ,
89
+ external : string [ ] ,
90
+ outfile : string ,
91
+ tsConfigPath : string ,
92
+ mappedPaths : MappedPath [ ] ,
93
+ plugins : esbuild . Plugin [ ] | null = null ,
94
+ absWorkingDir : string | undefined = undefined
95
+ ) {
25
96
await esbuild . build ( {
26
97
entryPoints : [ entryPoint ] ,
27
98
external,
99
+ // absWorkingDir,
28
100
outfile,
29
101
bundle : true ,
30
102
sourcemap : true ,
31
103
minify : true ,
32
- platform : 'node ' ,
104
+ platform : 'browser ' ,
33
105
format : 'esm' ,
34
106
target : [ 'esnext' ] ,
35
- plugins : [
107
+ plugins : plugins || [
36
108
createCompilerPlugin (
37
109
{
38
110
sourcemap : true ,
@@ -51,11 +123,10 @@ export const AngularEsBuildAdapter: BuildAdapter = async (options) => {
51
123
: [ ] ) ,
52
124
] ,
53
125
} ) ;
54
- } ;
55
-
56
- function inferePkgName ( entryPoint : string ) {
57
- return entryPoint
58
- . replace ( / .* ?n o d e _ m o d u l e s / g, "" )
59
- . replace ( / [ ^ A - Z a - z 0 - 9 . ] / g, "_" ) ;
60
126
}
61
127
128
+ export function loadEsmModule < T > ( modulePath : string | URL ) : Promise < T > {
129
+ return new Function ( 'modulePath' , `return import(modulePath);` ) (
130
+ modulePath
131
+ ) as Promise < T > ;
132
+ }
0 commit comments