Skip to content

Implement CSS module scripts - #74961

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
tuankiet65:eng/Implement-CSS-module-scripts
Oct 6, 2026
Merged

webkit-commit-queue merged 1 commit into
WebKit:mainfrom
tuankiet65:eng/Implement-CSS-module-scripts

Conversation

@tuankiet65

@tuankiet65 tuankiet65 commented Sep 25, 2026 •

Copy link
Copy Markdown
Member

b1913f6

Implement CSS module scripts
rdar://80917329
https://bugs.webkit.org/show_bug.cgi?id=227967

Reviewed by Ryosuke Niwa and Yusuke Suzuki.

This patch implements importing CSS stylesheets as JavaScript modules, using the
`import ... with { type: 'css' }` and `import(..., { with: { type: 'css' } })`
syntax.

On the JSC side, we add support for the `css` module type, and requests the host
to fetch it. If the host supports CSS modules (like WebCore), it fetches the
stylesheet, constructs a CSSStyleSheet object out of it, wraps it in a
JSCSSStyleSheet, and returns it back to JSC as a JSValue. JSC uses this as the
default export value of the CSS module. If the host doesn't support CSS modules
(like `jsc` command line or workers/worklets), then the import simply fails.

To implement this requires some changes in how script modules loading works. For
JavaScript/WASM/JSON/text, JSC asks the host (e.g WebCore) to fetch the source code.
The host fetches the source code and passes it back to JSC so it can turn it into
modules. CSS is a WebCore construct however, so only WebCore can parse and create
stylesheets out of it, not JSC. So to support CSS, the host now returns either source
code or a JSValue. When it's a source code, JSC will parse and create modules like
before. When it's a JSValue, JSC assumes the host has already created a module, and
it uses that value as the default module export.

This is gated by the CSSModuleScriptsEnabled feature flag, which is disabled by default.

There is one failing test only on GTK, which is tracked at webkit.org/b/326330:
- imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub.html

Tests: imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-allowed.sub.html
       imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-blocked.sub.html
       imported/w3c/web-platform-tests/fetch/api/request/destination/fetch-destination.https.html
       imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/

