Skip to content

Commit

Permalink
ability to ignore global variable (rollup#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
calvinmetcalf committed Mar 14, 2016
1 parent 3b22781 commit f9e81b0
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ rollup({

// search for files other than .js files (must already
// be transpiled by a previous plugin!)
extensions: [ '.js', '.coffee' ] // defaults to [ '.js' ]
extensions: [ '.js', '.coffee' ], // defaults to [ '.js' ]

// if true then uses of `global` won't be dealt with by this plugin
ignoreGlobal: false
})
]
}).then(...)
Expand Down
7 changes: 5 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import MagicString from 'magic-string';
import { attachScopes, createFilter, makeLegalIdentifier } from 'rollup-pluginutils';
import { flatten, isReference } from './ast-utils.js';

var firstpass = /\b(?:require|module|exports|global)\b/;
var firstpassGlobal = /\b(?:require|module|exports|global)\b/;
var firstpassNoGlobal = /\b(?:require|module|exports)\b/;
var exportsPattern = /^(?:module\.)?exports(?:\.([a-zA-Z_$][a-zA-Z_$0-9]*))?$/;

const reserved = 'abstract arguments boolean break byte case catch char class const continue debugger default delete do double else enum eval export extends false final finally float for function goto if implements import in instanceof int interface let long native new null package private protected public return short static super switch synchronized this throw throws transient true try typeof var void volatile while with yield'.split( ' ' );
Expand Down Expand Up @@ -39,6 +40,8 @@ function getCandidates ( resolved, extensions ) {
export default function commonjs ( options = {} ) {
const extensions = options.extensions || ['.js'];
const filter = createFilter( options.include, options.exclude );
const ignoreGlobal = options.ignoreGlobal;
const firstpass = ignoreGlobal ? firstpassNoGlobal : firstpassGlobal;
let bundleUsesGlobal = false;
let bundleRequiresWrappers = false;

Expand Down Expand Up @@ -146,7 +149,7 @@ export default function commonjs ( options = {} ) {
return;
}

if ( node.type === 'ThisExpression' && scopeDepth === 0 ) {
if ( node.type === 'ThisExpression' && scopeDepth === 0 && !ignoreGlobal ) {
uses.global = true;
magicString.overwrite( node.start, node.end, `__commonjs_global`, true );
return;
Expand Down
2 changes: 2 additions & 0 deletions test/samples/ignore-global/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export var immediate = typeof global.setImmediate === 'function' ?
global.setImmediate : global.setTimeout;
20 changes: 20 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,4 +222,24 @@ describe( 'rollup-plugin-commonjs', () => {
plugins: [ commonjs() ]
}).then( executeBundle );
});

it( 'can ignore references to `global`', () => {
return rollup({
entry: 'samples/ignore-global/main.js',
plugins: [ commonjs({
ignoreGlobal: true
}) ]
}).then( bundle => {
const generated = bundle.generate({
format: 'cjs'
});

let mod = {};

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

assert.equal( global.setImmediate, mod.immediate, generated.code );
});
});
});

0 comments on commit f9e81b0

Please sign in to comment.