Skip to content

Commit 4ead45c

Browse files
alan-agius4clydin
authored andcommitted
feat(@angular-devkit/build-angular): add ng-server-context when using app-shell builder
With this change we configure the app-shell builder to set the `ɵSERVER_CONTEXT` private provider.
1 parent f0933d3 commit 4ead45c

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

packages/angular_devkit/build_angular/BUILD.bazel

+1-3
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ ts_library(
106106
"@npm//@angular/compiler-cli",
107107
"@npm//@angular/core",
108108
"@npm//@angular/localize",
109+
"@npm//@angular/platform-server",
109110
"@npm//@angular/service-worker",
110111
"@npm//@babel/core",
111112
"@npm//@babel/generator",
@@ -282,9 +283,6 @@ ts_library(
282283

283284
LARGE_SPECS = {
284285
"app-shell": {
285-
"extra_deps": [
286-
"@npm//@angular/platform-server",
287-
],
288286
"tags": [
289287
# TODO: node crashes with an internal error on node16
290288
"node16-broken",

packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ describe('AppShell Builder', () => {
140140

141141
const fileName = 'dist/index.html';
142142
const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName)));
143-
expect(content).toMatch(/Welcome to app!/);
143+
expect(content).toMatch('Welcome to app');
144+
expect(content).toMatch('ng-server-context="app-shell"');
144145
});
145146

146147
it('works with route', async () => {

packages/angular_devkit/build_angular/src/builders/app-shell/index.ts

+19-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import {
1313
targetFromTargetString,
1414
} from '@angular-devkit/architect';
1515
import { JsonObject } from '@angular-devkit/core';
16+
import type { Type } from '@angular/core';
17+
import type * as platformServer from '@angular/platform-server';
18+
import assert from 'assert';
1619
import * as fs from 'fs';
1720
import * as path from 'path';
1821
import { normalizeOptimization } from '../../utils';
@@ -74,24 +77,28 @@ async function _renderUniversal(
7477
localeDirectory,
7578
);
7679

77-
const { AppServerModule, renderModule } = await import(serverBundlePath);
80+
const { AppServerModule, renderModule, ɵSERVER_CONTEXT } = (await import(serverBundlePath)) as {
81+
renderModule: typeof platformServer.renderModule | undefined;
82+
ɵSERVER_CONTEXT: typeof platformServer.ɵSERVER_CONTEXT | undefined;
83+
AppServerModule: Type<unknown> | undefined;
84+
};
7885

79-
const renderModuleFn: ((module: unknown, options: {}) => Promise<string>) | undefined =
80-
renderModule;
81-
82-
if (!(renderModuleFn && AppServerModule)) {
83-
throw new Error(
84-
`renderModule method and/or AppServerModule were not exported from: ${serverBundlePath}.`,
85-
);
86-
}
86+
assert(renderModule, `renderModule was not exported from: ${serverBundlePath}.`);
87+
assert(AppServerModule, `AppServerModule was not exported from: ${serverBundlePath}.`);
88+
assert(ɵSERVER_CONTEXT, `ɵSERVER_CONTEXT was not exported from: ${serverBundlePath}.`);
8789

8890
// Load platform server module renderer
89-
const renderOpts = {
91+
let html = await renderModule(AppServerModule, {
9092
document: indexHtml,
9193
url: options.route,
92-
};
94+
extraProviders: [
95+
{
96+
provide: ɵSERVER_CONTEXT,
97+
useValue: 'app-shell',
98+
},
99+
],
100+
});
93101

94-
let html = await renderModuleFn(AppServerModule, renderOpts);
95102
// Overwrite the client index file.
96103
const outputIndexPath = options.outputIndexPath
97104
? path.join(root, options.outputIndexPath)

0 commit comments

Comments
 (0)