@bpnetguy/webpack-inject-plugin

中文文档
2.0.4发布于 2022/07/23MITTypeScriptCommonJS零依赖近 30 天下载 1.8万

A webpack plugin to dynamically inject code into the bundle.

原始使用文档
文档目录6 节
  1. Usage
  2. options
  3. options.entryName
  4. options.loaderID
  5. Additional Use Cases
  6. Contributors

webpack-inject-plugin

All Contributors CircleCI npm version npm XO code style codecov

A webpack plugin to dynamically inject code into the bundle.

You can check out an example here

Usage

# webpack.config.js

const InjectPlugin = require('webpack-inject-plugin').default;


module.exports = {
    // Other stuff in your webpack config

    plugins: [
        new InjectPlugin(function() {
            return "console.log('Hello World');"
        });
    ]
};

This webpack plugin accepts a single argument, a function to which returns the code to inject into the bundle.

The function is called using the same context as the loader, so everything here applies.

You can either return the raw content to load, or a Promise which resolves to the content, if you wish to be async.

options

You can also pass in more options:

import InjectPlugin, { ENTRY_ORDER } from 'webpack-inject-plugin';

new InjectPlugin(loader, {
    entryName: 'entry name',         //  Limit the injected code to only the entry w/ this name
    entryOrder: ENTRY_ORDER.First    //  Make the injected code be the first entry point
                ENTRY_ORDER.Last     //  Make the injected code be the last entry point
                ENTRY_ORDER.NotLast  //  Make the injected code be second to last. (The last entry module is the API of the bundle. Useful when you don't want to override that.) This is the default.
});

options.entryName

string | function

A filter for which entries to inject code into. If a string, only an entry with the same name will be used. If a function, it will be called with each entry name -- and only inject code for each truthy response

ex.

new InjectPlugin(loader, {
  // This will inject code into every entry that's not named `foo`
  entryName: key => key !== 'foo'
});

options.loaderID

string

An optional uniquie ID for the injected loader. If omitted, one will automatically be generated for you.

Additional Use Cases

Though this could be used as a standalone plugin, you could also use it to create other webpack plugins, such as injecting code into the build based on a config file.

Example:

import InjectPlugin from 'webpack-inject-plugin';

function customLoader(options) {
  return () => {
    return "console.log('My custom code generated from `options`');";
  };
}

export default class MyPlugin {
  constructor(options) {
    this.options = options;
  }

  apply(compiler) {
    new InjectPlugin(customLoader(this.options)).apply(compiler);
  }
}

Contributors

Thanks goes to these wonderful people (emoji key):

Adam Dierkens
Adam Dierkens

💻
YellowKirby
YellowKirby

💻
Chvin
Chvin

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

版本与发布记录

@bpnetguy/webpack-inject-plugin 共发布 5 个版本,首次发布于 2022/07/21;过去 12 个月发布了 0 个版本。

版本发布日期距今
2.0.4latest2022/07/234 年前
2.0.32022/07/234 年前
2.0.22022/07/214 年前
2.0.12022/07/214 年前
2.0.02022/07/214 年前

关于 @bpnetguy/webpack-inject-plugin 的常见问题

@bpnetguy/webpack-inject-plugin 是什么?

A webpack plugin to dynamically inject code into the bundle.

如何安装 @bpnetguy/webpack-inject-plugin?

在项目目录运行 npm install @bpnetguy/webpack-inject-plugin,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:webpack(>=5.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@bpnetguy/webpack-inject-plugin 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@bpnetguy/webpack-inject-plugin 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@bpnetguy/webpack-inject-plugin 可以商用吗?

@bpnetguy/webpack-inject-plugin 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@bpnetguy/webpack-inject-plugin 的下载量有多少?

根据 npm 官方下载统计,@bpnetguy/webpack-inject-plugin 在 2026/10/09 – 2026/11/07 这 30 天内被下载 18,249 次,日均约 608 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bpnetguy/webpack-inject-plugin 还在维护吗?

最新版本 2.0.4 发布于 2022/07/23(4 年前);至今共发布 5 个版本,其中过去 12 个月发布了 0 个;首次发布于 2022/07/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。