-
-
Notifications
You must be signed in to change notification settings - Fork 388
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
Support for Web Components, Shadow DOM, adoptedStyleSheets #1092
Comments
Currently possible only with css-loader https://github.com/webpack-contrib/css-loader?tab=readme-ov-file#exporttype |
The TL;DR of this issue is: can I specify anything other than This is possible via |
Indeed, the main challenge is that all the existing solutions seem to be around the way the CSS file is imported, but I need to act on the generated CSS chunk instead, as a whole, not on a specific real CSS file. |
How do you imagine this if you have about 100 components? |
It's not an issue with the number of components but rather 100 imports of one component, because each import would need to disable/alter the In my project there would be a single I'm not super convinced about the solution suggested above, I think that from webpack's viewpoint it's difficult to know in what context the code is being imported. As a better solution, I think I'd have to move The easiest solution is to wrap the component in an iframe so that |
@fregante Can you provide a small example how do you see it (using github repo), maybe I can provide solution right now, because specification clearly says what you should write https://developer.mozilla.org/en-US/docs/Web/API/Document/adoptedStyleSheets#examples, What can I do here? Just allow to this plugin autogenerate:
But you don't need this plugin, because css-loader already can do it, extra async import will decrease your perf... |
Another solution - you can do it on own side using |
The only way to do it in this scenario would be for // component.js
import './some-style.css'
import './some-other-style.css' const {__stylesheet} = await import('./component.js')
const node = document.createElement("div");
const shadow = node.attachShadow({ mode: "open" });
shadow.adoptedStyleSheets = [__stylesheet]; |
That's the issue, I don't have a file.css, the file to load is what Best I can do is use |
You can disable runtime logic here using https://github.com/webpack-contrib/mini-css-extract-plugin?tab=readme-ov-file#runtime |
Thank you, finally got around checking that out. Unfortunately that disables the injection for all generated bundles.
I don’t think there's really a comprehensive solution here that works with nested For truly isolated components, webpack needs to export a distinct configuration per component. This way I could set In this scenario, I could also ask |
It turns out, my first feature request was still the best way to achieve this:
In the PR linked above you can see I'm manually detecting and disabling the injected stylesheet. It still won't supported nested In my case I want to preserve the behavior so that a missed stylesheet can be detected and reported as an error, but if it might be useful to others. |
Why do not use |
What's "./file.css"? Where should I use that URL? How would it fix/avoid the issue? |
As I understand it, you want to load CSS chunk and get // Pseudocode and yes, it is not statistical analizable
async function loadCssChunk(filename) {
const url = new URL(filename, import.neta.url);
const response = await fetch(url.toString());
const css = await response.text();
const sheet = new CSSStyleSheet();
sheet.replaceSync(css);
return sheet;
} Shortly - You don't want to use "css-loader" because it bloats chunks due to storing the CSS inside the JS file, right? |
What CSS chunk? If you're talking about the chunks generated by The problems here where a combination of:
|
It turns out, this is still an issue. There are situations where the CSS bundle name is not what's suggested in Likely repro: import "./file.css";
import(/* webpackChunkName: FOO */ "./file.js") // file.js
import "./file.css";
import "./other.css";
console.log('file'); It will likely create:
The long filename doesn't match the example but it's a real filename I'm seeing. So I'm back to having to specify The problem now is removing Reopening for this request specifically: import(/* webpackCss: no-inject */ "./file.js"); |
Alternative solutions:
It feels like either one of these is already possible via some webpack config. In my PR above I'm detecting and disabling any local stylesheets added while |
import(/* webpackCss: no-inject */ "./file.js"); I don't feel it is a right idea, what this import should return? |
The Depending on the specific verb chosen:
For me, the first choice is enough, while the last would be great |
I don't really like this approach, since it will contradict the specification Now we suppirt three things:
These three things should solve any things. Also we can setup the plugin only for CSS generation without runtime using For more flexibility we can implement |
Where did I say to change that behavior? What I'm suggesting with Perhaps it needs to be |
Got it, I will try to find a time on it, but I don't know when, anyway if you want to send a PR, feel free to do it |
Context
I want to generate a standalone component to be loaded in a shadow DOM.
It seems that this gets me most of what I need:
At this point, a
Component.css
file is being generated and appended todocument.head
.**The problem is that this stylesheet should be inside
shadow.div
Feature Proposal
I'm not sure what would be the best way to achieve this, but perhaps:
import()
to disable the automatic injection of the stylesheete.g.
import(/* webpackCss: no-inject */ './Component');
e.g.
const {Component, __miniCssUrl} = await import('./Component');
Related links
The text was updated successfully, but these errors were encountered: