Skip to content

Commit 379a8b1

Browse files
committed
addresses #27
- added clean-html to beautify html content before write - added .nvmrc
1 parent 7a2c9a3 commit 379a8b1

File tree

5 files changed

+153
-13
lines changed

5 files changed

+153
-13
lines changed

.nvmrc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lts/erbium
2+

README.md

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,30 @@ module.exports = {
4848
```
4949
<br/>
5050

51-
>**Important - Make sure that the partials to be included are not in the same path as the `source` configuration. For the above sample, your partials must not be in `./src/templates`.\
52-
Refer issue - https://github.com/Vishal0203/file-include-webpack-plugin/issues/18#issuecomment-635701581**
51+
>**Important** - Make sure that the partials to be included are not in the same path as the `source` configuration. For the above sample, your partials must not be in `./src/templates`.\
52+
Refer issue - https://github.com/Vishal0203/file-include-webpack-plugin/issues/18#issuecomment-635701581
53+
54+
#### How to pass custom html cleaner options
55+
56+
This plugin uses [clean-html](https://www.npmjs.com/package/clean-html) for html beautification.
57+
58+
Use the config `cleanerOptions` to pass custom cleaning / beautifier options. Refer to the package `clean-html` to know more about the possible options.
59+
60+
Example config -
61+
```javascript
62+
module.exports = {
63+
plugins: [
64+
new FileIncludeWebpackPlugin(
65+
{
66+
source: './src/templates',
67+
cleanerOptions: {
68+
'add-break-around-tags': ['span']
69+
}
70+
},
71+
)
72+
]
73+
}
74+
```
5375

5476

5577
#### How to change the output destination?

package-lock.json

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

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "file-include-webpack-plugin",
3-
"version": "1.3.5",
3+
"version": "1.3.6",
44
"description": "replace @@include partials with content",
55
"main": "src/index.js",
66
"scripts": {
@@ -18,6 +18,7 @@
1818
"node": ">=6.11.5"
1919
},
2020
"dependencies": {
21+
"clean-html": "^1.5.0",
2122
"fs": "0.0.1-security",
2223
"path": "^0.12.7"
2324
},

src/index.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
const path = require('path')
22
const fs = require('fs')
3+
const cleaner = require('clean-html')
34
const utils = require('./utils')
45

6+
const htmlCleanerOpts = {
7+
'add-break-around-tags': ['li', 'ul']
8+
}
9+
510
class FileIncludeWebpackPlugin {
611
constructor(config) {
7-
this.source = config.source // source from the context
12+
// source from the context
13+
this.source = config.source
814
this.replace = config.replace
915
this.destination = config.destination
1016
this.context = null
17+
this.cleanerOptions = {
18+
...htmlCleanerOpts,
19+
...(config.cleanerOptions || {}),
20+
}
1121

1222
// handlers
1323
this.process = this.process.bind(this)
@@ -53,16 +63,18 @@ class FileIncludeWebpackPlugin {
5363

5464
utils.logger.info(`Working on ${files.length} .html files`)
5565

56-
files.forEach(file => {
66+
for (const file of files) {
5767
const sourcePath = path.join(this.context, file)
5868
const destinationPath = this.destination ? path.join(this.destination, file) : file
5969
const content = this.processFile(compilation, this.context, sourcePath)
6070

61-
compilation.assets[destinationPath] = {
62-
source: () => content,
63-
size: () => content.length
64-
}
65-
})
71+
cleaner.clean(content, this.cleanerOptions, cleanHtml => {
72+
compilation.assets[destinationPath] = {
73+
source: () => cleanHtml,
74+
size: () => cleanHtml.length
75+
}
76+
})
77+
}
6678

6779
callback()
6880
}

0 commit comments

Comments
 (0)