Skip to content

Commit 7defb36

Browse files
alan-agius4clydin
authored andcommitted
fix(@angular-devkit/build-angular): ensure that server dependencies are loaded also in ssr entrypoint
This commit ensure that server "polyfills" like `zone.js` and `@angular/compiler` are loaded as well when using the `ssr` option
1 parent e8e0fa7 commit 7defb36

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

Diff for: packages/angular_devkit/build_angular/src/tools/esbuild/application-code-bundle.ts

+46-19
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,16 @@ export function createServerCodeBundleOptions(
110110
sourceFileCache,
111111
);
112112

113-
const namespace = 'angular:main-server';
113+
const mainServerNamespace = 'angular:main-server';
114+
const ssrEntryNamespace = 'angular:ssr-entry';
115+
114116
const entryPoints: Record<string, string> = {
115-
'main.server': namespace,
117+
'main.server': mainServerNamespace,
116118
};
117119

118120
const ssrEntryPoint = ssrOptions?.entry;
119121
if (ssrEntryPoint) {
120-
entryPoints['server'] = ssrEntryPoint;
122+
entryPoints['server'] = ssrEntryNamespace;
121123
}
122124

123125
const buildOptions: BuildOptions = {
@@ -159,37 +161,62 @@ export function createServerCodeBundleOptions(
159161
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
160162
}
161163

164+
const polyfills = [`import '@angular/platform-server/init';`];
165+
166+
if (options.polyfills?.includes('zone.js')) {
167+
polyfills.push(`import 'zone.js/node';`);
168+
}
169+
170+
if (jit) {
171+
polyfills.push(`import '@angular/compiler';`);
172+
}
173+
162174
buildOptions.plugins.push(
163175
createVirtualModulePlugin({
164-
namespace,
176+
namespace: mainServerNamespace,
165177
loadContent: () => {
166178
const mainServerEntryPoint = path
167179
.relative(workspaceRoot, serverEntryPoint)
168180
.replace(/\\/g, '/');
169-
const importAndExportDec: string[] = [
170-
`import '@angular/platform-server/init';`,
171-
`import moduleOrBootstrapFn from './${mainServerEntryPoint}';`,
172-
`export default moduleOrBootstrapFn;`,
173-
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
174-
];
175-
176-
if (jit) {
177-
importAndExportDec.unshift(`import '@angular/compiler';`);
178-
}
179-
180-
if (options.polyfills?.includes('zone.js')) {
181-
importAndExportDec.unshift(`import 'zone.js/node';`);
182-
}
183181

184182
return {
185-
contents: importAndExportDec.join('\n'),
183+
contents: [
184+
...polyfills,
185+
`import moduleOrBootstrapFn from './${mainServerEntryPoint}';`,
186+
`export default moduleOrBootstrapFn;`,
187+
`export * from './${mainServerEntryPoint}';`,
188+
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
189+
].join('\n'),
186190
loader: 'js',
187191
resolveDir: workspaceRoot,
188192
};
189193
},
190194
}),
191195
);
192196

197+
if (ssrEntryPoint) {
198+
buildOptions.plugins.push(
199+
createVirtualModulePlugin({
200+
namespace: ssrEntryNamespace,
201+
loadContent: () => {
202+
const mainServerEntryPoint = path
203+
.relative(workspaceRoot, ssrEntryPoint)
204+
.replace(/\\/g, '/');
205+
206+
return {
207+
contents: [
208+
...polyfills,
209+
`import './${mainServerEntryPoint}';`,
210+
`export * from './${mainServerEntryPoint}';`,
211+
].join('\n'),
212+
loader: 'js',
213+
resolveDir: workspaceRoot,
214+
};
215+
},
216+
}),
217+
);
218+
}
219+
193220
return buildOptions;
194221
}
195222

0 commit comments

Comments
 (0)