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

How to achieve something similar to babel's require.context macro? #322

Closed
vijaybritto opened this issue Aug 10, 2020 · 5 comments
Closed

Comments

@vijaybritto
Copy link

Our codebase uses this babel Macro in multiple places. This breaks when compiled with esbuild . Is there a way to replicate this dynamic loading of modules?
The use cases are usually for dynamic loading multiple icons/images in a folder.

@evanw
Copy link
Owner

evanw commented Aug 10, 2020

This isn't possible with esbuild. If you need to use Babel plugins like this, then you will likely need to use Babel.

The only alternative that works with esbuild at the moment is to explicitly list out all of the imports in code. This could either be done manually by actually writing the imports yourself, or by a script that runs before esbuild and automatically generates a file containing the imports for esbuild to consume.

Babel macros could be potentially integrated into esbuild when the esbuild plugin API is released in the future, but it may negate a lot of the speed benefits of esbuild depending on how the plugin works. The esbuild plugin API is not ready yet but you can follow issue #111 for updates.

@vijaybritto
Copy link
Author

vijaybritto commented Aug 10, 2020 via email

@evanw
Copy link
Owner

evanw commented Nov 11, 2020

Closing. I'm not intending to add this feature to esbuild itself. It should be possible to do something like this yourself with the plugin API if you really need it.

@thomaschaaf
Copy link

Maybe this is interesting for you @vijaybritto: https://github.com/thomaschaaf/esbuild-plugin-import-glob

@vijaybritto
Copy link
Author

@thomaschaaf this is amazing! I wrote a custom plugin for our company codebase which does exactly the same!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants