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 #96 from rollup/unambiguous
Browse files Browse the repository at this point in the history
Don't transform modules with import/export
  • Loading branch information
Rich-Harris authored Sep 16, 2016
2 parents 259973e + 5d6d40f commit 4fee437
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/

const firstpassGlobal = /\b(?:require|module|exports|global)\b/;
const firstpassNoGlobal = /\b(?:require|module|exports)\b/;
const importExportDeclaration = /^(?:Import|Export(?:Named|Default))Declaration/;

function deconflict ( identifier, code ) {
let i = 1;
Expand Down Expand Up @@ -43,6 +44,12 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
if ( customNamedExports ) customNamedExports.forEach( name => namedExports[ name ] = true );

const ast = tryParse( code, id );

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

const magicString = new MagicString( code );

const required = {};
Expand Down
3 changes: 3 additions & 0 deletions test/form/unambiguous-with-default-export/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require( './foo.js' );

export default {};
3 changes: 3 additions & 0 deletions test/form/unambiguous-with-default-export/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require( './foo.js' );

export default {};
3 changes: 3 additions & 0 deletions test/form/unambiguous-with-import/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require( './foo.js' );

import './bar.js';
3 changes: 3 additions & 0 deletions test/form/unambiguous-with-import/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require( './foo.js' );

import './bar.js';
3 changes: 3 additions & 0 deletions test/form/unambiguous-with-named-export/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require( './foo.js' );

export {};
3 changes: 3 additions & 0 deletions test/form/unambiguous-with-named-export/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require( './foo.js' );

export {};
24 changes: 24 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ function executeBundle ( bundle ) {
}

describe( 'rollup-plugin-commonjs', () => {
describe( 'form', () => {
const { transform, options } = commonjs();
options({ entry: 'main.js' });

fs.readdirSync( 'form' ).forEach( dir => {
let config;

try {
config = require( `./form/${dir}/_config.js` );
} catch ( err ) {
config = {};
}

( config.solo ? it.only : it )( dir, () => {
const input = fs.readFileSync( `form/${dir}/input.js`, 'utf-8' );
const expected = fs.readFileSync( `form/${dir}/output.js`, 'utf-8' );

return transform( input, 'input.js' ).then( transformed => {
assert.equal( transformed ? transformed.code : input, expected );
});
});
});
});

describe( 'function', () => {
fs.readdirSync( 'function' ).forEach( dir => {
let config;
Expand Down

0 comments on commit 4fee437

Please sign in to comment.