@@ -3,6 +3,7 @@ import * as ts from 'typescript';
3
3
4
4
import { findAstNodes , getFirstNode } from './ast_helpers' ;
5
5
import { AddNodeOperation , TransformOperation } from './make_transform' ;
6
+ import { insertStarImport } from './insert_import' ;
6
7
7
8
8
9
export function registerLocaleData (
@@ -44,40 +45,33 @@ export function registerLocaleData(
44
45
return ;
45
46
}
46
47
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 ) ;
52
49
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
57
54
) ) ;
58
55
59
56
// Create the import node for the registerLocaleData function.
60
57
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
+ ) ;
72
62
73
63
// 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
+ ) ;
75
69
const registerFunctionStatement = ts . createStatement ( registerFunctionCall ) ;
76
70
77
71
ops . push ( new AddNodeOperation (
78
72
sourceFile ,
79
- getFirstNode ( sourceFile ) ,
80
- registerFunctionStatement
73
+ firstNode ,
74
+ registerFunctionStatement ,
81
75
) ) ;
82
76
} ) ;
83
77
0 commit comments