Skip to content

Commit

Permalink
Add support for complex config options
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasgeiter committed Dec 29, 2015
1 parent 5fb1cf3 commit 30db815
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 12 deletions.
33 changes: 21 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,31 @@ function randomIdent() {
return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
};

function getLoaderConfig(context) {
var query = loaderUtils.parseQuery(context.query);
var configKey = query.config || 'htmlLoader';
var config = context.options && context.options.hasOwnProperty(configKey) ? context.options[configKey] : {};

delete query.config;

return assign(query, config);
}

module.exports = function(content) {
this.cacheable && this.cacheable();
var query = loaderUtils.parseQuery(this.query);
var config = getLoaderConfig(this);
var attributes = ["img:src"];
if(query.attrs !== undefined) {
if(typeof query.attrs === "string")
attributes = query.attrs.split(" ");
else if(Array.isArray(query.attrs))
attributes = query.attrs;
else if(query.attrs === false)
if(config.attrs !== undefined) {
if(typeof config.attrs === "string")
attributes = config.attrs.split(" ");
else if(Array.isArray(config.attrs))
attributes = config.attrs;
else if(config.attrs === false)
attributes = [];
else
throw new Error("Invalid value to query parameter attrs");
throw new Error("Invalid value to config parameter attrs");
}
var root = query.root;
var root = config.root;
var links = attrParse(content, function(tag, attr) {
return attributes.indexOf(tag + ":" + attr) >= 0;
});
Expand Down Expand Up @@ -57,8 +66,8 @@ module.exports = function(content) {
});
content.reverse();
content = content.join("");
if(typeof query.minimize === "boolean" ? query.minimize : this.minimize) {
var minimizeOptions = assign({}, query);
if(typeof config.minimize === "boolean" ? config.minimize : this.minimize) {
var minimizeOptions = assign({}, config);

[
"removeComments",
Expand All @@ -78,7 +87,7 @@ module.exports = function(content) {
content = htmlMinifier.minify(content, minimizeOptions);
}

if (query.interpolate) {
if (config.interpolate) {
content = compile('`' + content + '`').code;
} else {
content = JSON.stringify(content);
Expand Down
25 changes: 25 additions & 0 deletions test/loaderTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,31 @@ describe("loader", function() {
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\\"\" + require("./image.png") + "\\\">";'
);
});
it("should accept complex options via a webpack config property", function() {
loader.call({
minimize: true,
options: {
htmlLoader: {
ignoreCustomFragments: [/\{\{.*?}}/]
}
}
}, '<h3>{{ count <= 1 ? "foo" : "bar" }}</h3>').should.be.eql(
'module.exports = "<h3>{{ count <= 1 ? \\"foo\\" : \\"bar\\" }}</h3>";'
);
});
it("should allow the webpack config property name to be configured", function() {
loader.call({
minimize: true,
options: {
htmlLoaderSuperSpecialConfig: {
ignoreCustomFragments: [/\{\{.*?}}/]
}
},
query: '?config=htmlLoaderSuperSpecialConfig'
}, '<h3>{{ count <= 1 ? "foo" : "bar" }}</h3>').should.be.eql(
'module.exports = "<h3>{{ count <= 1 ? \\"foo\\" : \\"bar\\" }}</h3>";'
);
});
it("should not translate root-relative urls (without root query)", function() {
loader.call({}, 'Text <img src="/image.png">').should.be.eql(
'module.exports = "Text <img src=\\"/image.png\\">";'
Expand Down

0 comments on commit 30db815

Please sign in to comment.