Skip to content

Commit 1199205

Browse files
gkalpakalan-agius4
authored andcommitted
feat(@schematics/angular): augment universal schematics to import platform-server shims
In angular/angular#40559, a new `@angular/platform-server/init` entry-point was introduced, which can be used to initialize the server environment by providing shims for 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`. Fixes angular/angular#40559
1 parent 96a4467 commit 1199205

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,15 @@
1+
/***************************************************************************************************
2+
* Initialize the server environment - for example, adding DOM built-in types to the global scope.
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/platform-server/init';
9+
110
<% 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_spec.ts

+8
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,14 @@ describe('Universal Schematic', () => {
242242
expect(tree.exists(filePath)).toEqual(true);
243243
});
244244

245+
it(`should add import to '@angular/platform-server/init' in main file`, async () => {
246+
const tree = await schematicRunner.runSchematicAsync('universal', defaultOptions, appTree)
247+
.toPromise();
248+
const filePath = '/projects/bar/src/main.server.ts';
249+
const contents = tree.readContent(filePath);
250+
expect(contents).toContain('import \'@angular/platform-server/init\'');
251+
});
252+
245253
it(`should not add import to '@angular/localize' in main file when it's not a depedency`, async () => {
246254
const tree = await schematicRunner.runSchematicAsync('universal', defaultOptions, appTree)
247255
.toPromise();

0 commit comments

Comments
 (0)