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

Import all files in folder #722

Closed
thomaschaaf opened this issue Jan 28, 2021 · 5 comments
Closed

Import all files in folder #722

thomaschaaf opened this issue Jan 28, 2021 · 5 comments

Comments

@thomaschaaf
Copy link

I have a folder containing database migrations. I would like to include all files in a directory so that I can pass them to the database migration executor. As I understand something like require.context from webpack is not supported or desired in esbuild. What would your suggestion be on how I could import all files in a folder?

@evanw
Copy link
Owner

evanw commented Jan 28, 2021

I would make a plugin that uses fs.readdir to read all of the files in the directory and then generates a virtual module with an import statement for each one. You don't need a plugin for this (you could just have a script generate that module before the build starts) but your workflow may be cleaner with a plugin.

@evanw
Copy link
Owner

evanw commented Feb 3, 2021

Closing this because I believe the above solution is sufficient.

@evanw evanw closed this as completed Feb 3, 2021
@thomaschaaf
Copy link
Author

@evanw I am currently trying to write a drop-in replacement for require.context from webpack. The problem is that I need to get the parameters passed in to the function e.g. require.context('./migrations'). For this I think I need the AST. I am currently thinking of using ts-morph for this (after checking if require.context is in the file at all).

With the onResolve Plugin hook I could create something like https://www.npmjs.com/package/import-glob fairly easily (like the wasm plugin example).

What do you think is the better option? I would think the drop-in would benefit more but getting the AST from ts-morph seems like overkill.

@evanw
Copy link
Owner

evanw commented Mar 12, 2021

I think the import-glob approach would be the best for now. It already works with the existing plugin API and it seems pretty ergonomic to me. It's also likely to be maximally efficient vs. AST manipulation plugins.

Doing ts-morph if require.context is in the file could be fine if there are only a few files containing require.context, especially if compatibility with existing code without modifying the code is critical. In that case it sounds reasonable to do something like this.

Long term I could imagine allowing require.context() to be implemented via plugins along with #700.

@thomaschaaf
Copy link
Author

thomaschaaf commented Mar 13, 2021

I ended up writing this plugin: https://npmjs.com/package/esbuild-plugin-import-glob

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

2 participants