1
votes

I'm using webpack 1.13.3, and in reply to this question I provided my tsconfig.json at bottom.

My loader is always including the node_modules packages no matter if I use include, exclude or combination of both.

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

};

I've tried all sorts of combination:

1) Only include with regex /src/ or with direct path.resolve(__dirname, "src")

2) Only exclude using regex /node_modules/ or with direct path.resolve(__dirname, "node_modules")

3) I've tried using both at same time include and exclude.

I'm always getting error from my ts-loader for files inside node_modules, i.e:

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

Not sure what to do, from what I can understand the exclude/include in loader config is suppose to indicate to webpack to include or exclude file to be transformed, how can node_modules files are always included?

EDIT 1: In case it matter, this is my 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
To avoid the error you can try: noParse section (webpack.github.io/docs/configuration.html#module-noparse). But the config seems ok. - Hosar
Hosar the error I'm showing is one of like 200 errors, I tried the noParse: ]/node_modules/] and still have those errors - Dominic St-Pierre
did you tried those options on tsconfig: "skipLibCheck": true and "skipDefaultLibCheck": true? - Mouneer

1 Answers

0
votes

I was using TypeScript 2.0.3 and updating it to 2.2 fixed this issue it appears. Now the node_modules is excluded from webpack ts-loader.