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 🙏

© 2024 – Pkg Stats / Ryan Hefner

html-webpack-assets-plugin

v1.2.0

Published

![npm bundle size](https://img.shields.io/bundlephobia/min/html-webpack-assets-plugin.svg) ![npm](https://img.shields.io/npm/dw/html-webpack-assets-plugin.svg) ![NPM](https://img.shields.io/npm/l/html-webpack-assets-plugin.svg?logo=green)

Downloads

14

Readme

npm bundle size npm NPM

  npm i --save-dev html-webpack-assets-plugin
  yarn add --dev html-webpack-assets-plugin

兼容性

  • webpack >=4
  • html-webpack-plugin >=3

警告 :warning::warning::warning:

!!!注意: 如果使用 [email protected] 的版本,需要把 html-webpack-plugin 放在 html-webpack-assets-plugin 之前, 确保 html-webpack-plugin 先于 assets-plugin 注册.

配置示例

webpack.config.js

const AssetsPlugin = require('html-webpack-assets-plugin')

module.exports = {
  entry: 'src/index.js',
  output: {
    path: __dirname + '/dist',
    filename: 'index_bundle.js'
  },
  plugins: [
    plugins: [
      new HtmlWebpackPlugin()
    ],
    new AssetsPlugin({
      // 可选, 给所有 script 添加属性
      attrs: {
        'data-all': 'all'
      },
      // 可选, 用于webpack 从入口打包而输出的 js 文件, 添加属性
      bundle: {
        'data-bundle': 'bundle'
      },

      //可选, 插入 script, assets 应该是远程地址, 因为不用有文件输出到打包目录
      online: [
        {
          append: true, // assets 列表的资源会添加到插入到后面, 默认false
          assets: [
            'http://a.b.com/aaa/index.js',
            'http://a.b.com/aaa/gc.js',
          ]
        },
        {
          assets: [
            'http://a.b.com/bbb/index.js',
            'http://a.b.com/bbb/gc.js',
          ]
        },
      ],
      // 可选
      // 插入script, src最终应该指向本地文件,
      // 本地资源会被复制到输出目录, 
      // 并对文件名添加 contentHash
      local: {
        root: './', // 本地资源的相对根目录目录, 默认为 当前工作目录,
        src: [
          './public/d3.js',
          './public/d100.js',
        ]
      }
    })
  ]
}

假设你有如下工作目录:

project
|---src
|---public
|   |---d3.js
|   |---d100.js
|---index.html
|---src
|   |---index.js
|---index.html
|---webpack.config.js

打包后 dist 的输出文件:

dist
|---index_bundle.js
|---d3.js
|---d100.js

搭配 html-webpack-plugin 后, 输出的 html 的 script 部分为:

...
<script data-all="all" src="http://a.b.com/bbb/index.js"></script>
<script data-all="all" src="http://a.b.com/bbb/gc.js"></script>
<script data-all="all" src="d3.js"></script>
<script data-all="all" src="d100.js"></script>
<script data-all="all" data-bundle="bundle" src="index_bundle.js"></script>
<script data-all="all" src="http://a.b.com/aaa/index.js"></script>
<script data-all="all" src="http://a.b.com/aaa/gc.js"></script>


...

options

attrs

  • 可选

为所有 script 添加 属性

{
  attrs: {
    'data-a': string
  },

}

bundle

  • 可选

webpack 入口打包生成的 script 标签 添加属性

{
  bundle: {
    'data-bundle': 'bundle'
  }

}

online

  • 可选

注意: 打包到输出目录

webpack 入口打包生成的 script 标签 添加属性

{
  online: [
    {
      append: true, // assets 列表的资源会添加到插入到后面, 默认false
      assets: [
        'http://a.b.com/aaa/index.js',
        'http://a.b.com/aaa/gc.js',
      ]
    },
    {
      assets: [
        'http://a.b.com/bbb/index.js',
        'http://a.b.com/bbb/gc.js',
      ]
    },
  ]

}

local

  • root: 可选, 指定文件的根目录, 值相对路径则为当前的工作目录,
  • src: 列出本地文件

webpack 入口打包生成的 script 标签 添加属性

{
  local: {
    root: <string>, // 可选, 默认: './', 
    src: Array<string>
  }

}

License

License