网络套接字插件
一个网络包插件可以动态地将代码注入捆绑.
您可以查看一个实例 这里
使用量
# 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');"
});
]
};
这个webpack插件接受一个单一的参数,这个函数返回代码以注入捆绑.
此功能与加载器的上下文相同, 所以所有 这里 适用.
您可以返回原始内容来装入, 或者 Promise 它决定了内容, 如果你想成为同步.
选项
您还可以通过更多选项 :
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.
});
选项。 输入Name
string|function
用于输入代码的过滤器 .
如果有的话 string,只使用同名条目.
如果有的话 function时,每个条目名都使用它 事实 回应
前辈.
new InjectPlugin(loader, {
// This will inject code into every entry that's not named `foo`
entryName: key => key !== 'foo'
});
选项。负载ID
string
注射式加载器的可选单相ID. 如果省略, 将自动为您生成一个 .
额外使用大小写
虽然这可以用作独立的插件,但您也可以使用它来创建其他的webpack插件,比如在配置文件的基础上将代码注入到构建中.
示例
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);
}
}
贡献者
感谢这些伟大的人emoji 密钥:
亚当·迪尔肯斯 💻 | 黄克比 💻 | 克温 💻 |
|---|---|---|
本项目遵循 全部贡献者 规格。 任何捐款都欢迎!