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

[Bug / Question] Paths seem to not be correctly preserved with plugins and dynamic imports #906

Closed
gkjohnson opened this issue Mar 1, 2021 · 2 comments

Comments

@gkjohnson
Copy link

Hello! I noticed that an incorrect output hierarchy is produced when a plugin processes an import that's been included from a dynamic import. I came across this while trying to write a loader for WebWorkers until something like #312 is addressed.

I've attached a zip with a simple repro case where a root file src/index.js dynamically imports src/nested/dynamicImport.js which in turn imports src/nested/dependency.js. The build is written to the "dist" folder and I've written a plugin to process dependency.js and modify the contents. When the plugin is enabled src/nested/dependency.js is written to dist/dependency.js while dist/nested/dynamicImport.js is still trying to import from ./dependency.js. The files are written out in the correct spot if the plugin is disabled but of course the plugin is not run. Admittedly it's a bit convoluted to explain through text so hopefully the example makes it clear.

Here's the example project zip to reproduce the issue. After running npm install run node ./run-esbuild.js to generate the incorrect "dist" folder output. You can comment out the "plugins" line in "run-esbuild.js" and delete /dist before rerunning to see that the output structure is different.

esbuild-repro.zip

Hopefully my explanation is clear. Let me know if I can explain anything better or if I'm just misunderstanding a feature! And of course thank you for maintaining this project!

@evanw
Copy link
Owner

evanw commented Mar 3, 2021

Import paths are only transformed by esbuild if they are in the file namespace. Otherwise they are preserved verbatim and are not transformed at all. The namespace feature lets you "take control" of path resolution and provide your own behavior for how paths work, so esbuild doesn't make any assumptions about how paths in non-file namespaces behave. For example, your paths might represent various non-file-system formats such as HTTP URLs or even arbitrary data. In your case you should be able to get this to work by just using the file namespace.

@gkjohnson
Copy link
Author

Import paths are only transformed by esbuild if they are in the file namespace

Got it -- thank you. I misunderstood that from the docs but I'll take another look. Either way removing the plugin namespace fixed the issue for me.

Thanks again!

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