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

webpack-plugin-html-append-tag

v1.0.1

Published

添加html模板中的内容

Downloads

1

Readme

添加html模板中的内容

webpack-html-plugin插件会在生成的模板文件追加额外的标签元素,如渲染框架需要的挂载元素。

用法

初始化:

npm install webpack-plugin-html-append-tag

使用:

const WebpackPluginHtmlAppendTag = require('webpack-plugin-html-append-tag');

module.exports = {
  // ...
  plugins: [
    new WebpackPluginHtmlAppendTag({
      body: {
        before: [{
          tagName: 'div',
          selfClosingTag: false,
          voidTag: false,
          attributes: {
            id: 'app',
          },
        }],
      },
    }),
  ],
};

将会生成的模板文件:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>测试</title>
  </head>
  <body>
  <!-- ...webpack打包的资源文件 -->
  <div id="app"></div>
  <!-- ...其他内容 -->
</body>
</html>

构造函数选项

选项为一个对象,可以设置为:

{ [tagName: 'body' | 'head']: Tags}

意为在html模板中body和head的标签中添加的标签内容。值类型Tags具体类型是:

 typeof htmlWebpackPlugin.tags.headTags[0] | typeof htmlWebpackPlugin.tags.bodyTags[0] | typeof htmlWebpackPlugin.tags.headTags | typeof htmlWebpackPlugin.tags.bodyTags | { [hookName: 'before' | 'after']: typeof htmlWebpackPlugin.tags.headTags | typeof htmlWebpackPlugin.tags.bodyTags }

可以配置为一个标签对象,标签数组或者指定追加位置的标签数组集。

标签对象为html-plugin-webpack提供在模板中使用的htmlWebpackPlugin对象中tagsheadTagsbodyTags,如:

const WebpackPluginHtmlAppendTag = require('webpack-plugin-html-append-tag');

module.exports = {
  // ...
  plugins: [
    new WebpackPluginHtmlAppendTag({
      body: {
        tagName: 'div',
        selfClosingTag: false,
        voidTag: false,
        attributes: {
          id: 'app',
        },
      },
      head: {
        tagName: 'link',
        selfClosingTag: false,
        voidTag: true,
        attributes: { rel: 'shortcut icon', href: 'favicon.ico' },
      },
    }),
  ],
};

也可以配置为一个标签数组,如:

const WebpackPluginHtmlAppendTag = require('webpack-plugin-html-append-tag');

module.exports = {
  // ...
  plugins: [
    new WebpackPluginHtmlAppendTag({
      body: [{
        tagName: 'div',
        selfClosingTag: false,
        voidTag: false,
        attributes: {
          id: 'app',
        },
      }, {
        tagName: 'span',
        selfClosingTag: false,
        voidTag: false,
        attributes: {
          class: 'red',
        },
      }],
      head: [{
        tagName: 'link',
        selfClosingTag: false,
        voidTag: true,
        attributes: { rel: 'shortcut icon', href: 'favicon.ico' },
      }, {
        tagName: 'link',
        selfClosingTag: false,
        voidTag: true,
        attributes: { rel: 'shortcut icon', href: 'favicon2.ico' },
      }],
    }),
  ],
};

还可以指定追加的位置,在webpack生成的一些标签之前还是之后,用于需要简单的顺序场景:

const WebpackPluginHtmlAppendTag = require('webpack-plugin-html-append-tag');

module.exports = {
  // ...
  plugins: [
    new WebpackPluginHtmlAppendTag({
      body: {
        before: [{
          tagName: 'div',
          selfClosingTag: false,
          voidTag: true,
          attributes: { id: 'app', style: 'color: red;' },
        }, {
          tagName: 'div',
          selfClosingTag: false,
          voidTag: true,
          attributes: { id: 'loading' },
        }],
        after: {
          tagName: 'iframe',
          selfClosingTag: false,
          voidTag: true,
          attributes: { src: 'http://path/to/advice' },
        },
      },
      head: {
        head: {
          before: {
            tagName: 'link',
            selfClosingTag: false,
            voidTag: true,
            attributes: { rel: 'shortcut icon', href: 'before.ico' },
          },
          after: [{
            tagName: 'link',
            selfClosingTag: false,
            voidTag: true,
            attributes: { rel: 'shortcut icon', href: 'after1.ico' },
          }, {
            tagName: 'link',
            selfClosingTag: false,
            voidTag: true,
            attributes: { rel: 'shortcut icon', href: 'after2.ico' },
          }],
      },
    }),
  ],
};