html-webpack-injector
Advanced tools
Comparing version 1.0.6 to 1.1.1
54
index.js
@@ -8,3 +8,3 @@ const HtmlWebpackPlugin = require("html-webpack-plugin"); | ||
chunks.forEach(chunk => { | ||
if (chunk.attributes.src && chunk.attributes.src.includes("_head")) { | ||
if ((chunk.attributes.src && chunk.attributes.src.includes("_head")) || chunk.attributes.href) { | ||
headChunks.push(chunk); | ||
@@ -19,2 +19,36 @@ } else { | ||
function addAttributesToTag(tag, name, attributes) { | ||
if (tag.attributes.src) { | ||
const regex = new RegExp(`(\/${name}\\.)|(${name}\\.)`); | ||
if (tag.attributes.src.match(regex)) { | ||
tag.attributes = {...tag.attributes, ...attributes} | ||
} | ||
} | ||
} | ||
function handleChunksConfig(data, tags) { | ||
if (data.plugin.options.chunksConfig) { | ||
const asyncNames = data.plugin.options.chunksConfig.async; | ||
const deferNames = data.plugin.options.chunksConfig.defer; | ||
if (typeof asyncNames === "object" && typeof deferNames === "object") { | ||
tags.forEach(tag => { | ||
// add async/defer only on script tags. | ||
if (!tag.attributes.href && tag.attributes.src) { | ||
asyncNames.forEach(name => { | ||
addAttributesToTag(tag, name, {async: true}); | ||
}); | ||
deferNames.forEach(name => { | ||
addAttributesToTag(tag, name, {defer: true}); | ||
}) | ||
} | ||
}); | ||
} else { | ||
console.log("-------------------------------------"); | ||
console.error("Invalid value given to chunksConfig option"); | ||
console.log("-------------------------------------"); | ||
} | ||
} | ||
} | ||
class HtmlWebpackInjectorPlugin { | ||
@@ -28,6 +62,8 @@ apply(compiler) { | ||
const ch = getHeadAndBodyChunks([...data.headTags, ...data.bodyTags]); | ||
const tags = [...data.bodyTags, ...data.headTags]; | ||
handleChunksConfig(data, tags); | ||
const chunks = getHeadAndBodyChunks(tags); | ||
data.headTags = ch.headChunks; | ||
data.bodyTags = ch.bodyChunks; | ||
data.headTags = chunks.headChunks; | ||
data.bodyTags = chunks.bodyChunks; | ||
@@ -43,6 +79,8 @@ callback(null, data) | ||
const ch = getHeadAndBodyChunks([...data.head, ...data.body]); | ||
const tags = [...data.body, ...data.head]; | ||
handleChunksConfig(data, tags); | ||
const chunks = getHeadAndBodyChunks(tags); | ||
data.head = ch.headChunks; | ||
data.body = ch.bodyChunks; | ||
data.head = chunks.headChunks; | ||
data.body = chunks.bodyChunks; | ||
}); | ||
@@ -54,2 +92,2 @@ }); | ||
module.exports = HtmlWebpackInjectorPlugin; | ||
module.exports = HtmlWebpackInjectorPlugin; |
{ | ||
"name": "html-webpack-injector", | ||
"version": "1.0.6", | ||
"version": "1.1.1", | ||
"description": "Helps to inject chunks to head and body via HtmlWebpackPlugin", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
21131
75