Skip to content

Commit 1bf6cc4

Browse files
committed
feat(@schematics/angular): augment universal schematics to import platform-server shims
In angular/angular#40559, a new `@angular/platform-server/shims` entry-point was introduced, which can be used to shim the server environment with APIs normally provided by the browser (such as DOM globals). If the project is using a version of Angular (and thus `@angular/platform-server`) that is known to include this new entry-point, the newly generated `main.server.ts` file should import it at the top in order to ensure that the shims will be available as soon as possible (before other direct or transitive imports that may rely on them). See also angular/angular#40559 for more details. NOTE: This `universal` schematic is the base for `app-shell`, `@nguniversal/express-engine` and `@nguniversal/hapi-engine`.
1 parent cc51432 commit 1bf6cc4

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

packages/schematics/angular/universal/files/src/__main@stripTsExtension__.ts.template

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1-
<% if (hasLocalizePackage) { %>/***************************************************************************************************
1+
<% if (hasPlatformServerShims) { %>/***************************************************************************************************
2+
* Load `@angular/platform-server` shims onto the global scope - provides DOM built-in types on the server.
3+
*
4+
* NOTE:
5+
* This import must come before any imports (direct or transitive) that rely on DOM built-ins being
6+
* available, such as `@angular/elements`.
7+
*/
8+
import '@angular/localize/init';
9+
10+
<% } %><% if (hasLocalizePackage) { %>/***************************************************************************************************
211
* Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
3-
*/
12+
*/
413
import '@angular/localize/init';
514

615
<% } %>import { enableProdMode } from '@angular/core';

packages/schematics/angular/universal/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ export default function (options: UniversalOptions): Rule {
248248
// and we don't want to break the 'ng add @nguniversal/express-engine schematics'
249249
const rootInSrc = clientProject.root === '' && clientTsConfig.includes('src/');
250250
const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : '');
251+
const ngVersion = getPackageJsonDependency(host, '@angular/core')?.version ?? '0.0.0';
252+
const ngMajorVersion = ngVersion.match(/^[^~>=]*(\d+)/)?.[1] ?? '0';
251253

252254
if (!options.skipInstall) {
253255
context.addTask(new NodePackageInstallTask());
@@ -259,6 +261,7 @@ export default function (options: UniversalOptions): Rule {
259261
...options as object,
260262
stripTsExtension: (s: string) => s.replace(/\.ts$/, ''),
261263
hasLocalizePackage: !!getPackageJsonDependency(host, '@angular/localize'),
264+
hasPlatformServerShims: Number(ngMajorVersion) >= 12,
262265
}),
263266
move(join(normalize(clientProject.root), 'src')),
264267
]);

0 commit comments

Comments
 (0)