Skip to content

feat(transform, webpack-plugin): handle imported assets with asset tags#767

Merged
layershifter merged 1 commit intomicrosoft:mainfrom
layershifter:feat/asset-tags
Mar 4, 2026
Merged

feat(transform, webpack-plugin): handle imported assets with asset tags#767
layershifter merged 1 commit intomicrosoft:mainfrom
layershifter:feat/asset-tags

Conversation

@layershifter
Copy link
Member

Summary

  • When styles reference imported assets (import logo from './logo.png'), the VM Module class now wraps the resolved absolute path in <griffel-asset>...</griffel-asset> tags instead of returning the raw relative id
  • The webpack plugin's new resolveAssetPathsInCSSRules utility strips the tags and resolves paths relative to the source file before CSS generation
  • This enables reliable asset path resolution when CSS is extracted to a different location (fixes babel-preset: handles relative paths as assets #313)

Changes

@griffel/transform

  • New constants.mts with ASSET_TAG_OPEN / ASSET_TAG_CLOSE, exported from package index
  • Module.require wraps non-JS/JSON assets: <griffel-asset>/abs/path/file.jpg</griffel-asset>
  • New module.test.mts with 3 unit tests for asset handling

@griffel/webpack-plugin

  • New resolveAssetPaths.mts utility — walks CSSRulesByBucket, replaces tagged paths with relative paths (uses indexOf/slice, no regex)
  • webpackLoader.mts calls resolveAssetPathsInCSSRules before generateCSSRules
  • New resolveAssetPaths.test.mts with 6 unit tests

Test infrastructure

  • transformSync.test.mts — added normalizeAssetFixtureOutput() to make asset fixture snapshots machine-independent (absolute paths → relative, hashes → ordered placeholders)
  • GriffelPlugin.test.mts — added normalizeGriffelHashes() for asset integration test stability
  • Updated 6 fixture snapshot files

Test plan

  • nx run @griffel/transform:type-check
  • nx run @griffel/transform:test — 56/56 pass
  • nx run @griffel/transform:build
  • nx run @griffel/webpack-plugin:type-check
  • nx run @griffel/webpack-plugin:test — 45/45 pass (including all asset integration tests)

🤖 Generated with Claude Code

@layershifter layershifter requested a review from a team as a code owner March 4, 2026 15:13
@github-actions
Copy link

github-actions bot commented Mar 4, 2026

📊 Bundle size report

✅ No changes found

@layershifter layershifter force-pushed the feat/asset-tags branch 4 times, most recently from 6dea7be to 53d61c5 Compare March 4, 2026 15:25
…-asset> tags

When styles reference imported assets (`import logo from './logo.png'`),
the VM Module class now wraps the resolved absolute path in
`<griffel-asset>...</griffel-asset>` tags instead of returning the raw
relative id. This allows the webpack plugin to reliably resolve asset
paths when CSS is extracted to a different location.

- Add ASSET_TAG_OPEN/ASSET_TAG_CLOSE constants and export from @griffel/transform
- Module.require wraps non-JS/JSON assets in tags with absolute path
- Add resolveAssetPathsInCSSRules utility in webpack-plugin to strip tags
  and resolve paths relative to source file before CSS generation
- Update fixture snapshots with normalized hashes for cross-machine stability
- Add unit tests for Module asset handling and resolveAssetPaths

Closes microsoft#313

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@layershifter layershifter enabled auto-merge (squash) March 4, 2026 15:37
@layershifter layershifter merged commit aaf1e0d into microsoft:main Mar 4, 2026
4 checks passed
@layershifter layershifter deleted the feat/asset-tags branch March 4, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

babel-preset: handles relative paths as assets

2 participants