umijs / mako

An extremely fast, production-grade web bundler based on Rust.
https://makojs.dev
MIT License
1.84k stars 69 forks source link

improve(tree-shaking): remove dynamic and require dep after tree-shaking #1426

Open stormslowly opened 3 months ago

stormslowly commented 3 months ago

problem

// ==> index.ts <==
import { foo } from "./foo";

console.log(foo());
// ==> foo/foo.js <==
console.log('foo/foo');
// ==> foo/index.js <==
export function foo() {
  console.log(1);
}

export function bar() {
  return require("./foo.js");
}

after tree-shaking, function bar is removed, but there is still a edge to foo/foo.js, so it will be kept in the module graph.

expect the foo/foo.js removed from the module graph

solution

after tree-shaking , analyze the shaken ast, to found if the dynamic and cjs-require are still in use.

stormslowly commented 3 months ago

@goo-yyh 增加一个 foo 在环上的 case,