Skip to content

Commit

Permalink
feat: add plugin to merge css (or any utf compatible content)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xMurage committed Jan 3, 2025
1 parent 6facea1 commit 60d4400
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion webpack.common.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const path = require('path');
const fs = require('fs');
const CopyPlugin = require('copy-webpack-plugin');

module.exports = {
Expand Down Expand Up @@ -60,4 +61,29 @@ module.exports = {
]
}),
]
};
};


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
});
}
}
}

0 comments on commit 60d4400

Please sign in to comment.