From 4f76b36973aa62e34d40555da2a1ebc2e13dac4d Mon Sep 17 00:00:00 2001 From: HerrHase Date: Fri, 25 Nov 2022 18:58:05 +0100 Subject: [PATCH] adding options for html minify --- index.js | 7 ++++--- src/engine.js | 11 +++++++++++ src/happySite.js | 5 ++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 3fea1af..01ed4c8 100644 --- a/index.js +++ b/index.js @@ -3,11 +3,12 @@ const fs = require('fs') class HappySiteWebpackPlugin { - constructor(source, views, destination = null) { + constructor(source, views, destination = null, options = {}) { this._options = { source: source, views: views, - destination: destination + destination: destination, + options: options } if (!fs.existsSync(source)) { @@ -38,7 +39,7 @@ class HappySiteWebpackPlugin { this._options.destination = compilation.outputOptions.path } - const happySite = new HappySite(this._options.source, this._options.views, this._options.destination) + const happySite = new HappySite(this._options.source, this._options.views, this._options.destination, this._options.options) happySite.run() }) } diff --git a/src/engine.js b/src/engine.js index c9a85b3..60dfd9c 100644 --- a/src/engine.js +++ b/src/engine.js @@ -55,6 +55,17 @@ class Engine { data = Object.assign({}, data, this._defaults) this.nunjucks.render(view, data, (error, response) => { + + const options = configStore.get('options') + + if (options.minifyHtml === true) { + response = minify(response, { + removeComments: true, + collapseWhitespace: true, + collapseInlineTagWhitespace: true + }) + } + done(error, response) }) } diff --git a/src/happySite.js b/src/happySite.js index 96b3762..5cbc8c1 100644 --- a/src/happySite.js +++ b/src/happySite.js @@ -29,7 +29,7 @@ class HappySite { * @param {string} destination * */ - constructor(source, views, destination) { + constructor(source, views, destination, options = {}) { this._source = source this._views = views this._destination = destination @@ -37,6 +37,9 @@ class HappySite { configStore.set('source', source) configStore.set('destination', destination) configStore.set('views', views) + configStore.set('options', Object.assign({}, { + 'minifyHtml': true + }, options)) // get config for site if (fs.existsSync(this._source + '/site.yml')) {