ts-loader icon indicating copy to clipboard operation
ts-loader copied to clipboard

When using typescript 1.9.0, "exculde" in tsconfig.json is ignored only on Windows

Open t246246 opened this issue 10 years ago • 15 comments

I am trying to transpile small sample with @angular/router 3.0.0-alpha.3 with webpack 2.1.0-beta.13 I tried to describe as simple, but I'm sorry for setting becomes slightly longer.

$ node -v
v4.4.5

Here is webpack.config

const path = require('path');
const webpack = require('webpack');

function root(p) {
    return path.resolve(__dirname, p);
}

module.exports = function(env) {
    const config = {
        context: root('app'),
        entry: {
            app: [
                './main.ts'
            ],
            vendor: [
                'reflect-metadata', 'zone.js',
                '@angular/common', '@angular/core', '@angular/http',
                '@angular/platform-browser-dynamic',
                '@angular/router'
            ]
        },
        output: {
            path: root('dist'),
            publicPath: '/dist',
            filename: 'bundle.js'
        },
        resolve: {
            modules: [
                root('app'),
                'node_modules'
            ],
            extensions: ['', '.webpack.js', '.web.js', '.ts', '.js'],
        },
        module: {
            loaders: [{
                test: /\.ts$/,
                loader: 'babel-loader!ts-loader'
            }, {
                test: /\.json$/,
                loader: 'json-loader'
            }],
        },
        devServer: {
            historyApiFallback: true,
            hot: true,
            progress: true,
        },
        debug: true,
        plugins: [
            new webpack.optimize.CommonsChunkPlugin({
                name: 'vendor',
                minChunks: Infinity,
                filename: 'vendor.bundle.js'
            }),
            new webpack.LoaderOptionsPlugin({
                minimize: true,
                debug: false
            })
        ]
    };

    if (env && env.production) {
        config.devtool = 'hidden-source-map';
        config.plugins.push.apply([
            new webpack.optimize.UglifyJsPlugin({
                beautify: false,
                mangle: false,
                comments: true
            })
        ]);
        return config;
    }

    config.devtool = 'eval-source-map';
    return config;
}

.babelrc

{
    "presets": ["es2015-native-modules"]
}

package.json

{
    "name": "work",
    "version": "1.0.0",
    "description": "",
    "main": "index.html",
    "directories": {
        "test": "test"
    },
    "scripts": {
        "postinstall": "npm run typings -- install",
        "typings": "typings",
        "build": "webpack",
        "start": "webpack-dev-server --hot --inline"
    },
    "license": "ISC",
    "devDependencies": {
        "babel-loader": "^6.2.4",
        "babel-preset-es2015-native-modules": "^6.6.0",
        "json-loader": "^0.5.4",
        "ts-loader": "^0.8.2",
        "typescript": "^1.9.0-dev.20160613-1.0",
        "typings": "^1.0.5",
        "webpack": "^2.1.0-beta.13",
        "webpack-dev-server": "^2.1.0-beta.0"
    },
    "dependencies": {
        "@angular/common": "^2.0.0-rc.1",
        "@angular/compiler": "^2.0.0-rc.1",
        "@angular/core": "^2.0.0-rc.1",
        "@angular/http": "^2.0.0-rc.1",
        "@angular/platform-browser": "^2.0.0-rc.1",
        "@angular/platform-browser-dynamic": "^2.0.0-rc.1",
        "@angular/router": "^3.0.0-alpha.3",
        "reflect-metadata": "^0.1.3",
        "rxjs": "^5.0.0-beta.6",
        "zone.js": "^0.6.12"
    }
}

I will show ts and html for sure.

app/main.ts

// main entry point
import { Component } from '@angular/core';
import { bootstrap } from '@angular/platform-browser-dynamic';
import { ROUTER_DIRECTIVES, provideRouter, RouterConfig } from '@angular/router';

const routes: RouterConfig = [
  {
    path: '/', component: AppComponent
  }
];

export const APP_ROUTER_PROVIDERS = [
  provideRouter(routes),
];

@Component({
  selector: 'my-app',
  template: `Hello`,
  directives: [ROUTER_DIRECTIVES]
})
export class AppComponent { }

bootstrap(AppComponent, [
  APP_ROUTER_PROVIDERS
]).catch(err => console.error(err));

index.html

<!DOCTYPE html>
    <html>
    <head>
        <!-- Set the base href -->
        <base href="." />
        <title>Router Sample</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
    </head>
    <body>
        <my-app>loading...</my-app>
        <script src="dist/vendor.bundle.js"></script>
        <script src="dist/bundle.js"></script>
    </body>
    </html>

As I exclude node_modules and typings in tsconfig.json

{
    "compilerOptions": {
        "target": "ES6",
        "module": "ES6",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "removeComments": false,
        "noImplicitAny": false,
        "declaration": false
    },
    "compileOnSave": false,
    "buildOnSave": false,
    "exclude": [
        "node_modules",
        "typings"
    ]
}

So

$ node_modules/.bin/tsc -p .

