Skip to content

Commit

Permalink
more nuanced check for wrapping (rollup#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Mar 15, 2016
1 parent a460544 commit cfa3193
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/index.js
Expand Up @@ -101,7 +101,7 @@ export default function commonjs ( options = {} ) {

let scope = attachScopes( ast, 'scope' );
let uses = { module: false, exports: false, global: false };

let usesRequire = false;
let namedExports = {};
if ( customNamedExports[ id ] ) {
customNamedExports[ id ].forEach( name => namedExports[ name ] = true );
Expand Down Expand Up @@ -145,7 +145,13 @@ export default function commonjs ( options = {} ) {
}

if ( node.type === 'Identifier' ) {
if ( ( node.name in uses && !uses[ node.name ] ) && isReference( node, parent ) && !scope.contains( node.name ) ) uses[ node.name ] = true;
if ( ( node.name in uses && !uses[ node.name ] ) && isReference( node, parent ) && !scope.contains( node.name ) ) {
if (parent && (parent.operator === 'typeof' || parent.type === 'ConditionalExpression')) {
return;
} else {
uses[ node.name ] = true;
}
}
return;
}

Expand All @@ -158,7 +164,7 @@ export default function commonjs ( options = {} ) {
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?

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

let existing = required[ source ];
Expand Down Expand Up @@ -187,8 +193,11 @@ export default function commonjs ( options = {} ) {
});

const sources = Object.keys( required );
if (options.ignoreGlobal) {
uses.global = false;
}

if ( !sources.length && !uses.module && !uses.exports && !uses.global ) {
if ( !sources.length && !uses.module && !uses.exports && !uses.global && !usesRequire) {
if ( Object.keys( namedExports ).length ) {
throw new Error( `Custom named exports were specified for ${id} but it does not appear to be a CommonJS module` );
}
Expand Down
10 changes: 10 additions & 0 deletions test/samples/references-require/encoder.js
@@ -0,0 +1,10 @@
var _foo;
(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
(function (global){
module.exports = function (data) {
return global.encodeURIComponent(data);
};
_foo = module.exports;
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{}]},{},[1]);
export default _foo;
5 changes: 5 additions & 0 deletions test/samples/references-require/main.js
@@ -0,0 +1,5 @@
import encoder from './encoder';

export function encode(data) {
return encoder(data);
}
20 changes: 20 additions & 0 deletions test/test.js
Expand Up @@ -254,4 +254,24 @@ describe( 'rollup-plugin-commonjs', () => {
assert.equal( global.setImmediate, mod.immediate, generated.code );
});
});

it( 'can handle references to `require`', () => {
return rollup({
entry: 'samples/references-require/main.js',
plugins: [ commonjs() ]
}).then( bundle => {
const generated = bundle.generate({
format: 'cjs'
});
var exp = {};
let mod = {
exports: exp
};

const fn = new Function ( 'module', 'exports', generated.code );
fn( mod, exp );

assert.equal( exp.encode('///'), '%2F%2F%2F', generated.code );
});
});
});

0 comments on commit cfa3193

Please sign in to comment.