webpack loader总是在编译中包含node_modules

时间:2017-02-25 14:12:45

标签: javascript webpack ts-loader

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

我的加载程序总是包含node_modules包,无论我是否使用include,exclude或两者的组合。

webpack config:

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

我总是从我的ts-loader中获取node_modules中的文件错误,即:

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"
  ]
}

1 个答案:

答案 0 :(得分:0)

我使用TypeScript 2.0.3并将其更新为2.2修复了此问题。现在,node_modules被排除在webpack ts-loader之外。