generates no error here. So far looks OK.

But when I try to transpile it

$ npm run build
> [email protected] build C:\work\test
> webpack

ts-loader: Using [email protected] and C:\work\test\tsconfig.json
Hash: fd3c3c14f7c8cd7c0794
Version: webpack 2.1.0-beta.13
Time: 20014ms
           Asset     Size  Chunks             Chunk Names
vendor.bundle.js  6.94 MB       0  [emitted]  vendor
       bundle.js  5.91 kB       1  [emitted]  app
 [490] multi app 28 bytes {1} [built]
 [491] multi vendor 100 bytes {0} [built]
    + 490 hidden modules

Seem still OK, but then following errors are on console.

ERROR in C:\work\test\node_modules\zone.js\dist\zone.js.d.ts
(122,11): error TS2451: Cannot redeclare block-scoped variable 'Zone'.

ERROR in C:\work\test\node_modules\zone.js\dist\zone.js.d.ts
(354,14): error TS2300: Duplicate identifier 'HasTaskState'.

[skip many errors]

ERROR in C:\work\test\node_modules\rxjs\src\observable\BoundCallbackObservable.ts
(130,9): error TS2341: Property 'callbackFunc' is private and only accessible within class 'BoundCallbackObservable<T>'.

ERROR in C:\work\test\node_modules\rxjs\src\Subject.ts
(127,14): error TS2415: Class 'AnonymousSubject<T>' incorrectly extends base class 'Subject<T>'.
  Property 'source' is optional in type 'AnonymousSubject<T>' but required in type 'Subject<T>'.

(Same error occurs when I invoke webpack-dev-server --hot --inline.)

It seems all errors are from typescript and all related .ts files are in node_modules, so I guess some process (that is not the first transpilation step) does not see exclude setting in tsconfig.json. Despite of this error, bundle.js files are generated and application seems to work. (Indeed, I tried little more complex one.) But as this is recognized as error, Hot Module Replacement won't be active, that's the problem.

And oddly, this happens only on Windows, both in command prompt, cygwin, mingw. I tried exact the same one on Mac and Linux, and everything is fine on both environment.

Only workaround I found is use files property instead of exculde in tsconfig.json. However there is no support for wildcard or regexp for files, I have to write all .ts file.

So I'd appreciate if someone confirm this is a bug (in loader or webpack itself) or kindly tell me what I am missing.

Thanks in advance.

t246246 avatar Jun 14 '16 01:06 t246246

I think you might need an exclude in your WebPack.config.js - something like this:

loaders: [{
      test: /\.ts(x?)$/,
      exclude: /node_modules/,
      loader: 'babel-loader?presets[]=es2015!ts-loader'
    }, {
      test: /\.js$/,
      exclude: /node_modules/,
      loader: 'babel',
      query: {
        presets: ['es2015']
      }
    }]

johnnyreilly avatar Jun 14 '16 05:06 johnnyreilly

I've just noticed you're using WebPack 2 beta. I don't know if that's supported.. i suspect not

johnnyreilly avatar Jun 14 '16 05:06 johnnyreilly

Thank you for your reply.

"exclude" in webpack.config.js does not work as well. Maybe true webpack-2.1.0-beta is not fully supported. yet.

But I tried with other project that do not have @angular-3.0.0-alpha, that means it does not require typescript 1.9. When I change typescript to current version 1.8.10, no errors generated when I use almost same config written for webpack-2.1.0-beta even on Windows.

So I guess this problem may be caused from something changed in typescript 1.9.0.

PS. Same error occurs if I changed ts-loader to awesome-typescript-loader, also only on Windows.

t246246 avatar Jun 15 '16 04:06 t246246

@johnnyreilly: As you suggested, I changed to webpack 1.13.1 and rewrite webpack.config.js to simpler one.

var path = require('path');
var webpack = require("webpack");

function root(p) {
    return path.join(__dirname, p);
}

module.exports = {
    context: root('app'),
    entry: ['main.ts'],
    output: {
        path: root('dist'),
        publicPath: '/dist',
        filename: 'bundle.js'
    },
    resolve: {
        modulesDirectories: ['node_modules'],
        root: root('app'),
        extensions: ['', '.webpack.js', '.web.js', '.ts', '.js']
    },
    module: {
        loaders: [
            {
                test: /\.ts(x?)$/,
                exclude: /node_modules/,
                loader: 'babel-loader?presets[]=es2015-native-modules!ts-loader'
            }, {
                test: /\.js$/,
                exclude: /node_modules/,
                loader: 'babel',
                query: {
                    presets: ['es2015-native-modules']
                }
            }
        ]
    },
    devServer: {
        historyApiFallback: true,
        progress: true
    },
    debug: true
};

But still, I got same typescript error.

$ npm run build

> [email protected] build C:\work\test-webpack1
> webpack

ts-loader: Using [email protected] and C:\work\test-webpack1\tsconfig.json
Hash: e9227aac77c204467d86
Version: webpack 1.13.1
Time: 17061ms
    Asset     Size  Chunks             Chunk Names
