Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
s1owjke opened this issue Mar 6, 2023 · 4 comments
Labels

Comments

@s1owjke
Copy link

s1owjke commented Mar 6, 2023

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
Copy link
Owner

evanw commented Mar 6, 2023

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

@s1owjke
Copy link
Author

s1owjke commented Mar 6, 2023

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
Copy link
Owner

evanw commented Mar 7, 2023

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
Copy link
Author

s1owjke commented Mar 7, 2023

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.

@evanw evanw changed the title Configure default loader for files Configure default loader for files + browser:false should not run onLoad plugins Mar 24, 2023
@evanw evanw added the plugins label Apr 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants