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

Commit

Permalink
allow custom named exports to work with optimised modules (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 17, 2016
1 parent 90cebe1 commit fe90220
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 16 deletions.
33 changes: 18 additions & 15 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
const HELPERS_NAME = deconflict( scope, globals, 'commonjsHelpers' ); // TODO technically wrong since globals isn't populated yet, but ¯\_(ツ)_/¯

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

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

const name = getName( id );

function addExport ( x ) {
let declaration;

if ( x === name ) {
const deconflicted = deconflict( scope, globals, name );
declaration = `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;
} else {
declaration = `export var ${x} = ${moduleName}.${x};`;
}

namedExportDeclarations.push( declaration );
}

if ( customNamedExports ) customNamedExports.forEach( addExport );

if ( shouldWrap ) {
const args = `module${uses.exports ? ', exports' : ''}`;

const name = getName( id );

wrapperStart = `var ${moduleName} = ${HELPERS_NAME}.createCommonjsModule(function (${args}) {\n`;
wrapperEnd = `\n});`;

Object.keys( namedExports )
.filter( key => !blacklistedExports[ key ] )
.forEach( x => {
let declaration;

if ( x === name ) {
const deconflicted = deconflict( scope, globals, name );
declaration = `var ${deconflicted} = ${moduleName}.${x};\nexport { ${deconflicted} as ${x} };`;
} else {
declaration = `export var ${x} = ${moduleName}.${x};`;
}

namedExportDeclarations.push( declaration );
});
.forEach( addExport );
} else {
let hasDefaultExport = false;
const names = [];
Expand Down
9 changes: 9 additions & 0 deletions test/function/reexports/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const path = require( 'path' );

module.exports = {
pluginOptions: {
namedExports: {
[ path.resolve( __dirname, 'foo.js' ) ]: [ 'named' ]
}
}
};
1 change: 1 addition & 0 deletions test/function/reexports/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
exports.named = 42;
1 change: 1 addition & 0 deletions test/function/reexports/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require( './bar.js' );
3 changes: 3 additions & 0 deletions test/function/reexports/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { named } from './foo.js';

assert.equal( named, 42 );
2 changes: 1 addition & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ describe( 'rollup-plugin-commonjs', () => {
( config.solo ? it.only : it )( dir, () => {
return rollup({
entry: `function/${dir}/main.js`,
plugins: [ commonjs() ]
plugins: [ commonjs( config.pluginOptions ) ]
}).then( bundle => {
const { code } = bundle.generate({ format: 'cjs' });
if ( config.show || config.solo ) {
Expand Down

0 comments on commit fe90220

Please sign in to comment.