Repository navigation
Read vanilla-extract stylesheet exports from ordinary modules - #973
Merged
Merged
Conversation
An ordinary module importing `vars` from `theme.css.ts` and reading `vars.space` in `style()`, `css()` or a JSX prop was a build error. The imported-constants reader now extracts an imported `.css.ts` / `.css.js` the way the bundler extracts it and reads its exports from that output, so theme vars objects, theme classes, createVar variables and style class names become the literals the stylesheet's own CSS uses. The stylesheet import stays so its CSS loads, the importer depends on the stylesheet and the files it reads, a stylesheet reached again while it is being compiled reads as unknown, and a stylesheet that fails to extract keeps the located error. The migration guide documents the supported reads. Refs #693 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codecov Report✅ All modified and coverable lines are covered by tests.
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Follow-up to DOC-09. This is what #760 said would be "supported in a separate PR".
Problem
.ts/.tsx, not.css.ts) importedvarsfromtheme.css.tsand readvars.space,vars.colors.bg, etc. instyle()/css()/JSX props, the build failed. The error message is`css()` cannot use `vars.space` at build time.theme.css.tsandtokens.tsresolved correctly.Fix
.css.ts/.css.js), the imported-constants reader (Modules::resolve_exports) extracts that file the same way the bundler does (extract_with_source_map). It reads the exports as literals from the extracted output code.imported_constants/stylesheet_exports.rscreateThemeContract/createTheme/createGlobalTheme, theme classes,createVarvariables,styleclass namesstyle({ margin: vars.space })compiles to the samevar(--space-…)name that the stylesheet's own CSS declares.style([base, …])also keeps the class the stylesheet generated.tokens.ts). So if either changes, the importer is rebuilt.Compiling). A stylesheet met again is read as unknown, so there is no infinite recursion.createThemeContractdirectly inside an ordinary module is still an error, as before.button.ts) works.Overlap with open PRs
var(--space-0-1)etc.) may need updating.Test plan
cargo test -p extractor --tests -j 2: all passvanilla_extract_theme_import_tests.rs:css()namespace / JSX prop /style([base, …])composition. In each case, checks that the names read match the stylesheet's own output, that the import is kept, and that the dependency is recordedtokens.ts, which the stylesheet reads, is included in the importer's dependencies/src/button.ts:3:23location errorbutton.tsexamplestylesheet_exportsunit test: None when re-entering a stylesheet that is being compiledcargo clippy -p extractor --all-targets -- -D warnings,cargo fmt --all --check요약
DOC-09 후속 작업입니다. #760에서 "별도 PR로 지원하겠다"고 했던 내용입니다.
문제
.css.ts가 아닌.ts/.tsx)이theme.css.ts의vars를 import해서vars.space,vars.colors.bg등을style()/css()/JSX prop에서 읽으면 빌드 오류가 났습니다. 오류 메시지는`css()` cannot use `vars.space` at build time입니다.theme.css.ts와tokens.ts가 정상적으로 해석되는 경우에도 마찬가지였습니다.수정
Modules::resolve_exports)가 import 대상이 vanilla-extract 스타일시트(.css.ts/.css.js)이면 그 파일을 번들러와 같은 방식으로 추출합니다(extract_with_source_map). 추출된 출력 코드에서 export를 리터럴로 읽습니다.imported_constants/stylesheet_exports.rscreateThemeContract/createTheme/createGlobalTheme의 vars 객체, 테마 클래스,createVar변수,style클래스 이름style({ margin: vars.space })는 스타일시트 자신의 CSS가 선언한var(--space-…)와 같은 이름으로 컴파일됩니다.style([base, …])도 스타일시트가 생성한 클래스를 그대로 유지합니다.tokens.ts)을 모두 넣었습니다. 그래서 둘 중 하나가 바뀌면 importer가 다시 빌드됩니다.Compiling)로 끊습니다. 다시 만난 스타일시트는 unknown으로 읽혀 무한 재귀가 생기지 않습니다.createThemeContract를 선언하는 경우도 기존처럼 오류입니다.button.ts)가 이제 동작한다고 설명합니다.열려 있는 PR과의 겹침
var(--space-0-1)등)을 갱신해야 할 수 있습니다.테스트 계획
cargo test -p extractor --tests -j 2: 전체 통과vanilla_extract_theme_import_tests.rs:css()네임스페이스 / JSX prop /style([base, …])조합. 각 경우에 읽힌 이름이 스타일시트 자신의 출력과 같은지, import가 남는지, 의존성이 기록되는지 확인tokens.ts가 importer 의존성에 포함되는지 확인/src/button.ts:3:23위치 오류가 유지되는지 확인button.ts예제 insta 스냅샷stylesheet_exports단위 테스트: 컴파일 중인 스타일시트 재진입 시 Nonecargo clippy -p extractor --all-targets -- -D warnings,cargo fmt --all --checkRefs #693
🤖 Generated with Claude Code