Skip to content
This repository has been archived by the owner on Aug 4, 2021. It is now read-only.

Commit

Permalink
Merge branch 'dmitrage-fix-ignore-global'
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Aug 31, 2016
2 parents 65a475d + 41bdbf5 commit 50dbf35
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,14 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
if ( node.type === 'Identifier' ) {
if ( ( node.name in uses ) && isReference( node, parent ) && !scope.contains( node.name ) ) {
uses[ node.name ] = true;
if ( node.name === 'global' ) magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsGlobal` );
if ( node.name === 'global' && !ignoreGlobal ) magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsGlobal` );
}
return;
}

if ( node.type === 'ThisExpression' && scopeDepth === 0 && !ignoreGlobal ) {
uses.global = true;
magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, true );
if ( !ignoreGlobal ) magicString.overwrite( node.start, node.end, `${HELPERS_NAME}.commonjsGlobal`, true );
return;
}

Expand Down Expand Up @@ -152,7 +152,7 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed
}
});

if ( !sources.length && !uses.module && !uses.exports && !uses.global ) {
if ( !sources.length && !uses.module && !uses.exports && ( ignoreGlobal || !uses.global ) ) {
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
2 changes: 2 additions & 0 deletions test/samples/ignore-global/firstpass.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;
4 changes: 4 additions & 0 deletions test/samples/ignore-global/identifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// require (firstpass)

export var immediate = typeof global.setImmediate === 'function' ?
global.setImmediate : global.setTimeout;
5 changes: 3 additions & 2 deletions test/samples/ignore-global/main.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export var immediate = typeof global.setImmediate === 'function' ?
global.setImmediate : global.setTimeout;
export { immediate as immediate1 } from './firstpass';
export { immediate as immediate2 } from './identifier';
export { immediate as immediate3 } from './this';
6 changes: 6 additions & 0 deletions test/samples/ignore-global/this.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// require (firstpass)

// "this" will be rewritten with "undefined" by rollup
export var immediate = typeof this === 'undefined' ?
null : typeof this.setImmediate === 'function' ?
this.setImmediate : this.setTimeout;
6 changes: 5 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,11 @@ describe( 'rollup-plugin-commonjs', () => {
format: 'cjs'
});

assert.equal( global.setImmediate, executeBundle( bundle ).immediate, generated.code );
const bundleExports = executeBundle( bundle );

assert.equal( bundleExports.immediate1, global.setImmediate, generated.code );
assert.equal( bundleExports.immediate2, global.setImmediate, generated.code );
assert.equal( bundleExports.immediate3, null, generated.code );
});
});

Expand Down

0 comments on commit 50dbf35

Please sign in to comment.