[FEATURE] Replace tag content in script templates
This commit is contained in:
parent
95fff9cd24
commit
ad00c8f666
52
index.js
52
index.js
@ -12,23 +12,13 @@ function HtmlWebpackAlterDataPlugin (options) {
|
|||||||
|
|
||||||
HtmlWebpackAlterDataPlugin.prototype.apply = function (compiler) {
|
HtmlWebpackAlterDataPlugin.prototype.apply = function (compiler) {
|
||||||
var self = this
|
var self = this
|
||||||
compiler.hooks.compilation.tap('HtmlWebpackAlterDataPlugin', (compilation) => {
|
|
||||||
|
|
||||||
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlWebpackAlterDataPlugin', (data, cb) => {
|
function processHtml(co, compilation) {
|
||||||
|
self.options.chunkFilenameReplacement.forEach(function(replacement) {
|
||||||
if ('chunkFilenameReplacement' in this.options) {
|
console.log('='.repeat(30))
|
||||||
var html = cheerio.load(data.html, {
|
console.log(replacement.tag)
|
||||||
recognizeSelfClosing: false,
|
co(replacement.tag).each(function(i, elem) {
|
||||||
lowerCaseTags: false,
|
var tag = co(elem)[0]
|
||||||
lowerCaseAttributeNames: false,
|
|
||||||
})
|
|
||||||
this.options.chunkFilenameReplacement.forEach(function(replacement) {
|
|
||||||
var tags = html(replacement.tag)
|
|
||||||
for (key in tags) {
|
|
||||||
var tag = tags[key]
|
|
||||||
if (tag.type != 'tag') {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if ( (!tag)
|
if ( (!tag)
|
||||||
|| (!('attribs' in tag))
|
|| (!('attribs' in tag))
|
||||||
|| (!(replacement.attribute in tag.attribs))
|
|| (!(replacement.attribute in tag.attribs))
|
||||||
@ -61,11 +51,37 @@ HtmlWebpackAlterDataPlugin.prototype.apply = function (compiler) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
data.html = html.html()
|
})
|
||||||
|
return co
|
||||||
}
|
}
|
||||||
|
|
||||||
|
compiler.hooks.compilation.tap('HtmlWebpackAlterDataPlugin', (compilation) => {
|
||||||
|
|
||||||
|
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlWebpackAlterDataPlugin', (data, cb) => {
|
||||||
|
|
||||||
|
if ('chunkFilenameReplacement' in this.options) {
|
||||||
|
var co = cheerio.load(data.html, {
|
||||||
|
recognizeSelfClosing: false,
|
||||||
|
lowerCaseTags: false,
|
||||||
|
lowerCaseAttributeNames: false,
|
||||||
|
})
|
||||||
|
co = processHtml(co, compilation)
|
||||||
|
co('script').each(function(i, elem) {
|
||||||
|
if (co(elem).attr('type') != 'text/x-template') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
var script = cheerio.load(co(elem).html(), {
|
||||||
|
recognizeSelfClosing: false,
|
||||||
|
lowerCaseTags: false,
|
||||||
|
lowerCaseAttributeNames: false,
|
||||||
|
})
|
||||||
|
var scriptResult = processHtml(script, compilation).html()
|
||||||
|
console.log(scriptResult)
|
||||||
|
co(co('script').get(i)).html(scriptResult)
|
||||||
|
})
|
||||||
|
data.html = co.html()
|
||||||
|
}
|
||||||
data.html = data.html.replace('<!DOCTYPE html5>', '<!DOCTYPE html>')
|
data.html = data.html.replace('<!DOCTYPE html5>', '<!DOCTYPE html>')
|
||||||
cb(null, data)
|
cb(null, data)
|
||||||
})
|
})
|
||||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -34,7 +34,7 @@
|
|||||||
},
|
},
|
||||||
"css-select": {
|
"css-select": {
|
||||||
"version": "1.2.0",
|
"version": "1.2.0",
|
||||||
"resolved": "http://registry.npmjs.org/css-select/-/css-select-1.2.0.tgz",
|
"resolved": "https://registry.npmjs.org/css-select/-/css-select-1.2.0.tgz",
|
||||||
"integrity": "sha1-KzoRBTnFNV8c2NMUYj6HCxIeyFg=",
|
"integrity": "sha1-KzoRBTnFNV8c2NMUYj6HCxIeyFg=",
|
||||||
"requires": {
|
"requires": {
|
||||||
"boolbase": "~1.0.0",
|
"boolbase": "~1.0.0",
|
||||||
@ -59,7 +59,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"domelementtype": {
|
"domelementtype": {
|
||||||
"version": "1.1.3",
|
"version": "1.1.3",
|
||||||
"resolved": "http://registry.npmjs.org/domelementtype/-/domelementtype-1.1.3.tgz",
|
"resolved": "https://registry.npmjs.org/domelementtype/-/domelementtype-1.1.3.tgz",
|
||||||
"integrity": "sha1-vSh3PiZCiBrsUVRJJCmcXNgiGFs="
|
"integrity": "sha1-vSh3PiZCiBrsUVRJJCmcXNgiGFs="
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user