Skip to content

Commit d68cb37

Browse files
committed
feat(nf): add /@id prefix to shared libs in dev mode for vite dev server
1 parent 77669c3 commit d68cb37

File tree

1 file changed

+48
-52
lines changed
  • libs/native-federation/src/builders/build

1 file changed

+48
-52
lines changed

libs/native-federation/src/builders/build/builder.ts

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
/* eslint-disable @nx/enforce-module-boundaries */
21
import {
32
BuilderContext,
43
BuilderOutput,
@@ -7,9 +6,7 @@ import {
76

87
import { Schema } from '@angular-devkit/build-angular/src/builders/application/schema';
98

10-
// import { buildEsbuildBrowser } from '@angular-devkit/build-angular/src/builders/browser-esbuild';
119
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';
1310

1411
import { serveWithVite } from '@angular-devkit/build-angular/src/builders/dev-server/vite-server';
1512
import { DevServerBuilderOptions } from '@angular-devkit/build-angular/src/builders/dev-server';
@@ -51,6 +48,8 @@ import {
5148
import { JsonObject } from '@angular-devkit/core';
5249
import { createSharedMappingsPlugin } from '../../utils/shared-mappings-plugin';
5350
import { Connect } from 'vite';
51+
import { PluginBuild } from 'esbuild';
52+
import { FederationInfo } from '@softarc/native-federation-runtime';
5453

5554
export async function* runBuilder(
5655
nfOptions: NfBuilderSchema,
@@ -110,61 +109,41 @@ export async function* runBuilder(
110109
const config = await loadFederationConfig(fedOptions);
111110
const externals = getExternals(config);
112111

113-
// options.externalDependencies = externals.filter((e) => e !== 'tslib');
114112
const plugins = [
115113
createSharedMappingsPlugin(config.sharedMappings),
116114
{
117115
name: 'externals',
118-
setup(build) {
119-
console.log('setup::')
116+
setup(build: PluginBuild) {
120117
build.initialOptions.external = externals.filter((e) => e !== 'tslib');
121118
},
122119
},
123-
// {
124-
// name: 'resolveId',
125-
// setup(build: PluginBuild) {
126-
// build.
127-
// console.log('resolveId', source, importer, options);
128-
// }
129-
// }
130120
];
131121

132122
const middleware: Connect.NextHandleFunction[] = [
133123
(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+
);
135129
const exists = fs.existsSync(fileName);
136130

137131
if (req.url !== '/' && req.url !== '' && exists) {
138-
console.log('loading from disk', req.url)
132+
console.log('loading from disk', req.url);
139133
const lookup = mrmime.lookup;
140134
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);
142137
res.writeHead(200, {
143138
'Content-Type': mimeType,
144139
});
145140
res.end(body);
146-
}
147-
else {
141+
} else {
148142
next();
149143
}
150-
}
144+
},
151145
];
152146

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-
168147
const memResults = new MemResults();
169148

170149
let first = true;
@@ -191,25 +170,21 @@ export async function* runBuilder(
191170

192171
options.deleteOutputPath = false;
193172

194-
// const x = buildEsbuildBrowser(
195-
// options,
196-
// context as any,
197-
// {
198-
// write,
199-
// },
200-
// plugins
201-
// );
202-
203173
const appBuilderName = '@angular-devkit/build-angular:application';
204174

205175
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+
)
213188
: buildApplication(options, context, plugins);
214189

215190
// builderRun.output.subscribe(async (output) => {
@@ -266,11 +241,10 @@ export async function* runBuilder(
266241
first = false;
267242
}
268243

269-
// updateIndexHtml(fedOptions);
270-
// const output = await lastValueFrom(builderRun.output as any);
271244
yield lastResult || { success: false };
272245
}
273246

247+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
274248
export default createBuilder(runBuilder) as any;
275249

276250
function infereConfigPath(tsConfig: string): string {
@@ -283,3 +257,25 @@ function infereConfigPath(tsConfig: string): string {
283257
function transformIndexHtml(content: string): Promise<string> {
284258
return Promise.resolve(updateScriptTags(content, 'main.js', 'polyfills.js'));
285259
}
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

Comments
 (0)