rollup/rollup

Tree shaking in dynamic modules

jakearchibald opened this issue ยท 10 comments

Feature Use Case

Repl example.

export2 is unused but appears in the bundle.

Feature Proposal

Object shape tracking was mentioned in #3417 (comment).

I'm aware this issue has been discussed in many other issues, but it felt like it didn't have its own issue. Feel free to close if it doesn't need one.

I think this is overall impossible

import(variable)

we will never know what gets used and whats not get used as it could be even imported in other context as well.

but we can keep it open maybe we have in future some sort of AI that can guess that after a full pupeteer test or something like that

I would not say that but at current priorities, I would give this a time frame of up to a year. Object property tree-shaking will be a pre-requisite to pull this off comprehensively. If we want a short-cut, one might try to code in some special cases first.

Oops, wrong account, now that was funny

future some sort of AI

Indeed...

@frank-dspeed

I think this is overall impossible

import(variable)

Right, but that isn't what this issue is about. This is about cases where the bundler knows which module is being imported.

@jakearchibald such code splitting is possible via wrappers

original-module.js

exports  { me, him, watson, otherDog, cat}

wrapper-i-call-this-lib.js

exports { watson, otherDog } from './original-module.js'

myapp.js

import { watson } from './wrapper-i-call-this-lib.js'
console.log(watson,'a cute dog')

other-part-of-app.js

import('./wrapper-i-call-this-lib.js').then(({ otherDog })=>console.log('Not as cute as watson))

Resulting

cunk of wrapper-i-call-this-lib.js that is shared will still contain only watson and otherDog

exports { watson, otherDog } from './original-module.js'

Update (15.08.20)

Repl example of the problem.

Repl example of the Solution

@jakearchibald the wrapper or lib.js is the file where you can put your information about the import exports that are used project wide that also allows easy code injection for tests and all that.

After working now some month on the vscode rollup integration i regonized that the algo that typescript uses is able to do this via unused vars so we rollup could reuse the typescript ast via a adapter at present and maybe also on the long run replace acornjs with typescript

we could also replicate the typescript algo that allows to do so but i am not a big fan of that idea.as we do not have the man power for it while it would be cool to do a acorn js version of un used variables inside a project

The most fast way would be to give the acorn ast to typescript then get from typescript the un used variables and strip them.

This issue has been resolved via #4952 as part of rollup@3.21.0. You can test it via npm install rollup.