Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): ensure that server dependencies a…
Browse files Browse the repository at this point in the history
…re 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
  • Loading branch information
alan-agius4 authored and clydin committed Jul 12, 2023
1 parent e8e0fa7 commit 7defb36
Showing 1 changed file with 46 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,16 @@ export function createServerCodeBundleOptions(
sourceFileCache,
);

const namespace = 'angular:main-server';
const mainServerNamespace = 'angular:main-server';
const ssrEntryNamespace = 'angular:ssr-entry';

const entryPoints: Record<string, string> = {
'main.server': namespace,
'main.server': mainServerNamespace,
};

const ssrEntryPoint = ssrOptions?.entry;
if (ssrEntryPoint) {
entryPoints['server'] = ssrEntryPoint;
entryPoints['server'] = ssrEntryNamespace;
}

const buildOptions: BuildOptions = {
Expand Down Expand Up @@ -159,37 +161,62 @@ export function createServerCodeBundleOptions(
buildOptions.plugins.push(createRxjsEsmResolutionPlugin());
}

const polyfills = [`import '@angular/platform-server/init';`];

if (options.polyfills?.includes('zone.js')) {
polyfills.push(`import 'zone.js/node';`);
}

if (jit) {
polyfills.push(`import '@angular/compiler';`);
}

buildOptions.plugins.push(
createVirtualModulePlugin({
namespace,
namespace: mainServerNamespace,
loadContent: () => {
const mainServerEntryPoint = path
.relative(workspaceRoot, serverEntryPoint)
.replace(/\\/g, '/');
const importAndExportDec: string[] = [
`import '@angular/platform-server/init';`,
`import moduleOrBootstrapFn from './${mainServerEntryPoint}';`,
`export default moduleOrBootstrapFn;`,
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
];

if (jit) {
importAndExportDec.unshift(`import '@angular/compiler';`);
}

if (options.polyfills?.includes('zone.js')) {
importAndExportDec.unshift(`import 'zone.js/node';`);
}

return {
contents: importAndExportDec.join('\n'),
contents: [
...polyfills,
`import moduleOrBootstrapFn from './${mainServerEntryPoint}';`,
`export default moduleOrBootstrapFn;`,
`export * from './${mainServerEntryPoint}';`,
`export { renderApplication, renderModule, ɵSERVER_CONTEXT } from '@angular/platform-server';`,
].join('\n'),
loader: 'js',
resolveDir: workspaceRoot,
};
},
}),
);

if (ssrEntryPoint) {
buildOptions.plugins.push(
createVirtualModulePlugin({
namespace: ssrEntryNamespace,
loadContent: () => {
const mainServerEntryPoint = path
.relative(workspaceRoot, ssrEntryPoint)
.replace(/\\/g, '/');

return {
contents: [
...polyfills,
`import './${mainServerEntryPoint}';`,
`export * from './${mainServerEntryPoint}';`,
].join('\n'),
loader: 'js',
resolveDir: workspaceRoot,
};
},
}),
);
}

return buildOptions;
}

Expand Down

0 comments on commit 7defb36

Please sign in to comment.