如何在 angular cli 中启用 gzip 压缩以进行生产构建 [英] how to enable gzip compression in angular cli for production build

查看:30
本文介绍了如何在 angular cli 中启用 gzip 压缩以进行生产构建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想压缩构建 angular 项目时创建的包文件.我使用 ng build --environment=${environment} 来构建当前的应用程序和 "@angular/compiler-cli": "^4.0.0" 的版本不要将 .gz 文件生成到 dist 文件夹.生成 .gz 包文件的最简单方法是什么(最好不接触 webpack.config.js 文件)?PS:我知道创建 .gz 文件的选项在某个时候被 angular/cli 团队删除了.但我非常需要它,因为我的捆绑文件很大.

I would want to compress the bundle files that are created when building the angular project. I use ng build --environment=${environment} to build the application currently and the version of "@angular/compiler-cli": "^4.0.0" do not generate the .gz files to the dist folder. What is the simplest way to generate .gz bundle files (preferable without touching webpack.config.js file)? PS: I knew the option for creating the .gz files was removed by the angular/cli team sometime back. But I need that desperately as my bundle files are huge.

推荐答案

在将它们传输到服务器之前,您可以使用一个简单的 bash 脚本来实现这一点,甚至可以将其作为命令添加到 package.json 中

You could achieve this with a simple bash script before transferring them to the server, or even adding it to package.json as a command

 "scripts": {
   "build.prod": "ng build --environment=prod && tar -zcvf archive.tar.gz dist/prod/*",

不确定您的文件夹结构是什么,但您可以在终端中使用 tar -zcvf archive.tar.gz dist/prod/*,直到找到适合您需要的路径.

Not sure what's your folder structure, but you can play around with tar -zcvf archive.tar.gz dist/prod/* in the terminal until you find paths that suite to your needs.

似乎我误解了这个问题,如果在向最终用户提供东西时它是关于包大小的,你应该看看 AOT + Rollup 来最小化你的包大小.并在提供文件时在您的网络服务器上启用 gzip 压缩(可能大多数服务器已经启用).

Seems I misunderstood the question, if it is about bundle size when serving the stuff to the end user, you should take a look at AOT + Rollup to minimize your bundle sizes. And enable gzip compression on your webserver when serving files (probably most servers have it enabled already).

这篇关于如何在 angular cli 中启用 gzip 压缩以进行生产构建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