@@ -3,6 +3,7 @@ import * as ts from 'typescript';
33
44import { findAstNodes , getFirstNode } from './ast_helpers' ;
55import { AddNodeOperation , TransformOperation } from './make_transform' ;
6+ import { insertStarImport } from './insert_import' ;
67
78
89export 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