feat(emotion): add sourcemap and auto-label for css prop with object styles#599
Conversation
…styles
When using the Emotion css prop with object styles (e.g. `css={{ color: 'red' }}`),
the plugin now wraps the expression in a `css()` call and adds sourceMappingURL
and auto-label arguments, matching the behavior of the css template literal syntax.
The transformation is applied after `fold_children_with` to avoid double-processing
by `fold_call_expr`. The label context is captured before folding to preserve the
correct class/function name in auto-label output.
Fixes #160
Co-authored-by: Donny/강동윤 <kdy1@users.noreply.github.com>
|
|
|
PR Review: feat(emotion): add sourcemap and auto-label for css prop with object stylesThis is a well-targeted fix for issue #160. The approach of deferring the rewrite to after Issues1. Unconditional wrapping when no label/sourcemap will be added The // Build args, then only rewrite if something was added
let mut args = vec![raw_expr.as_arg()];
// ... push label/sourcemap ...
if args.len() == 1 {
// Nothing to add — restore the expression and skip
*expr = args.remove(0).expr;
continue;
}2. Missing changeset The changeset-bot already flagged this — a Observations / Minor Notes3.
4. The save-and-restore of 5. Test coverage gaps The added fixture covers the happy path (class and function components). Consider adding fixtures for:
SummaryThe core logic is sound and the fix addresses the reported issue. The main actionable items before merging are:
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 17702908a7
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| // Find a css identifier (ExprKind::Css) in import_packages | ||
| let css_id = self | ||
| .import_packages | ||
| .iter() | ||
| .find_map(|(id, meta)| { | ||
| if matches!(meta, PackageMeta::Named(ExprKind::Css)) { | ||
| Some(id.clone()) | ||
| } else { | ||
| None | ||
| } | ||
| }); |
There was a problem hiding this comment.
Support namespace imports when wrapping css props
rewrite_css_prop_attr() only looks for PackageMeta::Named(ExprKind::Css), so it bails out for files that import Emotion as a namespace. That import style is explicitly supported elsewhere in this transformer (generate_import_info() records import * as emotionReact from '@emotion/react' as PackageMeta::Namespace, and tests/fixture/namespace-import covers it), but <div css={{ color: 'red' }} /> will still skip this new path and lose the added label/sourcemap metadata. Please handle the namespace case by building a member-expression callee instead of requiring a named css binding.
Useful? React with 👍 / 👎.
| _ => continue, | ||
| } | ||
|
|
||
| let expr_pos = container_span.lo; |
There was a problem hiding this comment.
Attach the purity annotation to the generated css() call
Using container_span.lo here points at the JSX expression braces rather than the rewritten css(...) call. In the new tests/fixture/issues/160/output.ts, the object/array css props are emitted as css: css(...) without the /*#__PURE__*/ marker that template-literal css props still get in tests/fixture/css-in-callback/output.ts, so bundlers can no longer tree-shake these injected calls when the surrounding JSX becomes dead code. The annotation needs to be attached to the generated call site, not the JSX container span.
Useful? React with 👍 / 👎.
## Summary - Addressed `chatgpt-codex-connector[bot]` review follow-ups across merged PRs: #585, #591, #592, #593, #594, #595, #597, #598, #599. - Applied the fixes as 9 separate commits (one commit per original PR) in a single follow-up branch. ## What Changed - #585 / #592 (`loadable-components`) - Fixed source-less default-import matching to honor the configured local name. - Updated `ssr: false` detection to respect final object-literal override order. - Added/updated fixtures for both behaviors. - #591 / #594 (`formatjs`) - Added JSX member-expression message component support (e.g. `ReactIntl.FormattedMessage`). - Updated #532 regression coverage to validate the `ast: true` path. - #593 / #595 (`graphql-codegen-client-preset`) - Extended `namingConvention` parsing to accept string/object forms. - Preserved names for `keep`/unknown conventions instead of forcing PascalCase. - Fixed Windows absolute `filename` path handling in WASM runtime path resolution. - Added unit coverage for the new config/path behaviors. - #597 / #599 (`emotion`) - Ensured tagged-template labels are terminated before sourcemap comments. - Added css-prop rewrite support for namespace imports (`emotionReact.css`). - Attached PURE comments to the generated call site span. - Updated emotion fixtures accordingly. - #598 (docs) - Corrected capability descriptions in `packages/jest/README.tmpl.md` and `packages/swc-sdk/README.tmpl.md`. ## Validation - `cargo test -p swc_plugin_loadable_components --test fixture -- --ignored` - `cargo test -p swc_plugin_graphql_codegen_client_preset` - `cargo test -p swc_emotion --test fixture -- --ignored` - `pnpm -C /Users/kdy1/.codex/worktrees/17e6/plugins/packages/formatjs test` All passed (formatjs has an existing non-blocking Vitest warning about an un-awaited rejects assertion).
When using the Emotion css prop with object styles (e.g.
css={{ color: 'red' }}), the plugin now wraps the expression in acss()call and adds sourceMappingURL and auto-label arguments, matching the behavior of the css template literal syntax.Fixes #160
Generated with Claude Code