html-webpack-inject-code-plugin
v1.0.1
Published
Inject Code tag for the HTML Webpack Plugin
Downloads
4
Maintainers
Readme
Inject Code tag for the HTML Webpack Plugin
This is an extension plugin for the webpack plugin html-webpack-plugin (version 3.x or 4 or higher). It allows you to embed javascript and css source inline.
Installation
You must be running webpack on node 6 or higher.
Install the plugin with npm:
$ npm install --save-dev html-webpack-inject-code-plugin
Basic Usage
Require the plugin in your webpack config:
var HtmlWebpackInjectCodePlugin = require('html-webpack-inject-code-plugin');
Add the plugin to your webpack config as follows:
plugins: [
new HtmlWebpackPlugin(),
new HtmlWebpackInjectCodePlugin()
]
The above configuration will actually do nothing due to the configuration defaults.
Options
The available options are:
headTags
|bodyTags
:array
tagsName
:string
, Specifies the code extensions to use to determine. One of thescript
orstyle
values;tagsCode
:string
Inserted specific code.
Example
config code
plugins: [
new HtmlWebpackPlugin(),
new HtmlWebpackInjectCodePlugin({
headTags: [{
tagName: 'script',
tagCode: `var startTime = +new Date();`
}],
bodyTags: [{
tagName: 'script',
tagCode: `var endTime = +new Date();`
}]
})
]
Source html code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Example</title>
</head>
<body>
<noscript>
<strong>很抱歉,如果没有启用JavaScript,此项目将无法正常运行。请启用它。</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
</body>
</html>
Build compiled code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Example</title>
<script type="text/javascript">var startTime = +new Date();</script>
</head>
<body>
<noscript>
<strong>很抱歉,如果没有启用JavaScript,此项目将无法正常运行。请启用它。</strong>
</noscript>
<div id="app"></div>
<!-- built files will be auto injected -->
<script type="text/javascript">var endTime = +new Date();</script>
</body>
</html>