-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
CSS module support #46689
Comments
This would indeed simplify a lot of build processes that we have on Adobe projects. Right now this is all handled out of band by additional build steps, or bundler plugins. |
Any updates here? |
+1 |
@andrewbranch just wondering what kind of feedback you might be looking for on this issue in order to help with this? |
Another 5 months in, what's the state of this? |
Well, the 52 👍 on the issue are a good signal. Let’s start here:
After a year, has anything changed here? Any other major browsers shipping? Any significant changes to the proposal? What does the process for proposals here moving into a spec or standard look like? Any progress on that? |
The precursor to CSS Modules is Constructable Stylesheets which in the past year have been shipped by Firefox and is now available in Safari Technical Preview. 🎉 That said, I don't think FF has shipped Import Assertions yet and Safari still has CSS Module Scripts in open status. But they are all coming along quickly! |
@andrewbranch CSS Module Scripts, along with JSON Module Scripts, are merged into the HTML specification: https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-css-module-script As mentioned by @thescientist13, Constructible StyleSheets is a prerequisite and is now implemented in all major browsers. Firefox also has a positive response to CSS Modules Scripts on their standard position issue. Webkit has listed a single concern about CSP integration (but this is just a decision that needs to be made, not a risk). Given that Chrome, Edge, Safari and Firefox were all very involved in the CSS Module Scripts discussion, including specs that build up to it like import assertions, constructible stylesheets, adopted stylesheets, and modifications to JS module records, I'm personally very confident that this will ship in all browser, and relatively soon at the pace that they're moving. |
@andrewbranch also, fixing #46135 first is an option that would give us the ability to type CSS Module Scripts correctly, like so: declare module '*' assert {type: 'css'} {
const stylesheet: CSSStyleSheet;
export default stylesheet;
} |
Currently I use Typed CSS Modules VSCode extension to generate d.ts files for each css/scss file. It helps a lot to find undefined classnames and fails build if somebody removes used classname from css/scss. It could be nice if classnames, scss variables and other selectors could be inferred automatically without the need to generate d.ts files. |
@JustFly1984 this is about typing support for the HTML-standard CSS module scripts, which are already supported in some browsers. They simply default export a CSSStyleSheet, and the feature request is to type Userland CSS module systems will need their own support for their semantics, and presumably already have such systems in their toolchains. |
Was that rather supposed to be
? |
@andrewbranch not sure if you're the best person to ping about this but as far as I can tell this is now officially part of the HTML spec https://html.spec.whatwg.org/multipage/webappapis.html#creating-a-css-module-script and is now natively supported in a number of browsers and has full cross browser support thanks to this https://github.com/guybedford/es-module-shims Would it make sense to revisit this perhaps? |
It would, except that import assertions were demoted to Stage 2, so for sure nothing that depends on them is happening in TypeScript right now. |
They're not going to be removed though, and the current spec change keeps the I've been using CSS Modules with recently with this snippet: declare module '*' assert {type: 'css'} {
const stylesheet: CSSStyleSheet;
export default stylesheet;
} And one big problem is that TypeScript doesn't attempt to understand the specifiers as actual file paths, so we don't get jump-to-definition on the import or specifier. It seem like treating the specifier more or less normal and trying to resolve it on disk would be a low-risk change that would be helpful for non-standard imports too. |
My understanding is that there’s a possibility that the proposal will come back with identical syntax and changed semantics, or might come back with alternative syntax (like |
According to https://github.com/tc39/proposal-import-attributes#history the proposal moved back to Stage3, pasting relevant entry below (emphasis mine) 2023-03: The proposal is renamed to Import attributes and moves back to Stage 3 (TODO: notes, slides). The restriction on the cache key is completely removed, and the keyword changes back from assert to with: import { x } from "./mod" with { type: "json" };. For compatibility with existing implementations, the assert keyword will still be supported until it's safe to remove it, if it will ever be. See commit from March 31st which updated he README file.
This is also addressed in the update linked above. Is the above a good enough signal for unblocking adding TypeScript compiler support for this feature? Thanks! |
Import attributes achieved conditional Stage 3 and the last bits of making it official are still outstanding. Please follow: |
It looks like import attributes support is going to ship in 5.3 🎉 Can CSS modules get another look to bring them up to parity with JSON modules? |
@andrewbranch an update on your question
Gecko and Webkit still haven't implemented CSS module scripts, but they have implemented Constructible Stylesheets and import attributes, which are the prerequisites. They still have positive support on their standards positions issues, and there's a request to add import attributes, and JSON and CSS module scripts to interop 2024. |
Is there a minimum working sample of a css module script (import attributes) using TypeScript 5.3? My this stackblitz sample fails to compile this code in TypeScript 5.3. const css = await import(cssfile, { with: { type: 'css' } });
if (this.shadowRoot !== null) {
this.shadowRoot.adoptedStyleSheets = [css]
} Of course, this stackblitz samples using fetch still work fine today. const cssstring = await (await fetch(cssfile)).text()
const css = new CSSStyleSheet()
css.replaceSync(cssstring)
if (this.shadowRoot !== null) {
this.shadowRoot.adoptedStyleSheets = [css]
} |
@andrewbranch I just wanted to re-ping on this topic if possible to see what roadblocks still remain? This is having an awful impact on my workflows for the last year or so and as far as I can tell all of the blockers you had previously mentioned were since resolved. |
@andrewbranch I'm taking a look at #56359, would it be best for me to give my answer to those questions in #46135 ? |
Yes, I think so. |
While TypeScript has added support for import attributes per [1], it is still not entirely clear how to resolve CSS modules [2]. Using a workaround from [3] where a proposal is discussed instead of resorting to using `@ts-ignore:next-line` comments to suppress errors. [1] https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html#import-attributes [2] microsoft/TypeScript#46689 [2] microsoft/TypeScript#46135 (comment) Bug: None Change-Id: Ia2315d35dbca7761441b1694e4446b12d0532fc5 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6086700 Reviewed-by: Rebekah Potter <rbpotter@chromium.org> Commit-Queue: Demetrios Papadopoulos <dpapad@chromium.org> Cr-Commit-Position: refs/heads/main@{#1395040}
Suggestion
Now that TS 4.5 supports import assertions and JSON modules, it'd be great if it similarly supported CSS modules which are now shipping in Chrome and Edge.
There are two aspects of JSON module support that would be important to add for CSS modules:
🔍 Search Terms
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
📃 Motivating Example
💻 Use Cases
Importing standard CSS modules...
The text was updated successfully, but these errors were encountered: