Skip to content

Commit 81a47cc

Browse files
authored
feat: add HtmlNewLineRemoverPlugin to remove all newline characters from HTML output (#64)
- Introduced a custom Webpack plugin (`HtmlNewLineRemoverPlugin`) to remove CRLF, CR, and LF characters from generated HTML files. - Hooked into `HtmlWebpackPlugin`'s `beforeEmit` lifecycle to process the HTML and normalize line endings. - Updated `webpack.config.js` to include the new plugin in the `plugins` array for seamless integration. - Ensured compatibility with both Unix (`\n`) and Windows (`\r\n`) newline formats.
1 parent ada4a65 commit 81a47cc

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

html-new-line-remover-plugin.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const HtmlWebpackPlugin = require('html-webpack-plugin');
2+
3+
class HtmlNewLineRemoverPlugin {
4+
apply(compiler) {
5+
compiler.hooks.compilation.tap('HtmlNewLineRemoverPlugin', (compilation) => {
6+
HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
7+
'HtmlNewLineRemoverPlugin',
8+
(data, cb) => {
9+
data.html = data.html.replace(/(\r)?\n|\r/g, '');
10+
cb(null, data);
11+
}
12+
);
13+
});
14+
}
15+
}
16+
17+
module.exports = HtmlNewLineRemoverPlugin;

webpack.config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const MangleCssClassPlugin = require('mangle-css-class-webpack-plugin');
1212
const HtmlInlineScriptWebpackPlugin = require('html-inline-script-webpack-plugin');
1313
const ImageMinimizerPlugin = require("image-minimizer-webpack-plugin");
1414
const SitemapWebpackPlugin = require('sitemap-webpack-plugin').default;
15+
const HtmlNewLineRemoverPlugin = require('./html-new-line-remover-plugin.js');
1516

1617
const { interpolateName } = require('loader-utils');
1718
const fs = require('fs');
@@ -175,6 +176,7 @@ module.exports = {
175176
lastmod: true,
176177
}
177178
}),
179+
new HtmlNewLineRemoverPlugin(),
178180
],
179181
optimization: {
180182
minimize: true,

0 commit comments

Comments
 (0)