From da1c1335ae21b30b2f2735c83632d79c8bdfd16f Mon Sep 17 00:00:00 2001 From: Gregorio Magini Date: Sun, 5 Jul 2020 15:53:34 +0200 Subject: [PATCH] hmtl-loader 1.* compatibility --- index.js | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 77a4e6c..1e7cc15 100644 --- a/index.js +++ b/index.js @@ -43,22 +43,18 @@ module.exports = function (content) { .filter(Boolean) .join(pathSep) .replace(new RegExp(escapeRegExp(pathSep) + '+', 'g'), pathSep); - var html; - if (content.match(/^module\.exports/)) { - var firstQuote = findQuote(content, false); - var secondQuote = findQuote(content, true); - html = content.substr(firstQuote, secondQuote - firstQuote + 1); + var codeStart = "var path = '"+jsesc(filePath)+"';\n"; + var codeEnd = (requireAngular ? "var angular = require('angular');\n" : "window.") + + "angular.module('" + ngModule + "').run(['$templateCache', function(c) { c.put(path, code) }]);\n" + + 'module.exports = path;'; + + if (content.match(/module\.exports/)) { + return codeStart + content.replace('module.exports = code;', codeEnd); } else { - html = content; + return codeStart + 'var code = "' + content + '";\n' + codeEnd; } - return "var path = '"+jsesc(filePath)+"';\n" + - "var html = " + html + ";\n" + - (requireAngular ? "var angular = require('angular');\n" : "window.") + - "angular.module('" + ngModule + "').run(['$templateCache', function(c) { c.put(path, html) }]);\n" + - "module.exports = path;"; - function getAndInterpolateOption(optionKey, def) { return options[optionKey] ? loaderUtils.interpolateName(this, options[optionKey], { @@ -69,17 +65,6 @@ module.exports = function (content) { : def } - function findQuote(content, backwards) { - var i = backwards ? content.length - 1 : 0; - while (i >= 0 && i < content.length) { - if (content[i] === '"' || content[i] === "'") { - return i; - } - i += backwards ? -1 : 1; - } - return -1; - } - // source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#Using_Special_Characters function escapeRegExp(string) { return string.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1");