From c2121228c95f9316076702681f4227cbff854abd Mon Sep 17 00:00:00 2001 From: Julien Elbaz Date: Mon, 5 May 2025 16:16:11 +0200 Subject: [PATCH 1/2] fix(rspack): prevent vfs concurrency issues --- src/rspack/index.ts | 12 +++++++++--- src/types.ts | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/rspack/index.ts b/src/rspack/index.ts index 662fac83..182fee39 100644 --- a/src/rspack/index.ts +++ b/src/rspack/index.ts @@ -56,7 +56,7 @@ export function getRspackPlugin>( if (plugin.resolveId) { const vfs = new FakeVirtualModulesPlugin(plugin) vfs.apply(compiler) - plugin.__vfsModules = new Set() + plugin.__vfsModules = new Map() plugin.__vfs = vfs as any compiler.hooks.compilation.tap(plugin.name, (compilation, { normalModuleFactory }) => { @@ -105,8 +105,14 @@ export function getRspackPlugin>( // we treat it as a virtual module if (!fs.existsSync(resolved)) { if (!plugin.__vfsModules!.has(resolved)) { - plugin.__vfsModules!.add(resolved) - await vfs.writeModule(resolved) + const fsPromise = vfs.writeModule(resolved) + plugin.__vfsModules!.set(resolved, fsPromise) + await fsPromise + } + else { + // Ensure that the module is written to the virtual file system + // before we use it. + await plugin.__vfsModules!.get(resolved) } resolved = encodeVirtualModuleId(resolved, plugin) } diff --git a/src/types.ts b/src/types.ts index b178dcaa..a66b5983 100644 --- a/src/types.ts +++ b/src/types.ts @@ -147,7 +147,7 @@ export interface UnpluginOptions { export interface ResolvedUnpluginOptions extends UnpluginOptions { // injected internal objects __vfs?: VirtualModulesPlugin - __vfsModules?: Set + __vfsModules?: Map> __virtualModulePrefix: string } From 0bd1252b43b3aeaa0d56fe44fe41bafae0228ccb Mon Sep 17 00:00:00 2001 From: Julien Elbaz Date: Mon, 5 May 2025 22:43:50 +0200 Subject: [PATCH 2/2] chore: typecheck --- src/webpack/index.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/webpack/index.ts b/src/webpack/index.ts index 0454276c..14082aa5 100644 --- a/src/webpack/index.ts +++ b/src/webpack/index.ts @@ -54,7 +54,7 @@ export function getWebpackPlugin>( vfs = new VirtualModulesPlugin() compiler.options.plugins.push(vfs) } - plugin.__vfsModules = new Set() + plugin.__vfsModules = new Map() plugin.__vfs = vfs const resolverPlugin: ResolvePluginInstance = { @@ -135,7 +135,7 @@ export function getWebpackPlugin>( // https://github.com/unjs/unplugin/pull/155 if (!plugin.__vfsModules!.has(resolved)) { plugin.__vfs!.writeModule(resolved, '') - plugin.__vfsModules!.add(resolved) + plugin.__vfsModules!.set(resolved, Promise.resolve('')) } }