Skip to content

[WIP] fix(loader): resolve errors from #55 #78

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
33 changes: 18 additions & 15 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,38 +42,42 @@ module.exports = function(content) {
return attributes.indexOf(tag + ":" + attr) >= 0;
});
var links = [];
rawLinks.forEach(function (link) {
rawLinks.forEach(function(link) {
var length = link.length;
var start = link.start;
var valueList = link.value.split(",");
valueList.forEach(function (newLink) {
valueList.forEach(function(newLink) {
var trimmed = newLink.trim();
var cLength = newLink.length;
var spacePos = trimmed.indexOf(" ");
var spaceStart = newLink.indexOf(trimmed);
var len = cLength+ spaceStart;
if (-1 != spacePos) {
var len = cLength + spaceStart;
if(-1 != spacePos) {
len = spacePos + spaceStart;
trimmed = trimmed.substring(0,spacePos);
trimmed = trimmed.substring(0, spacePos);
}
links.push({start: start, length: len , value: trimmed});
start += cLength+1;
links.push({
start: start,
length: len,
value: trimmed
});
start += cLength + 1;
});
});
links.reverse();
var data = {};
content = [content];
links.forEach(function(link) {
var newValue = link.value.split(",");
var newValue = newValue.map(function (value) {
var interimValue = link.value.split(",");
var newValue = interimValue.map(function(value) {
var valueArray = value.trim().split(" ");
var obj = {
value: valueArray.shift(),
additional: valueArray,
};
if(!loaderUtils.isUrlRequest(obj.value, root)) return;
var uri = url.parse(obj.value);
if (uri.hash !== null && uri.hash !== undefined) {
if(uri.hash !== null && uri.hash !== undefined) {
obj.hash = uri.hash;
uri.hash = null;
obj.value = uri.format();
Expand All @@ -83,11 +87,10 @@ module.exports = function(content) {

do {
var ident = randomIdent();
} while(data[ident]);
} while (data[ident]);
data[ident] = newValue;
var x = content.pop();


content.push(x.substr(link.start + link.length));
content.push(ident);
content.push(x.substr(0, link.start));
Expand Down Expand Up @@ -126,14 +129,14 @@ module.exports = function(content) {
}
return "module.exports = " + content.replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
if(!data[match]) return match;
return data[match].reduce(function (pV,cV, index, array) {
return data[match].reduce(function(pV, cV, index, array) {

var hash = cV.hash || "";
var additional = cV.additional.length != 0 ? " " + cV.additional.join(" ") : "";
if (index != array.length -1) {
if(index != array.length - 1) {
additional += ",";
}
return pV + '" + require(' + JSON.stringify(loaderUtils.urlToRequest(cV.value, root)) + ') + "' + hash + additional;
},"");
}, "");
}) + ";";
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"pretest": "npm run lint && npm run beautify-lint",
"test": "mocha --harmony --full-trace --check-leaks",
"travis": "npm run cover -- --report lcovonly",
"lint": "eslint lib bin hot",
"lint": "eslint lib bin hot index.js",
"beautify-lint": "beautify-lint lib/**/*.js hot/**/*.js bin/**/*.js benchmark/*.js test/*.js",
"beautify": "beautify-rewrite lib/**/*.js hot/**/*.js bin/**/*.js benchmark/*.js test/*.js",
"postcover": "npm run lint && npm run beautify-lint",
Expand Down
18 changes: 6 additions & 12 deletions test/loaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,18 @@ describe("loader", function() {
'module.exports = "Text <script src=\\"" + require("./script.js") + "\\"><img src=\\"" + require("./image.png") + "\\">";'
);
});
it("should handle srcset-attrubute by default", function ()
{
loader.call({
},'Text <img srcset="image.png 1x">').should.be.eql(
it("should handle srcset-attrubute by default", function() {
loader.call({}, 'Text <img srcset="image.png 1x">').should.be.eql(
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x\\">";'
)
});
it("should handle srcset-attrubute with comma seperated list", function ()
{
loader.call({
},'Text <img srcset="image.png 1x,[email protected] 2x">').should.be.eql(
it("should handle srcset-attrubute with comma seperated list", function() {
loader.call({}, 'Text <img srcset="image.png 1x,[email protected] 2x">').should.be.eql(
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x,\" + require("./[email protected]") + " 2x\\">";'
)
});
it("should handle srcset-attrubute with comma seperated list, independend of spaces in list", function ()
{
loader.call({
},'Text <img srcset="image.png 1x, [email protected] 2x">').should.be.eql(
it("should handle srcset-attrubute with comma seperated list, independend of spaces in list", function() {
loader.call({}, 'Text <img srcset="image.png 1x, [email protected] 2x">').should.be.eql(
'module.exports = "Text <img srcset=\\"" + require("./image.png") + " 1x,\" + require("./[email protected]") + " 2x\\">";'
)
});
Expand Down