Skip to content

Commit

Permalink
fix: get the right chunk name for preserve modules in Windows (#5625)
Browse files Browse the repository at this point in the history
* add test

* remove Windows drive prefix

---------

Co-authored-by: Lukas Taegert-Atkinson <[email protected]>
  • Loading branch information
TrickyPi and lukastaegert authored Aug 26, 2024
1 parent eb07813 commit fadebcd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1069,9 +1069,15 @@ export default class Chunk {
? sanitizedId.slice(0, -extensionName.length)
: sanitizedId;
if (isAbsolute(idWithoutExtension)) {
return preserveModulesRoot && resolve(idWithoutExtension).startsWith(preserveModulesRoot)
? idWithoutExtension.slice(preserveModulesRoot.length).replace(/^[/\\]/, '')
: relative(this.inputBase, idWithoutExtension);
if (preserveModulesRoot && resolve(idWithoutExtension).startsWith(preserveModulesRoot)) {
return idWithoutExtension.slice(preserveModulesRoot.length).replace(/^[/\\]/, '');
} else {
// handle edge case in Windows
if (this.inputBase === '/' && !idWithoutExtension.startsWith('/')) {
return relative(this.inputBase, idWithoutExtension.replace(/^[a-zA-Z]:[/\\]/, '/'));
}
return relative(this.inputBase, idWithoutExtension);
}
} else {
return (
this.outputOptions.virtualDirname.replace(/\/$/, '') + '/' + basename(idWithoutExtension)
Expand Down
11 changes: 11 additions & 0 deletions test/cli/samples/get-chunk-name/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const assert = require('node:assert');
module.exports = defineTest({
description: 'get right chunk name for preserve modules',
command: 'rollup -c',
result(code) {
assert.ok(
code.includes(`//→ ${__dirname.replace(/^(\/|[a-zA-Z]:\\)/, '').replace(/\\/g, '/')}/main.js`)
);
assert.ok(code.includes(`//→ sub.js`));
}
});
2 changes: 2 additions & 0 deletions test/cli/samples/get-chunk-name/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import sub from '/sub.js';
assert.ok(sub);
21 changes: 21 additions & 0 deletions test/cli/samples/get-chunk-name/rollup.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
input: 'main.js',
output: {
format: 'es',
preserveModules: true
},
plugins: [
{
resolveId(id) {
if (id.endsWith('sub.js')) {
return id;
}
},
load(id) {
if (id.endsWith('sub.js')) {
return 'export default "sub.js url"';
}
}
}
]
};

0 comments on commit fadebcd

Please sign in to comment.