From 60d4400e2479dfebaf8ac56a9d7e2d75e4280d30 Mon Sep 17 00:00:00 2001 From: Murage <25152892+0xMurage@users.noreply.github.com> Date: Fri, 3 Jan 2025 03:16:07 +0300 Subject: [PATCH] feat: add plugin to merge css (or any utf compatible content) --- webpack.common.js | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/webpack.common.js b/webpack.common.js index 7fda2362..0ff7881d 100644 --- a/webpack.common.js +++ b/webpack.common.js @@ -1,4 +1,5 @@ const path = require('path'); +const fs = require('fs'); const CopyPlugin = require('copy-webpack-plugin'); module.exports = { @@ -60,4 +61,29 @@ module.exports = { ] }), ] -}; \ No newline at end of file +}; + + +function CssMergePlugin(outputFilename, cssFilesToMerge = []) { + return { + apply: (compiler) => { + compiler.hooks.emit.tapAsync('CssMergePlugin', (compilation, callback) => { + + // Read content of the files to merge + const mergedCssContent = cssFilesToMerge + .map((filePath) => fs.readFileSync(filePath, 'utf8')) + .join('\n'); // Join the CSS content + + // Ensure the output directory exists + + fs.mkdirSync(path.resolve(compiler.options.output.path, path.dirname(outputFilename)), { recursive: true }); + + + // Write the merged content + fs.writeFileSync(path.resolve(compiler.options.output.path, outputFilename), mergedCssContent); + + callback(); // Continue Webpack build + }); + } + } +} \ No newline at end of file