From f9e81b06d4d1c4080f5c3cfc84662262a44f6c9b Mon Sep 17 00:00:00 2001 From: Calvin Metcalf Date: Fri, 11 Mar 2016 14:42:26 -0500 Subject: [PATCH] ability to ignore `global` variable (#48) --- README.md | 5 ++++- src/index.js | 7 +++++-- test/samples/ignore-global/main.js | 2 ++ test/test.js | 20 ++++++++++++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/samples/ignore-global/main.js diff --git a/README.md b/README.md index e4b3c6f..9d5440d 100644 --- a/README.md +++ b/README.md @@ -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(...) diff --git a/src/index.js b/src/index.js index 51a9f19..1286b9e 100644 --- a/src/index.js +++ b/src/index.js @@ -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( ' ' ); @@ -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; @@ -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; diff --git a/test/samples/ignore-global/main.js b/test/samples/ignore-global/main.js new file mode 100644 index 0000000..2602494 --- /dev/null +++ b/test/samples/ignore-global/main.js @@ -0,0 +1,2 @@ +export var immediate = typeof global.setImmediate === 'function' ? + global.setImmediate : global.setTimeout; diff --git a/test/test.js b/test/test.js index ef12760..e466ec1 100644 --- a/test/test.js +++ b/test/test.js @@ -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 ); + }); + }); });