1
- import { NgModule } from '@angular/core' ;
2
1
import * as ts from 'typescript' ;
2
+ import * as path from 'path' ;
3
+
4
+ import { NgModule } from '@angular/core' ;
3
5
import * as ngCompiler from '@angular/compiler-cli' ;
4
6
import { tsc } from '@angular/tsc-wrapped/src/tsc' ;
5
- import * as path from 'path' ;
6
7
8
+ import { patchReflectorHost } from './reflector_host' ;
7
9
import { WebpackResourceLoader } from './resource_loader' ;
8
10
import { createResolveDependenciesFromContextMap } from './utils' ;
9
11
import { AngularCompilerOptions } from '@angular/tsc-wrapped' ;
@@ -13,7 +15,10 @@ import { AngularCompilerOptions } from '@angular/tsc-wrapped';
13
15
* Option Constants
14
16
*/
15
17
export type NgcCompilerMode = 'aot' | 'jit'
16
-
18
+ export interface EmittedFile {
19
+ sourceMap : any ,
20
+ source : string
21
+ }
17
22
18
23
export interface AngularWebpackPluginOptions {
19
24
tsconfigPath ?: string ;
@@ -22,6 +27,8 @@ export interface AngularWebpackPluginOptions {
22
27
entryModule ?: string ;
23
28
project : string ;
24
29
baseDir : string ;
30
+ basePath : string ;
31
+ genDir : string ;
25
32
}
26
33
27
34
@@ -39,6 +46,7 @@ export class NgcWebpackPlugin {
39
46
lazyRoutes : any ;
40
47
loader : any ;
41
48
49
+ done : Promise < void > ;
42
50
43
51
nmf : any = null ;
44
52
cmf : any = null ;
@@ -61,12 +69,9 @@ export class NgcWebpackPlugin {
61
69
this . projectPath = options . project ;
62
70
this . rootModule = rootModule ;
63
71
this . rootModuleName = rootNgModule ;
64
-
65
72
this . compilerHost = ts . createCompilerHost ( this . compilerOptions , true ) ;
66
73
this . program = ts . createProgram ( this . files , this . compilerOptions , this . compilerHost ) ;
67
-
68
- this . reflectorHost = new ngCompiler . ReflectorHost (
69
- this . program , this . compilerHost , tsConfig . ngOptions ) ;
74
+ this . reflectorHost = new ngCompiler . ReflectorHost ( this . program , this . compilerHost , this . angularCompilerOptions ) ;
70
75
this . reflector = new ngCompiler . StaticReflector ( this . reflectorHost ) ;
71
76
}
72
77
@@ -78,43 +83,50 @@ export class NgcWebpackPlugin {
78
83
this . cmf = cmf ;
79
84
cmf . plugin ( 'before-resolve' , ( request , callback ) => {
80
85
if ( ! request ) {
81
- console . log ( 11 ) ;
82
86
return callback ( ) ;
83
87
}
84
88
85
89
request . request = path . resolve ( process . cwd ( ) , 'app/ngfactory' ) ;
86
90
request . recursive = true ;
87
91
request . dependencies . forEach ( d => d . critical = false ) ;
88
- console . log ( 1 ) ;
89
92
return callback ( null , request ) ;
90
93
} ) ;
91
94
cmf . plugin ( 'after-resolve' , ( result , callback ) => {
92
95
if ( ! result ) {
93
- console . log ( 10 ) ;
94
96
return callback ( ) ;
95
97
}
96
98
97
99
result . resource = path . resolve ( process . cwd ( ) , this . angularCompilerOptions . genDir + '/app' ) ;
98
100
result . recursive = true ;
99
101
result . dependencies . forEach ( d => d . critical = false ) ;
100
102
result . resolveDependencies = createResolveDependenciesFromContextMap ( ( fs , cb ) => {
101
- console . log ( 4 ) ;
102
103
return cb ( null , this . lazyRoutes ) ;
103
104
} ) ;
104
105
105
106
return callback ( null , result ) ;
106
107
} ) ;
107
108
} ) ;
108
- compiler . plugin ( 'make' , ( compiler , cb ) => this . _make ( compiler , cb ) ) ;
109
+
110
+ compiler . plugin ( 'make' , ( compilation , cb ) => this . _make ( compilation , cb ) ) ;
111
+ compiler . plugin ( 'after-emit' , ( compilation , cb ) => {
112
+ this . done = null ;
113
+ this . compilation = null ;
114
+ compilation . _ngToolsWebpackPluginInstance = null ;
115
+ cb ( ) ;
116
+ } ) ;
109
117
}
110
118
111
119
private _make ( compilation , cb ) {
112
120
const rootModulePath = path . normalize ( this . rootModule + '.ts' ) ;
113
121
const rootModuleName = this . rootModuleName ;
114
122
this . compilation = compilation ;
115
- this . loader = new WebpackResourceLoader ( compilation ) ;
116
123
117
- const program = ts . createProgram ( this . files , this . compilerOptions , this . compilerHost ) ;
124
+ if ( this . compilation . _ngToolsWebpackPluginInstance ) {
125
+ cb ( new Error ( 'A ngtools/webpack plugin already exist for this compilation.' ) ) ;
126
+ }
127
+ this . compilation . _ngToolsWebpackPluginInstance = this ;
128
+
129
+ this . loader = new WebpackResourceLoader ( compilation ) ;
118
130
119
131
const i18nOptions = {
120
132
i18nFile : undefined ,
@@ -127,13 +139,16 @@ export class NgcWebpackPlugin {
127
139
const codeGenerator = ngCompiler . CodeGenerator . create (
128
140
this . angularCompilerOptions ,
129
141
i18nOptions ,
130
- program ,
142
+ this . program ,
131
143
this . compilerHost ,
132
144
new ngCompiler . NodeReflectorHostContext ( this . compilerHost ) ,
133
145
this . loader
134
146
) ;
135
147
136
- codeGenerator . codegen ( )
148
+ // We need to temporarily patch the CodeGenerator until either it's patched or allows us
149
+ // to pass in our own ReflectorHost.
150
+ patchReflectorHost ( codeGenerator ) ;
151
+ this . done = codeGenerator . codegen ( )
137
152
. then ( ( ) => {
138
153
// process the lazy routes
139
154
const lazyModules = this . _processNgModule ( rootModulePath , rootModuleName , rootModulePath )
@@ -145,12 +160,8 @@ export class NgcWebpackPlugin {
145
160
) ;
146
161
return lazyRoutes ;
147
162
} , { } ) ;
148
- console . log ( 2 , this . lazyRoutes ) ;
149
163
} )
150
- . then ( ( ) => {
151
- console . log ( 3 ) ;
152
- cb ( )
153
- } , ( err ) => cb ( err ) ) ;
164
+ . then ( ( ) => cb ( ) , ( err ) => cb ( err ) ) ;
154
165
}
155
166
156
167
private _processNgModule ( mod : string , ngModuleName : string , containingFile : string ) : string [ ] {
0 commit comments