| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- function ReplaceHtmlStyles() {
- this.replace = function(htmlPluginData, callback) {
- //console.log(htmlPluginData);
- let html = htmlPluginData.html;
- // //console.log(html);
- let htmlRegx = new RegExp("\\<style[^\\>]*\\>.*?\\<\\/style\\>", "g");
- let match = html.match(htmlRegx);
- if (match) {
- //console.log(match)
- html = html.replace(htmlRegx, "");
- //console.log(html)
- match.forEach((item, index) => {
- html = html.replace(/<\/body>/, item + "</body>");
- });
- htmlPluginData.html = html;
- }
- callback(null, htmlPluginData);
- };
- }
- ReplaceHtmlStyles.prototype.apply = function(compiler) {
- if (compiler.hooks) {
- compiler.hooks.compilation.tap("ReplaceHtmlStyles", compilation => {
- if (compilation.hooks.htmlWebpackPluginAfterHtmlProcessing) {
- compilation.hooks.htmlWebpackPluginAfterHtmlProcessing.tapAsync(
- "ReplaceHtmlStyles",
- this.replace
- );
- } else {
- var HtmlWebpackPlugin = require("html-webpack-plugin");
- if (!HtmlWebpackPlugin) {
- throw new Error(
- "Please ensure that `html-webpack-plugin` was placed before `replace-html-styles` in your Webpack config if you were working with Webpack 4.x!"
- );
- }
- HtmlWebpackPlugin.getHooks(compilation).beforeEmit.tapAsync(
- "ReplaceHtmlStyles",
- this.replace
- );
- }
- });
- } else {
- compiler.plugin("compilation", compilation => {
- compilation.plugin("html-webpack-plugin-beforeEmit", this.replace);
- });
- }
- };
- module.exports = ReplaceHtmlStyles;
|