1.0.5 • Published 3 years ago
html-inline-code-plugin v1.0.5
HtmlInlineCodePlugin
使用该插件,可以给模板html插入指定code。
当然,更简单的方式是,直接在模板html中直接写入具体的code。
Usage
const HtmlInlineCodePlugin = require('html-inline-code-plugin');
plugins: [
// with `html-webpack-plugin`
new HtmlWebpackPlugin(),
new HtmlInlineCodePlugin()
]
// or
// 参数为 js 代码
new HtmlInlineCodePlugin(`
console.log('Inject body code!');
`)
// head 标签下插入inline style
new HtmlInlineCodePlugin({
inject: 'head',
tag: 'style',
code: `.div{ background: red; }`
})
new HtmlInlineCodePlugin([
{ inject: 'head', tag: 'style', code: '' },
{ inject: 'body', tag: 'style', code: '' },
{ inject: 'head', tag: 'script', code: '' },
{ inject: 'body', tag: 'script', code: '' },
{ inject: 'body', tag: 'script', begin: false, code: '' }
])
// script src
new HtmlInlineCodePlugin([
{ inject: 'body', tag: 'script', attr: { src: 'brace/index.js' } }
])
options
参数可为 对象或者数组以及 code
, 对象结构如下:
params | type | default | description |
---|---|---|---|
inject | string | body | 代码注入的位置,可选值为 head 和 body |
tag | string | - | 注入代码的具体标签,可选值为 style 和 script |
code | string | - | 具体的代码。当 attr.src 存在时,code 将不会被注入 |
begin | boolean | true | 注入顺序,默认在最前面 |
attr | Object | - | 内置属性,仅在 script 时起效,可用于配置 src、type、async 等 更多 |
当参数为具体的 code 时,只支持注入 js code。
example
webpack.config.js
plugins: [
...
new HtmlWebpackPlugin(),
new InlineCodePlugin({
begin: false,
tag: 'script',
inject: 'body',
code: `
var rrr = new MyDemo();
rrr.init();
`
}),
...
]
tpl.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
</head>
<body>
<div id="app"></div>
</body>
</html>
result: index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>demo</title>
<link href="/static/css/app.bad4bff9.css" rel="stylesheet">
</head>
</head>
<body>
<div id="app"></div>
<script type="text/javascript" src="/common.js"></script><script type="text/javascript" src="/app.e8b549110e71b9bad246.js"></script>
<script type="text/javascript">
var rrr = new MyDemo();
rrr.init();
</script>
</body>
</html>
changelog
- 2021-3-5 v1.0.4 modify
inline code plugin cb