New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Treeshake dynamic imports based on destructuring #16872
Comments
import('a').then(({ b }) => b)
Question: Do we need to support this syntax style also: const {uniq} = await import('lodash-es'); |
with import * as ns from 'a';
const {b} = ns;
const {c} = await import('c');
const {d} = process.env; // or any defined value |
What are the scenarios which couldn't be supported with |
We are facing the same issue too, for a really long time.
I understand that object deconstruction dead code elimination is very difficult, which means performing dead code on object property. async function main() {
const {uniq} = await import('lodash-es);
console.log(uniq);
}
main(); would be transpiled to import _regeneratorRuntime from "@babel/runtime/helpers/regeneratorRuntime";
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
function main() {
return _main.apply(this, arguments);
}
function _main() {
_main = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee() {
var _yield$import, uniq;
return _regeneratorRuntime().wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.next = 2;
return import('lodash-es');
case 2:
_yield$import = _context.sent;
uniq = _yield$import.uniq;
console.log(uniq);
case 5:
case "end":
return _context.stop();
}
}
}, _callee);
}));
return _main.apply(this, arguments);
}
main(); which uses "switch case" based on runtime value, making it impossible to statically analyze. |
BUT, Treeshake dynamic imports based on destructuring are still pretty important for bundlers because of the possible code size it could save.
|
@TheLarkInn because of javascript complexity: const a = import('a');
a.then(({b}) => {});
a.then(({c}) => {}); // this will work I guess
a.then(v => {
const {c} = v;
return v;
}).then(() => {}); // quite difficult to catch all thens here we could support only this syntax like: import().then((__destructing_here__) => {}); problem here we introduce some "non-stable" build in terms of tree-shaking. e.g. like in example user will "refactor" destructing to destructing from variable. in case of async/await: const b = await import('b');
const { a, c } = b; should work as I remember ( we have something like variable replacement in JavascriptParser ) |
What about |
Note - we have |
So there definitely appears to be a variety of scenarios which would be hard to analyze treeshaking. HOWEVER we can encourage people to achieve treeshaking from dynamic imports by using a particular set of conventions. For now let's just call it "inline import() destructuring": Using
|
This issue had no activity for at least three months. It's subject to automatic issue closing if there is no activity in the next 15 days. |
Issue was closed because of inactivity. If you think this is still a valid issue, please file a new issue with additional information. |
Given the following code:
When webpack bundles this code, it should only include the module tree for
uniq
and tree-shake the rest of the exports inlodash-es
as unused.The text was updated successfully, but these errors were encountered: