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

Allow ts-loader to be used more than once #909

Closed
nicojs opened this issue Feb 28, 2019 · 2 comments
Closed

Allow ts-loader to be used more than once #909

nicojs opened this issue Feb 28, 2019 · 2 comments

Comments

@nicojs
Copy link

nicojs commented Feb 28, 2019

Expected Behaviour

Given this webpack config:

const path = require('path');
module.exports = {
    mode: 'development',
    entry: [
        './src/index.ts',
        './test/index.spec.ts'
    ],
    module: {
        rules: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                options: { configFile: 'src/tsconfig.json' },
                include: path.resolve(__dirname, 'src')
            },
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                options: { configFile: 'test/tsconfig.json' },
                include: path.resolve(__dirname, 'test')
            }
        ]
    }
}

I would expect files in the test directory to be transpiled with test/tsconfig.json, while files in the src directory are transpiled using src/tsconfig.json

Actual Behaviour

Both test and src files seems to be transpiled using the src/tsconfig.json file, which results in errors. In my demo application:

ERROR in C:\z\github\nicojs\ts-loader-issue\test\index.spec.ts
./test/index.spec.ts
[tsl] ERROR in C:\z\github\nicojs\ts-loader-issue\test\index.spec.ts(1,1)
      TS2582: Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i @types/jest` or `npm i @types/mocha`  and then add `jest` or `mocha` to the types field in your tsconfig.

Steps to Reproduce the Problem

  1. Create a src/tsconfig.json file and a test/tsconfig.json file.
  2. Add the config above in your webpack.conf.js file
  3. Install @types/mocha (for example)
  4. Add types: [] in src/tsconfig.json, types: ['mocha'] in test/tsconfig.json.
  5. Create a file: test/index.spec.ts. Add a call to describe: describe('foo');
  6. Run webpack

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/nicojs/ts-loader-issue

Note: Tried to do this as a workaround for #851

@johnnyreilly
Copy link
Member

Does the instance option help at all? https://github.com/TypeStrong/ts-loader/blob/master/README.md#instance-string

@nicojs
Copy link
Author

nicojs commented Mar 15, 2019

@johnnyreilly thanks for the workaround. It works like a charm:

const path = require('path');
module.exports = {
    mode: 'development',
    entry: [
        './src/index.ts',
        './test/index.spec.ts'
    ],
    module: {
        rules: [
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                options: { instance: 'src', configFile: 'src/tsconfig.json' },
                include: path.resolve(__dirname, 'src')
            },
            {
                test: /\.ts$/,
                loader: 'ts-loader',
                options: { instance: 'test', configFile: 'test/tsconfig.json' },
                include: path.resolve(__dirname, 'test')
            }
        ]
    }
}

Could have been better documented though. I'm also surprised that it had to be explicitly enabled.

@nicojs nicojs closed this as completed Mar 15, 2019
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

2 participants