一旦Angular应用进入生产阶段,你该如何部署它们呢?
到目前为止我看到的所有指南(甚至在angular.io上)都依赖于一个lite-server来服务和browserSync来反映变化——但是当你完成开发时,你如何发布应用程序呢?
我是否导入index.html页面上所有已编译的.js文件,还是使用gulp缩小它们?它们会起作用吗?在生产版本中,我是否需要SystemJS ?
一旦Angular应用进入生产阶段,你该如何部署它们呢?
到目前为止我看到的所有指南(甚至在angular.io上)都依赖于一个lite-server来服务和browserSync来反映变化——但是当你完成开发时,你如何发布应用程序呢?
我是否导入index.html页面上所有已编译的.js文件,还是使用gulp缩小它们?它们会起作用吗?在生产版本中,我是否需要SystemJS ?
当前回答
截至2017年,最好的方法是在你的angular项目中使用angular-cli (v1.4.4)。
ng build --prod --env=prod --aot --build-optimizer --output-hashing none
你不需要显式地添加——aot,因为它在默认情况下是打开的——prod。-output-hashing的使用取决于你对缓存爆破的个人偏好。
您可以通过添加以下内容显式添加CDN支持:
--deploy-url "https://<your-cdn-key>.cloudfront.net/"
如果你计划使用CDN进行托管,这是相当快的。
其他回答
要在IIS中部署应用程序,请遵循以下步骤。
第一步:使用ng Build——prod命令构建Angular应用
步骤2:构建后,所有文件都存储在应用程序路径的dist文件夹中。
步骤3:在C:\inetpub\wwwroot中创建一个名为QRCode的文件夹。
第四步:复制dist文件夹的内容到C:\inetpub\wwwroot\QRCode文件夹。
步骤5:使用命令(Window + R)打开IIS管理器,输入inetmgr,单击“确定”。
步骤6:右键单击默认网站,然后单击添加应用程序。
第七步:输入别名“QRCode”,设置物理路径为C:\inetpub\wwwroot\QRCode。
第八步:打开index.html文件,找到行href="\"并删除'\'。
步骤9:现在在任何浏览器中浏览应用程序。
你也可以跟着视频更好地理解。
视频网址:https://youtu.be/F8EI-8XUNZc
在azure中部署Angular 2很简单
运行ng build——prod,它将生成一个dist文件夹,其中所有内容都捆绑在几个文件中,包括index.html。 创建一个资源组和其中的web应用程序。 使用FTP放置dist文件夹文件。在azure中,它将寻找index.html来运行应用程序。
就是这样。你的应用程序正在运行!
使用Angular CLI,这很简单。以Heroku为例:
创建Heroku帐号并安装CLI 移动angular-cli dep到package中的依赖项。json(这样当你推送到Heroku时它就会被安装。 添加一个postinstall脚本,当代码被推送到Heroku时将运行ng build。还要为将在下面的步骤中创建的Node服务器添加启动命令。这将把应用程序的静态文件放在服务器上的dist目录中,然后启动应用程序。
"scripts": {
// ...
"start": "node server.js",
"postinstall": "ng build --aot -prod"
}
创建一个Express服务器来服务应用程序。
// server.js
const express = require('express');
const app = express();
// Run the app by serving the static files
// in the dist directory
app.use(express.static(__dirname + '/dist'));
// Start the app by listening on the default
// Heroku port
app.listen(process.env.PORT || 8080);
创建一个Heroku远程和推动部署应用程序。
heroku create
git add .
git commit -m "first deploy"
git push heroku master
下面是我做的一个快速的书写,有更多的细节,包括如何强制请求使用HTTPS,以及如何处理PathLocationStrategy:)
你可以通过使用Ahead of Time编译器编译来获得最小和最快的加载产品包,并使用rollup来进行树摇/缩小,如angular AOT烹饪书中所示:https://angular.io/docs/ts/latest/cookbook/aot-compiler.html
在之前的回答中说过,这也可以用Angular-CLI来实现,但如果你还没有使用CLI来制作你的应用,你应该按照食谱来做。
我还有一个材料和SVG图表的工作示例(由Angular2支持),它包括一个用AOT烹饪书创建的包。您还可以找到创建bundle所需的所有配置和脚本。点击这里查看:https://github.com/fintechneo/angular2-templates/
我制作了一个快速视频,演示了AoT编译构建与开发环境的文件数量和大小之间的区别。它显示了上面github存储库中的项目。你可以在这里看到:https://youtu.be/ZoZDCgQwnmQ
你其实是在同时回答两个问题。
第一个问题是如何托管应用程序。 正如@toskv提到的,这个问题太宽泛了,无法回答,取决于许多不同的事情。
第二个问题是如何准备应用程序的部署版本? 这里有几个选项:
Deploy as it is. Just that - no minification, concatenation, name mangling, etc. Transpile all your ts project copy all your resulting js/css/... sources + dependencies to the hosting server and you are good to go. Deploy using special bundling tools, like webpack or systemjs builder. They come with all the possibilities that are lacking in #1. You can pack all your app code into just a couple of js/css/... files that you reference in your HTML. systemjs builder even allows you to get rid of the need to include systemjs as part of your deployment package. You can use ng deploy as of Angular 8 to deploy your app from your CLI. ng deploy will need to be used in conjunction with your platform of choice (such as @angular/fire). You can check the official docs to see what works best for you here
是的,你很可能需要将systemjs和其他一些外部库部署到你的包中。是的,你可以将它们捆绑到你从HTML页面引用的几个js文件中。
你不需要从页面中引用所有已编译的js文件——systemjs作为模块加载器会处理这些。
我知道这听起来很模糊——为了帮助你开始第2点,这里有两个非常好的示例应用程序:
SystemJS构建器:angular2种子
安检检测器