Skip to content
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

允许 Taro.getEnv() === Taro.ENV_TYPE.* 清除 dead code #9895

Closed
SyMind opened this issue Jul 26, 2021 · 1 comment
Closed

允许 Taro.getEnv() === Taro.ENV_TYPE.* 清除 dead code #9895

SyMind opened this issue Jul 26, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@SyMind
Copy link
Member

SyMind commented Jul 26, 2021

这个特性解决了什么问题?

在编译时产出的执行环境就已经被确定了,此时即可获得 Taro.getEnv() === Taro.ENV_TYPE.* 的最终结果,而非延迟到运行时进行判断。

在编译时进行替换的好处在于,terser 清除目标代码的 dead code,从而缩减最终产出的大小。

例如在 Next.js 中使用 webpack.DefinePlugin 替换 typeof window,详细的讨论在这里:https://github.com/vercel/next.js/pull/7651。

这个 API 长什么样?

Taro.getEnv() === Taro.ENV_TYPE.* 会比 typeof window 的替换麻烦很多,比如用户可能直接从 @tarojs/taro 命名导入 getEnv 方法。

可以使用 babel 插件,通过分析语法树的方式对 Taro.getEnv() === Taro.ENV_TYPE.* 进行替换。

@taro-bot2 taro-bot2 bot added the enhancement New feature or request label Jul 26, 2021
@luckyadam
Copy link
Member

https://docs.taro.zone/docs/envs#processenvtaro_env
建议使用这个

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants