developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8k stars 361 forks source link

有些js文件不想进行treeshaking,怎么配置 #997

Closed HeWenKe closed 1 year ago

HeWenKe commented 1 year ago

很多变量,定义后需要动态调用,但是打包的时候直接把变量打包没了

rschristian commented 1 year ago

Please provide a reproduction as well as the expected behavior.

HeWenKe commented 1 year ago

look under code const a = 'a'; const b = 'b'; const c = 'c'; let ary = []; for (let val of ['a','b','c']){ ary.push(eval(val)) } console.log(ary) //打包以上代码,会把变量a,b,c 全部remove 掉,导致我for 循环的时候,找不到


Edit by @rschristian for clarity:

const a = 'a';
const b = 'b';
const c = 'c';
let ary = [];
for (let val of ['a','b','c']){
  ary.push(eval(val))
}
console.log(ary)

a, b, and c are incorrectly stripped out, they're seen as dead code despite the later eval() call.

rschristian commented 1 year ago

This is probably an upstream issue, but will look into it.

HeWenKe commented 1 year ago

perfect, can you speak chinese ? can ,netx time please speak it。wowo

rschristian commented 1 year ago

No, I cannot. Just tossed it into Google translate and hoped for the best.

HeWenKe commented 1 year ago

ok,thanks

rschristian commented 1 year ago

Upstream "issue" in Rollup, see: https://github.com/rollup/rollup/issues/3228, https://github.com/rollup/rollup/issues/2963

We don't really have a solution for this here, nor does there seem to be one upstream. Using eval is something that should be avoided whenever possible anyways.

Closing this out as there's nothing actionable for us here to my knowledge.