stealjs / steal-tools

Build easy. Load fast.

Home Page:https://stealjs.com/docs/steal-tools.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add Tree Shaking

matthewp opened this issue · comments

This issue spins off of the tree shaking RFC.

With regard to the API tree-shaking will be on by default. It can be disabled in BuildOptions:

stealTools.build({}, {
  treeShaking: false
});

Also we will following the sideEffects value in package.json, like webpack does. This means for 3rd party packages we will only tree shake those modules if they have "sideEffects": false in their package.json.

This can be override by passing the treeShakingForce flag:

stealTools.build({}, {
  treeShakingForce: true
});

We will emit a warning when force is turned on.


Implementation wise, tree shaking will be a separate step in the process that will happen before transpile. This is because we need the untranspiled source to know what the imports and exports are for each module.

At first tree-shaking will only happen from ES source modules. Adding CommonJS will occur time-permitting and should following some safety rules like

  • Only tree-shake if the CJS module only modules module.exports and/or exports in the module body.
  • Do not tree-shake if the CJS module assigns itself to some local variable like var foo = module.exports because that would then mean we need to verify that foo is never modified.

Documentation to add:

  • - Add a topic page for Tree Shaking under the Bundling section.
  • - Document treeShaking and treeShakingForce options under BuildOptions.
  • - Document the sideEffects package.json boolean.

Things to Test

In addition to the tests I've already written also add:

  • - Importing an ES module from a CommonJS module (ES module is not tree-shaken, I think).
  • - When a package is no longer used at all after tree shaking (removed from graph, I think).
  • - treeShakingForce: true to ignore the sideEffects configuration and treeshake everything.
  • - Handle re-exporting from another module..
  • - let a = 'b'; export {a}; does not constitute usage. Has to be right hand side.

This is in major.