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

inject-html-webpack-plugin3

v1.0.0

Published

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

Downloads

2

Readme

InjectHtmlWebpackPlugin

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

依赖

  • webpack - 4.x
  • html-webpack-plugin - 4.0.0-beta.8

安装

npm install inject-html-webpack-plugin2 --save-dev

基本使用

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

默认情况下,使用InjectHtmlWebpackPlugin插件,会默认将HtmlWebpackPlugin的options.inject设置为false,并交由InjectHtmlWebpackPlugin设置模板变量传入。

在webpack配置中配置资源跨域

在webpack配置对资源进行跨域配置,最终也可以注入到模板变量的script和link标签中

{
    output: {
        crossOriginLoading: 'anonymous'
    }
}

options

  • inject - 默认为false,如果设置为true,那么将不会生成新的模板变量,使用HtmlWebpackPlugin自己的模板变量

模板支持

  • ejs(默认支持)
  • handlebars

例子

{
    entry: {
        app: './example/inline/src/app.js',
        app2: './example/inline/src/app2.js',
    },

    ...忽略若干配置

    plugins: [
        new HtmlWebpackPlugin( {
            template: `./src/html/page.hbs`,
            filename: './dist/html/page.html',
            xhtml: true,
            chunks: ['app'],
        } ),
        new HtmlWebpackPlugin( {
            template: `./src/html/page2.hbs`,
            filename: './dist/html/page2.html',
            xhtml: true,
            chunks: ['app2'],
        } ),
        new InjectHtmlWebpackPlugin()
    ]
}

那么实际上会生成一个这样的对象注入模板,

app模板属性
{ 
    js: [ 
        '<script src="https://imgcdn50.zuzuche.com/static/js/react-runtime-f6f38831f74b9a7f3775.js" crossorigin="anonymous"></script>',
        '<script src="https://imgcdn50.zuzuche.com/static/js/vender~app~app2-b0e019ca74a52cfa7c7f.js" crossorigin="anonymous"></script>',
        '<script src="https://imgcdn50.zuzuche.com/static/js/style~app-6ca9de33c0d05e1f278d.js" crossorigin="anonymous"></script>',
        '<script src="https://imgcdn50.zuzuche.com/static/js/app-ab23bfa0dea17ff99171.js" crossorigin="anonymous"></script>' 
    ],
    css: [ 
        '<link href="https://imgcdn50.zuzuche.com/static/css/style~app-752d171e573587446e3b.css" rel="stylesheet" crossorigin="anonymous"/>' 
    ],
    manifest: undefined,
    favicon: undefined 
}

app2模板属性
{ 
    js: [ 
        '<script src="https://imgcdn50.zuzuche.com/static/js/react-runtime-f6f38831f74b9a7f3775.js" crossorigin="anonymous"></script>',
        '<script src="https://imgcdn50.zuzuche.com/static/js/vender~app~app2-b0e019ca74a52cfa7c7f.js" crossorigin="anonymous"></script>',
        '<script src="https://imgcdn50.zuzuche.com/static/js/style~app2-cd35f66a9ad216025fc5.js" crossorigin="anonymous"></script>',
        '<script src="https://imgcdn50.zuzuche.com/static/js/app2-e3e6d564b273b5f42b43.js" crossorigin="anonymous"></script>'
    ],
    css: [ 
        '<link href="https://imgcdn50.zuzuche.com/static/css/style~app2-f0d566be024b468994ec.css" rel="stylesheet" crossorigin="anonymous"/>'
    ],
    manifest: undefined,
    favicon: undefined 
}

在模板中使用变量注入资源

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注入

  • webpack配置entry入口特殊处理
  • 建议只对node_modules中的包进行inline
  • inline会直接读取文件内容进行注入,并不会注入经过webpack编译的代码
{
    entry: {
        app: './example/inline/src/app.js',
        eaentry: '@eagleeye-jssdk/loader/zuzuche.js?__inline'
    },

    ...忽略若干配置

    plugins: [
        new HtmlWebpackPlugin( {
            template: `./src/html/page.hbs`,
            filename: './dist/html/page.html',
            xhtml: true,
            chunks: ['app'],
        } ),
        new InjectHtmlWebpackPlugin()
    ]
}

模板引入

<!DOCTYPE html>
<html>

<head>
  <meta charset="UTF-8">
  <title>Webpack App</title>
  {{> './tpl/head.handlebars'}}
  {{{inline.eaentry}}}
  {{#each assets.css}}
    {{{this}}}
  {{/each}}
</head>

<body>
  <div id='root'></div>
  {{#each assets.js}}
    {{{this}}}
  {{/each}}
</body>

</html>