Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #208 from pshrmn/rename-index
Browse files Browse the repository at this point in the history
Prefer parent directory over index for module name
  • Loading branch information
Rich-Harris authored Aug 22, 2017
2 parents 7467758 + dba3783 commit 81fdd71
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { basename, extname } from 'path';
import { basename, extname, dirname, sep } from 'path';
import { makeLegalIdentifier } from 'rollup-pluginutils';

export function getName ( id ) {
return makeLegalIdentifier( basename( id, extname( id ) ) );
const name = makeLegalIdentifier( basename( id, extname( id ) ) );
if (name !== 'index') {
return name;
} else {
const segments = dirname( id ).split( sep );
return makeLegalIdentifier( segments[segments.length - 1] );
}
}

1 change: 1 addition & 0 deletions test/samples/rename-index/invalid-var/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'invalid';
5 changes: 5 additions & 0 deletions test/samples/rename-index/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import invalid from './invalid-var';
import valid from './validVar';
import other from './other/nonIndex';

console.log(invalid, valid, other);
1 change: 1 addition & 0 deletions test/samples/rename-index/other/nonIndex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'not an index file';
1 change: 1 addition & 0 deletions test/samples/rename-index/validVar/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 'valid';
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,19 @@ describe( 'rollup-plugin-commonjs', () => {
const { exports } = await executeBundle( bundle );
assert.equal( exports, 'HELLO' );
});

it( 'prefers to set name using directory for index files', () => {
return rollup({
entry: 'samples/rename-index/main.js',
plugins: [ commonjs() ]
})
.then( async bundle => {
const { code } = await bundle.generate({ format: 'cjs' });
assert.equal( code.indexOf( 'var index' ), -1 );
assert.notEqual( code.indexOf( 'var invalidVar' ), -1 );
assert.notEqual( code.indexOf( 'var validVar' ), -1 );
assert.notEqual( code.indexOf( 'var nonIndex' ), -1 );
});
});
});
});

0 comments on commit 81fdd71

Please sign in to comment.