From b6565c4fa49e059fadb5d52ad551e293ee55183d Mon Sep 17 00:00:00 2001 From: Pavel Pertsev Date: Tue, 27 Oct 2015 21:19:13 +0300 Subject: [PATCH 1/2] fix tests --- test/loaderTest.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/loaderTest.js b/test/loaderTest.js index 104d11d6..2ccd5f3f 100644 --- a/test/loaderTest.js +++ b/test/loaderTest.js @@ -38,7 +38,7 @@ describe("loader", function() { loader.call({ minimize: true }, '

#{number} {customer}

\n

{title}

\n\t ').should.be.eql( - 'module.exports = "

#{number} {customer}

{title}

";' + 'module.exports = "

#{number} {customer}

{title}

";' ); }); it("should not translate root-relative urls (without root query)", function() { From ae7d9086eaae941aebaab1c74f0e19ab995c684d Mon Sep 17 00:00:00 2001 From: Pavel Pertsev Date: Tue, 27 Oct 2015 21:33:44 +0300 Subject: [PATCH 2/2] pass query minimize options to html minifier --- index.js | 27 ++++++++++++++++++--------- package.json | 7 ++++--- test/loaderTest.js | 20 ++++++++++++++++++-- 3 files changed, 40 insertions(+), 14 deletions(-) diff --git a/index.js b/index.js index 928ee2ab..1bb3ad44 100644 --- a/index.js +++ b/index.js @@ -7,6 +7,7 @@ var attrParse = require("./lib/attributesParser"); var SourceNode = require("source-map").SourceNode; var loaderUtils = require("loader-utils"); var url = require("url"); +var assign = require("object-assign"); function randomIdent() { return "xxxHTMLLINKxxx" + Math.random() + Math.random() + "xxx"; @@ -57,16 +58,24 @@ module.exports = function(content) { content.reverse(); content = content.join(""); if(typeof query.minimize === "boolean" ? query.minimize : this.minimize) { - content = htmlMinifier.minify(content, { - removeComments: query.removeComments !== false, - collapseWhitespace: query.collapseWhitespace !== false, - collapseBooleanAttributes: query.collapseBooleanAttributes !== false, - removeAttributeQuotes: query.removeAttributeQuotes !== false, - removeRedundantAttributes: query.removeRedundantAttributes !== false, - useShortDoctype: query.useShortDoctype !== false, - removeEmptyAttributes: query.removeEmptyAttributes !== false, - removeOptionalTags: query.removeOptionalTags !== false + var minimizeOptions = assign({}, query); + + [ + "removeComments", + "collapseWhitespace", + "collapseBooleanAttributes", + "removeAttributeQuotes", + "removeRedundantAttributes", + "useShortDoctype", + "removeEmptyAttributes", + "removeOptionalTags" + ].forEach(function(name) { + if (typeof minimizeOptions[name] === "undefined") { + minimizeOptions[name] = true; + } }); + + content = htmlMinifier.minify(content, minimizeOptions); } return "module.exports = " + JSON.stringify(content).replace(/xxxHTMLLINKxxx[0-9\.]+xxx/g, function(match) { if(!data[match]) return match; diff --git a/package.json b/package.json index 7a0d4c65..b742d2e0 100644 --- a/package.json +++ b/package.json @@ -4,10 +4,11 @@ "author": "Tobias Koppers @sokra", "description": "html loader module for webpack", "dependencies": { - "html-minifier": "^0.7.2", - "source-map": "0.1.x", "fastparse": "^1.0.0", - "loader-utils": "~0.2.2" + "html-minifier": "^0.7.2", + "loader-utils": "~0.2.2", + "object-assign": "^4.0.1", + "source-map": "0.1.x" }, "devDependencies": { "mocha": "1.17.x", diff --git a/test/loaderTest.js b/test/loaderTest.js index 2ccd5f3f..a95fef9f 100644 --- a/test/loaderTest.js +++ b/test/loaderTest.js @@ -37,8 +37,24 @@ describe("loader", function() { it("should minimize", function() { loader.call({ minimize: true - }, '

#{number} {customer}

\n

{title}

\n\t ').should.be.eql( - 'module.exports = "

#{number} {customer}

{title}

";' + }, '

#{number} {customer}

\n

{title}

\n\t ').should.be.eql( + 'module.exports = "

#{number} {customer}

{title}

";' + ); + }); + it("should preserve comments", function() { + loader.call({ + minimize: true, + query: "?-removeComments" + }, '

#{number} {customer}

\n

{title}

\n\t ').should.be.eql( + 'module.exports = "

#{number} {customer}

{title}

";' + ); + }); + it("should treat attributes as case sensitive", function() { + loader.call({ + minimize: true, + query: "?caseSensitive" + }, '

#{number} {customer}

\n

{title}

\n\t ').should.be.eql( + 'module.exports = "

#{number} {customer}

{title}

";' ); }); it("should not translate root-relative urls (without root query)", function() {