Ts-loader: Project References won't work if extends config file

Created on 28 Nov 2019  路  15Comments  路  Source: TypeStrong/ts-loader

Background

ts-load version: 6.2.1
webpack version: 4.28.3
webpack-cli version: 3.3.10
tyepscript version: 3.7.2

Given the following config files:

  1. tsconfig.json:
{
    "extends": "../tsconfig-global.json",
    "compilerOptions": {
        "outDir": "./dist",
        "strictNullChecks": false,
        "rootDir": "./src",
        "tsBuildInfoFile": "./tsconfig.tsbuildinfo"
    },
    "references": [{ "path": "../typescript-common" }],
    "include": ["./src"]
}
  1. tsconfig-web.json (extends the TS config file above):
{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "lib": [
            "dom",
            "es5",
            "scripthost",
            "es2015.core",
            "es2015.promise",
            "es2017"
        ]
    }
}
  1. Webpack config (use tsconfig-web.json to build TS code):
const path = require("path");
const webpack = require("webpack");
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");

const configFile = path.resolve(__dirname, "../tsconfig-web.json");

module.exports = {
    entry: "./src/createTransformer.ts",
    mode: "production",
    output: {
        filename: "createTransformerForBrowser.js",
        path: path.join(__dirname, "..", "dist"),
        library: "createTransformer"
    },
    devtool: "source-map",
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                exclude: /node_modules/,
                loader: "ts-loader",
                options: {
                    configFile,
                    projectReferences: true
                }
            }
        ]
    },
    resolve: {
        plugins: [
            new TsconfigPathsPlugin({
                configFile
            })
        ],
        extensions: [".tsx", ".ts", ".js"]
    },
    node: {
        fs: "empty"
    }
};

Actual Behaviour

Project reference doesn't work. And you will find the following errors:

ERROR in xx/xx.ts
[tsl] ERROR in /xx/typescript-common/src/xxxxxx.ts(9,30)
      TS6059: File '/xx/typescript-common/src/xx/xx.ts' is not under 'rootDir' '/xx'. 'rootDir' is expected to contain all source files.

Expected Behaviour

Should build without error.

However, if you either:

  • add "references": [{ "path": "../typescript-common" }], to tsconfig-web.json
  • Or modify webpack.config.js to use tsconfig.json to build (instead of tsconfig-web.json)

the build will complete with no error.

Steps to Reproduce the Problem

See: https://github.com/t83714/sample-repo-ts-loader

Location of a Minimal Repository that Demonstrates the Issue.

https://github.com/t83714/sample-repo-ts-loader

All 15 comments

@t83714 I believe this is actually an issue with tsconfigs not ts-loader as I have struggled with this a bunch recently. It would seem that there is no way to make files, includes, references, etc. relative to the current config. They are always relative to the config in which they were originally defined no matter what combination of rootDir or baseUrl compiler settings as they seem to have no effect on anything that is set outside of the compilerOptions object.

@jleider For my sample repo, once you change "../tsconfig-web.json" in the webpack config to "../tsconfig.json", everything will just work fine.

And tsconfig.json is located in the same directory as tsconfig-web.json --- the only difference is tsconfig.json comes with references property (rather than set via extends).

From this, we probably can say ts-loader has NO relative path resolving issues.
It just didn't process extends very well.

https://github.com/TypeStrong/ts-loader/issues/911

Dup of this (closed, but not resolved) issue. Linking so we have more reference info.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Closing as stale. Please reopen if you'd like to work on this further.

@t83714 can you please re-open this issue?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Closing as stale. Please reopen if you'd like to work on this further.

any update on this?
what's the badpractice to fix this problem? I need to use project reference

No update - if someone would like to work on this that would be amazing 馃グ

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Closing as stale. Please reopen if you'd like to work on this further.

Please reopen !

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

I'm experiencing an issue where it seems that configFile is not working at all. Related?

Also I have seen a lot of stale bot lately. Given the nature of the pandemic and the lack of resources, can it be switched off?

Was this page helpful?
0 / 5 - 0 ratings