From dc7e9323cfe5d2d75a6df5e92f15163c7487e3d7 Mon Sep 17 00:00:00 2001 From: Hans Larsen Date: Tue, 23 Jan 2018 16:52:47 -0800 Subject: [PATCH] fix(@angular/cli): eject now remove all hardcoded paths In favor of process.cwd(). Also added a new eject e2e test that verifies the webpack.config.js does NOT have the project path in it at any place. Fixed #9335. --- packages/@angular/cli/tasks/eject.ts | 25 +++++++++++++++++++------ tests/e2e/tests/commands/eject/eject.ts | 5 +++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/packages/@angular/cli/tasks/eject.ts b/packages/@angular/cli/tasks/eject.ts index 9f9ac2b171c4..afed5849a75a 100644 --- a/packages/@angular/cli/tasks/eject.ts +++ b/packages/@angular/cli/tasks/eject.ts @@ -225,6 +225,7 @@ class JsonWebpackSerializer { break; case CircularDependencyPlugin: this.variableImports['circular-dependency-plugin'] = 'CircularDependencyPlugin'; + args.cwd = this._escape('projectRoot'); break; case AotPlugin: args = this._aotPluginSerialize(plugin); @@ -264,11 +265,13 @@ class JsonWebpackSerializer { // CopyWebpackPlugin doesn't have a constructor nor save args. this.variableImports['copy-webpack-plugin'] = 'CopyWebpackPlugin'; const patternOptions = plugin['copyWebpackPluginPatterns'].map((pattern: any) => { - if (!pattern.context) { - return pattern; + if (pattern.context) { + pattern.context = path.relative(process.cwd(), pattern.context); } - const context = path.relative(process.cwd(), pattern.context); - return { ...pattern, context }; + if (pattern.from && pattern.from.glob) { + pattern.from.glob = path.relative(process.cwd(), pattern.from.glob); + } + return pattern; }); const patternsSerialized = serializer(patternOptions); const optionsSerialized = serializer(plugin['copyWebpackPluginOptions']) || 'undefined'; @@ -315,7 +318,7 @@ class JsonWebpackSerializer { if (loader.match(/\/node_modules\/extract-text-webpack-plugin\//)) { return 'extract-text-webpack-plugin'; } else if (loader.match(/@ngtools\/webpack\/src\/index.ts/)) { - // return '@ngtools/webpack'; + return '@ngtools/webpack'; } } else { if (loader.loader) { @@ -327,7 +330,17 @@ class JsonWebpackSerializer { Object.keys(args.variableImports) .forEach(key => this.variableImports[key] = args.variableImports[key]); Object.keys(args.variables) - .forEach(key => this.variables[key] = JSON.stringify(args.variables[key])); + .forEach(key => { + const value = args.variables[key]; + if (value === process.cwd()) { + this.variables[key] = 'process.cwd()'; + } else if (typeof value == 'string' && value.startsWith(process.cwd())) { + this.variables[key] = 'process.cwd() + ' + + JSON.stringify(value.substr(process.cwd().length)); + } else { + this.variables[key] = JSON.stringify(value); + } + }); this.variables['postcssPlugins'] = loader.options.plugins; loader.options.plugins = this._escape('postcssPlugins'); diff --git a/tests/e2e/tests/commands/eject/eject.ts b/tests/e2e/tests/commands/eject/eject.ts index 0205f675cf85..6a69635b9210 100644 --- a/tests/e2e/tests/commands/eject/eject.ts +++ b/tests/e2e/tests/commands/eject/eject.ts @@ -1,4 +1,5 @@ import * as path from 'path'; +import { expectFileToMatch, readFile } from '../../../utils/fs'; import {ng, silentNpm, exec} from '../../../utils/process'; import {expectToFail} from '../../../utils/utils'; @@ -12,6 +13,10 @@ export default function() { .then(() => expectToFail(() => ng('e2e'))) .then(() => expectToFail(() => ng('serve'))) .then(() => expectToFail(() => expectGitToBeClean())) + + // Check that no path appears anymore. + .then(() => expectToFail(() => expectFileToMatch('webpack.config.js', process.cwd()))) + .then(() => silentNpm('install')) .then(() => exec(path.join('node_modules', '.bin', 'webpack'))); }