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

Commit

Permalink
skip dead branches (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Sep 2, 2016
1 parent 6194b88 commit 1575259
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 17 deletions.
39 changes: 39 additions & 0 deletions src/ast-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,42 @@ export function flatten ( node ) {

return { name, keypath: parts.join( '.' ) };
}

export function isTruthy ( node ) {
if ( node.type === 'Literal' ) return !!node.value;
if ( node.type === 'ParenthesizedExpression' ) return isTruthy( node.expression );
if ( node.operator in operators ) return operators[ node.operator ]( node );
}

export function isFalsy ( node ) {
return not( isTruthy( node ) );
}

function not ( value ) {
return value === undefined ? value : !value;
}

function equals ( a, b, strict ) {
if ( a.type !== b.type ) return undefined;
if ( a.type === 'Literal' ) return strict ? a.value === b.value : a.value == b.value;
}

const operators = {
'==': x => {
return equals( x.left, x.right, false );
},

'!=': x => not( operators['==']( x ) ),

'===': x => {
return equals( x.left, x.right, true );
},

'!==': x => not( operators['===']( x ) ),

'!': x => isFalsy( x.argument ),

'&&': x => isTruthy( x.left ) && isTruthy( x.right ),

'||': x => isTruthy( x.left ) || isTruthy( x.right )
};
8 changes: 7 additions & 1 deletion src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import acorn from 'acorn';
import { walk } from 'estree-walker';
import MagicString from 'magic-string';
import { attachScopes, makeLegalIdentifier } from 'rollup-pluginutils';
import { flatten, isReference } from './ast-utils.js';
import { flatten, isReference, isTruthy, isFalsy } from './ast-utils.js';
import { PREFIX, HELPERS_ID } from './helpers.js';
import { getName } from './utils.js';

Expand Down Expand Up @@ -61,6 +61,12 @@ export default function transform ( code, id, isEntry, ignoreGlobal, customNamed

walk( ast, {
enter ( node, parent ) {
// skip dead branches
if ( parent && ( parent.type === 'IfStatement' || parent.type === 'ConditionalExpression' ) ) {
if ( node === parent.consequent && isFalsy( parent.test ) ) return this.skip();
if ( node === parent.alternate && isTruthy( parent.test ) ) return this.skip();
}

if ( node.scope ) scope = node.scope;
if ( /^Function/.test( node.type ) ) scopeDepth += 1;

Expand Down
9 changes: 9 additions & 0 deletions test/function/skips-dead-branches/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const assert = require( 'assert' );

module.exports = {
global: global => {
assert.equal( global.a, undefined );
assert.equal( global.b, 2 );
assert.equal( global.c, undefined );
}
};
1 change: 1 addition & 0 deletions test/function/skips-dead-branches/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
global.a = 1;
2 changes: 2 additions & 0 deletions test/function/skips-dead-branches/b.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global.b = 2;
module.exports = 'b';
2 changes: 2 additions & 0 deletions test/function/skips-dead-branches/c.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
global.c = 3;
module.exports = 'c';
7 changes: 7 additions & 0 deletions test/function/skips-dead-branches/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if ( 'development' === 'production' ) {
require( './a.js' );
}

module.exports = true ?
require( './b.js' ) :
require( './c.js' );
38 changes: 22 additions & 16 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,23 @@ function executeBundle ( bundle ) {
let fn;

try {
fn = new Function( 'module', 'exports', 'require', 'assert', code );
fn = new Function( 'module', 'exports', 'require', 'global', 'assert', code );
} catch ( err ) {
// syntax error
console.log( code );
throw err;
}

const module = { exports: {} };
const global = {};

fn( module, module.exports, () => {}, assert );
fn( module, module.exports, () => {}, global, assert );

return module.exports;
return {
code,
exports: module.exports,
global
};
}

describe( 'rollup-plugin-commonjs', () => {
Expand All @@ -49,11 +54,12 @@ describe( 'rollup-plugin-commonjs', () => {
entry: `function/${dir}/main.js`,
plugins: [ commonjs() ]
}).then( bundle => {
const exports = executeBundle( bundle );
const { code, exports, global } = executeBundle( bundle );

if ( config.exports ) {
config.exports( exports );
}
if ( config.show ) console.error( code );

if ( config.exports ) config.exports( exports );
if ( config.global ) config.global( global );
});
});
});
Expand Down Expand Up @@ -193,7 +199,7 @@ describe( 'rollup-plugin-commonjs', () => {
entry: 'samples/extension/main.coffee',
plugins: [ commonjs({ extensions: ['.coffee' ]}) ]
}).then( bundle => {
assert.equal( executeBundle( bundle ), 42 );
assert.equal( executeBundle( bundle ).exports, 42 );
});
});

Expand All @@ -208,11 +214,11 @@ describe( 'rollup-plugin-commonjs', () => {
format: 'cjs'
});

const bundleExports = executeBundle( bundle );
const { exports, global } = 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 );
assert.equal( exports.immediate1, global.setImmediate, generated.code );
assert.equal( exports.immediate2, global.setImmediate, generated.code );
assert.equal( exports.immediate3, null, generated.code );
});
});

Expand All @@ -222,7 +228,7 @@ describe( 'rollup-plugin-commonjs', () => {
entry: 'samples/umd/correct-scoping.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ), 'object' );
assert.equal( executeBundle( bundle ).exports, 'object' );
});
});

Expand All @@ -231,7 +237,7 @@ describe( 'rollup-plugin-commonjs', () => {
entry: 'samples/umd/protobuf.js',
plugins: [ commonjs() ]
}).then( bundle => {
assert.equal( executeBundle( bundle ), true );
assert.equal( executeBundle( bundle ).exports, true );
});
});

Expand All @@ -253,8 +259,8 @@ describe( 'rollup-plugin-commonjs', () => {
return rollup({
entry: 'samples/deconflict-helpers/main.js',
plugins: [ commonjs() ]
}).then( executeBundle ).then( module => {
assert.notEqual( module.exports, 'nope' );
}).then( executeBundle ).then( ({ exports }) => {
assert.notEqual( exports, 'nope' );
});
});

Expand Down

0 comments on commit 1575259

Please sign in to comment.