webpack AngularCompilerPlugin hostReplacementPaths option does not work

665 views Asked by At

After I've updated to Angular6 and updated @ngtools/webpack from "6.0.0-beta.8" to "6.1.2" I found that hostReplacementPaths option does not work anymore.

Normally it replaces correct env files:

new AngularCompilerPlugin({
  ...
  hostReplacementPaths: {
    'environments/environment.ts': environmentFiles[NODE_ENV]
  },
  ...
})

But now it does not.

I made deep debug into @ngtools/webpack and found that normalize function here https://github.com/angular/angular-cli/blob/master/packages/ngtools/webpack/src/virtual_file_system_decorator.ts#L188 does not work

this one https://github.com/angular/angular-cli/blob/master/packages/angular_devkit/core/src/virtual-fs/path.ts#L199

still could not find what went wrong with it. maybe somebody already solved this issue?

Created an issue in angular-cli repo - https://github.com/angular/angular-cli/issues/11801

2

There are 2 answers

0
Yuriy K. On

Looks like will be fixed soon by this PR - https://github.com/angular/angular-cli/pull/11809

As James mentioned, there was a bug introduced in 6.1.0-rc.2 by this commit: https://github.com/angular/angular-cli/commit/86a62adbe8faeb4628296d5d6915c54e6dbfd85b

Paths were not resolved properly.

Before:

const normalizedFrom = normalize(from);
const normalizedWith = normalize(this._options.hostReplacementPaths[from]);

Suggested fix:

const normalizedFrom = resolve(normalize(this._basePath), normalize(from));

const normalizedWith = resolve(
  normalize(this._basePath),
  normalize(this._options.hostReplacementPaths[from]),
);
0
James On

As mentioned in your issue, this is a known bug with version 6.1.0-rc.2 and until a patch has been pushed out, you should downgrade back to 6.1.0-rc.1.