|
5 | 5 | * Use of this source code is governed by an MIT-style license that can be |
6 | 6 | * found in the LICENSE file at https://angular.io/license |
7 | 7 | */ |
8 | | - |
9 | 8 | import { |
10 | 9 | BuildEvent, |
11 | 10 | Builder, |
12 | 11 | BuilderConfiguration, |
13 | 12 | BuilderContext, |
14 | 13 | } from '@angular-devkit/architect'; |
15 | | -import { Path, getSystemPath, normalize, resolve, virtualFs } from '@angular-devkit/core'; |
| 14 | +import { Path, getSystemPath, join, normalize, resolve, virtualFs } from '@angular-devkit/core'; |
16 | 15 | import * as fs from 'fs'; |
17 | 16 | import { Observable } from 'rxjs/Observable'; |
18 | 17 | import { concat as concatObservable } from 'rxjs/observable/concat'; |
@@ -90,11 +89,7 @@ export interface BrowserBuilderOptions { |
90 | 89 | styles: ExtraEntryPoint[]; |
91 | 90 | stylePreprocessorOptions: { includePaths: string[] }; |
92 | 91 |
|
93 | | - // Some options are not needed anymore. |
94 | | - // app?: string; // apps aren't used with build facade |
95 | | - |
96 | | - // TODO: figure out what to do about these. |
97 | | - environment?: string; // Maybe replace with 'fileReplacement' object? |
| 92 | + fileReplacements: { from: string; to: string; }[]; |
98 | 93 | } |
99 | 94 |
|
100 | 95 | export interface AssetPattern { |
@@ -130,7 +125,7 @@ export class BrowserBuilder implements Builder<BrowserBuilderOptions> { |
130 | 125 |
|
131 | 126 | return concatObservable( |
132 | 127 | options.deleteOutputPath |
133 | | - ? this._deleteOutputDir(root, normalize(options.outputPath)) |
| 128 | + ? this._deleteOutputDir(root, normalize(options.outputPath), this.context.host) |
134 | 129 | : empty<BuildEvent>(), |
135 | 130 | new Observable(obs => { |
136 | 131 | // Ensure Build Optimizer is only used with AOT. |
@@ -207,9 +202,22 @@ export class BrowserBuilder implements Builder<BrowserBuilderOptions> { |
207 | 202 | ); |
208 | 203 | } |
209 | 204 |
|
210 | | - buildWebpackConfig(root: Path, projectRoot: Path, options: BrowserBuilderOptions) { |
| 205 | + buildWebpackConfig( |
| 206 | + root: Path, |
| 207 | + projectRoot: Path, |
| 208 | + options: BrowserBuilderOptions, |
| 209 | + ) { |
211 | 210 | let wco: WebpackConfigOptions; |
212 | 211 |
|
| 212 | + const host = new virtualFs.AliasHost(this.context.host as virtualFs.Host<fs.Stats>); |
| 213 | + |
| 214 | + options.fileReplacements.forEach(({from, to}) => { |
| 215 | + host.aliases.set( |
| 216 | + join(root, normalize(from)), |
| 217 | + join(root, normalize(to)), |
| 218 | + ); |
| 219 | + }); |
| 220 | + |
213 | 221 | // TODO: make target defaults into configurations instead |
214 | 222 | // options = this.addTargetDefaults(options); |
215 | 223 |
|
@@ -268,24 +276,24 @@ export class BrowserBuilder implements Builder<BrowserBuilderOptions> { |
268 | 276 |
|
269 | 277 | if (wco.appConfig.main || wco.appConfig.polyfills) { |
270 | 278 | const typescriptConfigPartial = wco.buildOptions.aot |
271 | | - ? getAotConfig(wco, this.context.host as virtualFs.Host<fs.Stats>) |
272 | | - : getNonAotConfig(wco, this.context.host as virtualFs.Host<fs.Stats>); |
| 279 | + ? getAotConfig(wco, host) |
| 280 | + : getNonAotConfig(wco, host); |
273 | 281 | webpackConfigs.push(typescriptConfigPartial); |
274 | 282 | } |
275 | 283 |
|
276 | 284 | return webpackMerge(webpackConfigs); |
277 | 285 | } |
278 | 286 |
|
279 | | - private _deleteOutputDir(root: Path, outputPath: Path): Observable<void> { |
| 287 | + private _deleteOutputDir(root: Path, outputPath: Path, host: virtualFs.Host): Observable<void> { |
280 | 288 | const resolvedOutputPath = resolve(root, outputPath); |
281 | 289 | if (resolvedOutputPath === root) { |
282 | 290 | throw new Error('Output path MUST not be project root directory!'); |
283 | 291 | } |
284 | 292 |
|
285 | | - return this.context.host.exists(resolvedOutputPath).pipe( |
| 293 | + return host.exists(resolvedOutputPath).pipe( |
286 | 294 | switchMap(exists => { |
287 | 295 | if (exists) { |
288 | | - return this.context.host.delete(resolvedOutputPath); |
| 296 | + return host.delete(resolvedOutputPath); |
289 | 297 | } else { |
290 | 298 | return empty<void>(); |
291 | 299 | } |
|
0 commit comments