From 32e60b77639bf1aef304581e84a494ab1294635b Mon Sep 17 00:00:00 2001 From: Filipe Silva Date: Sun, 2 Oct 2016 17:06:07 +0100 Subject: [PATCH] fix(build): use baseUrl and paths from tsconfig (#2470) --- .../angular-cli/models/webpack-build-test.js | 8 ++++- .../models/webpack-build-typescript.ts | 7 ++++ packages/angular-cli/tasks/build-webpack.ts | 7 ++-- tests/e2e/tests/build/ts-paths.ts | 35 +++++++++++++++++++ tests/e2e/utils/fs.ts | 5 +++ 5 files changed, 56 insertions(+), 6 deletions(-) create mode 100644 tests/e2e/tests/build/ts-paths.ts diff --git a/packages/angular-cli/models/webpack-build-test.js b/packages/angular-cli/models/webpack-build-test.js index 403ed91fdeec..e1d45510ec70 100644 --- a/packages/angular-cli/models/webpack-build-test.js +++ b/packages/angular-cli/models/webpack-build-test.js @@ -2,6 +2,7 @@ const path = require('path'); const webpack = require('webpack'); +const atl = require('awesome-typescript-loader'); const getWebpackTestConfig = function (projectRoot, environment, appConfig) { @@ -11,7 +12,12 @@ const getWebpackTestConfig = function (projectRoot, environment, appConfig) { devtool: 'inline-source-map', context: path.resolve(__dirname, './'), resolve: { - extensions: ['.ts', '.js'] + extensions: ['.ts', '.js'], + plugins: [ + new atl.TsConfigPathsPlugin({ + tsconfig: path.resolve(appRoot, appConfig.tsconfig) + }) + ] }, entry: { test: path.resolve(appRoot, appConfig.test) diff --git a/packages/angular-cli/models/webpack-build-typescript.ts b/packages/angular-cli/models/webpack-build-typescript.ts index 339b03c604f6..09a5f3e10d1d 100644 --- a/packages/angular-cli/models/webpack-build-typescript.ts +++ b/packages/angular-cli/models/webpack-build-typescript.ts @@ -16,6 +16,13 @@ export const getWebpackNonAotConfigPartial = function(projectRoot: string, appCo const lazyModules = findLazyModules(appRoot); return { + resolve: { + plugins: [ + new atl.TsConfigPathsPlugin({ + tsconfig: path.resolve(appRoot, appConfig.tsconfig) + }) + ] + }, module: { rules: [ { diff --git a/packages/angular-cli/tasks/build-webpack.ts b/packages/angular-cli/tasks/build-webpack.ts index 88941955247b..80fc0b9e62a5 100644 --- a/packages/angular-cli/tasks/build-webpack.ts +++ b/packages/angular-cli/tasks/build-webpack.ts @@ -45,11 +45,8 @@ export default Task.extend({ if (err) { lastHash = null; - console.error(err.stack || err); - if (err.details) { - console.error(err.details); - } - reject(err.details); + console.error(err.details || err); + reject(err.details || err); } if (stats.hash !== lastHash) { diff --git a/tests/e2e/tests/build/ts-paths.ts b/tests/e2e/tests/build/ts-paths.ts new file mode 100644 index 000000000000..f5e5b125854b --- /dev/null +++ b/tests/e2e/tests/build/ts-paths.ts @@ -0,0 +1,35 @@ +import {updateTsConfig} from '../../utils/project'; +import {writeMultipleFiles, appendToFile} from '../../utils/fs'; +import {ng} from '../../utils/process'; +import {stripIndents} from 'common-tags'; + + +export default function() { + return updateTsConfig(json => { + json['compilerOptions']['baseUrl'] = '.'; + json['compilerOptions']['paths'] = { + '@shared': [ + 'app/shared' + ], + '@shared/*': [ + 'app/shared/*' + ] + }; + }) + .then(() => writeMultipleFiles({ + 'src/app/shared/meaning.ts': 'export var meaning = 42;', + 'src/app/shared/index.ts': `export * from './meaning'` + })) + .then(() => appendToFile('src/app/app.component.ts', stripIndents` + import { meaning } from 'app/shared/meaning'; + import { meaning as meaning2 } from '@shared'; + import { meaning as meaning3 } from '@shared/meaning'; + + // need to use imports otherwise they are ignored and + // no error is outputted, even if baseUrl/paths don't work + console.log(meaning) + console.log(meaning2) + console.log(meaning3) + `)) + .then(() => ng('build')); +} diff --git a/tests/e2e/utils/fs.ts b/tests/e2e/utils/fs.ts index f6a56ee76d43..5ff15116456e 100644 --- a/tests/e2e/utils/fs.ts +++ b/tests/e2e/utils/fs.ts @@ -93,6 +93,11 @@ export function replaceInFile(filePath: string, match: RegExp, replacement: stri } +export function appendToFile(filePath: string, text: string) { + return readFile(filePath) + .then((content: string) => writeFile(filePath, content.concat(text))); +} + export function expectFileToExist(fileName: string) { return new Promise((resolve, reject) => {