egoist / tsup

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

Plugins support? #733

Open melMass opened 2 years ago

melMass commented 2 years ago

Hi,

I'm using tsup for a library and I really like the simplicity. I want to use an esbuild plugin I wrote to append ".js" or "index.js" on required imports. To be sure I tested it in plain esbuild and it works well, but not in tsup for some reason.

The setup method is properly ran, but not the onLoad callback (its never called).

Is this a know issue? If not how would you debug it ?

Thanks

Upvote & Fund

Fund with Polar

melMass commented 2 years ago

Here is a simplified repro (removed some test case) (there is scroll):

const suffix_re = /^(export|import)((\s|\w|\*|{|,)*(})?.*)(from)(.*)("|')(\.\/(\w|\s|\/)*((?!\.js).)*)("|')(;)?/gm

const suffixLoad = async (
    {
        path,
        // namespace,
        // suffix,
        // pluginData
    }: OnLoadArgs
): Promise<OnLoadResult> => {

    const contents = await readFile(path, 'utf-8');
    const new_val = contents.replaceAll(suffix_re, "$1$2$3$5$6$7$8.js$10$11$12")
    return {
        pluginName: "suffix-js",
        loader: "ts",
        contents: new_val
    }
}

export const suffixJS: Plugin = {
    name: "suffix-js",
    setup(build) {
        build.onLoad({ filter: /ts$/ }, suffixLoad)
    }
}
melMass commented 2 years ago

Seems like if tsup was updating typescript to 4.7+ this workaround would not be needed anymore (not really related to the issue but still relevant I guess)

songkeys commented 1 year ago

Did you manage to get this issue solved? The ts in this repo is 5+ now but the issue still exists for me. the onLoad function in esbuild plugin never called.

edit: also related #888

edit: I found the issue. In my case it was the esbuild-sass-plugin having conflicts with the built-in postcss plugin. You can turn the built-in one off to solve.