ts-loader icon indicating copy to clipboard operation
ts-loader copied to clipboard

Project References won't work if extends config file

Open t83714 opened this issue 5 years ago • 18 comments
trafficstars

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

t83714 avatar Nov 28 '19 04:11 t83714

@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 avatar Dec 03 '19 20:12 jleider

@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.

t83714 avatar Dec 03 '19 22:12 t83714

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

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

FrozenKiwi avatar Jan 01 '20 14:01 FrozenKiwi

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.

stale[bot] avatar Mar 01 '20 15:03 stale[bot]

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

stale[bot] avatar Mar 08 '20 15:03 stale[bot]

@t83714 can you please re-open this issue?

FlorianWendelborn avatar Mar 09 '20 10:03 FlorianWendelborn

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.

stale[bot] avatar May 09 '20 01:05 stale[bot]

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

stale[bot] avatar May 16 '20 09:05 stale[bot]

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

mh-alahdadian avatar May 31 '20 07:05 mh-alahdadian

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

johnnyreilly avatar May 31 '20 07:05 johnnyreilly

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.

stale[bot] avatar Aug 01 '20 09:08 stale[bot]

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

stale[bot] avatar Aug 08 '20 10:08 stale[bot]

Please reopen !

elsasslegend avatar Oct 09 '20 15:10 elsasslegend

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.

stale[bot] avatar Dec 13 '20 01:12 stale[bot]

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?

lukescott avatar Dec 16 '20 08:12 lukescott

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.

stale[bot] avatar Apr 17 '22 07:04 stale[bot]

@stale don’t.

FlorianWendelborn avatar Apr 17 '22 14:04 FlorianWendelborn