webpack 2 ts-loader排除node_modules无法正常工作

时间:2017-03-29 14:57:46

标签: angular typescript webpack

我尝试在typescript 2.2.2中使用babelwebpack 2个加载器并排除node_modules,但是当尝试webpack命令时仍然会从angular 4.0.0获取错误}在node_modules中。

webpack.congfig.js:

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

var PROD = true;

    module.exports = {
        entry: path.join(__dirname, 'ng2', 'src','index.js'),
        output: {
            path: path.join(__dirname, 'ng2', 'dist'),
            filename: 'vendorNG2.js'
        },
        devtool: 'eval',
        resolve: {
            extensions: [".tsx", ".ts", ".js"]
        },
        watch: true,
        module: {
            rules: [
                {
                    test: /\.tsx?$/,
                    loader: 'ts-loader',
                    exclude: [/node_modules/],
                },
                {
                    test: /\.js$/,
                    exclude: [/node_modules/],
                    use: {

                        loader: "babel-loader?cacheDirectory=true",
                        options: {
                            presets: ['es2015']
                        }
                    }
                },
            ]
        },
        plugins: PROD ? [
            new webpack.optimize.UglifyJsPlugin({
                 compress: { warnings: false }
            })
      ] : []
    }

在webpack --progress命令之后:

...
ERROR in /home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts
(13,17): error TS2693: 'Map' only refers to a type, but is being used as a value here.

ERROR in /home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts
(14,17): error TS2693: 'Set' only refers to a type, but is being used as a value here.

ERROR in ./ng2/src/app/main.ts
(19,14): error TS1219: Experimental support for decorators is a feature that is subject to change in a future release. Set the 'experimentalDecorators' option to remove this warning.

您可以看到来自node_modules的错误。

tsconfig.json v1:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "allowJs": true
  }
}

tsconfig.json v2:

{
  "compilerOptions": {
    "outDir": "./ng2/dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "type":[],
    "allowJs": true
  },
  "exclude": [
    "node_modules"
  ]
}

v2错误:

ERROR in /home/user/Projects/project/front/admin/tsconfig.json
error TS5023: Unknown compiler option 'type'.
 @ ./ng2/src/index.js 8:0-21

ERROR in ./ng2/src/app/main.ts
Module build failed: error while parsing tsconfig.json
 @ ./ng2/src/index.js 8:0-21

tsconfig.js v3 add(" experimentalDecorators":true)一些错误消失了:

{
  "compilerOptions": {
    "outDir": "./ng2/dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
     "experimentalDecorators": true,
    "allowJs": true
  },
  "exclude": [
    "node_modules"
  ]
}

v3错误:

... /home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts中的错误 (13,17):错误TS2693:' Map'仅指类型,但在此处用作值。

/home/user/Projects/project/front/node_modules/angular2/src/facade/lang.d.ts中的错误     (14,17):错误TS2693:'设置'仅指类型,但在此处用作值。

1 个答案:

答案 0 :(得分:1)

您是否在您的tsconfig中添加了排除node_modules?

{
  "compilerOptions": {
    "outDir": "./dist/",
    "sourceMap": true,
    "noImplicitAny": true,
    "module": "commonjs",
    "target": "es5",
    "jsx": "react",
    "allowJs": true
  },
  "exclude": [
    "node_modules"
  ]
}

另外,您没有指定正在使用的Typescript和Angular版本。

编辑: 您更新了问题以指定您正在使用Angular 4,但更新的错误显示了一个似乎不正确的node_modules / angular2 /文件夹。在继续之前,请尝试删除node_modules文件夹并再次运行npm install

否则看起来你有一个打字问题。你有安装@types吗?

以下是我的tsconfig.json如何设置的示例:

{
    "compilerOptions": {
        "target": "ES5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [ "es2015", "dom" ],
        "removeComments": false,
        "noImplicitAny": false,
        "types": [ "jasmine", "lodash", "core-js" ],
        "typeRoots": [
            "../node_modules/@types"
        ]
    },
    "exclude": [
        "node_modules",
        "wwwroot",
        "temp"
    ]
}

我的node_modules文件夹位于父目录中,因此我在typeRoots中指定了该目录。 有了这个,我也安装了以下类型:

"@types/core-js": "0.9.37",
"@types/jasmine": "2.5.43",
"@types/lodash": "4.14.55",