Skip to content

Commit 58bea83

Browse files
committed
Merge pull request webpack-contrib#46 from lukasgeiter/support-complex-config
Add support for complex config options
2 parents b3947cd + 30db815 commit 58bea83

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",
@@ -82,7 +91,7 @@ module.exports = function(content) {
8291
content = htmlMinifier.minify(content, minimizeOptions);
8392
}
8493

85-
if (query.interpolate) {
94+
if (config.interpolate) {
8695
content = compile('`' + content + '`').code;
8796
} else {
8897
content = JSON.stringify(content);

test/loaderTest.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ describe("loader", function() {
6565
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\\"\" + require("./image.png") + "\\\"/>";'
6666
);
6767
});
68+
it("should accept complex options via a webpack config property", function() {
69+
loader.call({
70+
minimize: true,
71+
options: {
72+
htmlLoader: {
73+
ignoreCustomFragments: [/\{\{.*?}}/]
74+
}
75+
}
76+
}, '<h3>{{ count <= 1 ? "foo" : "bar" }}</h3>').should.be.eql(
77+
'module.exports = "<h3>{{ count <= 1 ? \\"foo\\" : \\"bar\\" }}</h3>";'
78+
);
79+
});
80+
it("should allow the webpack config property name to be configured", function() {
81+
loader.call({
82+
minimize: true,
83+
options: {
84+
htmlLoaderSuperSpecialConfig: {
85+
ignoreCustomFragments: [/\{\{.*?}}/]
86+
}
87+
},
88+
query: '?config=htmlLoaderSuperSpecialConfig'
89+
}, '<h3>{{ count <= 1 ? "foo" : "bar" }}</h3>').should.be.eql(
90+
'module.exports = "<h3>{{ count <= 1 ? \\"foo\\" : \\"bar\\" }}</h3>";'
91+
);
92+
});
6893
it("should not translate root-relative urls (without root query)", function() {
6994
loader.call({}, 'Text <img src="/image.png">').should.be.eql(
7095
'module.exports = "Text <img src=\\"/image.png\\">";'

0 commit comments

Comments
 (0)