Skip to content

Commit 6f6291d

Browse files
committed
Step 2 - Register custom loader before serveWithVite; restrict to SSR mode.
1 parent a22724f commit 6f6291d

File tree

4 files changed

+217
-179
lines changed

4 files changed

+217
-179
lines changed

libs/nx-angular-mf/src/builders/helpers/utils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { getSystemPath, normalize } from '@angular-devkit/core';
22
import { workspaceRoot, readJsonFile } from '@nx/devkit';
3-
import { join } from 'path';
3+
import { join, sep } from 'path';
44
import { ConfigMf, DataForImportMap } from '../types';
55
import { getMapName } from './dependencies';
6+
import { existsSync } from 'fs';
7+
import { pathToFileURL } from 'node:url';
68

79
export const workspaceRootPath = getSystemPath(normalize(workspaceRoot));
810

@@ -64,3 +66,18 @@ export function getDataForImportMap(
6466
remoteEntry: mfeConfig.remoteEntry,
6567
};
6668
}
69+
70+
export function getPathForRegister(
71+
path: 'custom-loader' | 'custom-loader-serve'
72+
) {
73+
const fileName = path + '.js';
74+
const pathToFile = join(__dirname, '..', 'custom-loader', fileName);
75+
if (!existsSync(pathToFile)) {
76+
throw new Error(`File ${fileName} not found`);
77+
}
78+
79+
return {
80+
parentUrl: pathToFileURL(pathToFile),
81+
fileName: `.${sep}${fileName}`,
82+
};
83+
}

libs/nx-angular-mf/src/builders/serve/index.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,19 @@ import { Plugin } from 'esbuild';
1212

1313
import { ServeExecutorSchema } from './schema';
1414
import { BuildExecutorSchema } from '../build/schema';
15-
import { deepMergeObject, getMapName, indexHtml, loadModule, patchBuilderContext, prepareConfig } from '../helpers';
15+
import {
16+
deepMergeObject,
17+
getMapName,
18+
getPathForRegister,
19+
indexHtml,
20+
loadModule,
21+
patchBuilderContext,
22+
prepareConfig
23+
} from '../helpers';
1624
import { entryPointForExtendDependencies, importMapConfigPlugin } from '../es-plugin';
25+
import { register } from 'node:module';
26+
27+
const { port1, port2 } = new MessageChannel();
1728

1829

1930
function getBuilderAction() {
@@ -113,6 +124,15 @@ export async function* runBuilder(
113124
},
114125
};
115126

127+
if (targetOptions['ssr']) {
128+
const { parentUrl, fileName } = getPathForRegister('custom-loader-serve');
129+
register(fileName, {
130+
parentURL: parentUrl,
131+
data: { port: port2 },
132+
transferList: [port2],
133+
});
134+
}
135+
116136
const runServer = serveWithVite(
117137
normalizeOuterOptions,
118138
'@angular-devkit/build-angular:application',

0 commit comments

Comments
 (0)