6

我想要的是

我的资产中有非常大的图像,对于较慢的网络,这会大大降低网站的速度。(您可以在此灯塔链接页面上阅读有关该主题的更多信息

  • 我想在构建时压缩它们(ng build --prod)。
  • 对于当地的发展,它是无关紧要的(ng serve)。
  • 理想情况下,我想为不同的屏幕尺寸生成多个版本(example.jpg→ 应该变成:example_x265.jpg,,example_x128.jpg...)

我试过的

我找到的最有希望的指南是这里的指南,它描述了如何结合使用imagemin包和ngx-build-plus包。

不幸的是,在按照教程进行操作后,我收到以下错误:

[error] TypeError: Cannot assign to read only property 'main.977fe6373cfd108d.js' of object '#<Object>'
    at ImageminPlugin._callee2$ (/.../node_modules/imagemin-webpack-plugin/dist/index.js:264:48)
    at tryCatch (/.../node_modules/babel-runtime/node_modules/regenerator-runtime/runtime.js:62:40)
     // ...

有没有办法在构建时压缩资产图像?

Angular Version: 13.1.0

注意:我不想知道如何将图像上传到第三方存储解决方案。
我特别想在构建时创建我的静态资产的压缩版本。

4

3 回答 3

5

您可以将 gulpfile 与gulp-responsive或一起使用gulp-sharp-responsive。我个人使用后者,因为它支持AVIF格式。

要将它与您的 Angular 项目很好地集成,您可以按照以下步骤操作:

  1. 安装依赖项:npm i --save-dev gulp gulp-sharp-responsive
  2. gulpfile.js使用以下内容在项目根目录中创建
const { src, dest } = require("gulp");
const sharpResponsive = require("gulp-sharp-responsive");

const compress = () =>
  src("images/*.{png,jpg}")
    .pipe(
      sharpResponsive({
        formats: [
          // jpeg
          { width: 256, format: "jpeg", rename: { suffix: "-256" } },
          { width: 512, format: "jpeg", rename: { suffix: "-512" } },
          { width: 1024, format: "jpeg", rename: { suffix: "-1024" } },
          // webp
          { width: 256, format: "webp", rename: { suffix: "-256" } },
          { width: 512, format: "webp", rename: { suffix: "-512" } },
          { width: 1024, format: "webp", rename: { suffix: "-1024" } },
          // avif
          { width: 256, format: "avif", rename: { suffix: "-256" } },
          { width: 512, format: "avif", rename: { suffix: "-512" } },
          { width: 1024, format: "avif", rename: { suffix: "-1024" } },
        ],
      })
    )
    .pipe(dest("src/assets/compressed"));

module.exports = {
  compress,
};
  1. 在您的项目根目录中创建一个文件夹,您的未压缩图像文件所在的位置(在此示例中称为images
  2. 将预安装脚本添加到您的package.js.,以便在每次构建时调用您的 gulpfile
"scripts": {
  "prebuild": "gulp compress",
  // ...
},

如果您npm run build现在调用,它会在实际运行之前压缩您的图像并将它们移动到指定的资产文件夹中ng build

现在您可以使用带有picture/source组合的图像文件,如下面的代码片段所示。请记住,源标签的顺序很重要。

<!-- {{image}} is the image name -->
<picture *ngIf="image">
  <!-- avif -->
  <source
    srcset="assets/compressed/{{image}}-256.avif"
    media="(max-width: 512px)"
    type="image/avif"
  />
  <source
    srcset="assets/compressed/{{image}}-512.avif"
    media="(max-width: 1024px)"
    type="image/avif"
  />
  <source
    srcset="assets/compressed/{{image}}-1024.avif"
    media="(max-width: 2048px)"
    type="image/avif"
  />
  <!-- webp -->
  <source
    srcset="assets/compressed/{{image}}-256.webp"
    media="(max-width: 512px)"
    type="image/webp"
  />
  <source
    srcset="assets/compressed/{{image}}-512.webp"
    media="(max-width: 1024px)"
    type="image/webp"
  />
  <source
    srcset="assets/compressed/{{image}}-1024.webp"
    media="(max-width: 2048px)"
    type="image/webp"
  />
  <!-- jpeg -->
  <source
    srcset="assets/compressed/{{image}}-256.jpg"
    media="(max-width: 512px)"
    type="image/jpeg"
  />
  <source
    srcset="assets/compressed/{{image}}-512.jpg"
    media="(max-width: 1024px)"
    type="image/jpeg"
  />
  <source
    srcset="assets/compressed/{{image}}-1024.jpg"
    media="(max-width: 2048px)"
    type="image/jpeg"
  />
  <!-- original -->
  <img src="assets/compressed/{{ image }}-1024.jpg" />
</picture>

于 2022-01-10T21:15:24.537 回答
0

我永远不会那样做!因为它违背了传统您应该尝试Firebase 存储,它们免费为您提供 1 GB,并且易于实施。

于 2021-12-19T22:55:51.020 回答
0

如果您有非常大的图像资产,则应将其单独托管在 amazons3 或 Google Cloud Storage 等服务中。

除了使您的加载时间和构建速度更快之外,它还有助于根据屏幕分辨率加载图像。您可以使用 lamda/cloud 功能自动执行此操作,也可以手动将图像压缩为不同大小并根据设备分辨率提供服务。

于 2021-12-20T05:19:23.870 回答