Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ts-loader ignores exclude in webpack loader config #544

Closed
quantuminformation opened this issue May 18, 2017 · 14 comments
Closed

ts-loader ignores exclude in webpack loader config #544

quantuminformation opened this issue May 18, 2017 · 14 comments

Comments

@quantuminformation
Copy link

quantuminformation commented May 18, 2017

Webpack is using this, which should explicitly ignore the test folder.

      {
        test: /\.ts$/,
        use: [{
          loader: 'ts-loader'
          //loader: 'awesome-typescript-loader'

        }]
        ,
        exclude: '/**/*.test.ts'
      }

but it tried to compile my tests (which results in a failing as tests run in node and the webpack build doesn't expect assert to be available in a browser build )

ERROR in /Users/nikos/WebstormProjects/solar-popup/src/tests/SolarPopup.test.ts
(2,20): error TS2307: Cannot find module 'assert'.

The browser webpack build targets es2015:

{
  "compilerOptions": {
    "target": "es2015",
    "declaration": true
  },
  "exclude": [
    "node_modules",
    "demo"
  ]
}

@quantuminformation quantuminformation changed the title ts-loader ignores exclude in loader config ts-loader ignores exclude in webpack loader config May 19, 2017
@quantuminformation
Copy link
Author

I also tried:
exclude: glob.sync('src/**/*.test.ts')
and
exclude:new RegExp('.test.ts')
but it still tried to compile the test file

@quantuminformation
Copy link
Author

Put the excludes in the ts config instead.

@lukescott
Copy link

@quantuminformation if you're using a test runner, how do you run tests if the test files are in tsconfig.json exclude? I think in most cases you'll want your .test.ts files to run with a test runner, but not be bundled with webpack.

@darkguy2008
Copy link

Wow, I also have the same issue, I need to exclude a /scaffold/ folder and it's not behaving correctly. Is this about to be fixed or is there an alternative to ts-loader that supports excluding folders? It doesn't work either in webpack-config.js or tsconfig.json :(

@lukescott
Copy link

This is what I ended up doing:

tsconfig.webpack.json

{
	"extends": "./tsconfig",
	"compilerOptions": {
		"sourceMap": true
	},
	"exclude": [
		"**/*.test.ts",
		"node_modules"
	]
}

Then for my loader I do "ts-loader?configFileName=tsconfig.webpack.json"

@darkguy2008
Copy link

darkguy2008 commented Jul 20, 2017

Thanks @lukescott , I actually managed to solve it by placing the relative folder path (./app/scaffold) instead of just a folder name in tsconfig.json, but I like your solution as well, I'll try it later. Thanks! :)

@quantuminformation
Copy link
Author

I'm using awesome typescript loader now, which seems to do all I need.

@odoe
Copy link

odoe commented Aug 22, 2017

Hi there, I'm running into this same issue. One of the npm libs I use has the ts files alongside the js files.

I can compile my TS normally with tsc and no issue.

// tsconfig.json
{
  "compilerOptions": {
    "plugins": [
      {
        "name": "tslint-language-service"
      }
    ],
    "module": "amd",
    "noImplicitAny": true,
    "outDir": "./src",
    "removeComments": false,
    "sourceMap": true,
    "jsx": "react",
    "jsxFactory": "tsx",
    "lib": ["dom", "es2015.promise", "es5", "es6"],
    "target": "es5",
    "moduleResolution": "node",
    "isolatedModules": false,
    "experimentalDecorators": true,
    "noEmitHelpers": false,
    "declaration": false,
    "noLib": false,
    "preserveConstEnums": true,
    "suppressImplicitAnyIndexErrors": true,
    "allowJs": true
  },
  "types": [
    "arcgis-js-api",
    "chai",
    "intern"
  ],
  "include": [
    "./src/**/*"
  ],
  "exclude": [
    "node_modules"
  ]
}

But with Webpack 3.5.5 and ts-loader 2.3.3 it keeps trying to compile the ts/tsx files in the node_modules folder.

I tried using the suggestion above to extend my tsconfig and loading it via ts-loader?configFileName=tsconfig.webpack.json

// tsconfig.webpack.tsconfig.json
{
  "extends": "./tsconfig",
  "exclude": ["./node_modules/**/*.tsx", "node_modules/**/*.ts", "node_modules"]
}

But same issue.

My folder structure is pretty basic.

node_modules/
src/
     main.ts
tsconfig.json
tsconfig.webpack.json
webpack.config.js

Any clues what the issue could be? I'm at a bit of a loss on this one.

Thanks!

@johnnyreilly
Copy link
Member

Thanks for the details - that's really helpful. I'm afraid I don't have any spare time to look at this at present. If you (or anyone else) does I'd be happy to assist. Help is always appreciated!

@odoe
Copy link

odoe commented Aug 22, 2017

Sorry, I feel dumb. Another plugin I was using had me add context: __dirname to the webpack config and that is what break the exclude in the loader. Once I removed that context, it works fine. I have a workaround for the other plugin.

Sorry to bother you! Thanks for the awesome work!

@johnnyreilly
Copy link
Member

Great - thanks for sharing the resolution. This will help others!

@jorroll
Copy link

jorroll commented May 22, 2018

I was running into this problem and in my case the issue was my workspace is a monorepo and the project experiencing problems had it's tsconfig named tsconfig.app.json and it was located in a subfolder. I had neglected to add the configFile option inside the project's webpack config file and so the tsconfig file webpack was loading was the base tsconfig.json file located in the workspace root (rather than the tsconfig.app.json file I wanted it to load).

@omakoleg
Copy link

The workaround with separate tsconfig.webpack.json works for me. ts-loader has different name already: 'ts-loader?configFile=tsconfig.webpack.json'

tomazy pushed a commit to tomazy/trivia-game that referenced this issue Aug 27, 2018
@keyiis
Copy link

keyiis commented Feb 25, 2022

same issues.I need to set exclude dynamically, so we can't use multiple tsconfig.json.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants