diff --git a/src/Chunk.ts b/src/Chunk.ts index 11e6e3035dd..bb7fdee3484 100644 --- a/src/Chunk.ts +++ b/src/Chunk.ts @@ -464,7 +464,7 @@ export default class Chunk { }); const currentPath = `${currentDir}/${fileName}`; const { preserveModulesRoot } = options; - if (preserveModulesRoot && currentPath.startsWith(preserveModulesRoot)) { + if (preserveModulesRoot && resolve(currentPath).startsWith(preserveModulesRoot)) { path = currentPath.slice(preserveModulesRoot.length).replace(/^[\\/]/, ''); } else { path = relative(preserveModulesRelativeDir, currentPath); diff --git a/test/chunking-form/samples/preserve-modules-root/_config.js b/test/chunking-form/samples/preserve-modules-root/_config.js index 61ff285d344..5518c9c5483 100644 --- a/test/chunking-form/samples/preserve-modules-root/_config.js +++ b/test/chunking-form/samples/preserve-modules-root/_config.js @@ -7,6 +7,14 @@ module.exports = { options: { input: ['src/under-build.js', 'src/below/module.js'], plugins: [ + { + name: 'convert-slashes', + // This simulates converted slashes as used by e.g. Vite + async resolveId(source, importer, options) { + const resolved = await this.resolve(source, importer, { ...options, skipSelf: true }); + return { ...resolved, id: resolved.id.replace(/\\/g, '/') }; + } + }, resolve({ moduleDirectories: ['custom_modules'] }),