1
- /* eslint-disable @nx/enforce-module-boundaries */
2
1
import {
3
2
BuilderContext ,
4
3
BuilderOutput ,
7
6
8
7
import { Schema } from '@angular-devkit/build-angular/src/builders/application/schema' ;
9
8
10
- // import { buildEsbuildBrowser } from '@angular-devkit/build-angular/src/builders/browser-esbuild';
11
9
import { buildApplication } from '@angular-devkit/build-angular/src/builders/application' ;
12
- // import { execute as executeDevServer } from '@angular-devkit/build-angular/src/builders/dev-server/builder';
13
10
14
11
import { serveWithVite } from '@angular-devkit/build-angular/src/builders/dev-server/vite-server' ;
15
12
import { DevServerBuilderOptions } from '@angular-devkit/build-angular/src/builders/dev-server' ;
@@ -51,6 +48,8 @@ import {
51
48
import { JsonObject } from '@angular-devkit/core' ;
52
49
import { createSharedMappingsPlugin } from '../../utils/shared-mappings-plugin' ;
53
50
import { Connect } from 'vite' ;
51
+ import { PluginBuild } from 'esbuild' ;
52
+ import { FederationInfo } from '@softarc/native-federation-runtime' ;
54
53
55
54
export async function * runBuilder (
56
55
nfOptions : NfBuilderSchema ,
@@ -110,61 +109,41 @@ export async function* runBuilder(
110
109
const config = await loadFederationConfig ( fedOptions ) ;
111
110
const externals = getExternals ( config ) ;
112
111
113
- // options.externalDependencies = externals.filter((e) => e !== 'tslib');
114
112
const plugins = [
115
113
createSharedMappingsPlugin ( config . sharedMappings ) ,
116
114
{
117
115
name : 'externals' ,
118
- setup ( build ) {
119
- console . log ( 'setup::' )
116
+ setup ( build : PluginBuild ) {
120
117
build . initialOptions . external = externals . filter ( ( e ) => e !== 'tslib' ) ;
121
118
} ,
122
119
} ,
123
- // {
124
- // name: 'resolveId',
125
- // setup(build: PluginBuild) {
126
- // build.
127
- // console.log('resolveId', source, importer, options);
128
- // }
129
- // }
130
120
] ;
131
121
132
122
const middleware : Connect . NextHandleFunction [ ] = [
133
123
( req , res , next ) => {
134
- const fileName = path . join ( fedOptions . workspaceRoot , fedOptions . outputPath , req . url ) ;
124
+ const fileName = path . join (
125
+ fedOptions . workspaceRoot ,
126
+ fedOptions . outputPath ,
127
+ req . url
128
+ ) ;
135
129
const exists = fs . existsSync ( fileName ) ;
136
130
137
131
if ( req . url !== '/' && req . url !== '' && exists ) {
138
- console . log ( 'loading from disk' , req . url )
132
+ console . log ( 'loading from disk' , req . url ) ;
139
133
const lookup = mrmime . lookup ;
140
134
const mimeType = lookup ( path . extname ( fileName ) ) || 'text/javascript' ;
141
- const body = fs . readFileSync ( fileName )
135
+ const rawBody = fs . readFileSync ( fileName , 'utf-8' ) ;
136
+ const body = addDebugInformation ( req . url , rawBody ) ;
142
137
res . writeHead ( 200 , {
143
138
'Content-Type' : mimeType ,
144
139
} ) ;
145
140
res . end ( body ) ;
146
- }
147
- else {
141
+ } else {
148
142
next ( ) ;
149
143
}
150
- }
144
+ } ,
151
145
] ;
152
146
153
- // for await (const r of buildEsbuildBrowser(options, context as any, { write: false })) {
154
- // const output = r.outputFiles ||[];
155
- // for (const o of output) {
156
- // console.log('got', o.path);
157
- // }
158
- // }
159
- // eslint-disable-next-line no-constant-condition
160
- // if (1===1) return;
161
-
162
- // const builderRun = await context.scheduleBuilder(
163
- // '@angular-devkit/build-angular:browser-esbuild',
164
- // options as any,
165
- // { target }
166
- // );
167
-
168
147
const memResults = new MemResults ( ) ;
169
148
170
149
let first = true ;
@@ -191,25 +170,21 @@ export async function* runBuilder(
191
170
192
171
options . deleteOutputPath = false ;
193
172
194
- // const x = buildEsbuildBrowser(
195
- // options,
196
- // context as any,
197
- // {
198
- // write,
199
- // },
200
- // plugins
201
- // );
202
-
203
173
const appBuilderName = '@angular-devkit/build-angular:application' ;
204
174
205
175
const builderRun = nfOptions . dev
206
- ? // eslint-disable-next-line @typescript-eslint/no-explicit-any
207
- serveWithVite ( normOuterOptions , appBuilderName , context , {
208
- indexHtml : transformIndexHtml
209
- } , {
210
- buildPlugins : plugins ,
211
- middleware
212
- } )
176
+ ? serveWithVite (
177
+ normOuterOptions ,
178
+ appBuilderName ,
179
+ context ,
180
+ {
181
+ indexHtml : transformIndexHtml ,
182
+ } ,
183
+ {
184
+ buildPlugins : plugins ,
185
+ middleware,
186
+ }
187
+ )
213
188
: buildApplication ( options , context , plugins ) ;
214
189
215
190
// builderRun.output.subscribe(async (output) => {
@@ -266,11 +241,10 @@ export async function* runBuilder(
266
241
first = false ;
267
242
}
268
243
269
- // updateIndexHtml(fedOptions);
270
- // const output = await lastValueFrom(builderRun.output as any);
271
244
yield lastResult || { success : false } ;
272
245
}
273
246
247
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
274
248
export default createBuilder ( runBuilder ) as any ;
275
249
276
250
function infereConfigPath ( tsConfig : string ) : string {
@@ -283,3 +257,25 @@ function infereConfigPath(tsConfig: string): string {
283
257
function transformIndexHtml ( content : string ) : Promise < string > {
284
258
return Promise . resolve ( updateScriptTags ( content , 'main.js' , 'polyfills.js' ) ) ;
285
259
}
260
+
261
+ function addDebugInformation ( fileName : string , rawBody : string ) : string {
262
+ if ( fileName !== '/remoteEntry.json' ) {
263
+ return rawBody ;
264
+ }
265
+
266
+ const remoteEntry = JSON . parse ( rawBody ) as FederationInfo ;
267
+ const shared = remoteEntry . shared ;
268
+
269
+ if ( ! shared ) {
270
+ return rawBody ;
271
+ }
272
+
273
+ const sharedForVite = shared . map ( ( s ) => ( {
274
+ ...s ,
275
+ packageName : `/@id/${ s . packageName } ` ,
276
+ } ) ) ;
277
+
278
+ remoteEntry . shared = [ ...shared , ...sharedForVite ] ;
279
+
280
+ return JSON . stringify ( remoteEntry , null , 2 ) ;
281
+ }
0 commit comments