CLI - 在 Angular 上为生产构建
在制作好 Web 应用程序并准备好部署后,我们要求 Angular 将所有不同的代码片段编译到一个构建中。构建被压缩成更小的尺寸。
在这最后一步中,我们必然会遇到一些小问题。考虑到构建应用程序所经历的严酷考验,这可能是一种令人沮丧的体验。
很容易认为这是一个孤立的问题,但即使是未被篡改的新项目也会遇到同样的问题。遇到的一个主要问题是没有创建 main.js
。然后会有其他警告,例如:
如何解决 CLI - 构建问题
在我们深入研究不同的工作解决方案之前,让我们看看 Angular 的不同标志设置:
# Prod these are equivalent
ng build --target=production --environment=prod
ng build --prod --env=prod
ng build --prod
# Dev and so are these
ng build --target=development --environment=dev
ng build --dev --env=dev
ng build --dev
ng build
在 Angular 中使用 --prod
时,它会设置一些不可标记的设置:
-
如果在
.angular-cli.json
中配置,则添加 service worker。 -
用
production
值替换模块中的process.env.NODE_ENV
(这对于某些库是必需的,例如 react)。 - 在代码上运行 UglifyJS。
在构建项目时,诸如 ng build --env=prod
之类的命令无法完成出色的构建生产工作,因为它们不会压缩文件。要记住的另一件事是确认已实施 AOT。默认情况下,AOT 主要是活动的。
如果实施了提前 (AOT),则 ng build --prod
命令应该可以工作。如果它不起作用,请使用 ng build --prod --aot=false
命令。
如果在 Angular 中启用了启用生产功能,则 ng serve --prod
命令也可以工作。此命令还有助于显着加快应用加载时间。
相关文章
Do you understand JavaScript closures?
发布时间:2025/02/21 浏览次数:108 分类:JavaScript
-
The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.
Do you know about the hidden traps in variables in JavaScript?
发布时间:2025/02/21 浏览次数:178 分类:JavaScript
-
Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av
How much do you know about the Prototype Chain?
发布时间:2025/02/21 浏览次数:150 分类:JavaScript
-
The prototype chain can be considered one of the core features of JavaScript, and certainly one of its more challenging aspects. If you've learned other object-oriented programming languages, you may find it somewhat confusing when you start
用 jQuery 检查复选框是否被选中
发布时间:2024/03/24 浏览次数:102 分类:JavaScript
-
在本教程中学习 jQuery 检查复选框是否被选中的所有很酷的方法。我们展示了使用直接 DOM 操作、提取 JavaScript 属性的 jQuery 方法以及使用 jQuery 选择器的不同方法。你还将找到许多有用的
jQuery 中的 Window.onload 与 $(document).ready
发布时间:2024/03/24 浏览次数:180 分类:JavaScript
-
本教程演示了如何在 jQuery 中使用 Window.onload 和 $(document).ready 事件。