[FEATURE] Replace tag content in script templates

This commit is contained in:
Philipp Dieter 2018-11-01 22:05:51 +01:00
parent 95fff9cd24
commit ad00c8f666
2 changed files with 60 additions and 44 deletions

100
index.js
View File

@ -12,60 +12,76 @@ function HtmlWebpackAlterDataPlugin (options) {
HtmlWebpackAlterDataPlugin.prototype.apply = function (compiler) { HtmlWebpackAlterDataPlugin.prototype.apply = function (compiler) {
var self = this var self = this
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))
) {
return
}
var url = tag.attribs[replacement.attribute]
if (url[0] != '~') {
return
}
url = url.substring(1)
var hash = url.slice(url.indexOf('#'))
var path = url.slice(0, url.indexOf('#'))
var file = path.slice(url.lastIndexOf('/') + 1)
var path = path.slice(0, url.lastIndexOf('/') + 1)
var filename = file.slice(0, file.indexOf('.'))
var extension = file.slice(file.indexOf('.') + 1)
compilation.chunks.forEach(function(chunk) {
if (chunk.name != filename) {
return
}
chunk.files.forEach(function(file) {
if (!file.startsWith(path)) {
return
}
if (!file.endsWith('.' + extension)) {
return
}
tag.attribs[replacement.attribute] = '/' + file + hash
})
})
})
})
return co
}
compiler.hooks.compilation.tap('HtmlWebpackAlterDataPlugin', (compilation) => { compiler.hooks.compilation.tap('HtmlWebpackAlterDataPlugin', (compilation) => {
compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlWebpackAlterDataPlugin', (data, cb) => { compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlWebpackAlterDataPlugin', (data, cb) => {
if ('chunkFilenameReplacement' in this.options) { if ('chunkFilenameReplacement' in this.options) {
var html = cheerio.load(data.html, { var co = cheerio.load(data.html, {
recognizeSelfClosing: false, recognizeSelfClosing: false,
lowerCaseTags: false, lowerCaseTags: false,
lowerCaseAttributeNames: false, lowerCaseAttributeNames: false,
}) })
this.options.chunkFilenameReplacement.forEach(function(replacement) { co = processHtml(co, compilation)
var tags = html(replacement.tag) co('script').each(function(i, elem) {
for (key in tags) { if (co(elem).attr('type') != 'text/x-template') {
var tag = tags[key] return;
if (tag.type != 'tag') {
return
}
if ( (!tag)
|| (!('attribs' in tag))
|| (!(replacement.attribute in tag.attribs))
) {
return
}
var url = tag.attribs[replacement.attribute]
if (url[0] != '~') {
return
}
url = url.substring(1)
var hash = url.slice(url.indexOf('#'))
var path = url.slice(0, url.indexOf('#'))
var file = path.slice(url.lastIndexOf('/') + 1)
var path = path.slice(0, url.lastIndexOf('/') + 1)
var filename = file.slice(0, file.indexOf('.'))
var extension = file.slice(file.indexOf('.') + 1)
compilation.chunks.forEach(function(chunk) {
if (chunk.name != filename) {
return
}
chunk.files.forEach(function(file) {
if (!file.startsWith(path)) {
return
}
if (!file.endsWith('.' + extension)) {
return
}
tag.attribs[replacement.attribute] = '/' + file + hash
})
})
} }
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 = html.html() 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
View File

@ -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="
} }
} }