Browse Source

Merge pull request #625 from Victorystick/disable-treeshaking

Add option to disable treeshaking
gh-669
Rich Harris 9 years ago
parent
commit
9bcf8b5831
  1. 9
      bin/runRollup.js
  2. 4
      src/Bundle.js
  3. 18
      src/Module.js
  4. 1
      src/rollup.js
  5. 4
      test/cli/no-treeshake/_config.js
  6. 10
      test/cli/no-treeshake/_expected.js
  7. 3
      test/cli/no-treeshake/main.js
  8. 11
      test/form/no-treeshake/_config.js
  9. 20
      test/form/no-treeshake/_expected/amd.js
  10. 20
      test/form/no-treeshake/_expected/cjs.js
  11. 17
      test/form/no-treeshake/_expected/es6.js
  12. 21
      test/form/no-treeshake/_expected/iife.js
  13. 24
      test/form/no-treeshake/_expected/umd.js
  14. 1
      test/form/no-treeshake/foo.js
  15. 11
      test/form/no-treeshake/main.js
  16. 3
      test/form/no-treeshake/quux.js
  17. 2
      test/test.js

9
bin/runRollup.js

@ -82,12 +82,14 @@ var equivalents = {
format: 'format', format: 'format',
globals: 'globals', globals: 'globals',
id: 'moduleId', id: 'moduleId',
indent: 'indent',
input: 'entry', input: 'entry',
intro: 'intro', intro: 'intro',
name: 'moduleName', name: 'moduleName',
output: 'dest', output: 'dest',
outro: 'outro', outro: 'outro',
sourcemap: 'sourceMap' sourcemap: 'sourceMap',
treeshake: 'treeshake'
}; };
function execute ( options, command ) { function execute ( options, command ) {
@ -113,12 +115,13 @@ function execute ( options, command ) {
options.onwarn = options.onwarn || log; options.onwarn = options.onwarn || log;
options.external = external; options.external = external;
options.indent = command.indent !== false;
options.noConflict = command.conflict === false; options.noConflict = command.conflict === false;
delete command.conflict;
// Use any options passed through the CLI as overrides.
Object.keys( equivalents ).forEach( function ( cliOption ) { Object.keys( equivalents ).forEach( function ( cliOption ) {
if ( command[ cliOption ] ) { if ( command.hasOwnProperty( cliOption ) ) {
options[ equivalents[ cliOption ] ] = command[ cliOption ]; options[ equivalents[ cliOption ] ] = command[ cliOption ];
} }
}); });

4
src/Bundle.js

@ -31,6 +31,8 @@ export default class Bundle {
this.entryId = null; this.entryId = null;
this.entryModule = null; this.entryModule = null;
this.treeshake = options.treeshake !== false;
this.resolveId = first( this.resolveId = first(
[ id => ~this.external.indexOf( id ) ? false : null ] [ id => ~this.external.indexOf( id ) ? false : null ]
.concat( this.plugins.map( plugin => plugin.resolveId ).filter( Boolean ) ) .concat( this.plugins.map( plugin => plugin.resolveId ).filter( Boolean ) )
@ -102,7 +104,7 @@ export default class Bundle {
settled = true; settled = true;
this.modules.forEach( module => { this.modules.forEach( module => {
if ( module.run() ) settled = false; if ( module.run( this.treeshake ) ) settled = false;
}); });
} }

18
src/Module.js

@ -601,7 +601,23 @@ export default class Module {
return magicString.trim(); return magicString.trim();
} }
run () { /**
* Statically runs the module marking the top-level statements that must be
* included for the module to execute successfully.
*
* @param {boolean} treeshake - if we should tree-shake the module
* @return {boolean} marked - if any new statements were marked for inclusion
*/
run ( treeshake ) {
if ( !treeshake ) {
this.statements.forEach( statement => {
if ( statement.isImportDeclaration ) return;
statement.mark();
});
return false;
}
let marked = false; let marked = false;
this.statements.forEach( statement => { this.statements.forEach( statement => {

1
src/rollup.js

@ -26,6 +26,7 @@ const ALLOWED_KEYS = [
'outro', 'outro',
'plugins', 'plugins',
'sourceMap', 'sourceMap',
'treeshake',
'useStrict' 'useStrict'
]; ];

4
test/cli/no-treeshake/_config.js

@ -0,0 +1,4 @@
module.exports = {
description: 'generates IIFE export with all code',
command: 'rollup main.js --format iife --name shakeless --no-treeshake'
};

10
test/cli/no-treeshake/_expected.js

@ -0,0 +1,10 @@
var shakeless = (function () {
'use strict';
const version = '1.2.0';
var main = ( x, y ) => x + y;
return main;
}());

3
test/cli/no-treeshake/main.js

@ -0,0 +1,3 @@
const version = '1.2.0';
export default ( x, y ) => x + y;

11
test/form/no-treeshake/_config.js

@ -0,0 +1,11 @@
module.exports = {
description: 'all code should be included if tree-shaking is disabled',
options: {
external: [ 'external' ],
globals: {
external: 'external'
},
moduleName: /* not shaken, but */ 'stirred',
treeshake: false
}
};

20
test/form/no-treeshake/_expected/amd.js

@ -0,0 +1,20 @@
define(['exports', 'external'], function (exports, external) { 'use strict';
var foo = 'unused';
const quux = 1;
const other = () => quux;
function bar () {
return foo;
}
function baz () {
return 13 + external.value;
}
exports.baz = baz;
exports.strange = quux;
});

20
test/form/no-treeshake/_expected/cjs.js

@ -0,0 +1,20 @@
'use strict';
var external = require('external');
var foo = 'unused';
const quux = 1;
const other = () => quux;
function bar () {
return foo;
}
function baz () {
return 13 + external.value;
}
exports.baz = baz;
exports.strange = quux;

17
test/form/no-treeshake/_expected/es6.js

@ -0,0 +1,17 @@
import * as external from 'external';
var foo = 'unused';
const quux = 1;
const other = () => quux;
function bar () {
return foo;
}
function baz () {
return 13 + external.value;
}
export { baz, quux as strange };

21
test/form/no-treeshake/_expected/iife.js

@ -0,0 +1,21 @@
(function (exports,external) {
'use strict';
var foo = 'unused';
const quux = 1;
const other = () => quux;
function bar () {
return foo;
}
function baz () {
return 13 + external.value;
}
exports.baz = baz;
exports.strange = quux;
}((this.stirred = this.stirred || {}),external));

24
test/form/no-treeshake/_expected/umd.js

@ -0,0 +1,24 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('external')) :
typeof define === 'function' && define.amd ? define(['exports', 'external'], factory) :
(factory((global.stirred = global.stirred || {}),global.external));
}(this, function (exports,external) { 'use strict';
var foo = 'unused';
const quux = 1;
const other = () => quux;
function bar () {
return foo;
}
function baz () {
return 13 + external.value;
}
exports.baz = baz;
exports.strange = quux;
}));

1
test/form/no-treeshake/foo.js

@ -0,0 +1 @@
export default 'unused';

11
test/form/no-treeshake/main.js

@ -0,0 +1,11 @@
import * as external from 'external';
import foo from './foo.js'
export { quux as strange } from './quux.js';
function bar () {
return foo;
}
export function baz () {
return 13 + external.value;
}

3
test/form/no-treeshake/quux.js

@ -0,0 +1,3 @@
export const quux = 1;
const other = () => quux;

2
test/test.js

@ -76,7 +76,7 @@ describe( 'rollup', function () {
return rollup.rollup({ entry: 'x', plUgins: [] }).then( function () { return rollup.rollup({ entry: 'x', plUgins: [] }).then( function () {
throw new Error( 'Missing expected error' ); throw new Error( 'Missing expected error' );
}, function ( err ) { }, function ( err ) {
assert.equal( 'Unexpected key \'plUgins\' found, expected one of: banner, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, plugins, sourceMap, useStrict', err.message ); assert.equal( err.message, 'Unexpected key \'plUgins\' found, expected one of: banner, dest, entry, exports, external, footer, format, globals, indent, intro, moduleId, moduleName, noConflict, onwarn, outro, plugins, sourceMap, treeshake, useStrict' );
}); });
}); });
}); });

Loading…
Cancel
Save