|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { optimize } = require('svgo'); |
| 4 | +const svgoConfig = require('./svgo.config'); |
| 5 | + |
| 6 | +/** |
| 7 | + * Webpack plugin that writes optimized standalone SVG files from icon sources. |
| 8 | + * Sprite sheets under dist/icons are unchanged; this mirrors src/img/icons/** into dist/images/**. |
| 9 | + * |
| 10 | + * @class WebpackIconFilesPlugin |
| 11 | + */ |
| 12 | +class WebpackIconFilesPlugin { |
| 13 | + /** |
| 14 | + * @param {Object} [options={}] Plugin options. |
| 15 | + * @param {string} [options.sourcePath='src/img/icons'] Source icons directory. |
| 16 | + * @param {string} [options.outputPath='dist/images'] Output directory (subfolders preserved). |
| 17 | + * @param {boolean} [options.silence=false] Suppress console output. |
| 18 | + */ |
| 19 | + constructor(options = {}) { |
| 20 | + this.options = { |
| 21 | + sourcePath: 'src/img/icons', |
| 22 | + outputPath: 'dist/images', |
| 23 | + silence: false, |
| 24 | + ...options, |
| 25 | + }; |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * @param {string} level Log level. |
| 30 | + * @param {...any} args Log arguments. |
| 31 | + */ |
| 32 | + log(level, ...args) { |
| 33 | + if (!this.options.silence) { |
| 34 | + console[level](...args); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param {string} directory Directory to scan. |
| 40 | + * @return {string[]} Absolute paths to SVG files. |
| 41 | + */ |
| 42 | + collectSvgFiles(directory) { |
| 43 | + const files = []; |
| 44 | + |
| 45 | + if (!fs.existsSync(directory)) { |
| 46 | + return files; |
| 47 | + } |
| 48 | + |
| 49 | + for (const entry of fs.readdirSync(directory, { withFileTypes: true })) { |
| 50 | + const fullPath = path.join(directory, entry.name); |
| 51 | + |
| 52 | + if (entry.isDirectory()) { |
| 53 | + files.push(...this.collectSvgFiles(fullPath)); |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if (entry.name.endsWith('.svg')) { |
| 58 | + files.push(fullPath); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return files; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * @param {import('webpack').Compiler} compiler Webpack compiler. |
| 67 | + */ |
| 68 | + apply(compiler) { |
| 69 | + compiler.hooks.afterEmit.tapAsync( |
| 70 | + 'WebpackIconFilesPlugin', |
| 71 | + (compilation, callback) => { |
| 72 | + const context = compiler.options.context; |
| 73 | + const sourceDir = path.resolve(context, this.options.sourcePath); |
| 74 | + const outputDir = path.resolve(context, this.options.outputPath); |
| 75 | + const svgFiles = this.collectSvgFiles(sourceDir); |
| 76 | + |
| 77 | + if (svgFiles.length === 0) { |
| 78 | + callback(); |
| 79 | + return; |
| 80 | + } |
| 81 | + |
| 82 | + let written = 0; |
| 83 | + |
| 84 | + for (const sourceFile of svgFiles) { |
| 85 | + const relativePath = path.relative(sourceDir, sourceFile); |
| 86 | + const destFile = path.join(outputDir, relativePath); |
| 87 | + |
| 88 | + fs.mkdirSync(path.dirname(destFile), { recursive: true }); |
| 89 | + |
| 90 | + const input = fs.readFileSync(sourceFile, 'utf8'); |
| 91 | + const result = optimize(input, { |
| 92 | + path: sourceFile, |
| 93 | + ...svgoConfig, |
| 94 | + }); |
| 95 | + |
| 96 | + if (result.error) { |
| 97 | + console.warn( |
| 98 | + `WebpackIconFilesPlugin: SVGO failed for ${relativePath}: ${result.error}` |
| 99 | + ); |
| 100 | + continue; |
| 101 | + } |
| 102 | + |
| 103 | + fs.writeFileSync(destFile, result.data); |
| 104 | + written++; |
| 105 | + } |
| 106 | + |
| 107 | + this.log( |
| 108 | + 'log', |
| 109 | + `WebpackIconFilesPlugin: Wrote ${written} optimized SVG(s) to ${this.options.outputPath}` |
| 110 | + ); |
| 111 | + |
| 112 | + callback(); |
| 113 | + } |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +module.exports = WebpackIconFilesPlugin; |
0 commit comments