Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hmtl-loader 1.* compatibility #87

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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], {
Expand All @@ -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");
Expand Down