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

Commit ab31b27

Browse files
authored
Merge pull request #109 from rollup/gh-35
allow custom named exports to work with optimised modules
2 parents 90cebe1 + fe90220 commit ab31b27

File tree

6 files changed

+33
-16
lines changed

6 files changed

+33
-16
lines changed

src/transform.js

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
6969
const HELPERS_NAME = deconflict( scope, globals, 'commonjsHelpers' ); // TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯
7070

7171
const namedExports = {};
72-
if ( customNamedExports ) customNamedExports.forEach( name => namedExports[ name ] = true );
7372

7473
// TODO handle transpiled modules
7574
let shouldWrap = /__esModule/.test( code );
@@ -224,28 +223,32 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
224223
namedExportDeclarations.push( exportModuleExports );
225224
}
226225

226+
const name = getName( id );
227+
228+
function addExport ( x ) {
229+
let declaration;
230+
231+
if ( x === name ) {
232+
const deconflicted = deconflict( scope, globals, name );
233+
declaration = `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;
234+
} else {
235+
declaration = `export var ${x} = ${moduleName}.${x};`;
236+
}
237+
238+
namedExportDeclarations.push( declaration );
239+
}
240+
241+
if ( customNamedExports ) customNamedExports.forEach( addExport );
242+
227243
if ( shouldWrap ) {
228244
const args = `module${uses.exports ? ', exports' : ''}`;
229245

230-
const name = getName( id );
231-
232246
wrapperStart = `var ${moduleName} = ${HELPERS_NAME}.createCommonjsModule(function (${args}) {\n`;
233247
wrapperEnd = `\n});`;
234248

235249
Object.keys( namedExports )
236250
.filter( key => !blacklistedExports[ key ] )
237-
.forEach( x => {
238-
let declaration;
239-
240-
if ( x === name ) {
241-
const deconflicted = deconflict( scope, globals, name );
242-
declaration = `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;
243-
} else {
244-
declaration = `export var ${x} = ${moduleName}.${x};`;
245-
}
246-
247-
namedExportDeclarations.push( declaration );
248-
});
251+
.forEach( addExport );
249252
} else {
250253
let hasDefaultExport = false;
251254
const names = [];

test/function/reexports/_config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require( 'path' );
2+
3+
module.exports = {
4+
pluginOptions: {
5+
namedExports: {
6+
[ path.resolve( __dirname, 'foo.js' ) ]: [ 'named' ]
7+
}
8+
}
9+
};

test/function/reexports/bar.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.named = 42;

test/function/reexports/foo.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require( './bar.js' );

test/function/reexports/main.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { named } from './foo.js';
2+
3+
assert.equal( named, 42 );

test/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ describe( 'rollup-plugin-commonjs', () => {
8989
( config.solo ? it.only : it )( dir, () => {
9090
return rollup({
9191
entry: `function/${dir}/main.js`,
92-
plugins: [ commonjs() ]
92+
plugins: [ commonjs( config.pluginOptions ) ]
9393
}).then( bundle => {
9494
const { code } = bundle.generate({ format: 'cjs' });
9595
if ( config.show || config.solo ) {

0 commit comments

Comments
 (0)