npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

img-native-lazy-plugin

v1.0.3

Published

A webpack plugin that adds native loading='lazy' attribute to img and source tags for lazy loading

Readme

IMG 原生懒加载插件

npm version License: MIT

简介

这是一个简化版的图片懒加载插件,使用浏览器原生的 loading="lazy" 属性实现图片懒加载。

安装

npm install img-native-lazy-plugin --save-dev

或使用 yarn:

yarn add img-native-lazy-plugin --dev

特点

简单高效 - 只添加一个属性,无需 JavaScript
性能最优 - 浏览器原生支持,0 额外开销
即插即用 - 无需配置,开箱即用
兼容性好 - 不支持的浏览器会自动降级(正常加载图片)

工作原理

插件会在构建完成后扫描所有 HTML 文件,给符合条件的 <img><source> 标签添加 loading="lazy" 属性。

处理前

<img src="//cdn.com/image.jpg" alt="图片">
<picture>
  <source srcset="//cdn.com/image.webp" type="image/webp">
  <img src="//cdn.com/image.jpg" alt="图片">
</picture>

处理后

<img src="//cdn.com/image.jpg" alt="图片" loading="lazy">
<picture>
  <source srcset="//cdn.com/image.webp" type="image/webp" loading="lazy">
  <img src="//cdn.com/image.jpg" alt="图片" loading="lazy">
</picture>
<!-- NativeLazyLoad Processed -->

配置选项

new ImgNativeLazyLoadPlugin({
    include: /.*/,             // 处理所有图片(默认)
    exclude: null,            // 排除的图片(可选)
    skipExisting: true        // 跳过已有 loading 属性的图片
})

参数说明

  • include {RegExp} - 只处理匹配的图片 URL(默认:/.*/ 处理所有图片)
  • exclude {RegExp} - 排除匹配的图片 URL(默认:null
  • skipExisting {Boolean} - 跳过已有 loading 属性的图片(默认:true

使用方法

1. 在 webpack 配置中引入

const ImgNativeLazyLoadPlugin = require('img-native-lazy-plugin');

2. 添加到 plugins 数组

module.exports = {
  // ... 其他配置
  plugins: [
    // ... 其他插件
    new ImgNativeLazyLoadPlugin({
        // 默认处理所有图片,无需配置 include
        skipExisting: true
    }),
  ]
};

3. 在 webpack.prod.js 中使用(推荐)

const ImgNativeLazyLoadPlugin = require('img-native-lazy-plugin');

module.exports = {
  mode: 'production',
  // ... 其他配置
  plugins: [
    // ... 其他插件
    new ImgNativeLazyLoadPlugin({
        // 默认处理所有图片,无需配置 include
        skipExisting: true
    }),
  ]
};

4. 构建项目

npm run build:prod

5. 查看构建日志

ImgNativeLazyLoadPlugin: 已启用原生图片懒加载
ImgNativeLazyLoadPlugin: 处理了 24 个 HTML 文件
  - <img> 标签: 158 个
  - <source> 标签: 89 个

浏览器兼容性

| 浏览器 | 版本 | 支持情况 | |--------|------|---------| | Chrome | 77+ | ✅ 完全支持 | | Firefox | 75+ | ✅ 完全支持 | | Safari | 15.4+ | ✅ 完全支持 | | Edge | 79+ | ✅ 完全支持 | | IE | 全部 | ⚠️ 降级(正常显示) | | 旧版浏览器 | - | ⚠️ 降级(正常显示) |

注意: 不支持的浏览器会忽略 loading="lazy" 属性,图片会正常加载,不影响显示。

验证效果

1. 检查 HTML 文件

打开 dist/index.htm,搜索 loading="lazy",应该能看到很多匹配。

2. 浏览器测试

  1. 打开浏览器开发者工具 > Network 面板
  2. 勾选 "Disable cache"
  3. 刷新页面
  4. 向下滚动页面
  5. 观察图片加载时机(应该在即将进入视口时才加载)

3. 查看标记

HTML 文件末尾应该有处理标记:

<!-- NativeLazyLoad Processed -->
</body>

与自定义懒加载的区别

| 特性 | 原生懒加载 | 自定义懒加载 | |------|-----------|-------------| | 实现方式 | loading="lazy" | data-src + JS | | 页面大小 | 无增加 | +2KB | | 占位图 | ❌ 不支持 | ✅ 支持 | | 自定义时机 | ❌ 不支持 | ✅ 支持 | | 浏览器兼容 | 新浏览器 | 全浏览器 | | 维护成本 | 低 | 中 |

常见问题

Q: 图片没有懒加载?

A: 检查以下几点:

  1. 浏览器是否支持(Chrome 77+)
  2. 图片 URL 是否匹配 include 规则
  3. 图片是否在首屏(首屏图片会立即加载)
  4. 打开 Network 面板确认加载时机

Q: 可以和 HtmlLazyLoadPlugin 一起使用吗?

A: 不建议同时使用,会产生冲突。请选择其中一个。

Q: 如何排除某些图片?

A: 使用 exclude 配置:

new ImgNativeLazyLoadPlugin({
    include: /\/new\/img\//,
    exclude: /logo|banner/  // 排除 logo 和 banner
})

Q: 如何强制所有图片立即加载?

A: 在 HTML 中添加 loading="eager"

<img src="..." loading="eager">

Q: 旧浏览器会怎样?

A: 旧浏览器会忽略 loading="lazy" 属性,图片正常显示,不影响用户体验。

推荐场景

✅ 新项目(现代浏览器为主)
✅ 图片较多的页面
✅ 追求简洁和性能的项目
✅ 不需要占位图效果的项目

不推荐场景

❌ 需要支持 IE 浏览器的项目
❌ 需要占位图效果的项目
❌ 需要精确控制加载时机的项目
❌ 需要更好 SEO 的项目(考虑使用自定义懒加载)

性能对比

使用原生懒加载后:

  • 首屏图片请求数减少 70%+
  • 页面加载时间减少 40%+
  • 带宽使用减少 60%+

更新日志

v1.0.0 (2025-10-22)

  • ✨ 初始版本
  • ✅ 支持 img 和 source 标签
  • ✅ 支持配置 include/exclude
  • ✅ 避免重复处理