Skip to content

Commit 6ac2d2e

Browse files
committed
fix compilation of resources
1 parent 20367f4 commit 6ac2d2e

File tree

9 files changed

+211
-172
lines changed

9 files changed

+211
-172
lines changed

packages/angular-cli/models/webpack-build-common.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ export function getWebpackCommonConfig(
115115

116116
       { test: /\.json$/, loader: 'json-loader' },
117117
       { test: /\.(jpg|png|gif)$/, loader: 'url-loader?limit=10000' },
118-
       { test: /\.html$/, loader: 'raw-loader' },
118+
       { test: /\.html$/, loader: 'html-loader' },
119119

120120
{ test: /\.(otf|woff|ttf|svg)$/, loader: 'url?limit=10000' },
121121
{ test: /\.woff2$/, loader: 'url?limit=10000&mimetype=font/woff2' },

packages/webpack/src/codegen.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

packages/webpack/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import 'reflect-metadata';
22

3-
export * from './codegen'
43
export * from './plugin'
54
export {ngcLoader as default} from './loader'

packages/webpack/src/plugin.ts

Lines changed: 87 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import 'reflect-metadata';
21
import {NgModule} from '@angular/core';
32
import * as ts from 'typescript';
43
import * as ngCompiler from '@angular/compiler-cli';
54
import {tsc} from '@angular/tsc-wrapped/src/tsc';
65
import * as path from 'path';
76

8-
9-
import { createCodeGenerator } from './codegen';
10-
import { createResolveDependenciesFromContextMap } from './utils';
7+
import {WebpackResourceLoader} from './resource_loader';
8+
import {createResolveDependenciesFromContextMap} from './utils';
9+
import { AngularCompilerOptions } from '@angular/tsc-wrapped';
1110

1211

1312
/**
@@ -20,154 +19,153 @@ export interface AngularWebpackPluginOptions {
2019
tsconfigPath?: string;
2120
compilerMode?: NgcCompilerMode;
2221
providers?: any[];
23-
entryModule: string;
22+
entryModule?: string;
23+
project: string;
24+
baseDir: string;
2425
}
2526

2627

2728
export class NgcWebpackPlugin {
2829
projectPath: string;
2930
rootModule: string;
3031
rootModuleName: string;
31-
codeGeneratorFactory: any;
3232
reflector: ngCompiler.StaticReflector;
3333
reflectorHost: ngCompiler.ReflectorHost;
3434
program: ts.Program;
3535
compilerHost: ts.CompilerHost;
3636
compilerOptions: ts.CompilerOptions;
37-
angularCompilerOptions: any;
37+
angularCompilerOptions: AngularCompilerOptions;
3838
files: any[];
39-
contextRegex = /.*/;
4039
lazyRoutes: any;
40+
loader: any;
41+
42+
43+
nmf: any = null;
44+
cmf: any = null;
45+
compiler: any = null;
46+
compilation: any = null;
4147

42-
constructor(public options: any = {}) {
48+
constructor(public options: AngularWebpackPluginOptions) {
4349
const tsConfig = tsc.readConfiguration(options.project, options.baseDir);
4450
this.compilerOptions = tsConfig.parsed.options;
4551
this.files = tsConfig.parsed.fileNames;
4652
this.angularCompilerOptions = tsConfig.ngOptions;
4753

4854
if (!this.angularCompilerOptions) {
49-
// TODO:robwormald more validation here
55+
// TODO: more validation here
5056
throw new Error(`"angularCompilerOptions" is not set in your tsconfig file!`);
5157
}
5258
this.angularCompilerOptions.basePath = options.baseDir || process.cwd();
5359

54-
const [rootModule, rootNgModule] = this.angularCompilerOptions.entryModule.split('#');
60+
const [rootModule, rootNgModule] = (this.angularCompilerOptions as any).entryModule.split('#');
5561
this.projectPath = options.project;
5662
this.rootModule = rootModule;
5763
this.rootModuleName = rootNgModule;
5864

5965
this.compilerHost = ts.createCompilerHost(this.compilerOptions, true);
6066
this.program = ts.createProgram(this.files, this.compilerOptions, this.compilerHost);
6167

62-
// TODO: pick this up from ngOptions
63-
const i18nOptions = {
64-
i18nFile: undefined,
65-
i18nFormat: undefined,
66-
locale: undefined,
67-
basePath: options.baseDir
68-
};
69-
7068
this.reflectorHost = new ngCompiler.ReflectorHost(
7169
this.program, this.compilerHost, tsConfig.ngOptions);
7270
this.reflector = new ngCompiler.StaticReflector(this.reflectorHost);
73-
this.codeGeneratorFactory = createCodeGenerator({
74-
ngcOptions: tsConfig.ngOptions,
75-
i18nOptions,
76-
compilerHost: this.compilerHost,
77-
resourceLoader: undefined // TODO: handle styles/templatess here.
78-
});
7971
}
8072

8173
// registration hook for webpack plugin
8274
apply(compiler) {
83-
compiler.plugin('context-module-factory', (cmf) => this._resolveImports(cmf));
84-
compiler.plugin('make', (compiler, cb) => this._make(compiler, cb));
85-
}
86-
87-
private _resolveImports(contextModuleFactory) {
88-
contextModuleFactory.plugin('before-resolve', (request, callback) => {
89-
return this._beforeResolveImports(request, callback);
90-
});
91-
contextModuleFactory.plugin('after-resolve', (request, callback) => {
92-
return this._afterResolveImports(request, callback);
93-
});
94-
return contextModuleFactory;
95-
}
96-
97-
private _beforeResolveImports(result, callback) {
98-
if (!result) {
99-
return callback();
100-
}
101-
if (this.contextRegex.test(result.request)) {
102-
result.request = path.resolve(process.cwd(), 'app/ngfactory');
103-
result.recursive = true;
104-
result.dependencies.forEach(d => d.critical = false);
105-
}
106-
107-
return callback(null, result);
108-
}
109-
110-
private _afterResolveImports(result, callback) {
111-
if (!result) {
112-
return callback();
113-
}
114-
115-
if (this.contextRegex.test(result.resource)) {
116-
result.resource = path.resolve(process.cwd(), this.angularCompilerOptions.genDir + '/app');
117-
result.recursive = true;
118-
result.dependencies.forEach(d => d.critical = false);
119-
result.resolveDependencies = createResolveDependenciesFromContextMap((fs, cb) => {
120-
return cb(null, this.lazyRoutes);
75+
this.compiler = compiler;
76+
compiler.plugin('normal-module-factory', (nmf) => this.nmf = nmf);
77+
compiler.plugin('context-module-factory', (cmf) => {
78+
this.cmf = cmf;
79+
cmf.plugin('before-resolve', (request, callback) => {
80+
if (!request) {
81+
console.log(11);
82+
return callback();
83+
}
84+
85+
request.request = path.resolve(process.cwd(), 'app/ngfactory');
86+
request.recursive = true;
87+
request.dependencies.forEach(d => d.critical = false);
88+
console.log(1);
89+
return callback(null, request);
12190
});
122-
}
123-
return callback(null, result);
91+
cmf.plugin('after-resolve', (result, callback) => {
92+
if (!result) {
93+
console.log(10);
94+
return callback();
95+
}
96+
97+
result.resource = path.resolve(process.cwd(), this.angularCompilerOptions.genDir + '/app');
98+
result.recursive = true;
99+
result.dependencies.forEach(d => d.critical = false);
100+
result.resolveDependencies = createResolveDependenciesFromContextMap((fs, cb) => {
101+
console.log(4);
102+
return cb(null, this.lazyRoutes);
103+
});
104+
105+
return callback(null, result);
106+
});
107+
});
108+
compiler.plugin('make', (compiler, cb) => this._make(compiler, cb));
124109
}
125110

126111
private _make(compilation, cb) {
127112
const rootModulePath = path.normalize(this.rootModule + '.ts');
128113
const rootModuleName = this.rootModuleName;
114+
this.compilation = compilation;
115+
this.loader = new WebpackResourceLoader(compilation);
129116

130-
// process the lazy routes
131-
const lazyModules = this._processNgModule(rootModulePath, rootModuleName, rootModulePath)
132-
.map(moduleKey => moduleKey.split('#')[0]);
133117
const program = ts.createProgram(this.files, this.compilerOptions, this.compilerHost);
134118

135-
this.codeGeneratorFactory(program)
136-
.reduce((files, generatedFile) => files.concat(generatedFile), [])
137-
.do(files => console.log(`ngc: generated ${files.length} files`))
138-
.map(() => {
139-
return lazyModules.reduce((lazyRoutes, lazyModule) => {
140-
const lazyPath = `${lazyModule}.ngfactory`;
141-
lazyRoutes[lazyPath] = path.join(
142-
path.resolve(process.cwd(), this.angularCompilerOptions.genDir + '/app'),
143-
lazyModule + '.ngfactory.ts'
119+
const i18nOptions = {
120+
i18nFile: undefined,
121+
i18nFormat: undefined,
122+
locale: undefined,
123+
basePath: this.options.baseDir
124+
};
125+
126+
// Create the Code Generator.
127+
const codeGenerator = ngCompiler.CodeGenerator.create(
128+
this.angularCompilerOptions,
129+
i18nOptions,
130+
program,
131+
this.compilerHost,
132+
new ngCompiler.NodeReflectorHostContext(this.compilerHost),
133+
this.loader
134+
);
135+
136+
codeGenerator.codegen()
137+
.then(() => {
138+
// process the lazy routes
139+
const lazyModules = this._processNgModule(rootModulePath, rootModuleName, rootModulePath)
140+
.map(moduleKey => moduleKey.split('#')[0]);
141+
this.lazyRoutes = lazyModules.reduce((lazyRoutes, lazyModule) => {
142+
lazyRoutes[`${lazyModule}.ngfactory`] = path.join(
143+
path.resolve(process.cwd(), this.angularCompilerOptions.genDir),
144+
'app', lazyModule + '.ngfactory.ts'
144145
);
145146
return lazyRoutes;
146147
}, {});
148+
console.log(2, this.lazyRoutes);
147149
})
148-
.do(lazyRouteConfig => this.lazyRoutes = lazyRouteConfig)
149-
.forEach(v => console.log('codegen complete'))
150-
.then(
151-
_ => cb(),
152-
err => cb(err)
153-
);
150+
.then(() => {
151+
console.log(3);
152+
cb()
153+
}, (err) => cb(err));
154154
}
155155

156156
private _processNgModule(mod: string, ngModuleName: string, containingFile: string): string[] {
157157
const staticSymbol = this.reflectorHost.findDeclaration(mod, ngModuleName, containingFile);
158158
const entryNgModuleMetadata = this.getNgModuleMetadata(staticSymbol);
159159
const loadChildren = this.extractLoadChildren(entryNgModuleMetadata);
160160

161-
const moduleChildren = loadChildren.reduce((res, lc) => {
162-
const [childMoudle, childNgModule] = lc.split('#');
161+
return loadChildren.reduce((res, lc) => {
162+
const [childModule, childNgModule] = lc.split('#');
163163

164164
// TODO calculate a different containingFile for relative paths
165165

166-
const children = this._processNgModule(childMoudle, childNgModule, containingFile);
166+
const children = this._processNgModule(childModule, childNgModule, containingFile);
167167
return res.concat(children);
168168
}, loadChildren);
169-
170-
return moduleChildren;
171169
}
172170

173171
private getNgModuleMetadata(staticSymbol: ngCompiler.StaticSymbol) {
@@ -209,32 +207,14 @@ export class NgcWebpackPlugin {
209207
}
210208
return routes.reduce((m, r) => {
211209
if (r.loadChildren) {
212-
return m.concat([r.loadChildren]);
213-
210+
return m.concat(r.loadChildren);
214211
} else if (Array.isArray(r)) {
215212
return m.concat(this.collectLoadChildren(r));
216-
217213
} else if (r.children) {
218214
return m.concat(this.collectLoadChildren(r.children));
219-
220215
} else {
221216
return m;
222217
}
223218
}, []);
224219
}
225220
}
226-
227-
class ParseConfigHost implements ts.ParseConfigHost {
228-
useCaseSensitiveFileNames: boolean = true;
229-
230-
readDirectory(rootDir: string, extensions: string[], excludes: string[], includes: string[]) {
231-
return ts.sys.readDirectory(rootDir, extensions, excludes, includes);
232-
}
233-
/**
234-
* Gets a value indicating whether the specified path exists and is a file.
235-
* @param path The path to test.
236-
*/
237-
fileExists(path: string): boolean {
238-
return ts.sys.fileExists(path);
239-
}
240-
}

0 commit comments

Comments
 (0)