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

code-insert-html-webpack-plugin

v0.1.7

Published

insert code to htmlfile for html-webpack-plugin

Downloads

16

Readme

code-insert-html-webpack-plugin

TODO: description

Usage

该插件用于向html-webpack-plugin生成的html文件中插入代码片段,

const codeInsertPlugin = require('code-insert-html-webpack-plugin');

new CodeInsertPlugin({
    sourcePath: './add.html.js', // 默认代码片段文件位置 /add.html.js
    defaultAnchor: '</head>', // 默认代码插入位置
    htmlNamePrefix: '', // 默认为空字符串
})

sourcePath

指向承载 代码片段 的 【代码片段配置文件】的位置路径,该路径为相对于项目根目录的相对路径

defaultAnchor

是默认的代码片段【插入位置】

插入位置

字符串或正则表达式,指将制定的代码片段插入某个位置之前,当为正则表达式时,则会插入到全部匹配到的位置之前

htmlNamePrefix

代码片段文件中导出的对象属性名 与 期望对应的真实文件名 的差距,该前缀将加入到代码片段模块全部的标识html文件名的属性名之前

<html>
    <head>
    <-- something inserted -->
    </head>
    <body>
    </body>
</html>

代码片段配置文件:

支持commonJS、ESM两种风格,以cjs风格为例,文件导出一个键值对对象,该对象为文件属性的键名为 html-webpack-plugin 生成的 index 文件名(或匹配文件名的正则表达式,优先以字符串类型解释键名),键值为待插入匹配的html文件的多个代码片段形成的对象,其中,’before‘ ’after‘表示插入时机( html-webpack-plugin的两个hook:html-webpack-plugin-before-html-processing、html-webpack-plugin-after-html-processing ),每个html文件可插入若干代码片段,每段代码片段对应一个【代码片段对象】,【代码片段对象】详见下文

// 该模块在执行时可获得 webpack 配置对象,process对象,可调用require导入文件模块
const { mode, entry } = global.webpack;
const { defaultEnv } = require('./config/constant.js');

const AMap = mode !== defaultEnv
    ? '' : `<script type="text/javascript" src="//webapi.amap.com/maps"></script>`;

const env = `<script>
  +function (g) {
    var GLOBAL_CONFIG = {
      ENV: ${mode}
    }

    g.__ENV__ = {
      get: function (gN) {
        return GLOBAL_CONFIG[gN];
      }
    }
  }(window);
</script>`;


module.exports = {
    // 通过entry做默认设置
    ...Object.keys(entry).reduce((res, entryName) => {
        res[entryName] = {
            before: [{
                scripts: [AMap],
                anchor: '</head>',
            },],
            after: [{
                scripts: [env],
                anchor: '</body>',
            },],
        }
        return res;
    }, {}),
    // 具名页面的方式覆盖上述的默认设置中的某些特殊页面
    'from-one': {
        before: [{
            scripts: [env, AMap],
            anchor: '</head>',
        },],
    },
    'from-two': {
        before: [{
            scripts: [env],
            anchor: '</head>',
        }, {
            scripts: [AMap],
            anchor: '</body>',
        },],
    },
}

代码片段对象

具有两个属性,scripts是代码片段字符串形成的数组,anchor为为该代码片段数组单独设定的【插入位置】

代码片段

要插入的代码字符串

导出的对象的属性名(例如’from-two‘)

会被解释为文件名(要对应的html文件的文件名)和正则表达式的字面量字符串,当某html文件名与导出对象的某个属性名相同,则插入这个属性名所对应的的代码片段,否则则用html文件名去匹配正则表达式,首先被匹配到的导出对象的属性名所对应的代码片段将被插入html文件中,匹配顺序为按照导出文件属性名的排列顺序自上而下

插件为代码片段文件提供的全局函数

global.require:即 nodejs 提供的 require 函数;

在代码片段文件中,global.require 不等于 require ,global.require要求传入绝对路径用来寻找目标文件 global.webpack:即 webpack 的 compiler 实例的 options 属性; global.env:即构建进程的 process.env; global.console:即构建进程上下文的console对象;

TODO

有时 该插件 会与 ’@babel/plugin-transform-runtime’ 产生冲突

解决方式:使用 ‘export default’ 替代 ‘module.exports’,

尽量不要将页面名称写为‘default’