egoist / tsup

The simplest and fastest way to bundle your TypeScript libraries.
https://tsup.egoist.dev
MIT License
8.89k stars 213 forks source link

Tsup doesn't accurately collect tsconfig.json preferences #524

Closed Lioness100 closed 2 years ago

Lioness100 commented 2 years ago

After some testing, it looks like tsup doesn't resolve tsconfig extensions. For example, if my tsconfig was:

{
    "extends": "../tsconfig.base.json",
    "compilerOptions": {
        "baseUrl": "."
    }
}

And ../tsconfig.base.json was:

{
    "compilerOptions": {
        "emitDecoratorMetadata": true
        ...
    }
}

Tsup wouldn't detect any of the extended options, such as emitDecoratorMetadata. This seems to be because you only read the file through fs.readFileSync and use the results directly. However, I believe you need to use typescript's compiler API to get the full contents. Something like this:

const parsedTsConfig = typescript.parseJsonConfigFileContent(tsconfig, typescript.sys, outDir);

You can find a good example of that here, and I could also probably make PR if needed.

Lioness100 commented 2 years ago

My tsconfig:

{
    "extends": "../tsconfig.base.json",
    "compilerOptions": {
        "outDir": "../dist",
        "rootDir": ".",
        "baseUrl": ".",
        "noEmit": true,
        "paths": {
            "#root/*": ["*"],
            "#lib/*": ["lib/*"],
            "#types/*": ["lib/types/*"],
            "#structures/*": ["lib/structures/*"],
            "#utils/*": ["lib/utils/*"],
            "#database/*": ["lib/database/*"]
        }
    },
    "include": ["."]
}

The extended tsconfig has emitDecoratorMetadata

Logs: image

Lioness100 commented 2 years ago

Furthermore, if there isn't one already, might I suggest an option to manually disable the swc plugin? This would be useful if:

Lioness100 commented 2 years ago

Should I just submit a PR to the package that reads the tsconfig?

egoist commented 2 years ago

I'd like to use a library that doesn't use the typescript compiler to load tsconfig.json, since tsup should work without typescript

Maybe add an option: loadTsconfigWithCompiler: true (or some other name) before this is implemented.

Lioness100 commented 2 years ago

What if, in this library, once and if the raw tsconfig data is collected, the code checks if typescript is installed, uses it to expand the tsconfig if it is, and just keeps the original otherwise?

Edit: loadTsconfigWithCompiler would work fine as well

cenk1cenk2 commented 2 years ago

Maybe this one would fit the bill in the sense of not using Typescript.

https://www.npmjs.com/package/tsconfig-loader

Lioness100 commented 2 years ago

Oooh that looks perfect!

egoist commented 2 years ago

This should be fixed in 5.11.12, please give it a try

btw I made a package for this: https://github.com/egoist/load-tsconfig (even more light-weight than tsconfig-loader)

Lioness100 commented 2 years ago

Great, thank you so much! Can I also make a PR that adds the option to force tsup to use esbuild over swc, even if the emitDecoratorMetadata is enabled somewhere? (Nevermind, I just won't install @swc/core)