Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 2 additions & 3 deletions packages/commonjs/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable import/prefer-default-export */

import { basename, dirname, extname, sep } from 'path';
import { basename, dirname, extname } from 'path';

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

Expand All @@ -27,8 +27,7 @@ export function getName(id) {
if (name !== 'index') {
return name;
}
const segments = dirname(id).split(sep);
return makeLegalIdentifier(segments[segments.length - 1]);
return makeLegalIdentifier(basename(dirname(id)));
}

export function normalizePathSlashes(path) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports.a = 1;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const foo = require("./foo");
console.log(foo.a);
29 changes: 29 additions & 0 deletions packages/commonjs/test/test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable line-comment-position, no-new-func, no-undefined */
import * as path from 'path';
import os from 'os';

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

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

t.is(code1, code2);
});

// This test works only on Windows, which treats both forward and backward
// slashes as path separators
if (os.platform() === 'win32') {
test('supports both forward and backward slash as path separator in directory-based modules', async (t) => {
const bundle = await rollup({
input: 'fixtures/samples/module-path-separator/main.js',
plugins: [
// Ad-hoc plugin that reverses the path separator of foo/index.js
{
name: 'test-path-separator-reverser',
async resolveId(source, importer) {
if (source.endsWith('foo')) {
const fullPath = path.resolve(path.dirname(importer), source, 'index.js');
// Ensure that the module ID uses a non-default path separator
return fullPath.replace(/[\\/]/g, (sep) => (sep === '/' ? '\\' : '/'));
}
return null;
}
},
commonjs()
]
});

const code = await getCodeFromBundle(bundle);
t.regex(code, /\bfoo = {}/);
});
}