一旦Angular应用进入生产阶段,你该如何部署它们呢?
到目前为止我看到的所有指南(甚至在angular.io上)都依赖于一个lite-server来服务和browserSync来反映变化——但是当你完成开发时,你如何发布应用程序呢?
我是否导入index.html页面上所有已编译的.js文件,还是使用gulp缩小它们?它们会起作用吗?在生产版本中,我是否需要SystemJS ?
一旦Angular应用进入生产阶段,你该如何部署它们呢?
到目前为止我看到的所有指南(甚至在angular.io上)都依赖于一个lite-server来服务和browserSync来反映变化——但是当你完成开发时,你如何发布应用程序呢?
我是否导入index.html页面上所有已编译的.js文件,还是使用gulp缩小它们?它们会起作用吗?在生产版本中,我是否需要SystemJS ?
当前回答
我用永远:
使用Angular -cli到dist文件夹ng Build——prod——output-path ./dist来构建你的Angular应用 在你的Angular应用程序路径下创建server.js文件: Const express = require('express'); Const path = require('path'); Const app = express(); app.use(表达。Static (__dirname + '/dist')); App.get ('/*', function(req,res) { res.sendFile(路径。Join (__dirname + '/dist/index.html')); }); app.listen (80); 永远启动server.js
这是所有!您的应用程序应该正在运行!
其他回答
你其实是在同时回答两个问题。
第一个问题是如何托管应用程序。 正如@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种子
安检检测器
你可以通过使用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
Angular 2在Github Pages中的部署
测试Angular2 Webpack在ghpages中的部署
首先从dist文件夹中获取所有相关文件,对我来说是: + CSS文件在资产文件夹 + main.bundle.js + polyfills.bundle.js + vendor.bundle.js
然后将这些文件推入您创建的repo中。
1 -如果你想让应用程序在根目录上运行-创建一个特殊的repo,名称为[yourgithubusername].github。IO并将这些文件推到主分支中
2——如果你想在子目录或根目录以外的不同分支中创建这些页面,创建一个分支gh-pages并将这些文件推到该分支中。
在这两种情况下,我们访问这些部署页面的方式将是不同的。
对于第一种情况,它将是https://[yourgithubusername].github.io,对于第二种情况,它将是[yourgithubusername].github。io /(回购的名字)。
如果你想使用第二种情况部署它,请确保在dist中更改index.html文件的基本url,因为所有的路由映射都依赖于你给出的路径,它应该设置为[/branchname]。
链接到本页
https://rahulrsingh09.github.io/Deployment
去回购
https://github.com/rahulrsingh09/Deployment
对于那些看到“加载模块脚本失败:…”的错误。简单的去你的棱角分明。将“OutputPath”改为“dist”。然后运行ng build——configuration production来构建你的应用。
原因是angular认为你的应用程序在根目录下,但如果你不做这个更改,它就在根目录/YourAppName下
为了快速廉价地托管angular应用,我一直在使用Firbase托管。它在第一层是免费的,使用Firebase CLI部署新版本非常容易。本文解释了将你的angular 2产品应用部署到Firebase的必要步骤:https://medium.com/codingthesmartway-com-blog/hosting-angular-2-applications-on-firebase-f194688c978d
简而言之,你运行ng build——prod,它会在包中创建一个dist文件夹,这个文件夹会被部署到Firebase Hosting上。