webpack 加载器总是在编译中包含 node_modules

webpack loader always include node_modules in compilation

我正在使用 webpack 1.13.3,作为对 this question 的回复,我在底部提供了我的 tsconfig.json。

我的加载器总是包含 node_modules 包,无论我使用包含、排除还是两者的组合。

webpack 配置:

var path = require("path");

module.exports = {
    entry: "./src/bootstrap.tsx",
    output: {
        filename: "./public/dist/bundle.js",
    },

    // Enable sourcemaps for debugging webpack's output.
    devtool: "cheap-module-source-map",

    resolve: {
        // Add '.ts' and '.tsx' as resolvable extensions.
        extensions: ["", ".webpack.js", ".web.js", ".ts", ".tsx", ".js"]
    },

    module: {
        loaders: [
            // All files with a '.ts' or '.tsx' extension will be handled by 'ts-loader'.
            { include: path.resolve(__dirname, "src"), exclude: /node_modules/, test: /\.tsx?$/, loader: "ts-loader" }
        ],

        preLoaders: [
            // All output '.js' files will have any sourcemaps re-processed by 'source-map-loader'.
            { test: /\.js$/, loader: "source-map-loader" }
        ]
    },

};

我试过各种组合:

1) 仅包含正则表达式 /src/ 或直接 path.resolve(__dirname, "src")

2) 仅排除使用正则表达式 /node_modules/ 或使用直接 path.resolve(__dirname, "node_modules")

3) 我试过同时使用 includeexclude.

对于 node_modules 中的文件,我的 ts-loader 总是出错,即:

ERROR in /path/to/prj/node_modules/@types/enzyme/index.d.ts
(337,22): error TS2304: Cannot find name 'ComponentClass'.

不知道该怎么做,据我所知,加载程序配置中的 exclude/include 应该指示 webpack 包含或排除要转换的文件,node_modules 文件怎么总是包括?

编辑 1:以防万一,这是我的 tsconfig.json

{
  "compilerOptions": {
    "outDir": "./public/dist",
    "target": "es5",
    "module": "commonjs",
    "noImplicitAny": false,
    "removeComments": true,
    "sourceMap": true,
    "jsx": "react"
  },
  "include": [
    "./src/**/*.tsx",
    "./src/**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ]
}

我使用的是 TypeScript 2.0.3 并将其更新到 2.2 修复了出现的这个问题。现在 node_modules 被排除在 webpack ts-loader 之外。