From ad00c8f666f9cbb4a4768236aaced5adc8e5b9d5 Mon Sep 17 00:00:00 2001 From: Philipp Dieter Date: Thu, 1 Nov 2018 22:05:51 +0100 Subject: [PATCH] [FEATURE] Replace tag content in script templates --- index.js | 100 +++++++++++++++++++++++++++------------------- package-lock.json | 4 +- 2 files changed, 60 insertions(+), 44 deletions(-) diff --git a/index.js b/index.js index a993c1f..2ee6bc1 100644 --- a/index.js +++ b/index.js @@ -12,60 +12,76 @@ function HtmlWebpackAlterDataPlugin (options) { HtmlWebpackAlterDataPlugin.prototype.apply = function (compiler) { 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) => { compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync('HtmlWebpackAlterDataPlugin', (data, cb) => { if ('chunkFilenameReplacement' in this.options) { - var html = cheerio.load(data.html, { + var co = 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 - } - 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 - }) - }) - + 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 = html.html() + data.html = co.html() } - data.html = data.html.replace('', '') cb(null, data) }) diff --git a/package-lock.json b/package-lock.json index c8dab3b..62912e7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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=" } }