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/orexports
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 thatfoo
is never modified.
Documentation to add:
- - Add a topic page for Tree Shaking under the Bundling section.
- - Document
treeShaking
andtreeShakingForce
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 thesideEffects
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.
have to fix stealjs/steal-conditional#55
This is in major.