Skip to content

Commit dc218c0

Browse files
committed
fix(@ngtools/webpack): fix locale refactoring code for commonjs
And just general be right (since nothing prevents TypeScript from renaming imports).
1 parent 0b80fba commit dc218c0

File tree

3 files changed

+30
-27
lines changed

3 files changed

+30
-27
lines changed

packages/@ngtools/webpack/src/transformers/insert_import.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export function insertStarImport(
99
sourceFile: ts.SourceFile,
1010
identifier: ts.Identifier,
1111
modulePath: string,
12+
target?: ts.Node,
13+
before = false,
1214
): TransformOperation[] {
1315
const ops: TransformOperation[] = [];
1416
const allImports = findAstNodes(null, sourceFile, ts.SyntaxKind.ImportDeclaration);
@@ -21,7 +23,14 @@ export function insertStarImport(
2123
const newImport = ts.createImportDeclaration(undefined, undefined, importClause,
2224
ts.createLiteral(modulePath));
2325

24-
if (allImports.length > 0) {
26+
if (target) {
27+
ops.push(new AddNodeOperation(
28+
sourceFile,
29+
target,
30+
before ? newImport : undefined,
31+
before ? undefined : newImport
32+
));
33+
} else if (allImports.length > 0) {
2534
// Find the last import and insert after.
2635
ops.push(new AddNodeOperation(
2736
sourceFile,

packages/@ngtools/webpack/src/transformers/register_locale_data.spec.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ describe('@ngtools/webpack transformers', () => {
2121
platformBrowserDynamic().bootstrapModule(AppModule);
2222
`;
2323
const output = stripIndent`
24-
import __locale_fr__ from "@angular/common/locales/fr";
25-
import { registerLocaleData } from "@angular/common";
26-
registerLocaleData(__locale_fr__);
24+
import * as __NgCli_locale_1 from "@angular/common/locales/fr";
25+
import * as __NgCli_locale_2 from "@angular/common";
26+
__NgCli_locale_2.registerLocaleData(__NgCli_locale_1.default);
2727
2828
import { enableProdMode } from '@angular/core';
2929
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

packages/@ngtools/webpack/src/transformers/register_locale_data.ts

+17-23
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as ts from 'typescript';
33

44
import { findAstNodes, getFirstNode } from './ast_helpers';
55
import { AddNodeOperation, TransformOperation } from './make_transform';
6+
import { insertStarImport } from './insert_import';
67

78

89
export function registerLocaleData(
@@ -44,40 +45,33 @@ export function registerLocaleData(
4445
return;
4546
}
4647

47-
// Create the import node for the locale.
48-
const localeIdentifier = ts.createIdentifier(`__locale_${locale.replace(/-/g, '')}__`);
49-
const localeImportClause = ts.createImportClause(localeIdentifier, undefined);
50-
const localeNewImport = ts.createImportDeclaration(undefined, undefined, localeImportClause,
51-
ts.createLiteral(`@angular/common/locales/${locale}`));
48+
const firstNode = getFirstNode(sourceFile);
5249

53-
ops.push(new AddNodeOperation(
54-
sourceFile,
55-
getFirstNode(sourceFile),
56-
localeNewImport
50+
// Create the import node for the locale.
51+
const localeNamespaceId = ts.createUniqueName('__NgCli_locale_');
52+
ops.push(...insertStarImport(
53+
sourceFile, localeNamespaceId, `@angular/common/locales/${locale}`, firstNode, true
5754
));
5855

5956
// Create the import node for the registerLocaleData function.
6057
const regIdentifier = ts.createIdentifier(`registerLocaleData`);
61-
const regImportSpecifier = ts.createImportSpecifier(undefined, regIdentifier);
62-
const regNamedImport = ts.createNamedImports([regImportSpecifier]);
63-
const regImportClause = ts.createImportClause(undefined, regNamedImport);
64-
const regNewImport = ts.createImportDeclaration(undefined, undefined, regImportClause,
65-
ts.createLiteral('@angular/common'));
66-
67-
ops.push(new AddNodeOperation(
68-
sourceFile,
69-
getFirstNode(sourceFile),
70-
regNewImport
71-
));
58+
const regNamespaceId = ts.createUniqueName('__NgCli_locale_');
59+
ops.push(
60+
...insertStarImport(sourceFile, regNamespaceId, '@angular/common', firstNode, true)
61+
);
7262

7363
// Create the register function call
74-
const registerFunctionCall = ts.createCall(regIdentifier, undefined, [localeIdentifier]);
64+
const registerFunctionCall = ts.createCall(
65+
ts.createPropertyAccess(regNamespaceId, regIdentifier),
66+
undefined,
67+
[ts.createPropertyAccess(localeNamespaceId, 'default')],
68+
);
7569
const registerFunctionStatement = ts.createStatement(registerFunctionCall);
7670

7771
ops.push(new AddNodeOperation(
7872
sourceFile,
79-
getFirstNode(sourceFile),
80-
registerFunctionStatement
73+
firstNode,
74+
registerFunctionStatement,
8175
));
8276
});
8377

0 commit comments

Comments
 (0)