@@ -16,6 +16,10 @@ import { DevServerBuilderOptions } from '@angular-devkit/build-angular/src/build
16
16
import { normalizeOptions } from '@angular-devkit/build-angular/src/builders/dev-server/options' ;
17
17
18
18
import * as path from 'path' ;
19
+ import * as fs from 'fs' ;
20
+
21
+ import * as mrmime from 'mrmime' ;
22
+
19
23
import { setLogLevel , logger } from '@softarc/native-federation/build' ;
20
24
21
25
import { FederationOptions } from '@softarc/native-federation/build' ;
@@ -37,7 +41,7 @@ import {
37
41
startServer ,
38
42
} from '../../utils/dev-server' ;
39
43
import { RebuildHubs } from '../../utils/rebuild-events' ;
40
- import { updateIndexHtml } from '../../utils/updateIndexHtml' ;
44
+ import { updateIndexHtml , updateScriptTags } from '../../utils/updateIndexHtml' ;
41
45
import { existsSync , mkdirSync , rmSync } from 'fs' ;
42
46
import {
43
47
EsBuildResult ,
@@ -46,12 +50,12 @@ import {
46
50
} from '../../utils/mem-resuts' ;
47
51
import { JsonObject } from '@angular-devkit/core' ;
48
52
import { createSharedMappingsPlugin } from '../../utils/shared-mappings-plugin' ;
53
+ import { Connect } from 'vite' ;
49
54
50
55
export async function * runBuilder (
51
56
nfOptions : NfBuilderSchema ,
52
57
context : BuilderContext
53
58
) : AsyncIterable < BuilderOutput > {
54
-
55
59
let target = targetFromTargetString ( nfOptions . target ) ;
56
60
let _options = ( await context . getTargetOptions (
57
61
target
@@ -64,19 +68,19 @@ export async function* runBuilder(
64
68
) ) as JsonObject & Schema ;
65
69
66
70
const outerOptions = options as DevServerBuilderOptions ;
67
- const normOuterOptions = nfOptions . dev ? await normalizeOptions ( context , context . target . project , outerOptions ) : null ;
71
+ const normOuterOptions = nfOptions . dev
72
+ ? await normalizeOptions ( context , context . target . project , outerOptions )
73
+ : null ;
68
74
69
75
if ( nfOptions . dev ) {
70
76
target = targetFromTargetString ( outerOptions . buildTarget ) ;
71
77
_options = ( await context . getTargetOptions (
72
78
target
73
79
) ) as unknown as JsonObject & Schema ;
74
-
80
+
75
81
builder = await context . getBuilderNameForTarget ( target ) ;
76
- options = ( await context . validateOptions (
77
- _options ,
78
- builder
79
- ) ) as JsonObject & Schema ;
82
+ options = ( await context . validateOptions ( _options , builder ) ) as JsonObject &
83
+ Schema ;
80
84
}
81
85
82
86
const runServer = ! ! nfOptions . port ;
@@ -90,7 +94,7 @@ export async function* runBuilder(
90
94
setBuildAdapter ( adapter ) ;
91
95
92
96
setLogLevel ( options . verbose ? 'verbose' : 'info' ) ;
93
-
97
+
94
98
const outputPath = path . join ( options . outputPath , 'browser' ) ;
95
99
96
100
const fedOptions : FederationOptions = {
@@ -112,9 +116,38 @@ export async function* runBuilder(
112
116
{
113
117
name : 'externals' ,
114
118
setup ( build ) {
119
+ console . log ( 'setup::' )
115
120
build . initialOptions . external = externals . filter ( ( e ) => e !== 'tslib' ) ;
116
121
} ,
117
122
} ,
123
+ // {
124
+ // name: 'resolveId',
125
+ // setup(build: PluginBuild) {
126
+ // build.
127
+ // console.log('resolveId', source, importer, options);
128
+ // }
129
+ // }
130
+ ] ;
131
+
132
+ const middleware : Connect . NextHandleFunction [ ] = [
133
+ ( req , res , next ) => {
134
+ const fileName = path . join ( fedOptions . workspaceRoot , fedOptions . outputPath , req . url ) ;
135
+ const exists = fs . existsSync ( fileName ) ;
136
+
137
+ if ( req . url !== '/' && req . url !== '' && exists ) {
138
+ console . log ( 'loading from disk' , req . url )
139
+ const lookup = mrmime . lookup ;
140
+ const mimeType = lookup ( path . extname ( fileName ) ) || 'text/javascript' ;
141
+ const body = fs . readFileSync ( fileName )
142
+ res . writeHead ( 200 , {
143
+ 'Content-Type' : mimeType ,
144
+ } ) ;
145
+ res . end ( body ) ;
146
+ }
147
+ else {
148
+ next ( ) ;
149
+ }
150
+ }
118
151
] ;
119
152
120
153
// for await (const r of buildEsbuildBrowser(options, context as any, { write: false })) {
@@ -169,10 +202,15 @@ export async function* runBuilder(
169
202
170
203
const appBuilderName = '@angular-devkit/build-angular:application' ;
171
204
172
- const builderRun = nfOptions . dev ?
173
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
174
- serveWithVite ( normOuterOptions , appBuilderName , context , undefined , { buildPlugins : plugins } ) :
175
- buildApplication ( options , context , plugins ) ;
205
+ 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
+ } )
213
+ : buildApplication ( options , context , plugins ) ;
176
214
177
215
// builderRun.output.subscribe(async (output) => {
178
216
for await ( const output of builderRun ) {
@@ -196,7 +234,7 @@ export async function* runBuilder(
196
234
) ;
197
235
}
198
236
199
- if ( write ) {
237
+ if ( write && ! nfOptions . dev ) {
200
238
updateIndexHtml ( fedOptions ) ;
201
239
}
202
240
@@ -241,3 +279,7 @@ function infereConfigPath(tsConfig: string): string {
241
279
242
280
return relConfigPath ;
243
281
}
282
+
283
+ function transformIndexHtml ( content : string ) : Promise < string > {
284
+ return Promise . resolve ( updateScriptTags ( content , 'main.js' , 'polyfills.js' ) ) ;
285
+ }
0 commit comments