Skip to content

webpack / rspack load virtual module id with / prefix on windows caused error #421

Open

Description

@tmg0

Environment

  • platform: windows 10
  • nodejs: 18.19.1
  • package manager: pnpm@9.11.0

Reproduction

https://github.com/tmg0/unplugin

Describe the bug

When I try to load a virtual module with / prefix on windows, id path has been converted from / to \, and this issue caused unplugin-vue-router doesn't work with rspack and webpack

I have forked this repo and modify the test file in virtual-module

// test\fixtures\virtual-module\unplugin.js
const { createUnplugin } = require('unplugin')

module.exports = createUnplugin(() => {
  return {
    name: 'virtual-module-fixture',
    resolveId(id) {
      return id.startsWith('virtual/') ? `/__${id}` : null
    },
    loadInclude(id) {
      return id.startsWith('/__virtual/')
    },
    load(id) {
      if (id === '/__virtual/1')
        return 'export default "VIRTUAL:ONE"'

      else if (id === '/__virtual/2')
        return 'export default "VIRTUAL:TWO"'

      else
        throw new Error(`Unexpected id: ${id}`)
    },
  }
})

Additional context

No response

Logs

throw new Error(`Unexpected id: ${id}`)
              ^

Error: Unexpected id: \__virtual\1
    at Object.load (\unplugin\test\fixtures\virtual-module\unplugin.js:20:15)
    at Object.load (\unplugin\dist\rspack\loaders\load.js:115:33)
    at LOADER_EXECUTION (\unplugin\node_modules\.pnpm\@rspack+core@1.0.0-rc.1_@swc+helpers@0.5.12\node_modules\@rspack\core\dist\loader-runner\index.js:213:23)
    at runSyncOrAsync (\unplugin\node_modules\.pnpm\@rspack+core@1.0.0-rc.1_@swc+helpers@0.5.12\node_modules\@rspack\core\dist\loader-runner\index.js:214:11)
    at node:internal/util:441:7
    at new Promise (<anonymous>)
    at runSyncOrAsync (node:internal/util:427:12)
    at runLoaders (\unplugin\node_modules\.pnpm\@rspack+core@1.0.0-rc.1_@swc+helpers@0.5.12\node_modules\@rspack\core\dist\loader-runner\index.js:695:28)

Node.js v18.19.1
node:internal/errors:865
  const err = new Error(message);
              ^

Error: Command failed: npx rspack
    at checkExecSyncError (node:child_process:890:11)
    at execSync (node:child_process:962:15)
    at run (\unplugin\scripts\buildFixtures.ts:37:33) {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 9396,
  stdout: null,
  stderr: null
}

Node.js v18.19.1

Activity

  1. added
    bugSomething isn't working
    on Sep 23, 2024
  2. tmg0 commented on Sep 25, 2024

    @tmg0
    Author

    I have created a demo for this issue, it works well on stackblitz and unix like platform:

    /__virtual/1
    /__virtual/2
    asset main.js 5.11 KiB [compared for emit] (name: main)
    runtime modules 670 bytes 3 modules
    cacheable modules 139 bytes
      ./src/index.js 83 bytes [built] [code generated]
      ./_virtual_%2F__virtual%2F1 28 bytes [built] [code generated]
      ./_virtual_%2F__virtual%2F2 28 bytes [built] [code generated]
    webpack 5.94.0 compiled successfully in 305 ms

    But on windows, it was broken:

    \__virtual\1
    \__virtual\2
    C:\webpack-webpack-js-org-fy87xb\plugin\virtual.js:20
            throw new Error(`Unexpected id: ${id}`)
                  ^
    
    Error: Unexpected id: \__virtual\1
        at Object.load (C:\webpack-webpack-js-org-fy87xb\plugin\virtual.js:20:15)
        at Object.load (C:\webpack-webpack-js-org-fy87xb\node_modules\.pnpm\unplugin-doctor@0.1.1\node_modules\unplugin-doctor\dist\index.cjs:5:524)
        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
        at async Object.load (C:\webpack-webpack-js-org-fy87xb\node_modules\.pnpm\unplugin@1.14.1\node_modules\unplugin\dist\webpack\loaders\load.js:130:15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingwebpack

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions