lucacasonato / esbuild_deno_loader

Deno module resolution for `esbuild`
https://jsr.io/@luca/esbuild-deno-loader
MIT License
176 stars 46 forks source link

Conflicts with other loaders #77

Open mayerraphael opened 1 year ago

mayerraphael commented 1 year ago

Because of the automatically registered "file" specifier, this conflicts with any defined loaders that also use file.

For example following esbuild options:

{
    plugins: [...denoPlugins()],
    entryPoints: entryPoints,
    ...
    loader: {
      ".graphql": "file",
    },
  }

If i now import a graphql file like this:

import query from "./query.graphql";

The deno-loader plugin throws the following error:

 The module's source code could not be parsed: Expected ',', got ':'
 ~ [plugin deno-loader]

The plugin should not handle all file namespaces as TypeScript/JavaScript files.

niedzielski commented 11 months ago

I agree with your assessment. I was able to wrap the default loaders like text and dataurl to lock files to a new namespace and ordering the plugin before esbuild_deno_loader's plugins like:

const dataURILoader: esbuild.Plugin = {
  name: 'DataURILoader',
  setup(build) {
    build.onResolve({ filter: /\.svg$/ }, (args) => ({
      path: path.join(args.resolveDir, args.path),
      namespace: textLoader.name,
    }))

    build.onLoad(
      { filter: /\.svg$/, namespace: textLoader.name },
      async (args) => ({
        contents: await Deno.readTextFile(args.path),
        loader: 'dataurl',
      }),
    )
  },
}
const opts: esbuild.BuildOptions = {
  ...
  plugins: [
    dataURILoader,
    ...denoPlugins() as unknown as esbuild.Plugin[],
  ],
  ...
}

It's very clumsy but fortunately isolated to my build script.

The bigger issue to me is https://github.com/denoland/deno/issues/15132 which has nothing to do with esbuild.