* LayoutTests/TestExpectations:
* LayoutTests/imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-allowed.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-blocked.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/fetch/api/request/destination/fetch-destination.https-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/document-metadata/the-style-element/tentative/style-element-basic-import-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/document-metadata/the-style-element/tentative/style-element-fixed-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-2-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-bom-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/content-type-checking-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/content-type-checking.html:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/cors-crossorigin-requests-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/css-module-parse-error-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/import-css-module-basic-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/import-css-module-dynamic-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/integrity-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/load-error-events-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/redirect-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/referrer-policies.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/relative-urls-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/repeated-import-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/resources/should-not-be-loaded.js: Added.
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/declarative/tentative/shadowrootadoptedstylesheets/shadowrootadoptedstylesheets-fetched-module-expected.txt:
* LayoutTests/platform/glib/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub-expected.txt: Copied from LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub-expected.txt.
* Source/JavaScriptCore/inspector/agents/InspectorDebuggerAgent.cpp:
(Inspector::scriptTypeForScript):
* Source/JavaScriptCore/jsc.cpp:
(GlobalObject::moduleLoaderFetch):
* Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp:
(JSC::tryCreateAttributes):
* Source/JavaScriptCore/parser/SourceProvider.h:
(JSC::SourceProvider::isModuleType const):
* Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp:
(JSC::AbstractModuleRecord::moduleType const):
* Source/JavaScriptCore/runtime/Completion.cpp:
(JSC::getSourceType):
(JSC::retrieveTypeImportAttribute):
* Source/JavaScriptCore/runtime/JSMicrotask.cpp:
(JSC::moduleRegistryFetchSettled):
(JSC::moduleLoadTopSettled):
* Source/JavaScriptCore/runtime/JSModuleLoader.cpp:
(JSC::JSModuleLoader::provideFetch):
(JSC::JSModuleLoader::hostLoadImportedModule):
(JSC::JSModuleLoader::makeModule):
* Source/JavaScriptCore/runtime/JSModuleLoader.h:
* Source/JavaScriptCore/runtime/ModuleRegistryEntry.cpp:
(JSC::ModuleRegistryEntry::provideFetch):
* Source/JavaScriptCore/runtime/ModuleRegistryEntry.h:
* Source/JavaScriptCore/runtime/ScriptFetchParameters.h:
(JSC::ScriptFetchParameters::parseType):
* Source/JavaScriptCore/runtime/SyntheticModuleRecord.h:
* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml:
* Source/WebCore/bindings/js/CachedModuleScriptLoader.cpp:
(WebCore::CachedModuleScriptLoader::load):
(WebCore::CachedModuleScriptLoader::notifyFinished):
(WebCore::CachedModuleScriptLoader::setCSSStyleSheet):
(WebCore::CachedModuleScriptLoader::notifyFinishedInternal):
* Source/WebCore/bindings/js/CachedModuleScriptLoader.h:
* Source/WebCore/bindings/js/CachedScriptFetcher.cpp:
(WebCore::CachedScriptFetcher::requestResourceWithCache const):
* Source/WebCore/bindings/js/JSDOMGlobalObject.cpp:
(WebCore::JSDOMGlobalObject::moduleTypeIsAllowed):
* Source/WebCore/bindings/js/JSDOMGlobalObject.h:
* Source/WebCore/bindings/js/JSDOMWindowBase.cpp:
(WebCore::JSDOMWindowBase::globalObjectMethodTable):
* Source/WebCore/bindings/js/ScriptModuleLoader.cpp:
(WebCore::ScriptModuleLoader::importModule):
(WebCore::createCSSModuleScript):
(WebCore::ScriptModuleLoader::notifyFinished):
* Source/WebCore/bindings/js/ScriptModuleLoader.h:
* Source/WebCore/bindings/js/WorkerModuleScriptLoader.cpp:
(WebCore::WorkerModuleScriptLoader::load):
* Source/WebCore/css/CSSStyleSheet.cpp:
(WebCore::CSSStyleSheet::replaceSync):
* Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp:
(WebCore::CachedCSSStyleSheet::sheetText const):
* Source/WebCore/loader/cache/CachedCSSStyleSheet.h:
* Tools/Scripts/webkitpy/style/checkers/cpp.py:

Canonical link: https://commits.webkit.org/322887@main

1052d11

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows Apple Internal
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win ✅ 🛠 ios-apple
✅ 🧪 bindings ✅ 🛠 ios-sim ✅ 🛠 mac-AS-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests ❌ 🛠 mac-apple
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe ✅ 🛠 vision-apple
✅ 🧪 webkitpy ✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-debug ✅ 🧪 jsc-wpe
✅ 🧪 jsc-x86-64 ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🛠 🧪 jsc-debug-arm64 ✅ 🛠 ios-safer-cpp ✅ 🧪 mac-AS-debug-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 mac-wk2-stress ✅ 🧪 gtk-wk2
✅ 🛠 🧪 merge ✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

@tuankiet65 tuankiet65 self-assigned this Sep 25, 2026
@tuankiet65 tuankiet65 added the CSS Cascading Style Sheets implementation label Sep 25, 2026
@tuankiet65
tuankiet65 marked this pull request as draft September 25, 2026 21:41
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Sep 26, 2026
@tuankiet65 tuankiet65 removed the merging-blocked Applied to prevent a change from being merged label Sep 28, 2026
@tuankiet65
tuankiet65 force-pushed the eng/Implement-CSS-module-scripts branch from c03b0b2 to 7a7d81e Compare September 28, 2026 09:58
@tuankiet65
tuankiet65 marked this pull request as ready for review September 28, 2026 09:58
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Sep 28, 2026
@tuankiet65 tuankiet65 removed the merging-blocked Applied to prevent a change from being merged label Sep 29, 2026
@tuankiet65
tuankiet65 force-pushed the eng/Implement-CSS-module-scripts branch from 7a7d81e to f65513b Compare September 29, 2026 03:50
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Sep 29, 2026
@tuankiet65 tuankiet65 removed the merging-blocked Applied to prevent a change from being merged label Oct 5, 2026
@tuankiet65
tuankiet65 force-pushed the eng/Implement-CSS-module-scripts branch from f65513b to cdf904c Compare October 5, 2026 20:22
@tuankiet65 tuankiet65 added the safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks label Oct 5, 2026
@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Oct 6, 2026
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

