From d11a15e1636ea7c7f291d0cfbf73b75cc719e839 Mon Sep 17 00:00:00 2001 From: Fotis Koutoupas Date: Sun, 31 Dec 2017 05:51:25 +0200 Subject: [PATCH] Make htmlnano configurable for production (#445) * Parse htmlnano config Note that htmlnano fires only if Bundler.options.minify is true. * Add .htmlnanorc test * Fix naming --- src/transforms/posthtml.js | 10 +++--- test/html.js | 33 ++++++++++++++++++-- test/integration/htmlnano-config/.htmlnanorc | 5 +++ test/integration/htmlnano-config/index.html | 27 ++++++++++++++++ 4 files changed, 68 insertions(+), 7 deletions(-) create mode 100644 test/integration/htmlnano-config/.htmlnanorc create mode 100644 test/integration/htmlnano-config/index.html diff --git a/src/transforms/posthtml.js b/src/transforms/posthtml.js index b17b1525b16..dbbcdfae2dd 100644 --- a/src/transforms/posthtml.js +++ b/src/transforms/posthtml.js @@ -32,10 +32,12 @@ async function getConfig(asset) { config.plugins = await loadPlugins(config.plugins, asset.name); if (asset.options.minify) { - const htmlNanoOptions = { - collapseWhitespace: 'conservative' - }; - config.plugins.push(htmlnano(htmlNanoOptions)); + const htmlNanoConfig = asset.package.htmlnano || + (await Config.load(asset.name, ['.htmlnanorc', '.htmlnanorc.js'])) || { + collapseWhitespace: 'conservative' + }; + + config.plugins.push(htmlnano(htmlNanoConfig)); } config.skipParse = true; diff --git a/test/html.js b/test/html.js index 59763725948..dfcfe59e602 100644 --- a/test/html.js +++ b/test/html.js @@ -124,9 +124,36 @@ describe('html', function() { production: true }); - let css = fs.readFileSync(__dirname + '/dist/index.html', 'utf8'); - assert(css.includes('Other page')); - assert(!css.includes('\n')); + let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8'); + assert(html.includes('Other page')); + assert(!html.includes('\n')); + }); + + it('should read .htmlnanorc and minify HTML in production mode', async function() { + await bundle(__dirname + '/integration/htmlnano-config/index.html', { + production: true + }); + + let html = fs.readFileSync(__dirname + '/dist/index.html', 'utf8'); + + // mergeStyles + assert( + html.includes( + '' + ) + ); + + // minifyJson + assert( + html.includes('') + ); + + // minifySvg is false + assert( + html.includes( + 'SVG' + ) + ); }); it('should not prepend the public path to assets with remote URLs', async function() { diff --git a/test/integration/htmlnano-config/.htmlnanorc b/test/integration/htmlnano-config/.htmlnanorc new file mode 100644 index 00000000000..6c7e9749e8d --- /dev/null +++ b/test/integration/htmlnano-config/.htmlnanorc @@ -0,0 +1,5 @@ +{ + mergeStyles: true, + minifySvg: false, + minifyJson: true +} diff --git a/test/integration/htmlnano-config/index.html b/test/integration/htmlnano-config/index.html new file mode 100644 index 00000000000..cc738a33be0 --- /dev/null +++ b/test/integration/htmlnano-config/index.html @@ -0,0 +1,27 @@ + + + + Test + + + + + + + +

Index page

+ + + + + + SVG + + + + +