Skip to content
This repository was archived by the owner on Apr 4, 2025. It is now read-only.

Commit e9ccc62

Browse files
committed
fix(@ngtools/webpack): fix ngtools/webpack to properly use Host<>
1 parent 40a52fb commit e9ccc62

File tree

7 files changed

+120
-75
lines changed

7 files changed

+120
-75
lines changed

packages/angular_devkit/build_webpack/src/angular-cli-files/models/webpack-configs/typescript.ts

+16-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// tslint:disable
22
// TODO: cleanup this file, it's copied as is from Angular CLI.
3-
3+
import { virtualFs } from '@angular-devkit/core';
4+
import { Stats } from 'fs';
45
import * as path from 'path';
56
import { stripIndent } from 'common-tags';
67
import {
@@ -19,7 +20,12 @@ const webpackLoader: string = g['angularCliIsLocal']
1920
: '@ngtools/webpack';
2021

2122

22-
function _createAotPlugin(wco: WebpackConfigOptions, options: any, useMain = true) {
23+
function _createAotPlugin(
24+
wco: WebpackConfigOptions,
25+
options: any,
26+
host: virtualFs.Host<Stats>,
27+
useMain = true,
28+
) {
2329
const { appConfig, root, buildOptions } = wco;
2430
options.compilerOptions = options.compilerOptions || {};
2531

@@ -102,22 +108,23 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any, useMain = tru
102108
additionalLazyModules,
103109
nameLazyFiles: buildOptions.namedChunks,
104110
forkTypeChecker: buildOptions.forkTypeChecker,
105-
...options
111+
...options,
112+
host,
106113
};
107114
return new AngularCompilerPlugin(pluginOptions);
108115
}
109116

110-
export function getNonAotConfig(wco: WebpackConfigOptions) {
117+
export function getNonAotConfig(wco: WebpackConfigOptions, host: virtualFs.Host<Stats>) {
111118
const { appConfig, root } = wco;
112119
const tsConfigPath = path.resolve(root, appConfig.tsConfig);
113120

114121
return {
115122
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
116-
plugins: [_createAotPlugin(wco, { tsConfigPath, skipCodeGeneration: true })]
123+
plugins: [_createAotPlugin(wco, { tsConfigPath, skipCodeGeneration: true }, host)]
117124
};
118125
}
119126

120-
export function getAotConfig(wco: WebpackConfigOptions) {
127+
export function getAotConfig(wco: WebpackConfigOptions, host: virtualFs.Host<Stats>) {
121128
const { root, buildOptions, appConfig } = wco;
122129
const tsConfigPath = path.resolve(root, appConfig.tsConfig);
123130

@@ -133,11 +140,11 @@ export function getAotConfig(wco: WebpackConfigOptions) {
133140

134141
return {
135142
module: { rules: [{ test, use: loaders }] },
136-
plugins: [_createAotPlugin(wco, { tsConfigPath })]
143+
plugins: [_createAotPlugin(wco, { tsConfigPath }, host)]
137144
};
138145
}
139146

140-
export function getNonAotTestConfig(wco: WebpackConfigOptions) {
147+
export function getNonAotTestConfig(wco: WebpackConfigOptions, host: virtualFs.Host<Stats>) {
141148
const { root, appConfig } = wco;
142149
const tsConfigPath = path.resolve(root, appConfig.tsConfig);
143150

@@ -152,6 +159,6 @@ export function getNonAotTestConfig(wco: WebpackConfigOptions) {
152159

153160
return {
154161
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
155-
plugins: [_createAotPlugin(wco, pluginOptions, false)]
162+
plugins: [_createAotPlugin(wco, pluginOptions, host, false)]
156163
};
157164
}

packages/angular_devkit/build_webpack/src/browser/index.ts

+2-9
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
statsToString,
3636
statsWarningsToString,
3737
} from '../angular-cli-files/utilities/stats';
38-
import { WebpackFileSystemHostAdapter } from '../utils/webpack-file-system-host-adapter';
3938
const webpackMerge = require('webpack-merge');
4039

4140

@@ -152,12 +151,6 @@ export class BrowserBuilder implements Builder<BrowserBuilderOptions> {
152151
return;
153152
}
154153
const webpackCompiler = webpack(webpackConfig);
155-
156-
// TODO: fix webpack typings.
157-
// tslint:disable-next-line:no-any
158-
(webpackCompiler as any).inputFileSystem = new WebpackFileSystemHostAdapter(
159-
this.context.host as virtualFs.Host<fs.Stats>,
160-
);
161154
const statsConfig = getWebpackStatsConfig(options.verbose);
162155

163156
const callback: webpack.compiler.CompilerCallback = (err, stats) => {
@@ -277,8 +270,8 @@ export class BrowserBuilder implements Builder<BrowserBuilderOptions> {
277270

278271
if (wco.appConfig.main || wco.appConfig.polyfills) {
279272
const typescriptConfigPartial = wco.buildOptions.aot
280-
? getAotConfig(wco)
281-
: getNonAotConfig(wco);
273+
? getAotConfig(wco, this.context.host as virtualFs.Host<fs.Stats>)
274+
: getNonAotConfig(wco, this.context.host as virtualFs.Host<fs.Stats>);
282275
webpackConfigs.push(typescriptConfigPartial);
283276
}
284277

packages/angular_devkit/build_webpack/src/karma/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ import {
1212
BuilderConfiguration,
1313
BuilderContext,
1414
} from '@angular-devkit/architect';
15-
import { Path, getSystemPath, normalize, resolve } from '@angular-devkit/core';
15+
import { Path, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core';
16+
import * as fs from 'fs';
1617
import { Observable } from 'rxjs/Observable';
1718
import * as ts from 'typescript'; // tslint:disable-line:no-implicit-dependencies
1819
import {
@@ -158,7 +159,7 @@ export class KarmaBuilder implements Builder<KarmaBuilderOptions> {
158159
const webpackConfigs: {}[] = [
159160
getCommonConfig(wco),
160161
getStylesConfig(wco),
161-
getNonAotTestConfig(wco),
162+
getNonAotTestConfig(wco, this.context.host as virtualFs.Host<fs.Stats>),
162163
getTestConfig(wco),
163164
];
164165

packages/angular_devkit/core/src/virtual-fs/host/buffer.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,11 @@ export function stringToFileBuffer(str: string): FileBuffer {
4646
}
4747

4848
export function fileBufferToString(fileBuffer: FileBuffer): string {
49-
if (typeof TextDecoder !== 'undefined') {
49+
if (fileBuffer.toString.length == 1) {
50+
return (fileBuffer.toString as (enc: string) => string)('utf-8');
51+
} else if (typeof Buffer !== 'undefined') {
52+
return new Buffer(fileBuffer).toString('utf-8');
53+
} else if (typeof TextDecoder !== 'undefined') {
5054
// Modern browsers implement TextEncode.
5155
return new TextDecoder('utf-8').decode(new Uint8Array(fileBuffer));
5256
} else {

packages/ngtools/webpack/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"webpack-sources": "^1.1.0"
3232
},
3333
"peerDependencies": {
34+
"@angular-devkit/core": "0.0.0",
3435
"typescript": "~2.5.0 | ~2.6.0 | ~2.7.0",
3536
"webpack": "^4.0.0"
3637
}

packages/ngtools/webpack/src/angular_compiler_plugin.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// TODO: fix webpack typings.
99
// tslint:disable-next-line:no-global-tslint-disable
1010
// tslint:disable:no-any
11+
import { virtualFs } from '@angular-devkit/core';
1112
import { ChildProcess, ForkOptions, fork } from 'child_process';
1213
import * as fs from 'fs';
1314
import * as path from 'path';
@@ -87,6 +88,8 @@ export interface AngularCompilerPluginOptions {
8788

8889
// Use tsconfig to include path globs.
8990
compilerOptions?: ts.CompilerOptions;
91+
92+
host: virtualFs.Host<fs.Stats>;
9093
}
9194

9295
export enum PLATFORM {
@@ -258,7 +261,11 @@ export class AngularCompilerPlugin {
258261
}
259262

260263
// Create the webpack compiler host.
261-
const webpackCompilerHost = new WebpackCompilerHost(this._compilerOptions, this._basePath);
264+
const webpackCompilerHost = new WebpackCompilerHost(
265+
this._compilerOptions,
266+
this._basePath,
267+
this._options.host,
268+
);
262269
webpackCompilerHost.enableCaching();
263270

264271
// Create and set a new WebpackResourceLoader.

0 commit comments

Comments
 (0)