Skip to content

Commit 0765308

Browse files
committed
fix coverage
1 parent 3d83a7a commit 0765308

File tree

3 files changed

+49
-29
lines changed

3 files changed

+49
-29
lines changed

packages/angular-cli/models/webpack-build-test.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,11 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig, test
8383
{
8484
loader: webpackLoader,
8585
query: {
86-
tsconfig: path.resolve(appRoot, appConfig.tsconfig),
86+
tsConfigPath: path.resolve(appRoot, appConfig.tsconfig),
8787
module: 'commonjs',
88-
target: 'es5',
89-
forkChecker: true
88+
inlineSourceMap: true,
89+
sourceRoot: appRoot
9090
}
91-
},
92-
{
93-
loader: 'angular2-template-loader'
9491
}
9592
],
9693
exclude: [/\.e2e\.ts$/]

packages/webpack/src/loader.ts

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import * as ts from 'typescript';
33
import {AotPlugin} from './plugin';
44
import {TypeScriptFileRefactor} from './refactor';
55

6+
const loaderUtils = require('loader-utils');
7+
8+
69
function _getContentOfKeyLiteral(source: ts.SourceFile, node: ts.Node): string {
710
if (node.kind == ts.SyntaxKind.Identifier) {
811
return (node as ts.Identifier).text;
@@ -83,7 +86,7 @@ function _replaceBootstrap(plugin: AotPlugin, refactor: TypeScriptFileRefactor)
8386
refactor.insertImport(entryModule.className + 'NgFactory', ngFactoryPath);
8487
}
8588

86-
function _replaceResources(refactor: TypeScriptFileRefactor) {
89+
function _replaceResources(refactor: TypeScriptFileRefactor): void {
8790
const sourceFile = refactor.sourceFile;
8891

8992
// Find all object literals.
@@ -143,12 +146,11 @@ function _checkDiagnostics(refactor: TypeScriptFileRefactor) {
143146
// Super simple TS transpiler loader for testing / isolated usage. does not type check!
144147
export function ngcLoader(source: string) {
145148
this.cacheable();
149+
const cb: any = this.async();
146150

147151
const plugin = this._compilation._ngToolsWebpackPluginInstance as AotPlugin;
148152
// We must verify that AotPlugin is an instance of the right class.
149153
if (plugin && plugin instanceof AotPlugin) {
150-
const cb: any = this.async();
151-
152154
const refactor = new TypeScriptFileRefactor(
153155
this.resourcePath, plugin.compilerHost, plugin.program);
154156

@@ -180,11 +182,26 @@ export function ngcLoader(source: string) {
180182
})
181183
.catch(err => cb(err));
182184
} else {
183-
return ts.transpileModule(source, {
184-
compilerOptions: {
185-
target: ts.ScriptTarget.ES5,
186-
module: ts.ModuleKind.ES2015,
185+
const options = loaderUtils.parseQuery(this.query);
186+
const tsConfigPath = options.tsConfigPath;
187+
const tsConfig = ts.readConfigFile(tsConfigPath, ts.sys.readFile);
188+
189+
if (tsConfig.error) {
190+
throw tsConfig.error;
191+
}
192+
193+
const compilerOptions = tsConfig.config.compilerOptions as ts.CompilerOptions;
194+
for (const key of Object.keys(options)) {
195+
if (key == 'tsConfigPath') {
196+
continue;
187197
}
188-
}).outputText;
198+
compilerOptions[key] = options[key];
199+
}
200+
const compilerHost = ts.createCompilerHost(compilerOptions);
201+
const refactor = new TypeScriptFileRefactor(this.resourcePath, compilerHost);
202+
_replaceResources(refactor);
203+
204+
const result = refactor.transpile(compilerOptions);
205+
cb(null, result.outputText, result.sourceMap);
189206
}
190207
}

packages/webpack/src/refactor.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,21 +161,27 @@ export class TypeScriptFileRefactor {
161161
fileName: this._fileName
162162
});
163163

164-
const consumer = new SourceMapConsumer(JSON.parse(result.sourceMapText));
165-
const map = SourceMapGenerator.fromSourceMap(consumer);
166-
if (this._changed) {
167-
const sourceMap = this._sourceString.generateMap({
168-
file: this._fileName.replace(/\.ts$/, '.js'),
169-
source: this._fileName,
170-
hires: true,
171-
includeContent: true,
172-
});
173-
map.applySourceMap(new SourceMapConsumer(sourceMap));
174-
}
164+
if (result.sourceMapText) {
165+
const consumer = new SourceMapConsumer(JSON.parse(result.sourceMapText));
166+
const map = SourceMapGenerator.fromSourceMap(consumer);
167+
if (this._changed) {
168+
const sourceMap = this._sourceString.generateMap({
169+
file: this._fileName.replace(/\.ts$/, '.js'),
170+
source: this._fileName,
171+
hires: true,
172+
includeContent: true,
173+
});
174+
map.applySourceMap(new SourceMapConsumer(sourceMap));
175+
}
175176

176-
return {
177-
outputText: result.outputText,
178-
sourceMap: map.toJSON()
179-
};
177+
return {
178+
outputText: result.outputText,
179+
sourceMap: map.toJSON()
180+
};
181+
} else {
182+
return {
183+
outputText: result.outputText
184+
};
185+
}
180186
}
181187
}

0 commit comments

Comments
 (0)