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 #175 from rollup/gh-174
Browse files Browse the repository at this point in the history
allow require statements to pass through unmolested
  • Loading branch information
Rich-Harris committed Mar 7, 2017
2 parents c4e197e + 3b707c8 commit 6095a26
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 10 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,13 @@ export default {

// explicitly specify unresolvable named exports
// (see below for more details)
namedExports: { './module.js': ['foo', 'bar' ] } // Default: undefined
namedExports: { './module.js': ['foo', 'bar' ] }, // Default: undefined

// sometimes you have to leave require statements
// unconverted. Pass an array containing the IDs
// or a `id => boolean` function. Only use this
// option if you know what you're doing!
ignore: [ 'conditional-runtime-dependency' ]
})
]
};
Expand Down
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ export default function commonjs ( options = {} ) {
});
}

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

const ignoreRequire = typeof options.ignore === 'function' ?
options.ignore :
Array.isArray( options.ignore ) ? id => ~options.ignore.indexOf( id ) :
() => false;

let entryModuleIdPromise = null;
let entryModuleId = null;

Expand Down Expand Up @@ -150,7 +157,7 @@ export default function commonjs ( options = {} ) {
if ( extensions.indexOf( extname( id ) ) === -1 ) return null;

return entryModuleIdPromise.then( () => {
const transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, customNamedExports[ id ], sourceMap );
const transformed = transformCommonjs( code, id, id === entryModuleId, ignoreGlobal, ignoreRequire, customNamedExports[ id ], sourceMap, allowDynamicRequire );

if ( transformed ) {
commonjsModules.set( id, true );
Expand Down
12 changes: 7 additions & 5 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ function tryParse ( code, id ) {
}
}

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

Expand Down Expand Up @@ -140,15 +140,16 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
if ( node.type === 'Identifier' ) {
if ( isReference( node, parent ) && !scope.contains( node.name ) ) {
if ( node.name in uses ) {
if ( node.name === 'require' ) {
if ( allowDynamicRequire ) return;
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, true );
}

uses[ node.name ] = true;
if ( node.name === 'global' && !ignoreGlobal ) {
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, true );
}

if ( node.name === 'require' ) {
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsRequire`, true );
}

// if module or exports are used outside the context of an assignment
// expression, we need to wrap the module
if ( node.name === 'module' || node.name === 'exports' ) {
Expand All @@ -175,6 +176,7 @@ export default function transformCommonjs ( code, id, isEntry, ignoreGlobal, cus
if ( node.type !== 'CallExpression' ) return;
if ( node.callee.name !== 'require' || scope.contains( 'require' ) ) return;
if ( node.arguments.length !== 1 || node.arguments[0].type !== 'Literal' ) return; // TODO handle these weird cases?
if ( ignoreRequire( node.arguments[0].value ) ) return;

const source = node.arguments[0].value;

Expand Down
5 changes: 5 additions & 0 deletions test/form/ignore-ids-function/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
ignore: id => id === 'foo'
}
};
2 changes: 2 additions & 0 deletions test/form/ignore-ids-function/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var foo = require( 'foo' );
var bar = require( 'bar' );
12 changes: 12 additions & 0 deletions test/form/ignore-ids-function/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'bar';
import require$$0 from 'commonjs-proxy:bar';

var foo = require( 'foo' );
var bar = require$$0;

var input = {

};

export default input;
export { input as __moduleExports };
5 changes: 5 additions & 0 deletions test/form/ignore-ids/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
options: {
ignore: [ 'foo' ]
}
};
2 changes: 2 additions & 0 deletions test/form/ignore-ids/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
var foo = require( 'foo' );
var bar = require( 'bar' );
12 changes: 12 additions & 0 deletions test/form/ignore-ids/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'bar';
import require$$0 from 'commonjs-proxy:bar';

var foo = require( 'foo' );
var bar = require$$0;

var input = {

};

export default input;
export { input as __moduleExports };
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ function executeBundle ( bundle, { context, exports } = {} ) {

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

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

Expand All @@ -69,6 +66,9 @@ describe( 'rollup-plugin-commonjs', () => {
}

( config.solo ? it.only : it )( dir, () => {
const { transform, options } = commonjs( config.options );
options({ entry: 'main.js' });

const input = fs.readFileSync( `form/${dir}/input.js`, 'utf-8' );
const expected = fs.readFileSync( `form/${dir}/output.js`, 'utf-8' ).trim();

Expand Down

0 comments on commit 6095a26

Please sign in to comment.