diff --git a/README.md b/README.md
index ad4c427..a43b85c 100644
--- a/README.md
+++ b/README.md
@@ -8,18 +8,22 @@ add custom attributes to inject tags
### use
+please use it after `html-webpack-plugin`, especially in webpack2+.
+
add to all inject tags
```javascript
plugins = [
new htmlWebpackInjectAttributesPlugin({
- inject: "true"
+ inject: "true",
+ async: true,
+ test: {}
}) // Object, key should be string, value can be string or function
]
```
you got
```html
-
+
```
add to chunks in HtmlWebpackPlugin
diff --git a/index.js b/index.js
index 09ecf02..e4a6994 100644
--- a/index.js
+++ b/index.js
@@ -10,6 +10,8 @@ function addAttr(compilation, tags, key, val) {
var value = val;
if (typeof val === "function") {
value = val(tag, compilation, index);
+ } else if (typeof val === 'object') {
+ value = JSON.stringify(val);
}
tag.attributes[key] = value;
});
@@ -17,9 +19,6 @@ function addAttr(compilation, tags, key, val) {
function alterAssetTags(compilation, htmlPluginData, callback) {
var options = assign({}, this.options, htmlPluginData.plugin.options && htmlPluginData.plugin.options.attributes);
forEach(options, function (val, key) {
- if (typeof val !== 'string' && typeof val !== 'function') {
- return;
- }
addAttr(compilation, htmlPluginData.head, key, val);
addAttr(compilation, htmlPluginData.body, key, val);
});
@@ -37,9 +36,19 @@ function htmlWebpackInjectAttributesPlugin(options) {
htmlWebpackInjectAttributesPlugin.prototype.apply = function (compiler) {
var self = this;
- compiler.plugin('compilation', function (compilation) {
- compilation.plugin('html-webpack-plugin-alter-asset-tags', alterAssetTags.bind(self, compilation));
- });
+
+ if (compiler.hooks) {
+ compiler.hooks.compilation.tap("htmlWebpackInjectAttributesPlugin", function (compilation) {
+ compilation
+ .hooks
+ .htmlWebpackPluginAlterAssetTags
+ .tap("htmlWebpackInjectAttributesPlugin", alterAssetTags.bind(self, compilation));
+ });
+ } else {
+ compiler.plugin('compilation', function (compilation) {
+ compilation.plugin('html-webpack-plugin-alter-asset-tags', alterAssetTags.bind(self, compilation));
+ });
+ }
};
module.exports = htmlWebpackInjectAttributesPlugin;
\ No newline at end of file