evanw / esbuild

An extremely fast bundler for the web
https://esbuild.github.io/
MIT License
37.94k stars 1.13k forks source link

Configure default loader for files + browser:false should not run onLoad plugins #2970

Open s1owjke opened 1 year ago

s1owjke commented 1 year ago

For example I'm building a frontend application. I want to load all assets with file loader.

At this moment I need to explicitly specify a loader for all used extensions, to do this I need to additionally traverse all files. As we can't use regular expressions for extensions, it would be nice to have something like this.

import esbuild from 'esbuild';

await esbuild.build({
  entryPoints: ['./src/index.tsx'],
  outdir: OUT_DIR,
  bundle: true,
  loader: {
    '.*': 'file',
  },
});

Here is an example of a webpack configuration from react-scripts package.

evanw commented 1 year ago

You can completely customize loading behavior with plugins: https://esbuild.github.io/plugins/#on-load

s1owjke commented 1 year ago

If path was resolved using one of the resolveExtensions (for example ./util.inspect resolved as /node_modules/object-inspect/util.inspect.js), then path received in OnLoadArgs will not include extension.

So, I need to resolve it again in the onLoad callback, because there is no other way to do that, right?

evanw commented 1 year ago

Ah that's no good. Looks like this is happening because that package contains this in its package.json file:

  "browser": {
    "./util.inspect.js": false
  },

Running onLoad without the extension is not correct. I could change this to run onLoad with the extension, but arguably esbuild just shouldn't even call onLoad in that case because the package is using browser to request that the file be treated as an empty file.

s1owjke commented 1 year ago

It's not good and also not fast at all to guess extension again in the callback.

According to the initial specification in this case we shouldn’t load ./util.inspect.js at all, an approach to add extension in this specific case would be a workaround.