Skip to content

Commit f408497

Browse files
authored
fix(commonjs)!: Correctly infer module name for any separator (#924)
* test(commonjs): Add test for different path separators Add test for module IDs that use a path separator that is different from `path.sep`. Previously, when a module whose file name is `index.js` was resolved to a module ID containing different path separators, the commonjs plugin generated a variable name containing the module's absolute path. * fix(commonjs)!: Correctly infer module name for any separator BREAKING CHANGES: Correctly infer the module name from the module ID, regardless of the path separator used in the module ID and the value of `path.sep`. This generates variable names like `react` instead of `C__testViteApp_node_modules_react` when a module ID of `C:/test-vite-app/node_modules/react/index.js` is given on Windows.
1 parent 31aad90 commit f408497

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

packages/commonjs/src/utils.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable import/prefer-default-export */
22

3-
import { basename, dirname, extname, sep } from 'path';
3+
import { basename, dirname, extname } from 'path';
44

55
import { makeLegalIdentifier } from '@rollup/pluginutils';
66

@@ -27,8 +27,7 @@ export function getName(id) {
2727
if (name !== 'index') {
2828
return name;
2929
}
30-
const segments = dirname(id).split(sep);
31-
return makeLegalIdentifier(segments[segments.length - 1]);
30+
return makeLegalIdentifier(basename(dirname(id)));
3231
}
3332

3433
export function normalizePathSlashes(path) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports.a = 1;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const foo = require("./foo");
2+
console.log(foo.a);

packages/commonjs/test/test.js

+29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* eslint-disable line-comment-position, no-new-func, no-undefined */
22
import * as path from 'path';
3+
import os from 'os';
34

45
import resolve from '@rollup/plugin-node-resolve';
56

@@ -728,3 +729,31 @@ test('does not affect subsequently created instances when called with `requireRe
728729

729730
t.is(code1, code2);
730731
});
732+
733+
// This test works only on Windows, which treats both forward and backward
734+
// slashes as path separators
735+
if (os.platform() === 'win32') {
736+
test('supports both forward and backward slash as path separator in directory-based modules', async (t) => {
737+
const bundle = await rollup({
738+
input: 'fixtures/samples/module-path-separator/main.js',
739+
plugins: [
740+
// Ad-hoc plugin that reverses the path separator of foo/index.js
741+
{
742+
name: 'test-path-separator-reverser',
743+
async resolveId(source, importer) {
744+
if (source.endsWith('foo')) {
745+
const fullPath = path.resolve(path.dirname(importer), source, 'index.js');
746+
// Ensure that the module ID uses a non-default path separator
747+
return fullPath.replace(/[\\/]/g, (sep) => (sep === '/' ? '\\' : '/'));
748+
}
749+
return null;
750+
}
751+
},
752+
commonjs()
753+
]
754+
});
755+
756+
const code = await getCodeFromBundle(bundle);
757+
t.regex(code, /\bfoo = {}/);
758+
});
759+
}

0 commit comments

Comments
 (0)