forked from angular/angular-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.ts
More file actions
225 lines (194 loc) · 7.56 KB
/
Copy pathplugin.ts
File metadata and controls
225 lines (194 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import * as ts from 'typescript';
import * as path from 'path';
import {NgModule} from '@angular/core';
import * as ngCompiler from '@angular/compiler-cli';
import {tsc} from '@angular/tsc-wrapped/src/tsc';
import {patchReflectorHost} from './reflector_host';
import {WebpackResourceLoader} from './resource_loader';
import {createResolveDependenciesFromContextMap} from './utils';
import { AngularCompilerOptions } from '@angular/tsc-wrapped';
/**
* Option Constants
*/
export interface AngularWebpackPluginOptions {
tsconfigPath?: string;
providers?: any[];
entryModule?: string;
project: string;
baseDir: string;
basePath?: string;
genDir?: string;
}
export class NgcWebpackPlugin {
projectPath: string;
rootModule: string;
rootModuleName: string;
reflector: ngCompiler.StaticReflector;
reflectorHost: ngCompiler.ReflectorHost;
program: ts.Program;
compilerHost: ts.CompilerHost;
compilerOptions: ts.CompilerOptions;
angularCompilerOptions: AngularCompilerOptions;
files: any[];
lazyRoutes: any;
loader: any;
genDir: string;
entryModule: string;
done: Promise<void>;
nmf: any = null;
cmf: any = null;
compiler: any = null;
compilation: any = null;
constructor(public options: AngularWebpackPluginOptions) {
const tsConfig = tsc.readConfiguration(options.project, options.baseDir);
this.compilerOptions = tsConfig.parsed.options;
this.files = tsConfig.parsed.fileNames;
this.angularCompilerOptions = Object.assign({}, tsConfig.ngOptions, options);
this.angularCompilerOptions.basePath = options.baseDir || process.cwd();
this.genDir = this.options.genDir
|| path.resolve(process.cwd(), this.angularCompilerOptions.genDir + '/app');
this.entryModule = options.entryModule || (this.angularCompilerOptions as any).entryModule;
const entryModule = this.entryModule;
const [rootModule, rootNgModule] = entryModule.split('#');
this.projectPath = options.project;
this.rootModule = rootModule;
this.rootModuleName = rootNgModule;
this.compilerHost = ts.createCompilerHost(this.compilerOptions, true);
this.program = ts.createProgram(this.files, this.compilerOptions, this.compilerHost);
this.reflectorHost = new ngCompiler.ReflectorHost(
this.program, this.compilerHost, this.angularCompilerOptions);
this.reflector = new ngCompiler.StaticReflector(this.reflectorHost);
}
// registration hook for webpack plugin
apply(compiler: any) {
this.compiler = compiler;
compiler.plugin('normal-module-factory', (nmf: any) => this.nmf = nmf);
compiler.plugin('context-module-factory', (cmf: any) => {
this.cmf = cmf;
cmf.plugin('before-resolve', (request: any, callback: (err?: any, request?: any) => void) => {
if (!request) {
return callback();
}
request.request = this.genDir;
request.recursive = true;
request.dependencies.forEach((d: any) => d.critical = false);
return callback(null, request);
});
cmf.plugin('after-resolve', (result: any, callback: (err?: any, request?: any) => void) => {
if (!result) {
return callback();
}
result.resource = this.genDir;
result.recursive = true;
result.dependencies.forEach((d: any) => d.critical = false);
result.resolveDependencies = createResolveDependenciesFromContextMap((_: any, cb: any) => {
return cb(null, this.lazyRoutes);
});
return callback(null, result);
});
});
compiler.plugin('make', (compilation: any, cb: any) => this._make(compilation, cb));
compiler.plugin('after-emit', (compilation: any, cb: any) => {
this.done = null;
this.compilation = null;
compilation._ngToolsWebpackPluginInstance = null;
cb();
});
}
private _make(compilation: any, cb: (err?: any, request?: any) => void) {
const rootModulePath = path.normalize(this.rootModule + '.ts');
const rootModuleName = this.rootModuleName;
this.compilation = compilation;
if (this.compilation._ngToolsWebpackPluginInstance) {
cb(new Error('A ngtools/webpack plugin already exist for this compilation.'));
}
this.compilation._ngToolsWebpackPluginInstance = this;
this.loader = new WebpackResourceLoader(compilation);
const i18nOptions: any = {
i18nFile: undefined,
i18nFormat: undefined,
locale: undefined,
basePath: this.options.baseDir
};
// Create the Code Generator.
const codeGenerator = ngCompiler.CodeGenerator.create(
this.angularCompilerOptions,
i18nOptions,
this.program,
this.compilerHost,
new ngCompiler.NodeReflectorHostContext(this.compilerHost),
this.loader
);
// We need to temporarily patch the CodeGenerator until either it's patched or allows us
// to pass in our own ReflectorHost.
patchReflectorHost(codeGenerator);
this.done = codeGenerator.codegen()
.then(() => {
// process the lazy routes
const lazyModules = this._processNgModule(rootModulePath, rootModuleName, rootModulePath)
.map(moduleKey => moduleKey.split('#')[0]);
this.lazyRoutes = lazyModules.reduce((lazyRoutes: any, lazyModule: any) => {
const genDir = this.genDir;
lazyRoutes[`${lazyModule}.ngfactory`] = path.join(genDir, lazyModule + '.ngfactory.ts');
return lazyRoutes;
}, {});
})
.then(() => cb(), (err) => cb(err));
}
private _processNgModule(mod: string, ngModuleName: string, containingFile: string): string[] {
const staticSymbol = this.reflectorHost.findDeclaration(mod, ngModuleName, containingFile);
const entryNgModuleMetadata = this.getNgModuleMetadata(staticSymbol);
const loadChildren = this.extractLoadChildren(entryNgModuleMetadata);
return loadChildren.reduce((res, lc) => {
const [childModule, childNgModule] = lc.split('#');
// TODO calculate a different containingFile for relative paths
const children = this._processNgModule(childModule, childNgModule, containingFile);
return res.concat(children);
}, loadChildren);
}
private getNgModuleMetadata(staticSymbol: ngCompiler.StaticSymbol) {
const ngModules = this.reflector.annotations(staticSymbol).filter(s => s instanceof NgModule);
if (ngModules.length === 0) {
throw new Error(`${staticSymbol.name} is not an NgModule`);
}
return ngModules[0];
}
private extractLoadChildren(ngModuleDecorator: any): any[] {
const routes = ngModuleDecorator.imports.reduce((mem: any[], m: any) => {
return mem.concat(this.collectRoutes(m.providers));
}, this.collectRoutes(ngModuleDecorator.providers));
return this.collectLoadChildren(routes);
}
private collectRoutes(providers: any[]): any[] {
if (!providers) {
return [];
}
const ROUTES = this.reflectorHost.findDeclaration(
'@angular/router/src/router_config_loader', 'ROUTES', undefined);
return providers.reduce((m, p) => {
if (p.provide === ROUTES) {
return m.concat(p.useValue);
} else if (Array.isArray(p)) {
return m.concat(this.collectRoutes(p));
} else {
return m;
}
}, []);
}
private collectLoadChildren(routes: any[]): any[] {
if (!routes) {
return [];
}
return routes.reduce((m, r) => {
if (r.loadChildren) {
return m.concat(r.loadChildren);
} else if (Array.isArray(r)) {
return m.concat(this.collectLoadChildren(r));
} else if (r.children) {
return m.concat(this.collectLoadChildren(r.children));
} else {
return m;
}
}, []);
}
}