question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add support for using lazyStyleTag use() from a JavaScript module (injecting into multiple shadow DOMs)

See original GitHub issue

Feature Proposal

When using lazyStyleTag and use({ target: shadowRoot }) inside a shared JavaScript module, only the first call to use injects a style element. The other shadow elements that call use() have no styles. I’ve duplicated the module and loaded each and the problem does not exist. There may be other options such somehow forcing separate module instances, but I’m not sure.

In index.js, I believe the following condition prevents use() from injecting duplicate styles:

if (!(refs++)) {
    update = API(content, options);
}

https://github.com/webpack-contrib/style-loader/blob/master/src/index.js#L131

One possible solution is to give an id to each injected style element, instead of having a global refs value.

Feature Use Case

loading JS modules injected into shadow elements, so modules can be loaded dynamically.

Please paste the results of npx webpack-cli info here, and mention other relevant information

System: OS: macOS 12.1 CPU: (4) x64 Intel® Core™ i5-8210Y CPU @ 1.60GHz Memory: 122.11 MB / 16.00 GB Binaries: Node: 14.18.1 - /usr/local/bin/node npm: 6.14.15 - /usr/local/bin/npm Browsers: Chrome: 101.0.4951.64 Chrome Canary: 104.0.5071.0 Firefox: 98.0.1 Safari: 15.2 Safari Technology Preview: 15.4 Packages: css-loader: ^6.7.1 => 6.7.1 css-modules-typescript-loader: ^4.0.1 => 4.0.1 styles-loader: ^4.0.1 => 4.0.1 ts-loader: ^9.3.0 => 9.3.0 webpack-cli: ^4.9.2 => 4.9.2

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:3
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
mditulliocommented, Oct 19, 2022

Thanks for the suggestion about dynamic import, I will look onto it.

For the polyfill of adoptedStyleSheets, I tried this lib: https://github.com/calebdwilliams/construct-style-sheets But there are some issues regarding Safari: https://github.com/calebdwilliams/construct-style-sheets/issues/110 But in general, to polyfill it, no other way than replicating style tags in each component instance.

So, for now, I decided to use Shadow DOM for intranet-only needs (just supporting Edge / Chrome), waiting for Apple updating its browsers… Safari is the new IE11 🐢

1reaction
alexander-akaitcommented, Oct 19, 2022

@mikeaustin Tha main problem style-loader is not extract CSS, it is for CSS in runtime code, anyway if you can use mini-css-extract-plugin and dynamic import(...) (alternative solution - set runtime: false and implement own logic for loading), but yes, CSSStyleSheet is right solution (that is bad it has a bad support right now, it was design for Shadow DOM). You have written polyfill is not wroking, can you desribe what is broken? Maybe you can create own small polyfill if you don’t need extra complex logic

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issues · webpack-contrib/style-loader - GitHub
Add support for using lazyStyleTag use() from a JavaScript module (injecting into multiple shadow DOMs). #565 opened on May 19 by mikeaustin.
Read more >
Webpack style-loader insert SCSS into shadow-dom
I am trying to configure webpack to insert my custom SCSS styles into a shadow DOM node instead of at the head of...
Read more >
Using shadow DOM - Web Components | MDN
Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree — this shadow DOM tree starts with...
Read more >
5. Working with the Shadow DOM - Modern JavaScript [Book]
Shadow root child nodes are not returned by DOM queries even if a child node matches the given query selector. Creating a shadow...
Read more >
Winning the war of CSS conflicts through the Shadow DOM
Fun fact: Multiple Shadow DOMs can be hosted in the document, ... This approach is definitely preferable to using vanilla Javascript, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found