bundle.js  3.48 kB       0  [emitted]  main
   [0] multi main 28 bytes {0} [built]
    + 1 hidden modules

ERROR in C:\work\test-webpack1\node_modules\zone.js\dist\zone.js.d.ts
(122,11): error TS2451: Cannot redeclare block-scoped variable 'Zone'.

ERROR in C:\work\test-webpack1\node_modules\zone.js\dist\zone.js.d.ts
(354,14): error TS2300: Duplicate identifier 'HasTaskState'.

ERROR in C:\work\test-webpack1\node_modules\zone.js\dist\zone.js.d.ts
(363,14): error TS2300: Duplicate identifier 'TaskType'.

ERROR in C:\work\test-webpack1\node_modules\zone.js\dist\zone.js.d.ts
(445,15): error TS2451: Cannot redeclare block-scoped variable 'Zone'.

ERROR in C:\work\test-webpack1\node_modules\typings-core\node_modules\typescript\lib\typescriptServices.d.ts
(18,9): error TS2374: Duplicate string index signature.

[ ... skip many errors ...]

ERROR in C:\work\test-webpack1\node_modules\rxjs\src\observable\BoundNodeCallbackObservable.ts
(130,9): error TS2341: Property 'args' is private and only accessible within class 'BoundNodeCallbackObservable<T>'.

ERROR in C:\work\test-webpack1\node_modules\rxjs\src\observable\BoundNodeCallbackObservable.ts
(130,9): error TS2341: Property 'callbackFunc' is private and only accessible within class 'BoundNodeCallbackObservable<T>'.

ERROR in C:\work\test-webpack1\node_modules\rxjs\src\Subject.ts
(127,14): error TS2415: Class 'AnonymousSubject<T>' incorrectly extends base class 'Subject<T>'.
  Property 'source' is optional in type 'AnonymousSubject<T>' but required in type 'Subject<T>'.

But in this case (i.e. Webpack 1), awesome-typescript-loader (1.0.0) does not generate error at all even on Windows.

t246246 avatar Jun 16 '16 00:06 t246246

It seems awesome-typescript-loader does not generate errors even though I use webpack 2.1.0-beta-13. (Although I also changed some modules since my first report --- angular2 from rc1 to rc2, typescript from 1.9.0-dev.20160613-1.0 to 1.9.0-dev.20160616-1.0. ts-loader does generate typescript errors with this version.)

Summary:

Problem occurs:

  • when I use typescript 1.9.0-dev, webpack-2.1.0-beta.13 and ts-loader on Windows.

Solved if:

  • use files instead of exculde in tsconfig.json

    or

  • use typescript 1.8.10

    or

  • use awesome-typescript-loader (instead of ts-loader)

    or

  • use MacOS X or linux instead of Windows.

Doesn't solved when:

  • use webpack 1.13.1

t246246 avatar Jun 16 '16 01:06 t246246

Just confirming the same on my system. Everything works with TSC as expected but the ts-loader fails to respect the "excludes".

Psvensso avatar Jun 18 '16 11:06 Psvensso

Typescript issue: 9123 Pull req. 9138

Seems to work fine after thoose fixes.

Psvensso avatar Jun 18 '16 12:06 Psvensso

Yeah same problem here and fixed in TypeScript issue&PR above. Maybe we can get some attention on this. (waiting for merge or something else :))

otbe avatar Jun 18 '16 14:06 otbe

Everything works with TSC as expected but the ts-loader fails to respect the "excludes".

@Psvensso Does ts-loader really take "exclude" and "files" property of tsconfig.json into account (see this question on StackOverflow)? I would expect, that the root files of tsconfig.json have no impact on ts-loader, since entry points are specified in webpack.config.json.

maiermic avatar Jul 23 '16 15:07 maiermic

Is this still a problem?

johnnyreilly avatar Oct 12 '16 08:10 johnnyreilly

Seems to be fixed for me (CI job is green) :)

otbe avatar Oct 12 '16 15:10 otbe

Awesome!

johnnyreilly avatar Oct 12 '16 15:10 johnnyreilly

Not fixed here - using typescript 2.0.3, webpack 1.13.2, ts-loader 1.13.2.

I'd like NOT to specify 'exclude' in tsconfig, but instead do it in webpack.conf.js. However, whatever I do (even following the official example with loader: 'ts-loader' and exclude: /\.spec\.ts$/, but same goes for /node_modules/), it fails to exclude those files (in my case, '.spec.ts' or even 'node_modules').

Is there something else to it? Mind that it is happening on Trusty, not just Windows.. Thanks!

dinvlad avatar Oct 20 '16 23:10 dinvlad

Even if I just specify test: /src\/.*\.ts(x?)$/, it still looks inside node_modules! So it appears to always include everything that is not explicitly excluded in tsconfig..

dinvlad avatar Oct 20 '16 23:10 dinvlad

Could you open another issue and provide a repro please?

johnnyreilly avatar Oct 21 '16 05:10 johnnyreilly