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 🙏

© 2026 – Pkg Stats / Ryan Hefner

omg-inject-html-webpack-plugin

v2.0.0

Published

A webpack 5 plugin for custom HTML resource injection with inline script support

Readme

omg-inject-html-webpack-plugin

基于 html-webpack-plugin 扩展的可自定义注入资源路径的 webpack 插件。

依赖

| 依赖 | 版本要求 | |---|---| | webpack | >= 5.0.0 | | html-webpack-plugin | >= 5.0.0 | | Node.js | >= 18.0.0 |

安装

npm install omg-inject-html-webpack-plugin html-webpack-plugin --save-dev

基本使用

const HtmlWebpackPlugin = require('html-webpack-plugin');
const InjectHtmlWebpackPlugin = require('omg-inject-html-webpack-plugin');

module.exports = {
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/html/page.hbs',
            filename: './dist/html/page.html',
            xhtml: true
        }),
        new InjectHtmlWebpackPlugin()
    ]
};

ESM 导入:

import HtmlWebpackPlugin from 'html-webpack-plugin';
import InjectHtmlWebpackPlugin from 'omg-inject-html-webpack-plugin';

默认情况下,InjectHtmlWebpackPlugin 会将 HtmlWebpackPlugin 的 options.inject 设置为 false,并通过模板变量注入资源。

跨域配置

在 webpack output 中配置 crossOriginLoading,插件会自动将 crossorigin 属性注入到生成的 <script><link> 标签中:

module.exports = {
    output: {
        crossOriginLoading: 'anonymous'
    }
};

Options

| 参数 | 类型 | 默认值 | 说明 | |---|---|---|---| | inject | boolean | false | 设为 true 则使用 HtmlWebpackPlugin 默认注入行为 | | htmlDir | string | '' | HTML 模板目录路径 | | templateParameters | object | {} | 额外的模板参数,会合并到模板变量中 |

模板支持

  • EJS(默认支持)
  • Handlebars

EJS 模板示例

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Webpack App</title>
    <% assets.css.forEach(function(file){ %>
      <%= file %>
    <% }); %>
  </head>
  <body>
    <div id='root'></div>
    <% assets.js.forEach(function(file){ %>
      <%= file %>
    <% }); %>
  </body>
</html>

Handlebars 模板示例

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Webpack App</title>
  {{#each assets.css}}
    {{{this}}}
  {{/each}}
</head>
<body>
  <div id='root'></div>
  {{#each assets.js}}
    {{{this}}}
  {{/each}}
</body>
</html>

Inline 模式

将资源直接内联注入 HTML,支持对 node_modules 中的包进行 inline:

module.exports = {
    entry: {
        app: './src/app.js',
        tracker: 'some-package/tracker.js?__inline'
    },
    plugins: [
        new HtmlWebpackPlugin({
            template: './src/html/page.hbs',
            filename: './dist/html/page.html',
            xhtml: true,
            chunks: ['app'],
        }),
        new InjectHtmlWebpackPlugin()
    ]
};

模板中使用:

<head>
  {{{inline.tracker}}}
  {{#each assets.css}}
    {{{this}}}
  {{/each}}
</head>

注意: inline 模式直接读取文件内容注入,不会经过 webpack 编译。建议仅对 node_modules 中的包使用。

模板变量

inject: false(默认)时,模板中可用的变量:

{
    assets: {
        js: ['<script src="..." crossorigin="anonymous"></script>', ...],
        css: ['<link href="..." rel="stylesheet" crossorigin="anonymous"/>', ...],
        manifest: undefined,
        favicon: undefined
    },
    inline: {
        tracker: '<script>...inline content...</script>'
    },
    // ...custom templateParameters
}

迁移指南 (v1.x → v2.0)

  1. 升级 Node.js 至 >= 18.0.0
  2. 升级 webpack 至 >= 5.0.0
  3. 单独安装 html-webpack-plugin@^5.0.0(现在是 peerDependency)
  4. 插件 API 无变化,直接替换即可

License

ISC