Skip to content

Commit d389315

Browse files
committed
ci(svg): output svg files in dist
1 parent 66b1cc2 commit d389315

5 files changed

Lines changed: 118 additions & 7 deletions

File tree

‎config/plugins.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPl
1212
const WebpackImageSizesPlugin = require('./webpack-image-sizes-plugin')
1313
const WebpackThemeJsonPlugin = require('./webpack-theme-json-plugin')
1414
const SpriteHashPlugin = require('./webpack-sprite-hash-plugin')
15+
const WebpackIconFilesPlugin = require('./webpack-icon-files-plugin')
1516

1617
module.exports = {
1718
get: function (mode) {
@@ -20,6 +21,11 @@ module.exports = {
2021
watch: mode !== 'production',
2122
}),
2223
new SpriteHashPlugin(),
24+
new WebpackIconFilesPlugin({
25+
sourcePath: 'src/img/icons',
26+
outputPath: 'dist/images',
27+
silence: true,
28+
}),
2329
new CleanWebpackPlugin({
2430
cleanOnceBeforeBuildPatterns: ['**/*', '!images', '!images/**'],
2531
}),

‎config/svgo.config.js‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,7 @@ module.exports = {
22
plugins: [
33
{
44
name: 'preset-default',
5-
params: {
6-
overrides: {
7-
// Disable a plugin included by default that you don't want (false)
8-
removeViewBox: false,
9-
},
10-
},
115
},
12-
// Plugins that are not in the "preset-default" and that you want to activate
136
'removeTitle',
147
'convertStyleToAttrs',
158
'prefixIds',
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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('WebpackIconFilesPlugin', (compilation, callback) => {
70+
const context = compiler.options.context
71+
const sourceDir = path.resolve(context, this.options.sourcePath)
72+
const outputDir = path.resolve(context, this.options.outputPath)
73+
const svgFiles = this.collectSvgFiles(sourceDir)
74+
75+
if (svgFiles.length === 0) {
76+
callback()
77+
return
78+
}
79+
80+
let written = 0
81+
82+
for (const sourceFile of svgFiles) {
83+
const relativePath = path.relative(sourceDir, sourceFile)
84+
const destFile = path.join(outputDir, relativePath)
85+
86+
fs.mkdirSync(path.dirname(destFile), { recursive: true })
87+
88+
const input = fs.readFileSync(sourceFile, 'utf8')
89+
const result = optimize(input, {
90+
path: sourceFile,
91+
...svgoConfig,
92+
})
93+
94+
if (result.error) {
95+
console.warn(`WebpackIconFilesPlugin: SVGO failed for ${relativePath}: ${result.error}`)
96+
continue
97+
}
98+
99+
fs.writeFileSync(destFile, result.data)
100+
written++
101+
}
102+
103+
this.log('log', `WebpackIconFilesPlugin: Wrote ${written} optimized SVG(s) to ${this.options.outputPath}`)
104+
105+
callback()
106+
})
107+
}
108+
}
109+
110+
module.exports = WebpackIconFilesPlugin

‎package-lock.json‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"stylelint-scss": "^6.14.0",
6262
"stylelint-webpack-plugin": "^5.1.0",
6363
"svg-sprite-loader": "^6.0.11",
64+
"svgo": "^4.0.2",
6465
"svgo-loader": "^3.0.3",
6566
"terser-webpack-plugin": "^5.3.14",
6667
"webpack": "^5.109.2",

0 commit comments

Comments
 (0)