Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: webpack / rspack load virtual module with '/' prefix on windows platform #422

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/rspack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,18 @@ export default async function load(this: LoaderContext, source: string, map: any
id = decodeVirtualModuleId(id, plugin)

const context = createContext(this)

id = normalizeAbsolutePath(id)
if (id.startsWith('\\'))
id = id.replace(/\\/g, '/')

const res = await plugin.load.call(
Object.assign(
{},
this._compilation && createBuildContext(this._compiler, this._compilation, this),
context,
),
normalizeAbsolutePath(id),
id,
)

if (res == null)
Expand Down
7 changes: 6 additions & 1 deletion src/webpack/loaders/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export default async function load(this: LoaderContext<{ unpluginName: string }>
id = decodeURIComponent(id.slice(plugin.__virtualModulePrefix.length))

const context = createContext(this)

id = normalizeAbsolutePath(id)
if (id.startsWith('\\'))
id = id.replace(/\\/g, '/')

const res = await plugin.load.call(
Object.assign({}, createBuildContext({
addWatchFile: (file) => {
Expand All @@ -25,7 +30,7 @@ export default async function load(this: LoaderContext<{ unpluginName: string }>
return this.getDependencies()
},
}, this._compiler!, this._compilation, this), context),
normalizeAbsolutePath(id),
id,
)

if (res == null)
Expand Down
8 changes: 4 additions & 4 deletions test/fixtures/virtual-module/unplugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ module.exports = createUnplugin(() => {
return {
name: 'virtual-module-fixture',
resolveId(id) {
return id.startsWith('virtual/') ? id : null
return id.startsWith('virtual/') ? `/__${id}` : null
tmg0 marked this conversation as resolved.
Show resolved Hide resolved
},
loadInclude(id) {
return id.startsWith('virtual/')
return id.startsWith('/__virtual/')
},
load(id) {
if (id === 'virtual/1')
if (id === '/__virtual/1')
return 'export default "VIRTUAL:ONE"'

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

else
Expand Down