Skip to content

feat(@angular/cli): use separate tsconfigs #4924

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/documentation/stories/third-party-lib.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ npm install d3 --save
npm install @types/d3 --save-dev
```

Then open `src/tsconfig.app.json` and add it to the `types` array:

```
"types":[
"d3"
]
```

If the library you added typings for is only to be used on your e2e tests,
instead use `e2e/tsconfig.e2e.json`.
The same goes for unit tests and `src/tsconfig.spec.json`.

If the library doesn't have typings available at `@types/`, you can still use it by
manually adding typings for it:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{<% if (ng4) { %>
"extends": "<%= relativeRootPath %>/tsconfig.json",
"compilerOptions": {
"lib": [
"es2016",
"dom"
],<% } else { %>
"compilerOptions": {
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016",
"dom"
],<% } %>
"outDir": "<%= relativeRootPath %>/out-tsc/app",
"target": "es5",
"module": "es2015",
"baseUrl": "",
"types": []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@filipesilva A bit late question but why declaring types manually is better than having one global typeRoots (as there was before)?

"typeRoots": [
    "../node_modules/@types"
]

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of reasons:

  • Some typings might actually have duplicate indentifiers. This has happened before with protractor and jquery both using $.
  • You might be using typings that really don't shouldn't in your app, like describe that should only be in unit tests, or node typings.

That being said, it is more convenient to use typeRoots if you're not too concerned with those things.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds convincing! Thanks for such a detail comment!! 🍺 🍺

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was hard to dig out, but thanks for clarifying. 👍

},
"exclude": [
"test.ts",
"**/*.spec.ts"
]
}
21 changes: 0 additions & 21 deletions packages/@angular/cli/blueprints/ng2/files/__path__/tsconfig.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{<% if (ng4) { %>
"extends": "<%= relativeRootPath %>/tsconfig.json",
"compilerOptions": {<% } else { %>
"compilerOptions": {
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016"
],<% } %>
"outDir": "<%= relativeRootPath %>/out-tsc/spec",
"module": "commonjs",
"target": "es6",
"baseUrl": "",
"types": [
"jasmine",
"node"
]
},
"files": [
"test.ts"
],
"include": [
"**/*.spec.ts"
]
}
12 changes: 7 additions & 5 deletions packages/@angular/cli/blueprints/ng2/files/angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"main": "main.ts",
"polyfills": "polyfills.ts",
"test": "test.ts",
"tsconfig": "tsconfig.json",
"tsconfig": "tsconfig.app.json",
"testTsconfig": "tsconfig.spec.json",
"prefix": "<%= prefix %>",
"styles": [
"styles.<%= styleExt %>"
Expand All @@ -35,12 +36,13 @@
},
"lint": [
{
"files": "<%= sourceDir %>/**/*.ts",
"project": "<%= sourceDir %>/tsconfig.json"
"project": "<%= sourceDir %>/tsconfig.app.json"
},
{
"files": "e2e/**/*.ts",
"project": "e2e/tsconfig.json"
"project": "<%= sourceDir %>/tsconfig.spec.json"
},
{
"project": "e2e/tsconfig.e2e.json"
}
],
"test": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
{
"compileOnSave": false,
{<% if (ng4) { %>
"extends": "../tsconfig.json",
"compilerOptions": {<% } else { %>
"compilerOptions": {
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016"
],
"module": "commonjs",
"moduleResolution": "node",
],<% } %>
"outDir": "../dist/out-tsc-e2e",
"sourceMap": true,
"module": "commonjs",
"target": "es6",
"typeRoots": [
"../node_modules/@types"
"types":[
"jasmine",
"node"
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports.config = {
},
beforeLaunch: function() {
require('ts-node').register({
project: 'e2e'
project: 'e2e/tsconfig.e2e.json'
});
},
onPrepare() {
Expand Down
14 changes: 14 additions & 0 deletions packages/@angular/cli/blueprints/ng2/files/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2016"
]
}
}
6 changes: 5 additions & 1 deletion packages/@angular/cli/lib/config/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,13 @@
},
"tsconfig": {
"type": "string",
"default": "tsconfig.json",
"default": "tsconfig.app.json",
"description": "The name of the TypeScript configuration file."
},
"testTsconfig": {
"type": "string",
"description": "The name of the TypeScript configuration file for unit tests."
},
"prefix": {
"type": "string",
"description": "The prefix to apply to generated selectors."
Expand Down
1 change: 1 addition & 0 deletions packages/@angular/cli/models/webpack-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ export class NgCliWebpackConfig {

public addAppConfigDefaults(appConfig: any) {
const appConfigDefaults: any = {
testTsconfig: appConfig.tsconfig,
scripts: [],
styles: []
};
Expand Down
86 changes: 34 additions & 52 deletions packages/@angular/cli/models/webpack-configs/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,79 +64,61 @@ function _createAotPlugin(wco: WebpackConfigOptions, options: any) {
}

return new AotPlugin(Object.assign({}, {
tsConfigPath: path.resolve(projectRoot, appConfig.root, appConfig.tsconfig),
mainPath: path.join(projectRoot, appConfig.root, appConfig.main),
i18nFile: buildOptions.i18nFile,
i18nFormat: buildOptions.i18nFormat,
locale: buildOptions.locale,
hostReplacementPaths
hostReplacementPaths,
// If we don't explicitely list excludes, it will default to `['**/*.spec.ts']`.
exclude: []
}, options));
}


export const getNonAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
let exclude = [ '**/*.spec.ts' ];
if (appConfig.test) {
exclude.push(path.join(projectRoot, appConfig.root, appConfig.test));
}
const { appConfig, projectRoot } = wco;
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);

return {
module: {
rules: [
{
test: /\.ts$/,
loader: webpackLoader,
exclude: [/\.(spec|e2e)\.ts$/]
}
]
},
plugins: [
_createAotPlugin(wco, { exclude, skipCodeGeneration: true }),
]
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
plugins: [ _createAotPlugin(wco, { tsConfigPath, skipCodeGeneration: true }) ]
};
};

export const getAotConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
let exclude = [ '**/*.spec.ts' ];
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);
const testTsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.testTsconfig);

let pluginOptions: any = { tsConfigPath };

// Fallback to exclude spec files from AoT compilation on projects using a shared tsconfig.
if (testTsConfigPath === tsConfigPath) {
let exclude = [ '**/*.spec.ts' ];
if (appConfig.test) { exclude.push(path.join(projectRoot, appConfig.root, appConfig.test)); };
pluginOptions.exclude = exclude;
}

return {
module: {
rules: [
{
test: /\.ts$/,
loader: webpackLoader,
exclude: [/\.(spec|e2e)\.ts$/]
}
]
},
plugins: [
_createAotPlugin(wco, { exclude })
]
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
plugins: [ _createAotPlugin(wco, pluginOptions) ]
};
};

export const getNonAotTestConfig = function(wco: WebpackConfigOptions) {
const { projectRoot, appConfig } = wco;
const tsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.testTsconfig);
const appTsConfigPath = path.resolve(projectRoot, appConfig.root, appConfig.tsconfig);

let pluginOptions: any = { tsConfigPath, skipCodeGeneration: true };

// Fallback to correct module format on projects using a shared tsconfig.
if (tsConfigPath === appTsConfigPath) {
pluginOptions.compilerOptions = { module: 'commonjs' };
}

return {
module: {
rules: [
{
test: /\.ts$/,
loader: webpackLoader,
query: { module: 'commonjs' },
exclude: [/\.(e2e)\.ts$/]
}
]
},
plugins: [
_createAotPlugin(wco, {
exclude: [],
skipCodeGeneration: true,
compilerOptions: {
module: 'commonjs'
}
}),
]
module: { rules: [{ test: /\.ts$/, loader: webpackLoader }] },
plugins: [ _createAotPlugin(wco, pluginOptions) ]
};
};
46 changes: 21 additions & 25 deletions packages/@ngtools/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,35 +122,31 @@ export class AotPlugin implements Tapable {
);
}

// Default exclude to **/*.spec.ts files.
if (!options.hasOwnProperty('exclude')) {
options['exclude'] = ['**/*.spec.ts'];
}

// Add custom excludes to default TypeScript excludes.
if (options.hasOwnProperty('exclude')) {
// If the tsconfig doesn't contain any excludes, we must add the default ones before adding
// any extra ones (otherwise we'd include all of these which can cause unexpected errors).
// This is the same logic as present in TypeScript.
if (!tsConfigJson.exclude) {
tsConfigJson['exclude'] = ['node_modules', 'bower_components', 'jspm_packages'];
if (tsConfigJson.compilerOptions && tsConfigJson.compilerOptions.outDir) {
tsConfigJson.exclude.push(tsConfigJson.compilerOptions.outDir);
}
}

// Join our custom excludes with the existing ones.
tsConfigJson.exclude = tsConfigJson.exclude.concat(options.exclude);
}

const tsConfig = ts.parseJsonConfigFileContent(
tsConfigJson, ts.sys, basePath, null, this._tsConfigPath);

let fileNames = tsConfig.fileNames;
if (options.hasOwnProperty('exclude')) {
let exclude: string[] = typeof options.exclude == 'string'
? [options.exclude as string] : (options.exclude as string[]);

exclude.forEach((pattern: string) => {
const basePathPattern = '(' + basePath.replace(/\\/g, '/')
.replace(/[\-\[\]\/{}()+?.\\^$|*]/g, '\\$&') + ')?';
pattern = pattern
// Replace windows path separators with forward slashes.
.replace(/\\/g, '/')
// Escape characters that are used normally in regexes, except stars.
.replace(/[\-\[\]{}()+?.\\^$|]/g, '\\$&')
// Two stars replacement.
.replace(/\*\*/g, '(?:.*)')
// One star replacement.
.replace(/\*/g, '(?:[^/]*)')
// Escape characters from the basePath and make sure it's forward slashes.
.replace(/^/, basePathPattern);

const re = new RegExp('^' + pattern + '$');
fileNames = fileNames.filter(x => !x.replace(/\\/g, '/').match(re));
});
} else {
fileNames = fileNames.filter(fileName => !/\.spec\.ts$/.test(fileName));
}
this._rootFilePath = fileNames;

// Check the genDir. We generate a default gendir that's under basepath; it will generate
Expand Down
Loading