diff --git a/src/Module.js b/src/Module.js index 9be65dc..6ff7331 100644 --- a/src/Module.js +++ b/src/Module.js @@ -304,7 +304,7 @@ export default class Module { } walk( ast, { - enter: (node, parent, prop) => { + enter: node => { // eliminate dead branches early if ( node.type === 'IfStatement' ) { if ( isFalsy( node.test ) ) { @@ -314,7 +314,15 @@ export default class Module { this.magicString.overwrite( node.alternate.start, node.alternate.end, '{}' ); node.alternate = emptyBlockStatement( node.alternate.start, node.alternate.end ); } - } else if ( node.type === 'ConditionalExpression' ) { + } + + this.magicString.addSourcemapLocation( node.start ); + this.magicString.addSourcemapLocation( node.end ); + }, + + leave: ( node, parent, prop ) => { + // eliminate dead branches early + if ( node.type === 'ConditionalExpression' ) { if ( isFalsy( node.test ) ) { this.magicString.remove( node.start, node.alternate.start ); parent[prop] = node.alternate; @@ -324,9 +332,6 @@ export default class Module { parent[prop] = node.consequent; } } - - this.magicString.addSourcemapLocation( node.start ); - this.magicString.addSourcemapLocation( node.end ); } }); diff --git a/test/function/trim-conditional-branches-in-exports/_config.js b/test/function/trim-conditional-branches-in-exports/_config.js new file mode 100644 index 0000000..c084f7e --- /dev/null +++ b/test/function/trim-conditional-branches-in-exports/_config.js @@ -0,0 +1,3 @@ +module.exports = { + description: 'trims conditional branches with a renamed default export' +}; diff --git a/test/function/trim-conditional-branches-in-exports/foo.js b/test/function/trim-conditional-branches-in-exports/foo.js new file mode 100644 index 0000000..82df459 --- /dev/null +++ b/test/function/trim-conditional-branches-in-exports/foo.js @@ -0,0 +1,2 @@ +var foo = 0; +export default Math.random() < 0.5 ? foo : foo; diff --git a/test/function/trim-conditional-branches-in-exports/main.js b/test/function/trim-conditional-branches-in-exports/main.js new file mode 100644 index 0000000..8e67e6f --- /dev/null +++ b/test/function/trim-conditional-branches-in-exports/main.js @@ -0,0 +1,2 @@ +import foo from './foo.js'; +console.log( true ? false ? foo : 0 : 1 );