Skip to content

Commit 759b130

Browse files
committed
Step 3 - Use esbuild to bundle and transfer loader with dependencies to SSR build directory.
1 parent d6ab6a0 commit 759b130

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { join } from 'path';
2+
import { Plugin, PluginBuild } from 'esbuild';
3+
4+
export const CUSTOM_LOADER_NAME = 'custom-loader';
5+
6+
export function moveCustomLoaderPlugin(): Plugin {
7+
const pathToCustomLoaderFolder = join(__dirname, '../custom-loader');
8+
return {
9+
name: 'moveCustomLoaderPlugin',
10+
setup(build: PluginBuild) {
11+
if (build.initialOptions.platform !== 'node') return;
12+
const customLoaderName = CUSTOM_LOADER_NAME;
13+
build.initialOptions.entryPoints = {
14+
...build.initialOptions.entryPoints,
15+
[customLoaderName]: customLoaderName,
16+
};
17+
build.initialOptions.external = [
18+
...build.initialOptions.external,
19+
'./server.mjs',
20+
];
21+
build.onResolve(
22+
{ filter: new RegExp('^' + customLoaderName) },
23+
({ kind, path }) => {
24+
console.log(kind, path);
25+
if (kind === 'import-statement') {
26+
return {
27+
path: join(pathToCustomLoaderFolder, 'custom-loader.js'),
28+
};
29+
}
30+
return {
31+
path: path,
32+
namespace: customLoaderName,
33+
};
34+
}
35+
);
36+
build.onLoad({ filter: /./, namespace: customLoaderName }, () => {
37+
return {
38+
contents: `
39+
import * as customLoader from "custom-loader";
40+
const {
41+
initialize,
42+
resolve,
43+
load
44+
} = customLoader;
45+
export {
46+
initialize,
47+
resolve,
48+
load
49+
}
50+
`,
51+
loader: 'js',
52+
};
53+
});
54+
},
55+
}
56+
}

libs/nx-angular-mf/src/builders/es-plugin/server-ssr-plugin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Plugin, PluginBuild } from 'esbuild';
22
import { DEPLOY_URL } from '../custom-loader/custom-loader';
3+
import { CUSTOM_LOADER_NAME } from './move-custom-loader-plugin';
34

45

56
export function serverSSRPlugin(deployUrl: string): Plugin {
@@ -34,7 +35,7 @@ export function serverSSRPlugin(deployUrl: string): Plugin {
3435
contents: `
3536
import {register} from "node:module";
3637
const { port1, port2 } = new MessageChannel();
37-
register('./custom-loader.mjs', {
38+
register('./${CUSTOM_LOADER_NAME}.mjs', {
3839
parentURL: import.meta.url,
3940
data: { port: port2 },
4041
transferList: [port2],

0 commit comments

Comments
 (0)