r/javascript • u/lukasbuenger • Aug 21 '21
Microsoft and Google have collaborated to bring native support for CSS modules to Chromium, available in Edge and Chrome 93.
https://blogs.windows.com/msedgedev/2021/08/17/css-module-scripts-import-stylesheets-like-javascript-modules/13
u/Mestyo Aug 21 '21 edited Aug 21 '21
I don't really get it. This seems powerful, but I wouldn't know when to use it, and it's not "CSS Modules" as it doesn't provide a meaningful way to scope the styles.
I suppose scoping could be achieved by re-writing the selectors with the Constructible Stylesheets API, but that seems like an awful lot of overhead.
Edit: Oh wait, I think got it; I could just serve my pre-compiled and scoped CSS Modules like native CSS Modules instead of one stylesheet as a further optimization in a code-splitting environment. That's pretty neat.
18
u/brainless_badger Aug 21 '21
This is primarily meant to be used with Web Components and Shadow DOM.
9
u/Garbee Aug 21 '21
Exactly this with Shadow DOM being the main focus. Right now it's actually kinda hard to get styles into them without it being inlined or an external reference that needs to be kept track of on deployment and pulled in. By exposing styles as a module you're able to deploy using all the existing goodness we have without needing to manually track assets going into web components.
I found a hack a few months ago with esbuild that got the job done. Import a style as a binary blob. Then decode that and put it into an inline style tag. It only worked with esbuild and was quite a hack, CSS Modules entirely resolve this when native.
1
u/pixeldrew Aug 21 '21
Without @import rules being allowed it'll make it hard to compose only in css.
2
u/Garbee Aug 21 '21
I've written tons of CSS, never needed @import for anything. Your custom properties penetrate the Shadow DOM so any trivial stuff you can leave for external modification with defaults if undefined. If your components are well written, you generally won't have import anyways since a web component is entirely isolated to itself for the majority of it's work and requirements.
It all comes down to how much work you want to put into isolating things. If you keep components small and isolated, imports are not an issue.
3
u/pixeldrew Aug 21 '21
Lol, what I read when you wrote...
I've written tons of CSS, never needed @import for anything!
"I've written tons of css and never had to conditionally import styles for things like printers"
There are use cases import supports which you won't be able to do using this method.
1
u/Garbee Aug 21 '21
You can media query print styles. Importing conditionally isn’t the only way to handle things like that.
4
1
u/thunfremlinc Aug 21 '21 edited Aug 21 '21
You should not be inlining styles into JS. Parse time for JS and CSS is drastically different.
This change allows you to treat CSS as it should be, and extract it from your component, which requires imports.
2
u/Garbee Aug 21 '21
Can you show a use case that requires import as the solution to the problem?
1
u/thunfremlinc Aug 21 '21
What? Your only alternative (no imports) is inlining the styles into JS. That’s awful for perf.
0
u/Garbee Aug 21 '21
You do realize the original comment is about the @import rules. Not module importing.
0
8
u/krileon Aug 21 '21
Cool. Another feature I can't use for at least 5 years. :(
12
1
1
1
u/Sereina4 Aug 21 '21
It would be even more useful if they supported less or sass...
2
u/Garbee Aug 22 '21
The primary purpose of CSS as module imports is for web components. If they are designed properly then in most cases you wll have a Shadow DOM. So your styles only affect your component and your targets should be pretty minimal. Variables are baked into CSS now with custom properties.
You shouldn't have too much of a need for preprocessors overall. Certain situations they may still be extremely useful to handle generating more complex style situations (like animation stuff that gets detailed and is a pain to write out by hand.)
The few things that preprocessors still provide will hopefully get built into CSS in some way down the road. It's a hard beast to wrangle though.
0
Aug 21 '21
[deleted]
30
u/davidstraka2 Aug 21 '21 edited Aug 21 '21
Opera is built on Chromium since a long time ago. So it's mostly about Firefox and Safari. As for the second question, probably - that's been the case with a lot of other things. Also, even if it somehow didn't end up being adopted by the other browser engines (whether or not it becomes part of an actual standard), it could be used in cases where browser support is not a concern, such as in an Electron app (which is also built on Chromium, alongside Node), Edge WebView2 app, or with CEF (Chromium Embedded Framework).
44
u/liamnesss Aug 21 '21
Safari is a big part of the picture too, at least on mobile.
70
Aug 21 '21
Mobile safari already refuses to support the full spec for the web, so that won't be any different
9
u/snejk47 Aug 21 '21
Also it's now 2 years since Firefox and Safari refused even to comment. Isn't that sabotage? Why they are even on standard committee if they do not care about things.
10
u/liamnesss Aug 21 '21
2 years, where are you getting that from?
5
u/snejk47 Aug 21 '21
Explainer. PR and discussion followed few days after. Those incubators and WHATWG are of course official.
1
u/CharlieandtheRed Sep 14 '21
Safari is the new IE. I don't get how we still face these issues. We need standards that every browser follows. It's so simple and we're so close.
0
Aug 21 '21
[deleted]
22
Aug 21 '21
I honestly hope that Apple gets fucked in the ass by antitrust laws for not allowing other browsers.
1
u/WingersAbsNotches Aug 21 '21
How are they stopping you from using other browsers?
44
Aug 21 '21
They allow browsers on the App Store, but those browsers all are required to use Safari’s engine, so they are basically just Safari skins. It’s complete and utter bullshit, especially when their browser sucks so bad.
8
Aug 21 '21
Explains why I loaded up Chrome on my iPhone and it felt like Safari in different clothing.
10
2
u/WingersAbsNotches Aug 21 '21
Thanks for the serious response. I never thought about the fact other browsers would need to use the WebKit view.
1
Aug 21 '21
Yeah it’s shockingly counterintuitive that they would even think of something like that.
2
Aug 22 '21
It's not at all, control the JavaScript engine that runs in your system and you've locked out your system from chrome/third party web engines exploits
It fucking sucks because there are exploits already in safari, but that's their thinking about the subject, not just "we are lazy fucktards"
-4
u/kent2441 Aug 21 '21
^ someone who obviously never had to work with IE
10
u/rasGazoo Aug 21 '21
Safari doesn't follow W3C standards, vast majority of web development issues encountered in terms of CSS are Safari specific. It's well optimized on their devices, they just need to fix their own browser. In the time of automatic updates it's mind-boggling.
-8
u/kent2441 Aug 22 '21
Of course Safari follows W3C standards. What you’re confused by is that Chrome does its own thing which people think are standards.
13
u/rasGazoo Aug 22 '21
You are wrong. Safari doesn't support
containsproperty in CSS, doesn't implement 'scroll into view', focus anchors, touch events are broken or not implemented correctly. These are W3C standards, not proprietary implementations. They also avoid simple things like new image formats like AVIF. You can look atcaniuseand delve into what is a W3C standard or not.3
u/kent2441 Aug 22 '21
Contains is in development, and Safari does support scrollIntoView which is a draft anyway. Do you mean overflow-anchor? Because that’s a draft. Touch events are fine, Safari created them. And if you’d checked caniuse, you’d know that no one supports AVIF except Chrome.
-1
-2
u/rasGazoo Aug 22 '21
4
u/kent2441 Aug 22 '21
Did you even look? It clearly says Safari has supported it since version 3. Are you a troll or just a liar?
→ More replies (0)4
u/redldr1 Aug 21 '21
Go to hell punk.
been working with ie since it was v3
-7
u/kent2441 Aug 21 '21
Then you should know better.
5
u/redldr1 Aug 21 '21
iOS safaris handling of canvas is a travesty.
2
u/aniforprez Aug 22 '21
iOS Safari and desktop Safari routinely have horrible CSS bugs and workarounds and weird browser behaviour. Chrome does too but Safari has way more stuff that breaks but works on FF and Chrome so it remains the odd one out. Follow the Tailwind twitter for occasional developments where they go "wtf is Safari doing"
Dunno what the person you're replying to is talking about
19
u/black3rr Aug 21 '21
Years ago there were standardized versions of HTML, CSS and JS (you may remember things like HTML4, HTML5, XHTML, CSS2, CSS3, ES6, ...).
These days with the major browsers releasing new versions every 6 weeks or so we have "living standards", e.g. HTML standard is a GitHub repository and new features are proposed as pull requests. This change was already approved by the HTML standards committee (WHATWG) as you can see its PR was already merged: https://github.com/whatwg/html/pull/4898
Given that "new standards" are introduced every once in a while, standardization is not about 100% support of all features anymore, but about standardizing "the way how to do something" for when the browsers want to add support for it.
The browsers then usually have their own roadmap of when to implement what features based on their customers' needs.
Features like this are mostly useful to developers because this was already possible with webpack years ago but now you can use it without webpack for rapid prototyping. Chrome is a browser that tries to cater to developers a lot more than the other ones so it makes sense they implemented it first.
5
3
u/ivosaurus Aug 21 '21
Haven't you heard? Chromium is the only engine you need to worry about now! Just like in the good ol' days with Internet Explorer! /s
-21
Aug 21 '21 edited Aug 21 '21
Firefox uses chromium engine too bruh. They just have a rust engine on top of it. IMO they'll probably end up taking a fork of it too
Edit: this is not true - see below link. Thanks u/mkantor
1
15
Aug 21 '21
[removed] — view removed comment
3
Aug 21 '21 edited Aug 21 '21
Hold up: let me fish out the news where I heard it. You're right, it may not be correct, but I heard some news on this front. Wonder if it was a lesser component and not the whole engine, V8 or something. Ill come back and post it if I can find it.
Really need to start saving this stuff when I come across it because its important esp if I cant remember it word for word lol. Thanks though for the link and the correction!
Edit: Perhaps not at all! Right on their FAQ ~ https://imgur.com/a/EiZCHk0
Could have sworn I heard something about collaboration, maybe it was more so in regards to mozilla on iOS having to be based on safaris webkit? IDK. If I find the news Im thinking of, I'll make a post about it in javascript separately.
4
48
u/liamnesss Aug 21 '21 edited Aug 21 '21
Seems this is built on top of a standard that is about to be adopted:
https://github.com/tc39/proposal-import-assertions
But CSS modules are not yet on track to be a standard, only JSON modules, so I would be a bit wary of using this in production code. Wouldn't be the first time a feature was introduced in Chromium and then later removed because it never became a standard.
edit—I was incorrect, the proposal for CSS modules specifically has been accepted by the WHATWG https://github.com/whatwg/html/pull/4898