unjs / unplugin

Unified plugin system for Vite, Rollup, Webpack, esbuild, Rolldown, and more
https://unplugin.unjs.io
MIT License
3.09k stars 114 forks source link

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

Open tmg0 opened 1 month ago

tmg0 commented 1 month ago

Environment

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
tmg0 commented 1 month ago

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)