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

html-loader-withhook

v5.2.0

Published

Enhanced html-loader with tag/attribute hooks support

Readme

html-loader-withhook

Enhanced HTML loader with Hooks support for webpack.

新增功能

  • 支持标签处理器 Hook
  • 支持属性处理器 Hook
  • 继承原 html-loader 所有功能

安装

npm install --save-dev html-loader-withhook

基础配置

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader-withhook",
      },
    ],
  },
};

Hook 选项

sources.tags

类型:

type TagHandler = (
  params: {
    value: string;
    startOffset: number;
    endOffset: number;
    resourcePath: string;
  },
  sources: Array<Source>,
) => boolean | void;

type tags = {
  [tagName: string]: TagHandler;
};

允许为特定标签注册处理器:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader-withhook",
        options: {
          sources: {
            tags: {
              // 处理 img 标签
              img: (params, sources) => {
                sources.push({
                  name: "data-src",
                  value: params.value,
                  isValueQuoted: false,
                  startOffset: params.startOffset,
                  endOffset: params.endOffset,
                });
                return true; // 返回 true 表示处理完成,阻断后续处理
              },
            },
          },
        },
      },
    ],
  },
};

sources.attributes

类型:

type AttributeHandler = (
  params: {
    tagName: string;
    value: string;
    startOffset: number;
    endOffset: number;
    resourcePath: string;
  },
  sources: Array<Source>,
) => boolean | void;

type attributes = {
  [attributeName: string]: AttributeHandler;
};

允许为特定属性注册处理器:

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader-withhook",
        options: {
          sources: {
            attributes: {
              // 处理 src 属性
              src: (params, sources) => {
                if (params.tagName === "img") {
                  sources.push({
                    name: "data-src",
                    value: params.value,
                    isValueQuoted: true,
                    startOffset: params.startOffset,
                    endOffset: params.endOffset,
                  });
                  return true; //返回 true 表示处理完成,阻断后续处理
                }
              },
            },
          },
        },
      },
    ],
  },
};

示例

图片懒加载处理

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader-withhook",
        options: {
          sources: {
            tags: {
              img: (params, sources) => {
                // 将 src 转换为 data-src
                sources.push({
                  name: "data-src",
                  value: params.value,
                  isValueQuoted: true,
                  startOffset: params.startOffset,
                  endOffset: params.endOffset,
                });
                return true;
              },
            },
          },
        },
      },
    ],
  },
};

自定义属性处理

module.exports = {
  module: {
    rules: [
      {
        test: /\.html$/i,
        loader: "html-loader-withhook",
        options: {
          sources: {
            attributes: {
              "custom-src": (params, sources) => {
                sources.push({
                  name: "src",
                  value: `/assets/${params.value}`,
                  isValueQuoted: true,
                  startOffset: params.startOffset,
                  endOffset: params.endOffset,
                });
                return true;
              },
            },
          },
        },
      },
    ],
  },
};

Hook 执行流程

  1. 解析HTML注释忽略标记
  2. 执行标签级hook(返回true则跳过后续处理)
  3. 执行属性级hook(返回true则跳过默认处理)
  4. 应用原始处理规则

错误处理机制

Hook 执行过程中的错误会被收集并通过 webpack 的错误系统报告。每个错误都包含:

  • Hook 的类型(标签/属性)
  • Hook 的名称
  • 错误信息

继承功能

html-loader-withhook 完全兼容原版 html-loader 的所有功能,包括:

  • 源码处理
  • 预处理器
  • 后处理器
  • 最小化
  • ES 模块支持

详细说明请参考原版 html-loader 文档

License

MIT