rollup-plugin-esbuild icon indicating copy to clipboard operation
rollup-plugin-esbuild copied to clipboard

tsconfig.json support?

Open plashenkov opened this issue 4 years ago • 6 comments

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?

plashenkov avatar Aug 10 '20 19:08 plashenkov

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({
      /**/
    })
  ]
};

ycnmhd avatar Dec 10 '20 17:12 ycnmhd

The solution is to use @rollup/plugin-alias as @ycnmhd said, there's also alias-hq (by @davestewart) to reduce boilerplate code.

egoist avatar Dec 11 '20 13:12 egoist

Thanks for the mention @egoist

davestewart avatar Dec 11 '20 15:12 davestewart

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 avatar Jun 25 '21 15:06 peats-bond

@peats-bond path alias is not part of esbuild's transform api

egoist avatar Jun 25 '21 17:06 egoist

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: [/* ... */]
})

JensDll avatar Sep 06 '22 11:09 JensDll

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.

sxzz avatar Sep 20 '23 14:09 sxzz