Skip to content

Commit b115e3f

Browse files
committed
fix(@angular-devkit/build-angular): export @angular/platform-server symbols in server bundle
With this commit we add an internal file to export all of the symbols from `@angular/platform-server` when building a server bundle. This is needed. This is needed so that DI tokens can be referenced and set at runtime outside of the bundle.
1 parent d098414 commit b115e3f

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

packages/angular_devkit/build_angular/src/builders/server/index.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ export function execute(
9191

9292
return from(initialize(options, context, transforms.webpackConfiguration)).pipe(
9393
concatMap(({ config, i18n, target }) => {
94+
addPlatformServerExportsAsEntry(config, root);
95+
9496
return runWebpack(config, context, {
9597
webpackFactory: require('webpack') as typeof webpack,
9698
logging: (stats, config) => {
@@ -187,3 +189,27 @@ async function initialize(
187189

188190
return { config: transformedConfig || config, i18n, target };
189191
}
192+
/**
193+
* Add `@angular/platform-server` exports as an entry-point.
194+
* This is needed so that DI tokens can be referenced and set at runtime outside of the bundle.
195+
*/
196+
function addPlatformServerExportsAsEntry(config: webpack.Configuration, root: string): void {
197+
try {
198+
// Only add `@angular/platform-server` exports when it is installed.
199+
// In some cases this builder is used when `@angular/platform-server` is not installed.
200+
// Example: when using `@nguniversal/common/clover` which does not need `@angular/platform-server`.
201+
require.resolve('@angular/platform-server', { paths: [root] });
202+
} catch {
203+
return;
204+
}
205+
206+
const platformServerExportsFile = path.join(__dirname, 'platform-server-exports.js');
207+
208+
if (typeof config.entry === 'object' && !Array.isArray(config.entry) && config.entry['main']) {
209+
if (Array.isArray(config.entry['main'])) {
210+
config.entry['main'].push(platformServerExportsFile);
211+
} else {
212+
config.entry['main'] = [platformServerExportsFile, config.entry['main'] as string];
213+
}
214+
}
215+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
export * from '@angular/platform-server';

0 commit comments

Comments
 (0)