Skip to content

Commit 640ae2f

Browse files
Alanvikerman
Alan
authored andcommitted
feat(@schematics/angular): augment universal/app-shell addition for @angular/localize
If i18n support is already present within an application, the newly generated `main.server.ts` file should also contain the `@angular/localize` polyfill to allow the universal application to function. This universal schematic here, is the base for app-shell, @nguniversal/express-engine, and @nguniversal/hapi-engine
1 parent 687ada9 commit 640ae2f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { enableProdMode } from '@angular/core';
1+
<% if (hasLocalizePackage) { %>/***************************************************************************************************
2+
* Load `$localize` onto the global scope - used if i18n tags appear in Angular templates.
3+
*/
4+
import '@angular/localize/init';
5+
6+
<% } %>import { enableProdMode } from '@angular/core';
27

38
import { environment } from './environments/environment';
49

packages/schematics/angular/universal/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ export default function (options: UniversalOptions): Rule {
250250
...strings,
251251
...options as object,
252252
stripTsExtension: (s: string) => s.replace(/\.ts$/, ''),
253+
hasLocalizePackage: !!getPackageJsonDependency(host, '@angular/localize'),
253254
}),
254255
move(join(normalize(clientProject.root), 'src')),
255256
]);

packages/schematics/angular/universal/index_spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88
import { SchematicTestRunner, UnitTestTree } from '@angular-devkit/schematics/testing';
99
import { Schema as ApplicationOptions, Style } from '../application/schema';
10+
import { NodeDependencyType, addPackageJsonDependency } from '../utility/dependencies';
1011
import { Schema as WorkspaceOptions } from '../workspace/schema';
1112
import { Schema as UniversalOptions } from './schema';
1213

@@ -227,4 +228,25 @@ describe('Universal Schematic', () => {
227228
expect(tree.exists(filePath)).toEqual(true);
228229
});
229230

231+
it(`should not add import to '@angular/localize' in main file when it's not a depedency`, async () => {
232+
const tree = await schematicRunner.runSchematicAsync('universal', defaultOptions, appTree)
233+
.toPromise();
234+
const filePath = '/projects/bar/src/main.server.ts';
235+
const contents = tree.readContent(filePath);
236+
expect(contents).not.toContain('@angular/localize');
237+
});
238+
239+
it(`should add import to '@angular/localize' in main file when it's a depedency`, async () => {
240+
addPackageJsonDependency(appTree, {
241+
name: '@angular/localize',
242+
type: NodeDependencyType.Default,
243+
version: 'latest',
244+
});
245+
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('@angular/localize/init');
251+
});
230252
});

0 commit comments

Comments
 (0)