Skip to content

Commit 30db815

Browse files
committed
Add support for complex config options
1 parent 5fb1cf3 commit 30db815

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

index.js

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,31 @@ function randomIdent() {
1414
return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
1515
};
1616

17+
function getLoaderConfig(context) {
18+
var query = loaderUtils.parseQuery(context.query);
19+
var configKey = query.config || 'htmlLoader';
20+
var config = context.options && context.options.hasOwnProperty(configKey) ? context.options[configKey] : {};
21+
22+
delete query.config;
23+
24+
return assign(query, config);
25+
}
1726

1827
module.exports = function(content) {
1928
this.cacheable && this.cacheable();
20-
var query = loaderUtils.parseQuery(this.query);
29+
var config = getLoaderConfig(this);
2130
var attributes = ["img:src"];
22-
if(query.attrs !== undefined) {
23-
if(typeof query.attrs === "string")
24-
attributes = query.attrs.split(" ");
25-
else if(Array.isArray(query.attrs))
26-
attributes = query.attrs;
27-
else if(query.attrs === false)
31+
if(config.attrs !== undefined) {
32+
if(typeof config.attrs === "string")
33+
attributes = config.attrs.split(" ");
34+
else if(Array.isArray(config.attrs))
35+
attributes = config.attrs;
36+
else if(config.attrs === false)
2837
attributes = [];
2938
else
30-
throw new Error("Invalid value to query parameter attrs");
39+
throw new Error("Invalid value to config parameter attrs");
3140
}
32-
var root = query.root;
41+
var root = config.root;
3342
var links = attrParse(content, function(tag, attr) {
3443
return attributes.indexOf(tag + ":" + attr) >= 0;
3544
});
@@ -57,8 +66,8 @@ module.exports = function(content) {
5766
});
5867
content.reverse();
5968
content = content.join("");
60-
if(typeof query.minimize === "boolean" ? query.minimize : this.minimize) {
61-
var minimizeOptions = assign({}, query);
69+
if(typeof config.minimize === "boolean" ? config.minimize : this.minimize) {
70+
var minimizeOptions = assign({}, config);
6271

6372
[
6473
"removeComments",
@@ -78,7 +87,7 @@ module.exports = function(content) {
7887
content = htmlMinifier.minify(content, minimizeOptions);
7988
}
8089

81-
if (query.interpolate) {
90+
if (config.interpolate) {
8291
content = compile('`' + content + '`').code;
8392
} else {
8493
content = JSON.stringify(content);

test/loaderTest.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,31 @@ describe("loader", function() {
5757
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\\"\" + require("./image.png") + "\\\">";'
5858
);
5959
});
60+
it("should accept complex options via a webpack config property", function() {
61+
loader.call({
62+
minimize: true,
63+
options: {
64+
htmlLoader: {
65+
ignoreCustomFragments: [/\{\{.*?}}/]
66+
}
67+
}
68+
}, '<h3>{{ count <= 1 ? "foo" : "bar" }}</h3>').should.be.eql(
69+
'module.exports = "<h3>{{ count <= 1 ? \\"foo\\" : \\"bar\\" }}</h3>";'
70+
);
71+
});
72+
it("should allow the webpack config property name to be configured", function() {
73+
loader.call({
74+
minimize: true,
75+
options: {
76+
htmlLoaderSuperSpecialConfig: {
77+
ignoreCustomFragments: [/\{\{.*?}}/]
78+
}
79+
},
80+
query: '?config=htmlLoaderSuperSpecialConfig'
81+
}, '<h3>{{ count <= 1 ? "foo" : "bar" }}</h3>').should.be.eql(
82+
'module.exports = "<h3>{{ count <= 1 ? \\"foo\\" : \\"bar\\" }}</h3>";'
83+
);
84+
});
6085
it("should not translate root-relative urls (without root query)", function() {
6186
loader.call({}, 'Text <img src="/image.png">').should.be.eql(
6287
'module.exports = "Text <img src=\\"/image.png\\">";'

0 commit comments

Comments
 (0)