From 2dea1ef0e54c5184489c1c530e478f2755d50f6c Mon Sep 17 00:00:00 2001 From: Rich-Harris Date: Sat, 31 Oct 2015 23:39:02 -0400 Subject: [PATCH] add aggressive mode --- src/Bundle.js | 7 ++++++- test/form/aggressive/_config.js | 6 ++++++ test/form/aggressive/_expected/amd.js | 9 +++++++++ test/form/aggressive/_expected/cjs.js | 7 +++++++ test/form/aggressive/_expected/es6.js | 5 +++++ test/form/aggressive/_expected/iife.js | 9 +++++++++ test/form/aggressive/_expected/umd.js | 13 +++++++++++++ test/form/aggressive/foo.js | 9 +++++++++ test/form/aggressive/main.js | 3 +++ 9 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 test/form/aggressive/_config.js create mode 100644 test/form/aggressive/_expected/amd.js create mode 100644 test/form/aggressive/_expected/cjs.js create mode 100644 test/form/aggressive/_expected/es6.js create mode 100644 test/form/aggressive/_expected/iife.js create mode 100644 test/form/aggressive/_expected/umd.js create mode 100644 test/form/aggressive/foo.js create mode 100644 test/form/aggressive/main.js diff --git a/src/Bundle.js b/src/Bundle.js index 4130909..0947286 100644 --- a/src/Bundle.js +++ b/src/Bundle.js @@ -50,6 +50,7 @@ export default class Bundle { this.external = options.external || []; this.onwarn = options.onwarn || onwarn; + this.aggressive = options.aggressive; // TODO strictly speaking, this only applies with non-ES6, non-default-only bundles [ 'module', 'exports' ].forEach( global => this.assumedGlobals[ global ] = true ); @@ -74,7 +75,11 @@ export default class Bundle { }); // mark statements that should appear in the bundle - this.modules.forEach( module => module.markStatements() ); + if ( this.aggressive ) { + entryModule.markStatements(); + } else { + this.modules.forEach( module => module.markStatements() ); + } this.orderedModules = this.sort(); this.deconflict(); diff --git a/test/form/aggressive/_config.js b/test/form/aggressive/_config.js new file mode 100644 index 0000000..bafb1e0 --- /dev/null +++ b/test/form/aggressive/_config.js @@ -0,0 +1,6 @@ +module.exports = { + description: 'ignores side-effects outside entry module in aggressive mode', + options: { + aggressive: true + } +} diff --git a/test/form/aggressive/_expected/amd.js b/test/form/aggressive/_expected/amd.js new file mode 100644 index 0000000..eac4f98 --- /dev/null +++ b/test/form/aggressive/_expected/amd.js @@ -0,0 +1,9 @@ +define(function () { 'use strict'; + + function foo () { + return 42; + } + + assert.equal( foo(), 42 ); + +}); \ No newline at end of file diff --git a/test/form/aggressive/_expected/cjs.js b/test/form/aggressive/_expected/cjs.js new file mode 100644 index 0000000..a547fb6 --- /dev/null +++ b/test/form/aggressive/_expected/cjs.js @@ -0,0 +1,7 @@ +'use strict'; + +function foo () { + return 42; +} + +assert.equal( foo(), 42 ); \ No newline at end of file diff --git a/test/form/aggressive/_expected/es6.js b/test/form/aggressive/_expected/es6.js new file mode 100644 index 0000000..f32ad00 --- /dev/null +++ b/test/form/aggressive/_expected/es6.js @@ -0,0 +1,5 @@ +function foo () { + return 42; +} + +assert.equal( foo(), 42 ); \ No newline at end of file diff --git a/test/form/aggressive/_expected/iife.js b/test/form/aggressive/_expected/iife.js new file mode 100644 index 0000000..3ba1058 --- /dev/null +++ b/test/form/aggressive/_expected/iife.js @@ -0,0 +1,9 @@ +(function () { 'use strict'; + + function foo () { + return 42; + } + + assert.equal( foo(), 42 ); + +})(); \ No newline at end of file diff --git a/test/form/aggressive/_expected/umd.js b/test/form/aggressive/_expected/umd.js new file mode 100644 index 0000000..1eb1ef2 --- /dev/null +++ b/test/form/aggressive/_expected/umd.js @@ -0,0 +1,13 @@ +(function (global, factory) { + typeof exports === 'object' && typeof module !== 'undefined' ? factory() : + typeof define === 'function' && define.amd ? define(factory) : + factory(); +}(this, function () { 'use strict'; + + function foo () { + return 42; + } + + assert.equal( foo(), 42 ); + +})); \ No newline at end of file diff --git a/test/form/aggressive/foo.js b/test/form/aggressive/foo.js new file mode 100644 index 0000000..16f57e9 --- /dev/null +++ b/test/form/aggressive/foo.js @@ -0,0 +1,9 @@ +function x () { + console.log( 'side-effect' ); +} + +x(); + +export function foo () { + return 42; +} diff --git a/test/form/aggressive/main.js b/test/form/aggressive/main.js new file mode 100644 index 0000000..66c6979 --- /dev/null +++ b/test/form/aggressive/main.js @@ -0,0 +1,3 @@ +import { foo } from './foo'; + +assert.equal( foo(), 42 );