rollup-plugin-esbuild
rollup-plugin-esbuild copied to clipboard
tsconfig.json support?
Hi there!
Thanks for the helpful plugin!
I'm trying to integrate esbuild into rollup using your plugin and it seems that it doesn't support tsconfig.json completely?
At least it doesn't respect compilerOptions.baseUrl
and compilerOptions.paths
.
These are path aliases which allow to write
import '@alias/module'
instead of
import `../../../../../../../../some-folder/module`
(well, I think you know, but just for clarity :) ) And as far as I know esbuild itself supports these options (https://github.com/evanw/esbuild/issues/60 and https://github.com/evanw/esbuild/issues/38#issuecomment-639784830).
Or maybe I missed something?
found a workaround using @rollup/plugin-alias
import path from "path";
import resolve from "rollup-plugin-node-resolve";
import esbuild from "rollup-plugin-esbuild";
import alias from "@rollup/plugin-alias";
const projectRootDir = path.resolve(__dirname);
export default {
/**/
plugins: [
alias({
customResolver: resolve({ extensions: [".tsx", ".ts"] }),
entries: Object.entries({
"::helpers/*": ["./src/helpers/*"],
"::hooks/*": ["./src/hooks/*"]
/**/
}).map(([alias, value]) => ({
find: new RegExp(`${alias.replace("/*", "")}`),
replacement: path.resolve(
projectRootDir,
`${value[0].replace("/*", "")}`
)
}))
}),
esbuild({
/**/
})
]
};
The solution is to use @rollup/plugin-alias
as @ycnmhd said, there's also alias-hq (by @davestewart) to reduce boilerplate code.
Thanks for the mention @egoist
The solution is to use
@rollup/plugin-alias
as @ycnmhd said
The workaround using @rollup/plugin-alias
adds ~5 seconds of build time to my project. @egoist do you have plans to better support tsconfig.json
? It looks like the esbuild library supports more fields than you support right now:
- https://esbuild.github.io/content-types/#tsconfig-json
- https://github.com/evanw/esbuild/blob/36073fc86ea3acf77764fa8fb6472e02243305ee/lib/shared/types.ts#L161
- https://github.com/evanw/esbuild/blob/36073fc86ea3acf77764fa8fb6472e02243305ee/lib/shared/types.ts#L54
@peats-bond path alias is not part of esbuild's transform
api
I couldn't get the newer @rollup/plugin-node-resolve to work as the customResolver
argument. However, in my trivial case of wanting to bundle TypeScript files, a simple resolveId
function that looks through a list of extensions was enough to resolve and bundle the aliased modules:
import alias, { type ResolverFunction } from '@rollup/plugin-alias'
function resolveExtensions(extensions: string[]): ResolverFunction {
return async function (source) {
for (const extension of extensions) {
try {
const moduleInfo = await this.load({ id: source + extension })
return moduleInfo.id
} catch {}
}
return null
}
}
alias({
customResolver: resolveExtensions(['.ts']),
entries: [/* ... */]
})
See https://github.com/egoist/rollup-plugin-esbuild/issues/70#issuecomment-743196686, and let's track https://github.com/evanw/esbuild/issues/394. As this project is a wrapper of esbuild.