nodejs / modules

Node.js Modules Team

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature: Tree shaking

GeoffreyBooth opened this issue · comments

  • Module bundlers like Rollup can do tree-shaking to create a bundle that includes only the code that’s used.
  • If a member is imported from a CommonJS module using the correct syntax, the tree shaking can include that module.

So the first part goes to the heart of ESM being statically analyzable, so that an all-ESM module graph can get pruned so that a bundle with only the used code is generated.

The second part I assume includes code like import { shuffle } from 'underscore' (as opposed to import _ from 'underscore') where we know that shuffle is the member being imported and so therefore the module graph can include just shuffle rather than all of underscore. See also #100 (comment)

Use case 12.

commented

what does tree shaking mean for node? does it delete the stuff from memory that it doesn't need?

what does tree shaking mean for node?

The use case says “Alice is using an existing npm package that is written in CommonJS. She wants to use one specific function exported from its module.exports and wants to ensure she does it in a way that bundlers will understand while tree-shaking.” So I assume this is only about ensuring that tree-shaking tools can continue to do their thing in a hybrid ESM and CommonJS environment.

I'm not sure how this applies to node - treeshaking isn't part of the spec; despite that bundlers do it, it's often unsafe to do; and any form of "treeshaking" is already identically possible with static CJS as with static ESM - I can't even conceive of how node would do anything to make it harder.

commented

@GeoffreyBooth

so like: "whatever code she uses to run her code in node should still be understandable to tree-shaking tools"?

whatever code she uses to run her code in node should still be understandable to tree-shaking tools

this is achieved with nothing more than "uses ESM import/export syntax".

commented

@ljharb thats my understanding as well, i just want to be 100% clear as the original text makes me think that people expect the node vm itself to perform some sort of treeshaking

It was @jkrems’ use case, so perhaps they could clarify? And feel free to edit my original post to make the request clearer. (This applies to all the feature issues I’m opening.)