Skip to content

Commit 7487744

Browse files
committed
feat(nf): also enable baseHref for ng serve
1 parent cc4de21 commit 7487744

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

libs/native-federation-core/src/lib/core/bundle-shared.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export async function bundleShared(
3939
// ? `${encName}-${encVersion}-dev.js`
4040
// : `${encName}-${encVersion}.js`;
4141

42-
const hash = calcFileHash(pi);
42+
const hash = calcHash(pi);
4343

4444
const outName = fedOptions.dev
4545
? `${encName}.${hash}-dev.js`
@@ -138,7 +138,7 @@ export async function bundleShared(
138138
});
139139
}
140140

141-
function calcFileHash(pi: PackageInfo) {
141+
function calcHash(pi: PackageInfo) {
142142
const hashBase = pi.version + '_' + pi.entryPoint;
143143
const hash = crypto
144144
.createHash('sha256')

libs/native-federation/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@angular-architects/native-federation",
3-
"version": "18.1.0",
3+
"version": "18.1.2",
44
"main": "src/index.js",
55
"generators": "./collection.json",
66
"builders": "./builders.json",

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ export async function* runBuilder(
127127
const watch = !!runServer || nfOptions.watch;
128128

129129
options.watch = watch;
130-
options.baseHref = nfOptions.baseHref;
130+
131+
if (nfOptions.baseHref) {
132+
options.baseHref = nfOptions.baseHref;
133+
}
134+
131135
const rebuildEvents = new RebuildHubs();
132136

133137
const adapter = createAngularBuildAdapter(options, context, rebuildEvents);
@@ -180,18 +184,21 @@ export async function* runBuilder(
180184

181185
const middleware = [
182186
(req, res, next) => {
187+
const url = removeBaseHref(req, options.baseHref);
188+
183189
const fileName = path.join(
184190
fedOptions.workspaceRoot,
185191
fedOptions.outputPath,
186-
req.url
192+
url
187193
);
194+
188195
const exists = fs.existsSync(fileName);
189196

190-
if (req.url !== '/' && req.url !== '' && exists) {
197+
if (url !== '/' && url !== '' && exists) {
191198
const lookup = mrmime.lookup;
192199
const mimeType = lookup(path.extname(fileName)) || 'text/javascript';
193200
const rawBody = fs.readFileSync(fileName, 'utf-8');
194-
const body = addDebugInformation(req.url, rawBody);
201+
const body = addDebugInformation(url, rawBody);
195202
res.writeHead(200, {
196203
'Content-Type': mimeType,
197204
'Access-Control-Allow-Origin': '*',
@@ -316,6 +323,15 @@ export async function* runBuilder(
316323
// eslint-disable-next-line @typescript-eslint/no-explicit-any
317324
export default createBuilder(runBuilder) as any;
318325

326+
function removeBaseHref(req: any, baseHref?: string) {
327+
let url = req.url;
328+
329+
if (baseHref && url.startsWith(baseHref)) {
330+
url = url.substr(baseHref.length);
331+
}
332+
return url;
333+
}
334+
319335
function infereConfigPath(tsConfig: string): string {
320336
const relProjectPath = path.dirname(tsConfig);
321337
const relConfigPath = path.join(relProjectPath, 'federation.config.js');
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { patchAngularBuild } from './utils/patch-angular-build';
1+
// import { patchAngularBuild } from './utils/patch-angular-build';
22

3-
const workspaceRoot = process.cwd();
4-
patchAngularBuild(workspaceRoot);
3+
// const workspaceRoot = process.cwd();
4+
// patchAngularBuild(workspaceRoot);
5+
6+
console.log('Please remove the postbuild task calling patch-angular-build. This is not needed since Native Federation 18.1 anymore!');

0 commit comments

Comments
 (0)