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

Expose the tsconfigRaw option in the build API #783

Closed
iamakulov opened this issue Feb 10, 2021 · 3 comments
Closed

Expose the tsconfigRaw option in the build API #783

iamakulov opened this issue Feb 10, 2021 · 3 comments

Comments

@iamakulov
Copy link

Hey,

Would it be possible to expose the tsconfigRaw option (from the transform API) in the build API as well?

Rationale: We’re using tsconfig.json to provide aliases and overrides for import paths. Eg, one of tsconfig.jsons that ESBuild uses looks like this:

{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "paths": {
            // Mocks
            "linaria": ["../esbuild/mocks/linaria/index.ts"],
            // Externals
            "react": ["../esbuild/externals/react/index.js"],
            "react-dom": ["../esbuild/externals/react-dom/index.js"],
            "react-dom/unstable-native-dependencies": [
                "../esbuild/externals/react-dom/unstable-native-dependencies/index.js"
            ],
            "react-dom/server": ["../esbuild/externals/react-dom/server/index.js"],
            // Aliases
            "library/*": ["../../../library/src/*"],
            "test/features": ["../test/features.ts"],
            // (10 more lines)
        }
    }
}

The challenge is that we have multiple tsconfigs with slightly different (but mostly common) paths:

Example
// tsconfig.dev.json includes the `linaria` mock + externals + aliases
{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "paths": {
            // Mocks
            "linaria": ["../esbuild/mocks/linaria/index.ts"],
            // Externals
            "react": ["../esbuild/externals/react/index.js"],
            "react-dom": ["../esbuild/externals/react-dom/index.js"],
            "react-dom/unstable-native-dependencies": [
                "../esbuild/externals/react-dom/unstable-native-dependencies/index.js"
            ],
            "react-dom/server": ["../esbuild/externals/react-dom/server/index.js"],
            // Aliases
            "library/*": ["../../../library/src/*"],
            "test/features": ["../test/features.ts"],
            // (10 more lines)
        }
    }
}

// tsconfig.prod.json includes externals + aliases
{
    "extends": "../tsconfig.json",
    "compilerOptions": {
        "paths": {
            // Externals
            "react": ["../esbuild/externals/react/index.js"],
            "react-dom": ["../esbuild/externals/react-dom/index.js"],
            "react-dom/unstable-native-dependencies": [
                "../esbuild/externals/react-dom/unstable-native-dependencies/index.js"
            ],
            "react-dom/server": ["../esbuild/externals/react-dom/server/index.js"],
            // Aliases
            "library/*": ["../../../library/src/*"],
            "test/features": ["../test/features.ts"],
            // (10 more lines)
        }
    }
}

// The parent ../tsconfig.json only includes aliases
{
    "compilerOptions": {
        "paths": {
            // Aliases
            "library/*": ["../../../library/src/*"],
            "test/features": ["../test/features.ts"],
            // (10 more lines)
        }
    }
}

We have to duplicate all these paths across all files (because you can’t extend the compilerOptions.paths field – if doesn’t merge with the parent’s compilerOptions.paths field but overrides it). This is fairly inconvenient.

Having a tsconfigRaw field in the build API would allow us to generate a virtual tsconfig.json every time ESBuild runs – and add paths as needed.

@evanw
Copy link
Owner

evanw commented Feb 11, 2021

Thanks for describing your use case in detail. I understand why you might want a virtual file here. In case it helps, another approach is to write out the generated JSON to an actual file and use the tsconfig setting. I mention this because it doesn't sound like you tried this from your description. Does this work for you?

@iamakulov
Copy link
Author

Yeah, that’d definitely work as well! Writing out temporary files is a bit less convenient than simply passing a string, but if you prefer to not implement tsconfigRaw, the temp file approach would be 100% suitable.

@iamakulov
Copy link
Author

Okay, apologies for spending your attention on this – I figured out how to implement what I wanted through resolve plugins! Closing this.

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