[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) {
|
||||
var self = this
|
||||
compiler.hooks.compilation.tap('HtmlWebpackAlterDataPlugin', (compilation) => {
|
||||
|
||||
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlWebpackAlterDataPlugin', (data, cb) => {
|
||||
|
||||
if ('chunkFilenameReplacement' in this.options) {
|
||||
var html = cheerio.load(data.html, {
|
||||
recognizeSelfClosing: false,
|
||||
lowerCaseTags: false,
|
||||
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
|
||||
}
|
||||
function processHtml(co, compilation) {
|
||||
self.options.chunkFilenameReplacement.forEach(function(replacement) {
|
||||
console.log('='.repeat(30))
|
||||
console.log(replacement.tag)
|
||||
co(replacement.tag).each(function(i, elem) {
|
||||
var tag = co(elem)[0]
|
||||
if ( (!tag)
|
||||
|| (!('attribs' in tag))
|
||||
|| (!(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>')
|
||||
cb(null, data)
|
||||
})
|
||||
|
4
package-lock.json
generated
4
package-lock.json
generated
@ -34,7 +34,7 @@
|
||||
},
|
||||
"css-select": {
|
||||
"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=",
|
||||
"requires": {
|
||||
"boolbase": "~1.0.0",
|
||||
@ -59,7 +59,7 @@
|
||||
"dependencies": {
|
||||
"domelementtype": {
|
||||
"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="
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user