diff --git a/packages/schematics/angular/universal/files/src/__main@stripTsExtension__.ts.template b/packages/schematics/angular/universal/files/src/__main@stripTsExtension__.ts.template index 4e9897c5ae49..b5c93cc71980 100644 --- a/packages/schematics/angular/universal/files/src/__main@stripTsExtension__.ts.template +++ b/packages/schematics/angular/universal/files/src/__main@stripTsExtension__.ts.template @@ -1,6 +1,15 @@ -<% if (hasLocalizePackage) { %>/*************************************************************************************************** +<% if (hasPlatformServerInit) { %>/*************************************************************************************************** + * Initialize the server environment - for example, adding DOM built-in types to the global scope. + * + * NOTE: + * This import must come before any imports (direct or transitive) that rely on DOM built-ins being + * available, such as `@angular/elements`. + */ +import '@angular/localize/init'; + +<% } %><% if (hasLocalizePackage) { %>/*************************************************************************************************** * Load `$localize` onto the global scope - used if i18n tags appear in Angular templates. -*/ + */ import '@angular/localize/init'; <% } %>import { enableProdMode } from '@angular/core'; diff --git a/packages/schematics/angular/universal/index.ts b/packages/schematics/angular/universal/index.ts index 67d6be6f7be8..efd80612298f 100644 --- a/packages/schematics/angular/universal/index.ts +++ b/packages/schematics/angular/universal/index.ts @@ -248,6 +248,8 @@ export default function (options: UniversalOptions): Rule { // and we don't want to break the 'ng add @nguniversal/express-engine schematics' const rootInSrc = clientProject.root === '' && clientTsConfig.includes('src/'); const tsConfigDirectory = join(normalize(clientProject.root), rootInSrc ? 'src' : ''); + const ngVersion = getPackageJsonDependency(host, '@angular/core')?.version ?? '0.0.0'; + const ngMajorVersion = ngVersion.match(/^[^~>=]*(\d+)/)?.[1] ?? '0'; if (!options.skipInstall) { context.addTask(new NodePackageInstallTask()); @@ -259,6 +261,7 @@ export default function (options: UniversalOptions): Rule { ...options as object, stripTsExtension: (s: string) => s.replace(/\.ts$/, ''), hasLocalizePackage: !!getPackageJsonDependency(host, '@angular/localize'), + hasPlatformServerInit: Number(ngMajorVersion) >= 12, }), move(join(normalize(clientProject.root), 'src')), ]);