Failed mac-safer-cpp, wpe-wk2 checks. Please resolve failures and re-apply safe-merge-queue label.

Rejecting #74961 from merge queue.

@webkit-ews-buildbot webkit-ews-buildbot removed the safe-merge-queue Applied to automatically send a pull-request to merge-queue after passing EWS checks label Oct 6, 2026
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

Safe-Merge-Queue: Build #106723.

@tuankiet65 tuankiet65 removed the merging-blocked Applied to prevent a change from being merged label Oct 6, 2026
@tuankiet65
tuankiet65 force-pushed the eng/Implement-CSS-module-scripts branch from cdf904c to 1052d11 Compare October 6, 2026 05:22
@tuankiet65 tuankiet65 added the merge-queue Applied to send a pull request to merge-queue label Oct 6, 2026
rdar://80917329
https://bugs.webkit.org/show_bug.cgi?id=227967

Reviewed by Ryosuke Niwa and Yusuke Suzuki.

This patch implements importing CSS stylesheets as JavaScript modules, using the
`import ... with { type: 'css' }` and `import(..., { with: { type: 'css' } })`
syntax.

On the JSC side, we add support for the `css` module type, and requests the host
to fetch it. If the host supports CSS modules (like WebCore), it fetches the
stylesheet, constructs a CSSStyleSheet object out of it, wraps it in a
JSCSSStyleSheet, and returns it back to JSC as a JSValue. JSC uses this as the
default export value of the CSS module. If the host doesn't support CSS modules
(like `jsc` command line or workers/worklets), then the import simply fails.

To implement this requires some changes in how script modules loading works. For
JavaScript/WASM/JSON/text, JSC asks the host (e.g WebCore) to fetch the source code.
The host fetches the source code and passes it back to JSC so it can turn it into
modules. CSS is a WebCore construct however, so only WebCore can parse and create
stylesheets out of it, not JSC. So to support CSS, the host now returns either source
code or a JSValue. When it's a source code, JSC will parse and create modules like
before. When it's a JSValue, JSC assumes the host has already created a module, and
it uses that value as the default module export.

This is gated by the CSSModuleScriptsEnabled feature flag, which is disabled by default.

There is one failing test only on GTK, which is tracked at webkit.org/b/326330:
- imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub.html

Tests: imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-allowed.sub.html
       imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-blocked.sub.html
       imported/w3c/web-platform-tests/fetch/api/request/destination/fetch-destination.https.html
       imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/

