-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 use esbuild with css import assertions/attributes #3384
Comments
The The |
In the browser (chrome) it had an effect. A import with assert type css, always returned a css stylesheet object. And if you served a wrong mime type to this the import failed. And can I return a script in the onLoad callback wich creates a CSS Stylesheet Object like I tried here: #1871 (comment) |
I also tried to ignore all css imports (external: ['*.css']), but this will not work, now all my relative import paths are wrong (where I use the asserts), and monaco editor, wich uses import statements without assert, has also wrong paht, but will also not work in browser if not transpiled. So atm. I've no Idee if or how I could use esbuild too bundle my app. |
There is already special code for "assert" with type "json" : esbuild/internal/js_parser/js_parser.go Line 6336 in a111cc4
|
Yes, there is. But the only thing that an import assertion does is cause a build error if the assertion fails. For example, you will get a build error if you use
The
I realize that you wish import assertions worked that way, but they don't. Doing that would be going against the specification. Import attributes do work that way but they are a separate feature (and one that hasn't been released yet). |
I do understand, but a browser could load 'css' with assert 'css' only if it was a css file, and then it would be a CSSStyleSheet object. If the file was no CSS, the browser would also fail. So I don't get why this could not be done by the bundler. If we have a file with".css" extension and with 'css' assertion, the bundler could treat it as CSSStyleSheet? What should break if this would be done? But if this is not done, there are many libs wich could not be bundeled by esbuild. Also imports without assertions or attributes are no feature of the ecosystem, but are realized by esbuild. I'm trying at the moment for myself to update esbuild so the import assertions or attributes are hand over to the onResolve call, so I can implement the correct handling myself. Is ther ean easy way how I can debug esbuild when run from inside javascript? |
Why do you need to see if the |
Because for example I also use monaco editor package, wich uses import of css files without assertions, and these need to return the text. |
Many librarys wich are only usable via bundlers import css (or other types), but without any assertion. So to distinguish between them and a correct import I need to know |
The latest release of esbuild now supports bundling with import assertions: https://github.com/evanw/esbuild/releases/tag/v0.19.7. This should let you write an esbuild plugin that implements |
i'll try |
@evanw that's awesome news! I'm not familiar with esbuild plugin authoring, but one question I have from the cheese example: const cheesePlugin = {
name: 'cheese',
setup(build) {
build.onLoad({ filter: /.*/ }, args => {
if (args.with.type === 'cheese') return {
contents: `export default "🧀"`,
}
})
}
} is whether the plugin can filter on import attributes? I figure, like the cheese example, a lot of plugins might only care about attributes and so have a filter of |
Do I only get the "with" attribute or also the old "assert" ? |
Yes, I think this would be a good idea. I also need to expose this to on-resolve plugins, and maybe make some other changes as well. Part of the problem is that I don't use these features myself so I'm not familiar with the use cases. I wanted to get something basic out there quickly so people could experiment with it and then have that inform the design. For example, I don't think I'm also still learning about these features, which is why I haven't added support for them to esbuild yet. I've read that CSS module scripts forbid using
Yes, this only works with import attributes (the
|
I now created a small sample repo wich includes css with import attributes into build: https://github.com/jogibear9988/esbuild-test |
Is there a way to run the esbuild css minifcation in my plugin on the css code? |
Found it...
|
You can use esbuild to minify CSS using esbuild the API: https://esbuild.github.io/api/#minify. It’s safe to call esbuild’s API from within a plugin. You can also access the esbuild API directly from within a plugin using the |
Yes, for now, until the semantics are agreed upon. (I prefer that
Yes, but I don't know that this needs to be bundled. I suppose if the URL is relative, either the user will be responsible for ensuring that it's valid in the build output, or esbuild could transform the URL similar to how some bundlers handle
I don't think this is true, because the pre-attribute cache key was already |
I seem to be hitting this issue with my plugin. The CSS I'm loading has a relative URL to a font in a Is there a way I can call |
do you mean smth like this?
|
also added this as a sample here: https://github.com/jogibear9988/esbuild-test |
Might be a dumb question, but is it feasible to get "args.with" property in onResolve() callback? However, with current implementation it is not possible to check for .with property, thus this cumbersome workaround is used which entails importing "css" files with some "resource query" identifier (stolen from angular/webpack) in order to discern those from other imports. Regards |
Also I've encountered an issue when "with" property is not set for dynamic import globs with import attributes, e.g. |
@magoniac I don't think the glob has anything to do with it. I have a dynamic import that uses a static string and the import assertion is removed by esbuild 🙁 |
Thanks that is great. I used to use |
I've code where some librarys import css without a assertion or a with attribute:
this works, but if I have this code:
or this:
then the type of bbSheet hast to be a CSSStyleSheet, but esbuild does not create one.
And I don't know in the onResolveCallback if the "assert" or "with" attribute is present (the assert keyword was changed to with, but it should be nearly the same), so I don't know how I could create a plugin
The text was updated successfully, but these errors were encountered: