Skip to content

Virtual modules breaks [path] in generator filename #20421

@neoncube2

Description

@neoncube2

Bug Description

webpack.config.mjs

module: {
  rules: [
    {
      test: /\.(svg)$/i,
      type: 'asset/resource',
      generator: {
        filename: '[path][name]-[contenthash][ext]'
      }
    }
  ],
  plugins: [
    new webpack.experiments.schemes.VirtualUrlPlugin(
      {
        'logo.svg': {
          type: '.svg',
          source: () => `<?xml version="1.0" encoding="iso-8859-1"?><svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve"></svg>`
        }
      }
    )
  ]
}

index.js

import Hammer from 'virtual:hammer.svg';

It looks like using [path] in the generator filename doesn't play nicely with the virtual: schema.

After compiling, instead of a hammer.svg file being generated in my dist folder, just a 0 byte filed named virtual gets generated:

Image

Link to Minimal Reproduction and step to reproduce

https://github.com/neoncube2/webpack-virtual-modules-svg-file

Expected Behavior

Compiling emits a hammer.svg file

Actual Behavior

Compiling emits a 0 byte file named virtual

Environment

System:
  OS: Windows 11 10.0.26200
  CPU: (12) x64 Intel(R) Core(TM) i7-10710U CPU @ 1.10GHz
  Memory: 12.95 GB / 31.84 GB
Binaries:
  Node: 25.2.1 - C:\Users\elibl\AppData\Local\Temp\xfs-3ea1ce4a\node.CMD
  Yarn: 4.12.0 - C:\Users\elibl\AppData\Local\Temp\xfs-3ea1ce4a\yarn.CMD
  npm: 11.1.0 - C:\Program Files\nodejs\npm.CMD
Browsers:
  Chrome: 144.0.7559.133
  Edge: Chromium (140.0.3485.54)
  Internet Explorer: 11.0.26100.7309

Is this a regression?

None

Last Working Version

No response

Additional Context

No response

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions