Skip to content

feat: resolve url() inside HTML style attributes via CSS parser as option#21157

Merged
alexander-akait merged 12 commits into
mainfrom
feat/css-url-in-style-attributes
Jun 10, 2026
Merged

feat: resolve url() inside HTML style attributes via CSS parser as option#21157
alexander-akait merged 12 commits into
mainfrom
feat/css-url-in-style-attributes

Conversation

@alexander-akait

Copy link
Copy Markdown
Member

Summary

webpack's experimental HTML support didn't resolve url() (nor image-set() / @import) references inside inline style="..." attributes — the value was emitted verbatim, so relative asset URLs broke. This routes a style attribute's value through webpack's CSS pipeline (like a <style> block) so those references resolve relative to the HTML file.

Because an attribute value is a CSS block's contents (a declaration list), not a full stylesheet, a new CSS parser option module.parser.css.as ("stylesheet" | "block-contents") selects the top-level production to parse as; the default stays "stylesheet". Developers can opt their own custom attributes into the CSS pipeline via the HTML source types stylesheet-style (a full inline stylesheet, like a <style> body) and stylesheet-style-attribute (a block's contents, like style=).

What kind of change does this PR introduce?

feat

Did you add tests for your changes?

Yes — test/configCases/html/style-attribute/ (url()/image-set() resolution inside style attributes, basic + cache), test/configCases/html/parser-sources-types/ (the new stylesheet-style-attribute source type), and unit coverage in test/walkCssTokensParser.unittest.js (the as: "block-contents" walk).

Does this PR introduce a breaking change?

No for stable APIs. Within experimental experiments.html, the recently-added source type stylesheet-inline is renamed to stylesheet-style — migration: rename that string in module.parser.html.sources.

If relevant, what needs to be documented once your changes are merged or what have you already documented?

The new module.parser.css.as option and the stylesheet-style / stylesheet-style-attribute HTML source types should be added to the parser-options docs.

Use of AI

Yes — implemented with the help of Claude Code (Anthropic). It drafted the CSS parser as option, the walk's top-level-consumer dispatch, the HTML source-type wiring, and the tests; every change was reviewed and verified locally by running yarn tsc and the targeted CSS/HTML Jest suites. Per the webpack AI policy, the output was checked for correctness rather than committed blindly.


Generated by Claude Code

…option

Add a CSS parser `as` option (default `"stylesheet"`) that selects the
parse entry point. With `as: "declaration-list"` the source is parsed as a
block's contents (CSS Syntax §5.4.5) instead of a full stylesheet, which is
the correct model for an element's `style="..."` attribute.

HTML modules now route URL-bearing `style` attributes through the CSS
pipeline (via the new `html-style-attribute` dependency category, parsed
with `as: "declaration-list"`), so `url()` / `image-set()` references in
inline styles resolve relative to the HTML file.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
Instead of switching on an `as` string inside `grammar`, `SourceProcessor`
now accepts a `consume` driver (a `TopLevelConsumer`) and defaults to the
stylesheet consumer. `consumeADeclarationList` is exported as a reusable
driver, and `CssParser` maps its `as` option to it — so the walk no longer
hardcodes how the source is parsed.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
The injectable top-level driver now routes through the §5.3 `parse*`
functions instead of the §5.4 `consume*` internals: the default uses
`parseAStylesheetsContents` and a `style` attribute uses the new
`parseADeclarationList` ("parse a block's contents", §5.3.6). Both receive
the already-built `TokenStream`, which `normalizeIntoTokenStream` returns
as-is, so there is no re-tokenization. `parseAStylesheetsContents` gains the
same optional streaming `onRule` sink `consumeAStylesheetsContents` already
had, so the default path keeps its rule-by-rule memory profile.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
Drop the `parseADeclarationList` wrapper — its name reflects the old spec
term "parse a list of declarations", which CSS Syntax renamed to "parse a
block's contents". CssParser now builds the `style`-attribute top-level
parser straight from `parseABlocksContents` (§5.3.6), reusing the existing
token stream as before.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
…lk driver

Drop the "declaration-list" terminology and the grammar's internal default.
`SourceProcessor#process` now requires the caller to pass the top-level parse
entry point, and CssParser passes one of the spec §5.3 functions directly:
`parseAStylesheet` for a stylesheet, `parseABlocksContents` for a block's
contents (the `as: "block"` mode, e.g. an HTML `style` attribute). The walk
reuses the existing token stream and walks the returned nodes. Revert the
now-unused streaming `onRule` extension on `parseAStylesheetsContents`.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
Rename the `style` attribute parse mode from `as: "block"` to
`as: "block-contents"` so the option values match the CSS Syntax §5.3 entry
points ("parse a block's contents" / "parse a stylesheet"), leaving room for
future spec-named modes. Also remove the now-unused `onRule` streaming param
from `consumeAStylesheetsContents`.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
…purely additive

