From 4ead45caba08cb0b67dc7df2f6a9b304c75fff7d Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 23 Sep 2022 13:44:44 +0000 Subject: [PATCH] feat(@angular-devkit/build-angular): add `ng-server-context` when using app-shell builder MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With this change we configure the app-shell builder to set the `ɵSERVER_CONTEXT` private provider. --- .../angular_devkit/build_angular/BUILD.bazel | 4 +-- .../src/builders/app-shell/app-shell_spec.ts | 3 +- .../src/builders/app-shell/index.ts | 31 ++++++++++++------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/packages/angular_devkit/build_angular/BUILD.bazel b/packages/angular_devkit/build_angular/BUILD.bazel index d9d738ef6907..848c0072b16c 100644 --- a/packages/angular_devkit/build_angular/BUILD.bazel +++ b/packages/angular_devkit/build_angular/BUILD.bazel @@ -106,6 +106,7 @@ ts_library( "@npm//@angular/compiler-cli", "@npm//@angular/core", "@npm//@angular/localize", + "@npm//@angular/platform-server", "@npm//@angular/service-worker", "@npm//@babel/core", "@npm//@babel/generator", @@ -282,9 +283,6 @@ ts_library( LARGE_SPECS = { "app-shell": { - "extra_deps": [ - "@npm//@angular/platform-server", - ], "tags": [ # TODO: node crashes with an internal error on node16 "node16-broken", diff --git a/packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts b/packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts index 8b2ba1734e64..592cbbdfe2e8 100644 --- a/packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts +++ b/packages/angular_devkit/build_angular/src/builders/app-shell/app-shell_spec.ts @@ -140,7 +140,8 @@ describe('AppShell Builder', () => { const fileName = 'dist/index.html'; const content = virtualFs.fileBufferToString(host.scopedSync().read(normalize(fileName))); - expect(content).toMatch(/Welcome to app!/); + expect(content).toMatch('Welcome to app'); + expect(content).toMatch('ng-server-context="app-shell"'); }); it('works with route', async () => { diff --git a/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts b/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts index 37b1c7cb29e5..84085370c0db 100644 --- a/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts +++ b/packages/angular_devkit/build_angular/src/builders/app-shell/index.ts @@ -13,6 +13,9 @@ import { targetFromTargetString, } from '@angular-devkit/architect'; import { JsonObject } from '@angular-devkit/core'; +import type { Type } from '@angular/core'; +import type * as platformServer from '@angular/platform-server'; +import assert from 'assert'; import * as fs from 'fs'; import * as path from 'path'; import { normalizeOptimization } from '../../utils'; @@ -74,24 +77,28 @@ async function _renderUniversal( localeDirectory, ); - const { AppServerModule, renderModule } = await import(serverBundlePath); + const { AppServerModule, renderModule, ɵSERVER_CONTEXT } = (await import(serverBundlePath)) as { + renderModule: typeof platformServer.renderModule | undefined; + ɵSERVER_CONTEXT: typeof platformServer.ɵSERVER_CONTEXT | undefined; + AppServerModule: Type | undefined; + }; - const renderModuleFn: ((module: unknown, options: {}) => Promise) | undefined = - renderModule; - - if (!(renderModuleFn && AppServerModule)) { - throw new Error( - `renderModule method and/or AppServerModule were not exported from: ${serverBundlePath}.`, - ); - } + assert(renderModule, `renderModule was not exported from: ${serverBundlePath}.`); + assert(AppServerModule, `AppServerModule was not exported from: ${serverBundlePath}.`); + assert(ɵSERVER_CONTEXT, `ɵSERVER_CONTEXT was not exported from: ${serverBundlePath}.`); // Load platform server module renderer - const renderOpts = { + let html = await renderModule(AppServerModule, { document: indexHtml, url: options.route, - }; + extraProviders: [ + { + provide: ɵSERVER_CONTEXT, + useValue: 'app-shell', + }, + ], + }); - let html = await renderModuleFn(AppServerModule, renderOpts); // Overwrite the client index file. const outputIndexPath = options.outputIndexPath ? path.join(root, options.outputIndexPath)