Skip to content

Commit d311109

Browse files
committed
Merge pull request webpack-contrib#34 from morhetz/master
Allow to pass minimize options via query params
2 parents 2a4388a + ae7d908 commit d311109

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

index.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ var attrParse = require("./lib/attributesParser");
77
var SourceNode = require("source-map").SourceNode;
88
var loaderUtils = require("loader-utils");
99
var url = require("url");
10+
var assign = require("object-assign");
1011

1112
function randomIdent() {
1213
return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx";
@@ -57,16 +58,24 @@ module.exports = function(content) {
5758
content.reverse();
5859
content = content.join("");
5960
if(typeof query.minimize === "boolean" ? query.minimize : this.minimize) {
60-
content = htmlMinifier.minify(content, {
61-
removeComments: query.removeComments !== false,
62-
collapseWhitespace: query.collapseWhitespace !== false,
63-
collapseBooleanAttributes: query.collapseBooleanAttributes !== false,
64-
removeAttributeQuotes: query.removeAttributeQuotes !== false,
65-
removeRedundantAttributes: query.removeRedundantAttributes !== false,
66-
useShortDoctype: query.useShortDoctype !== false,
67-
removeEmptyAttributes: query.removeEmptyAttributes !== false,
68-
removeOptionalTags: query.removeOptionalTags !== false
61+
var minimizeOptions = assign({}, query);
62+
63+
[
64+
"removeComments",
65+
"collapseWhitespace",
66+
"collapseBooleanAttributes",
67+
"removeAttributeQuotes",
68+
"removeRedundantAttributes",
69+
"useShortDoctype",
70+
"removeEmptyAttributes",
71+
"removeOptionalTags"
72+
].forEach(function(name) {
73+
if (typeof minimizeOptions[name] === "undefined") {
74+
minimizeOptions[name] = true;
75+
}
6976
});
77+
78+
content = htmlMinifier.minify(content, minimizeOptions);
7079
}
7180
return "module.exports = " + JSON.stringify(content).replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) {
7281
if(!data[match]) return match;

package.json

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"author": "Tobias Koppers @sokra",
55
"description": "html loader module for webpack",
66
"dependencies": {
7-
"html-minifier": "^0.7.2",
8-
"source-map": "0.1.x",
97
"fastparse": "^1.0.0",
10-
"loader-utils": "~0.2.2"
8+
"html-minifier": "^0.7.2",
9+
"loader-utils": "~0.2.2",
10+
"object-assign": "^4.0.1",
11+
"source-map": "0.1.x"
1112
},
1213
"devDependencies": {
1314
"mocha": "1.17.x",

test/loaderTest.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,24 @@ describe("loader", function() {
3737
it("should minimize", function() {
3838
loader.call({
3939
minimize: true
40-
}, '<!-- comment --><h3>#{number} {customer}</h3>\n<p> {title} </p>\n\t <!-- comment --> <img src="image.png" />').should.be.eql(
41-
'module.exports = "<h3>#{number} {customer}</h3><p>{title}</p><img src=" + require("./image.png") + ">";'
40+
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3>\n<p> {title} </p>\n\t <!-- comment --> <img src="image.png" />').should.be.eql(
41+
'module.exports = "<h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\\"\" + require("./image.png") + "\\\">";'
42+
);
43+
});
44+
it("should preserve comments", function() {
45+
loader.call({
46+
minimize: true,
47+
query: "?-removeComments"
48+
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3>\n<p> {title} </p>\n\t <!-- comment --> <img src="image.png" />').should.be.eql(
49+
'module.exports = "<!-- comment --><h3 customattr=\\"\\">#{number} {customer}</h3><p>{title}</p><!-- comment --><img src=\\"\" + require("./image.png") + "\\\">";'
50+
);
51+
});
52+
it("should treat attributes as case sensitive", function() {
53+
loader.call({
54+
minimize: true,
55+
query: "?caseSensitive"
56+
}, '<!-- comment --><h3 customAttr="">#{number} {customer}</h3>\n<p> {title} </p>\n\t <!-- comment --> <img src="image.png" />').should.be.eql(
57+
'module.exports = "<h3 customAttr=\\"\\">#{number} {customer}</h3><p>{title}</p><img src=\\"\" + require("./image.png") + "\\\">";'
4258
);
4359
});
4460
it("should not translate root-relative urls (without root query)", function() {

0 commit comments

Comments
 (0)