Restore the original "consume a stylesheet's contents" streaming driver
(`consumeAStylesheetsContents(ts, onRule)`) for the default `as: "stylesheet"`
mode — the spec parse logic is unchanged. The `as: "block-contents"` mode is
now a small additive branch in the walk's grammar that parses a block's
contents instead. Drops the `TopLevelParser` indirection.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
Give `consumeABlocksContents` the same streaming `onNode` callback that
`consumeAStylesheetsContents` already exposes, so both share a `(ts, onNode)`
shape. The walk's `grammar` now dispatches `as` through a `TOP_LEVEL_CONSUMERS`
map instead of a branch — a future `as` mode is one map entry, no new walk
code, and every mode streams (no full AST retained).

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
…heet-inline

Let developers route a custom HTML attribute's value through the CSS pipeline
as a block's contents (a declaration list, like a `style` attribute) via the
new `stylesheet-style-attribute` source type, alongside `stylesheet-style` for
a full inline stylesheet. Renames the experimental `stylesheet-inline` type to
`stylesheet-style` so the inline pair mirrors `<style>` / `style=`.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
@changeset-bot

changeset-bot Bot commented Jun 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 5486f05

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
webpack Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions

github-actions Bot commented Jun 10, 2026

Copy link
Copy Markdown
Contributor

This PR is packaged and the instant preview is available (bbb8c3f).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@bbb8c3f
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@bbb8c3f
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@bbb8c3f

@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 92.35%. Comparing base (7bf5a72) to head (5486f05).

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #21157      +/-   ##
==========================================
+ Coverage   92.34%   92.35%   +0.01%     
==========================================
  Files         581      581              
  Lines       63372    63402      +30     
  Branches    17522    17539      +17     
==========================================
+ Hits        58520    58558      +38     
+ Misses       4852     4844       -8     
Flag Coverage Δ
css-parsing 28.69% <93.33%> (+0.01%) ⬆️
html5lib 31.07% <10.00%> (-0.02%) ⬇️
integration 88.54% <100.00%> (+0.01%) ⬆️
test262 45.35% <13.33%> (-0.01%) ⬇️
unit 41.16% <50.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Annotate the visitor arrays and cast the `VisitorMap` / visitor params, matching
the strict test-suite typing introduced on main (#21147), so the two added
`as: "block-contents"` tests pass `tsc -p tsconfig.types.test.json`.

https://claude.ai/code/session_01RtuVwAXk4wSAPE85d8qadX
@codspeed-hq

codspeed-hq Bot commented Jun 10, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 30.47%

⚡ 1 improved benchmark
✅ 143 untouched benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Memory benchmark "asset-modules-bytes", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 321.4 KB 246.3 KB +30.47%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing feat/css-url-in-style-attributes (5486f05) with main (7bf5a72)

Open in CodSpeed

@github-actions

Copy link
Copy Markdown
Contributor

Types Coverage

Coverage after merging feat/css-url-in-style-attributes into main will be
99.33%
Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
bin
   webpack.js98.77%100%100%98.77%91
examples
   build-common.js100%100%100%100%
   buildAll.js100%100%100%100%
   examples.js100%100%100%100%
   template-common.js98.21%100%100%98.21%72
examples/custom-javascript-parser
   test.filter.js100%100%100%100%
examples/custom-javascript-parser/internals
   acorn-parse.js100%100%100%100%
   meriyah-parse.js100%100%100%100%
   oxc-parse.js91.30%100%100%91.30%140, 142–143, 145, 147, 153–154, 161, 168, 90
examples/markdown
   webpack.config.mjs100%100%100%100%
examples/typescript
   test.filter.js100%100%100%100%
examples/typescript-non-erasable
   test.filter.js50%100%100%50%5
examples/virtual-modules
   test.filter.js100%100%100%100%
examples/wasm-bindgen-esm
   test.filter.js100%100%100%100%
examples/wasm-complex
   test.filter.js100%100%100%100%
examples/wasm-simple
   test.filter.js100%100%100%100%
examples/wasm-simple-source-phase
   test.filter.js100%100%100%100%
lib
   APIPlugin.js100%100%100%100%
   AsyncDependenciesBlock.js100%100%100%100%
   AutomaticPrefetchPlugin.js100%100%100%100%
   BannerPlugin.js100%100%100%100%
   Cache.js98.21%100%100%98.21%101
   CacheFacade.js100%100%100%100%
   Chunk.js99.72%100%100%99.72%39
   ChunkGraph.js100%100%100%100%
   ChunkGroup.js100%100%100%100%
   ChunkTemplate.js100%100%100%100%
   CleanPlugin.js99.15%100%100%99.15%206, 226
   CodeGenerationResults.js100%100%100%100%
   CompatibilityPlugin.js100%100%100%100%
   Compilation.js98.44%100%100%98.44%1608, 1904, 1911, 1919, 1941, 2837, 3316–3317, 3349, 4013, 4043, 4096–4097, 4101, 4106, 4122–4123, 4137–4138, 4143–4144, 4621, 4647, 512, 517, 5455, 5487, 5504, 5520, 5536, 5551, 5576–5577, 5579, 5907, 5912, 5918, 5921, 5933, 5935, 5939, 5955, 5970, 6002, 6056, 6080, 6194, 762–763
   Compiler.js99.56%100%100%99.56%1135–1136, 1144
   ConcatenationScope.js98.59%100%100%98.59%189
   ConditionalInitFragment.js100%100%100%100%
   ConstPlugin.js100%100%100%100%
   ContextExclusionPlugin.js100%100%100%100%
   ContextModule.js100%100%100%100%
   ContextModuleFactory.js97.40%100%100%97.40%258, 395, 418, 420, 424, 433–434
   ContextReplacementPlugin.js100%100%100%100%
   DefinePlugin.js99%100%100%99%170–171, 187, 206, 280
   DependenciesBlock.js100%100%100%100%
   Dependency.js98.15%100%100%98.15%379, 425
   DependencyTemplate.js100%100%100%100%
   DependencyTemplates.js100%100%100%100%
   DotenvPlugin.js98.41%100%100%98.41%378, 391–392
   DynamicEntryPlugin.js100%100%100%100%
   EntryOptionPlugin.js100%100%100%100%
   EntryPlugin.js100%100%100%100%
   Entrypoint.js100%100%100%100%
   EnvironmentPlugin.js97.14%100%100%97.14%49
   ErrorHelpers.js100%100%100%100%
   EvalDevToolModulePlugin.js100%100%100%100%
   EvalSourceMapDevToolPlugin.js100%100%100%100%
   ExportsInfo.js100%100%100%100%
   ExportsInfoApiPlugin.js100%100%100%100%
   ExternalModule.js98.97%100%100%98.97%425–429, 577
   ExternalModuleFactoryPlugin.js100%100%100%100%
   ExternalsPlugin.js100%100%100%100%
   FileSystemInfo.js99.50%100%100%99.50%182, 2252–2253, 2256, 2267, 2278, 2289, 278, 3693, 3708, 3732
   FlagAllModulesAsUsedPlugin.js100%100%100%100%
   FlagDependencyExportsPlugin.js98.85%100%100%98.85%434, 436, 440
   FlagDependencyUsagePlugin.js100%100%100%100%
   FlagEntryExportAsUsedPlugin.js100%100%100%100%
   Generator.js100%100%100%100%
   HotModuleReplacementPlugin.js100%100%100%100%
   HotUpdateChunk.js100%100%100%100%
   IgnorePlugin.js100%100%100%100%
   IgnoreWarningsPlugin.js100%100%100%100%
   InitFragment.js100%100%100%100%
   JavascriptMetaInfoPlugin.js100%100%100%100%
   LibraryTemplatePlugin.js100%100%100%100%
   LoaderOptionsPlugin.js100%100%100%100%
   LoaderTargetPlugin.js100%100%100%100%
   MainTemplate.js100%100%100%100%
   ManifestPlugin.js100%100%100%100%
   Module.js98.50%100%100%98.50%1311, 1316, 1376, 1390, 1452, 1461
   ModuleFactory.js100%100%100%100%
   ModuleFilenameHelpers.js98.85%100%100%98.85%106, 108
   ModuleGraph.js99.73%100%100%99.73%1005
   ModuleGraphConnection.js100%100%100%100%
   ModuleInfoHeaderPlugin.js100%100%100%100%
   ModuleNotFoundError.js100%100%100%100%
   ModuleProfile.js100%100%100%100%
   ModuleSourceTypeConstants.js100%100%100%100%
   ModuleTemplate.js100%100%100%100%
   ModuleTypeConstants.js100%100%100%100%
   MultiCompiler.js99.69%100%100%99.69%659
   MultiStats.js100%100%100%100%
   MultiWatching.js100%100%100%100%
   NoEmitOnErrorsPlugin.js100%100%100%100%
   NodeStuffPlugin.js100%100%100%100%
   NormalModule.js97.90%100%100%97.90%1219, 1222, 1239, 1256, 1503, 1537, 1553, 1640, 1994, 2292, 2297–2307, 417, 421, 575
   NormalModuleFactory.js99.47%100%100%99.47%1083, 1392, 486, 498
   NormalModuleReplacementPlugin.js100%100%100%100%
   NullFactory.js100%100%100%100%
   OptimizationStages.js100%100%100%100%
   OptionsApply.js100%100%100%100%
   Parser.js100%100%100%100%
   PlatformPlugin.js100%100%100%100%
   PrefetchPlugin.js100%100%100%100%
   ProgressPlugin.js98.85%100%100%98.85%519–520, 525, 527, 591
   ProvidePlugin.js100%100%100%100%
   RawModule.js100%100%100%100%
   RecordIdsPlugin.js100%100%100%100%
   RequestShortener.js100%100%100%100%
   ResolverFactory.js100%100%100%100%
   RuntimeGlobals.js100%100%100%100%
   RuntimeModule.js100%100%100%100%
   RuntimePlugin.js100%100%100%100%
   RuntimeTemplate.js100%100%100%100%
   SelfModuleFactory.js100%100%100%100%
   SingleEntryPlugin.js100%100%100%100%
   SourceMapDevToolModuleOptionsPlugin.js100%100%100%100%
   SourceMapDevToolPlugin.js98.62%100%100%98.62%220, 224, 226, 419, 430, 891
   Stats.js100%100%100%100%
   Template.js100%100%100%100%
   TemplatedPathPlugin.js99.13%100%100%99.13%176–177
   UseStrictPlugin.js100%100%100%100%
   WarnCaseSensitiveModulesPlugin.js100%100%100%100%
   WarnDeprecatedOptionPlugin.js100%100%100%100%
   WarnNoModeSetPlugin.js100%100%100%100%
   WatchIgnorePlugin.js100%100%100%100%
   Watching.js100%100%100%100%
   WebpackError.js100%100%100%100%
   WebpackIsIncludedPlugin.js100%100%100%100%
   WebpackOptionsApply.js100%100%100%100%
   WebpackOptionsDefaulter.js100%100%100%100%
   buildChunkGraph.js99.87%100%100%99.87%326
   cli.js98.62%100%100%98.62%10, 119, 545, 577, 627, 897
   index.js99.72%100%100%99.72%165
   validateSchema.js94.67%100%100%94.67%100, 87, 89, 98
   webpack.js96.33%100%100%96.33%10, 198, 220, 222
lib/asset
   AssetBytesGenerator.js100%100%100%100%
   AssetBytesParser.js100%100%100%100%
   AssetGenerator.js100%100%100%100%
   AssetModulesPlugin.js97.32%100%100%97.32%283, 307, 310, 36, 362, 41
   AssetParser.js100%100%100%100%
   AssetSourceGenerator.js100%100%100%100%
   AssetSourceParser.js100%100%100%100%
   RawDataUrlModule.js100%100%100%100%
lib/async-modules
   AsyncModuleHelpers.js100%100%100%100%
   AwaitDependenciesInitFragment.js100%100%100%100%
   InferAsyncModulesPlugin.js100%100%100%100%
lib/cache
   AddBuildDependenciesPlugin.js100%100%100%100%
   AddManagedPathsPlugin.js100%100%100%100%
   IdleFileCachePlugin.js97.92%100%100%97.92%71, 83, 91
   MemoryCachePlugin.js95.83%100%100%95.83%33
   MemoryWithGcCachePlugin.js93.15%100%100%93.15%106, 113–114, 122, 89
   PackFileCacheStrategy.js96.40%100%100%96.40%1250, 1350, 1354, 1416, 628, 647, 657–659, 661, 677–678, 683, 686, 688, 693, 698, 722, 728, 762, 768, 774, 779, 790, 799, 804–805, 807, 824, 830–831, 833
   ResolverCachePlugin.js100%100%100%100%
   getLazyHashedEtag.js100%100%100%100%
   mergeEtags.js100%100%100%100%
lib/config
   browserslistTargetHandler.js100%100%100%100%
   defaults.js99.30%100%100%99.30%1439–1441, 1449, 274, 277, 282, 286
   normalization.js99.01%100%100%99.01%191–192, 258, 273
   target.js100%100%100%100%
lib/container
   ContainerEntryDependency.js100%100%100%100%
   ContainerEntryModule.js100%100%100%100%
   ContainerEntryModuleFactory.js100%100%100%100%
   ContainerExposedDependency.js100%100%100%100%
   ContainerPlugin.js100%100%100%100%
   

@alexander-akait alexander-akait merged commit bbb8c3f into main Jun 10, 2026
66 checks passed
@alexander-akait alexander-akait deleted the feat/css-url-in-style-attributes branch June 10, 2026 10:11
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.

1 participant