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

Access asset URL from CSS import #1757

Closed
lxsmnsyc opened this issue Nov 8, 2021 · 6 comments
Closed

Access asset URL from CSS import #1757

lxsmnsyc opened this issue Nov 8, 2021 · 6 comments

Comments

@lxsmnsyc
Copy link

lxsmnsyc commented Nov 8, 2021

Is there any way to do something like this? (at least in plugins?)

import styleURL from './styles.css';

Using "file" loader would definitely work for providing the asset URL however it skips the bundling process and thus causes a lot of fallthroughs (e.g. at-rule imports, fonts, images, etc.). I wanted to access the asset URL while bundling the imported CSS file.

@evanw
Copy link
Owner

evanw commented Nov 8, 2021

If you want to bundle the imported CSS file, you can write a plugin that invokes esbuild recursively to bundle the CSS file and then return the bundled CSS from the plugin so it can be imported. Plugin documentation is here: https://esbuild.github.io/plugins/#load-callbacks.

@lxsmnsyc
Copy link
Author

lxsmnsyc commented Nov 8, 2021

Thanks for the response Evan

Would this produce any asset conflicts if one or more build instances share the same output directory?

@vcaballero-salle
Copy link

@lxsmnsyc I don't know if this will answer your question. With esbuild I am bundling from multiple CSS root files (files with css imports) and the output directory is the same for all of them, I just need to change the output file name.

@lxsmnsyc
Copy link
Author

lxsmnsyc commented Nov 9, 2021

@vcaballero-salle Thanks for the response. However, it doesn't fit what I currently need right now and is counter-intuitive on how I load the CSS. The reason I wanted to acquire the URL is so that I know which CSS file to load for at that part of code.

@evanw
Copy link
Owner

evanw commented Nov 9, 2021

Would this produce any asset conflicts if one or more build instances share the same output directory?

You can use write: false to have esbuild bundle into memory without writing the result to the file system, which is useful for using esbuild to bundle from within a plugin. See
https://esbuild.github.io/api/#write.

@lxsmnsyc
Copy link
Author

lxsmnsyc commented Nov 9, 2021

Neat! I didn't know that! Thank you

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