* LayoutTests/TestExpectations:
* LayoutTests/imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-allowed.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/content-security-policy/style-src/import-style-blocked.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/fetch/api/request/destination/fetch-destination.https-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/document-metadata/the-style-element/tentative/style-element-basic-import-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/document-metadata/the-style-element/tentative/style-element-fixed-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-2-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-bom-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-bom.html:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/charset-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/content-type-checking-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/content-type-checking.html:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/cors-crossorigin-requests-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/css-module-parse-error-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/import-css-module-basic-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/import-css-module-dynamic-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/integrity-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/load-error-events-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/redirect-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/referrer-policies.sub-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/relative-urls-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/repeated-import-expected.txt:
* LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/resources/should-not-be-loaded.js: Added.
* LayoutTests/imported/w3c/web-platform-tests/shadow-dom/declarative/tentative/shadowrootadoptedstylesheets/shadowrootadoptedstylesheets-fetched-module-expected.txt:
* LayoutTests/platform/glib/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub-expected.txt: Copied from LayoutTests/imported/w3c/web-platform-tests/html/semantics/scripting-1/the-script-element/css-module/credentials.sub-expected.txt.
* Source/JavaScriptCore/inspector/agents/InspectorDebuggerAgent.cpp:
(Inspector::scriptTypeForScript):
* Source/JavaScriptCore/jsc.cpp:
(GlobalObject::moduleLoaderFetch):
* Source/JavaScriptCore/parser/NodesAnalyzeModule.cpp:
(JSC::tryCreateAttributes):
* Source/JavaScriptCore/parser/SourceProvider.h:
(JSC::SourceProvider::isModuleType const):
* Source/JavaScriptCore/runtime/AbstractModuleRecord.cpp:
(JSC::AbstractModuleRecord::moduleType const):
* Source/JavaScriptCore/runtime/Completion.cpp:
(JSC::getSourceType):
(JSC::retrieveTypeImportAttribute):
* Source/JavaScriptCore/runtime/JSMicrotask.cpp:
(JSC::moduleRegistryFetchSettled):
(JSC::moduleLoadTopSettled):
* Source/JavaScriptCore/runtime/JSModuleLoader.cpp:
(JSC::JSModuleLoader::provideFetch):
(JSC::JSModuleLoader::hostLoadImportedModule):
(JSC::JSModuleLoader::makeModule):
* Source/JavaScriptCore/runtime/JSModuleLoader.h:
* Source/JavaScriptCore/runtime/ModuleRegistryEntry.cpp:
(JSC::ModuleRegistryEntry::provideFetch):
* Source/JavaScriptCore/runtime/ModuleRegistryEntry.h:
* Source/JavaScriptCore/runtime/ScriptFetchParameters.h:
(JSC::ScriptFetchParameters::parseType):
* Source/JavaScriptCore/runtime/SyntheticModuleRecord.h:
* Source/WTF/Scripts/Preferences/UnifiedWebPreferences.yaml:
* Source/WebCore/bindings/js/CachedModuleScriptLoader.cpp:
(WebCore::CachedModuleScriptLoader::load):
(WebCore::CachedModuleScriptLoader::notifyFinished):
(WebCore::CachedModuleScriptLoader::setCSSStyleSheet):
(WebCore::CachedModuleScriptLoader::notifyFinishedInternal):
* Source/WebCore/bindings/js/CachedModuleScriptLoader.h:
* Source/WebCore/bindings/js/CachedScriptFetcher.cpp:
(WebCore::CachedScriptFetcher::requestResourceWithCache const):
* Source/WebCore/bindings/js/JSDOMGlobalObject.cpp:
(WebCore::JSDOMGlobalObject::moduleTypeIsAllowed):
* Source/WebCore/bindings/js/JSDOMGlobalObject.h:
* Source/WebCore/bindings/js/JSDOMWindowBase.cpp:
(WebCore::JSDOMWindowBase::globalObjectMethodTable):
* Source/WebCore/bindings/js/ScriptModuleLoader.cpp:
(WebCore::ScriptModuleLoader::importModule):
(WebCore::createCSSModuleScript):
(WebCore::ScriptModuleLoader::notifyFinished):
* Source/WebCore/bindings/js/ScriptModuleLoader.h:
* Source/WebCore/bindings/js/WorkerModuleScriptLoader.cpp:
(WebCore::WorkerModuleScriptLoader::load):
* Source/WebCore/css/CSSStyleSheet.cpp:
(WebCore::CSSStyleSheet::replaceSync):
* Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp:
(WebCore::CachedCSSStyleSheet::sheetText const):
* Source/WebCore/loader/cache/CachedCSSStyleSheet.h:
* Tools/Scripts/webkitpy/style/checkers/cpp.py:

Canonical link: https://commits.webkit.org/322887@main
@webkit-commit-queue
webkit-commit-queue force-pushed the eng/Implement-CSS-module-scripts branch from 1052d11 to b1913f6 Compare October 6, 2026 20:39
@webkit-commit-queue

Copy link
Copy Markdown
Collaborator

Committed 322887@main (b1913f6): https://commits.webkit.org/322887@main

Reviewed commits have been landed. Closing PR #74961 and removing active labels.

@webkit-commit-queue
webkit-commit-queue merged commit b1913f6 into WebKit:main Oct 6, 2026
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Oct 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CSS Cascading Style Sheets implementation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants