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 #243 from otolab/feature/1408
Browse files Browse the repository at this point in the history
add esModulesWithoutDefaultExport check
  • Loading branch information
Rich-Harris committed Oct 19, 2017
2 parents e810757 + 06e9122 commit 4152aa9
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 13 deletions.
33 changes: 25 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { sync as nodeResolveSync } from 'resolve';
import { createFilter } from 'rollup-pluginutils';
import { EXTERNAL, PREFIX, HELPERS_ID, HELPERS } from './helpers.js';
import defaultResolver from './defaultResolver.js';
import transformCommonjs from './transform.js';
import { checkFirstpass, checkEsModule, transformCommonjs } from './transform.js';
import { getName } from './utils.js';

function getCandidatesForExtension ( resolved, extension ) {
Expand Down Expand Up @@ -58,6 +58,8 @@ export default function commonjs ( options = {} ) {
});
}

const esModulesWithoutDefaultExport = [];

const allowDynamicRequire = !!options.ignore; // TODO maybe this should be configurable?

const ignoreRequire = typeof options.ignore === 'function' ?
Expand Down Expand Up @@ -153,9 +155,12 @@ export default function commonjs ( options = {} ) {
const actualId = id.slice( PREFIX.length );
const name = getName( actualId );

return commonjsModules.has( actualId ) ?
`import { __moduleExports } from ${JSON.stringify( actualId )}; export default __moduleExports;` :
`import * as ${name} from ${JSON.stringify( actualId )}; export default ( ${name} && ${name}['default'] ) || ${name};`;
if (commonjsModules.has( actualId ))
return `import { __moduleExports } from ${JSON.stringify( actualId )}; export default __moduleExports;`;
else if (esModulesWithoutDefaultExport.includes(actualId))
return `import * as ${name} from ${JSON.stringify( actualId )}; export default ${name};`;
else
return `import * as ${name} from ${JSON.stringify( actualId )}; export default ( ${name} && ${name}['default'] ) || ${name};`;
}
},

Expand All @@ -164,12 +169,24 @@ export default function commonjs ( options = {} ) {
if ( extensions.indexOf( extname( id ) ) === -1 ) return null;

return entryModuleIdPromise.then( () => {
const transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire );
const {isEsModule, hasDefaultExport, ast} = checkEsModule( code, id );
if ( isEsModule ) {
if ( !hasDefaultExport )
esModulesWithoutDefaultExport.push( id );
return;
}

if ( transformed ) {
commonjsModules.set( id, true );
return transformed;
// it is not an ES module but not a commonjs module, too.
if ( !checkFirstpass( code, ignoreGlobal ) ) {
esModulesWithoutDefaultExport.push( id );
return;
}

const transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire, ast );
if ( !transformed ) return;

commonjsModules.set( id, true );
return transformed;
});
}
};
Expand Down
21 changes: 17 additions & 4 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,30 @@ function tryParse ( code, id ) {
}
}

export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire ) {
export function checkFirstpass (code, ignoreGlobal) {
const firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal;
if ( !firstpass.test( code ) ) return null;
return firstpass.test(code);
}

const ast = tryParse( code, id );
export function checkEsModule (code, id) {
const ast = tryParse(code, id);

// if there are top-level import/export declarations, this is ES not CommonJS
let hasDefaultExport = false;
let isEsModule = false;
for ( const node of ast.body ) {
if ( importExportDeclaration.test( node.type ) ) return null;
if ( node.type === 'ExportDefaultDeclaration' )
hasDefaultExport = true;
if ( importExportDeclaration.test( node.type ) )
isEsModule = true;
}

return { isEsModule, hasDefaultExport, ast };
}

export function transformCommonjs ( code, id, isEntry, ignoreGlobal, ignoreRequire, customNamedExports, sourceMap, allowDynamicRequire, astCache ) {
const ast = astCache || tryParse( code, id );

const magicString = new MagicString( code );

const required = {};
Expand Down
3 changes: 3 additions & 0 deletions test/samples/es-modules-without-default-export/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { a } = require('./other.js');

assert.equal( a, 1 );
1 change: 1 addition & 0 deletions test/samples/es-modules-without-default-export/other.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const a = 1;
19 changes: 18 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -432,5 +432,22 @@ describe( 'rollup-plugin-commonjs', () => {
await executeBundle( bundle, { context: { window } } );
assert.notEqual( window.b.default, undefined );
});

it( 'does not warn even if the ES module not export "default"', async () => {
const warns = [];
await rollup({
input: 'samples/es-modules-without-default-export/main.js',
plugins: [ commonjs() ],
onwarn: (warn) => warns.push( warn )
});
assert.equal( warns.length, 0 );

await rollup({
input: 'function/bare-import/bar.js',
plugins: [ commonjs() ],
onwarn: (warn) => warns.push( warn )
});
assert.equal( warns.length, 0 );
});
});
});
});

0 comments on commit 4152aa9

Please sign in to comment.