What is it called when certain parts of the source code is executed during build for optimization?

huangapple go评论66阅读模式
英文:

What is it called when certain parts of the source code is executed during build for optimization?

问题

我注意到Next.js会自动优化我的代码,通过在构建过程中评估/执行代码,以便不是每次应用程序运行时都运行,而是在构建期间仅运行一次。

例如,在我的Next.js项目中:

  const string = (() => "Hello, World!")()

  console.log(string)

只会简化为:

  console.log("Hello, World!")

这种优化的正确术语是什么?Next.js如何知道这样做是可以的?

如何在webpack、esbuild、rollup等中进行配置?

我想要实现这一点,因为它将允许我在项目中添加某些静态参数,并使代码使用这些参数来构建某些“结构”,例如包含在构建中的SVG等。我不希望每次运行应用程序时都创建这些结构,只需要在构建期间创建一次。

英文:

I have noticed that next.js automatically optimizes my code, by evaluating/executing code during build, so that instead of it running every time the app runs, it only runs once during build.

For example, in my next.js project:

  const string = (() => "Hello, World!")()

  console.log(string)

Is simply turned into:

  console.log("Hello, World!")

What is the proper terminology for this? How does next.js know that this is ok to do?

How can it be configured in weback, esbuild, rollup etc?


I would like to accomplish this, becouse it will allow me to add certain static parameters in my project, and have the code use those parameters to build certain "structures", for example svg's, that are included in the build. I don't want these structures to be created every time i run the app, only once during build.

答案1

得分: 0

在OP提供的示例中,被称为常量折叠。在这里了解更多信息。

英文:

The example provided in OP, is called Constant folding. Read more about it here.

huangapple
  • 本文由 发表于 2023年6月12日 04:52:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76452472.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定