Skip to content

Commit 759af52

Browse files
committed
feat: app the pretty and prettyOptions options to format the generated HTML
1 parent abfd926 commit 759af52

File tree

22 files changed

+280
-42
lines changed

22 files changed

+280
-42
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change log
22

3+
## 5.1.0 (2024-03-21)
4+
5+
- feat: app the `pretty` and `prettyOptions` options to format the generated HTML
6+
37
## 5.0.3 (2024-03-17)
48

59
- fix: catching of the error when a peer dependency for a Pug filter is not installed

README.md

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ See the [full list of features](https://github.com/webdiscus/html-bundler-webpac
7474
7575
### 📋 [Table of Contents](https://github.com/webdiscus/html-bundler-webpack-plugin#contents)
7676
77+
### ⚙️ [Pug Plugin options](#options)
78+
7779
### 📜 [History of Pug Plugin](#history-pug-plugin)
7880
7981
---
@@ -167,8 +169,66 @@ The generated HTML contains URLs of the output filenames:
167169
</html>
168170
```
169171

172+
<a id="options" name="options"></a>
173+
174+
## Pug Plugin options
175+
176+
The Pug plugin has all the [options](https://github.com/webdiscus/html-bundler-webpack-plugin?#plugin-options) of the `HTML Bundler Plugin`, plus a few options specific to Pug plugin.
177+
178+
<a id="option-pretty" name="option-pretty"></a>
179+
### `pretty`
180+
181+
Type: `'auto'|boolean|Object` Default: `false`
182+
183+
The Pug compiler generate minimized HTML.
184+
For formatting generated HTML is used the [js-beautify](https://github.com/beautifier/js-beautify) with the following `default options`:
185+
186+
```js
187+
{
188+
html: {
189+
indent_size: 2,
190+
end_with_newline: true,
191+
indent_inner_html: true,
192+
preserve_newlines: true,
193+
max_preserve_newlines: 0,
194+
wrap_line_length: 120,
195+
extra_liners: [],
196+
space_before_conditional: true,
197+
js: {
198+
end_with_newline: false,
199+
preserve_newlines: true,
200+
max_preserve_newlines: 2,
201+
space_after_anon_function: true,
202+
},
203+
css: {
204+
end_with_newline: false,
205+
preserve_newlines: false,
206+
newline_between_rules: false,
207+
},
208+
},
209+
}
210+
```
211+
212+
Possible values:
213+
214+
- `false` - disable formatting
215+
- `true` - enable formatting with default options
216+
- `'auto'` - in `development` mode enable formatting with default options, in `production` mode disable formatting,
217+
use [prettyOptions](#option-pretty-options) to customize options
218+
- `{}` - enable formatting with custom options, this object are merged with `default options`\
219+
see [options reference](https://github.com/beautifier/js-beautify#options)
220+
221+
222+
<a id="option-pretty-options" name="option-pretty-options"></a>
223+
### `prettyOptions`
224+
225+
Type: `Object` Default: `null`
226+
227+
When the [pretty](#option-pretty) option is set to `auto` or `true`, you can configure minification options using the `prettyOptions`.
228+
170229
<a id="history-pug-plugin" name="history-pug-plugin"></a>
171230

231+
---
172232
## History of Pug Plugin
173233

174234
**Why the Pug Plugin since `v5.0` based on [html-bundler-webpack-plugin][html-bundler-webpack-plugin]?**
@@ -223,7 +283,7 @@ module.exports = {
223283
};
224284
```
225285

226-
> The `pug-plugin`'s heart 🧡 now lives in the `html-bundler-webpack-plugin`'s body 🏋️‍♂️.
286+
> The `pug-plugin`'s heart now lives in the `html-bundler-webpack-plugin`.
227287
>
228288
> `@webdiscus/pug-loader` -> `pug-plugin` -> `html-bundler-webpack-plugin` -> `pug-plugin`
229289
>

package-lock.json

Lines changed: 48 additions & 27 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pug-plugin",
3-
"version": "5.0.3",
3+
"version": "5.1.0",
44
"description": "Pug plugin for webpack handles a template as an entry point, extracts CSS and JS from their sources referenced in Pug.",
55
"keywords": [
66
"html",
@@ -67,8 +67,8 @@
6767
},
6868
"dependencies": {
6969
"ansis": "2.0.3",
70-
"html-bundler-webpack-plugin": "3.6.4",
71-
"js-beautify": "^1.14.11",
70+
"html-bundler-webpack-plugin": "3.7.0",
71+
"js-beautify": "^1.15.1",
7272
"pug": "3.0.2"
7373
},
7474
"devDependencies": {

src/index.js

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,32 @@ const Config = require('html-bundler-webpack-plugin/Config');
55
Config.init(path.join(__dirname, './config.js'));
66

77
const HtmlBundlerPlugin = require('html-bundler-webpack-plugin');
8+
const formatHtml = require('js-beautify').html;
9+
10+
/**
11+
* Resolve undefined|true|false|''|'auto' value depend on current Webpack mode dev/prod.
12+
*
13+
* @param {boolean|string|undefined} value The value one of the values: true, false, 'auto'.
14+
* @param {boolean} autoDevValue Returns the autoValue in dev mode when value is 'auto'.
15+
* @param {boolean} autoProdValue Returns the autoValue in prod mode when value is 'auto'.
16+
* @param {boolean|string} defaultValue Returns default value when value is undefined.
17+
* @param {boolean} isProd Is production mode.
18+
* @return {boolean}
19+
*/
20+
const toBool = (value, { autoDevValue, autoProdValue, defaultValue, isProd }) => {
21+
if (value == null) value = defaultValue;
22+
// note: if a parameter is defined without a value or value is empty, then the value is true
23+
if (value === '' || value === 'true') return true;
24+
if (value === 'false') return false;
25+
if (value === true || value === false) return value;
26+
27+
if (value === 'auto') {
28+
if (!isProd && autoDevValue != null) return autoDevValue;
29+
if (isProd && autoProdValue != null) return autoProdValue;
30+
}
31+
32+
return false;
33+
};
834

935
/**
1036
* @typedef {PluginOptions} HtmlBundlerPluginOptions
@@ -19,7 +45,7 @@ class PugPlugin extends HtmlBundlerPlugin {
1945
test: /\.(pug|jade)$/, // default extensions for the pug plugin
2046
enabled: true,
2147
verbose: false,
22-
pretty: false, // new option for the pug plugin
48+
pretty: false, // formatting html for the pug plugin
2349
minify: false,
2450
minifyOptions: null,
2551
sourcePath: null,
@@ -45,7 +71,55 @@ class PugPlugin extends HtmlBundlerPlugin {
4571
* @param {Compiler} compiler The instance of the webpack compilation.
4672
*/
4773
init(compiler) {
48-
// TODO: do some thing in an extended plugin
74+
const pretty = PugPlugin.option.options.pretty;
75+
const userPrettyOptions = PugPlugin.option.options.prettyOptions;
76+
77+
// formatting options: https://github.com/beautifier/js-beautify
78+
const defaultPrettyOptions = {
79+
html: {
80+
indent_size: 2,
81+
end_with_newline: true,
82+
indent_inner_html: true,
83+
preserve_newlines: true,
84+
max_preserve_newlines: 0,
85+
wrap_line_length: 120,
86+
extra_liners: [],
87+
space_before_conditional: true,
88+
js: {
89+
end_with_newline: false,
90+
preserve_newlines: true,
91+
max_preserve_newlines: 2,
92+
space_after_anon_function: true,
93+
},
94+
css: {
95+
end_with_newline: false,
96+
preserve_newlines: false,
97+
newline_between_rules: false,
98+
},
99+
},
100+
};
101+
102+
let isPretty;
103+
let prettyOption;
104+
105+
if (pretty && typeof pretty !== 'string') {
106+
isPretty = true;
107+
prettyOption = pretty;
108+
} else if (userPrettyOptions != null) {
109+
isPretty = true;
110+
prettyOption = { ...defaultPrettyOptions, ...userPrettyOptions };
111+
} else {
112+
isPretty = toBool(pretty, {
113+
defaultValue: false,
114+
autoDevValue: true,
115+
isProd: PugPlugin.option.productionMode,
116+
});
117+
prettyOption = defaultPrettyOptions;
118+
}
119+
120+
if (isPretty) {
121+
PugPlugin.option.addProcess('postprocess', (content) => formatHtml(content, prettyOption));
122+
}
49123
}
50124
}
51125

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
4+
<head>
5+
<title>Test</title>
6+
<script src="js/main.c6fe1e02.js" defer="defer"></script>
7+
<script>(()=>{"use strict";var r,e={116:(r,e,o)=>{o.d(e,{Q:()=>t});const t=(r,e)=>r+e}},o={};function t(r){var n=o[r];if(void 0!==n)return n.exports;var i=o[r]={exports:{}};return e[r](i,i.exports,t),i.exports}t.d=(r,e)=>{for(var o in e)t.o(e,o)&&!t.o(r,o)&&Object.defineProperty(r,o,{enumerable:!0,get:e[o]})},t.o=(r,e)=>Object.prototype.hasOwnProperty.call(r,e),r=t(116),console.log(">> inline script from inline-main.js",(0,r.Q)(3,4))})();</script>
8+
</head>
9+
10+
<body>
11+
<h1>Home</h1>
12+
</body>
13+
14+
</html>

0 commit comments

Comments
 (0)