Skip to content

Commit 847082c

Browse files
committed
refactor(svg): remove json return format
1 parent 97a49f8 commit 847082c

File tree

3 files changed

+31
-55
lines changed

3 files changed

+31
-55
lines changed

config/plugins.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ module.exports = {
1919
new WebpackThemeJsonPlugin({
2020
watch: mode !== 'production',
2121
}),
22-
new SpriteHashPlugin({
23-
returnFormat: 'php',
24-
}),
22+
new SpriteHashPlugin(),
2523
new CleanWebpackPlugin({
2624
cleanOnceBeforeBuildPatterns: ['**/*', '!images', '!images/**'],
2725
}),

config/webpack-sprite-hash-plugin.js

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,23 @@ const fs = require('fs')
22
const path = require('path')
33
const crypto = require('crypto')
44

5-
const ALLOWED_RETURN_FORMATS = ['json', 'php']
6-
75
/**
86
* Webpack plugin to generate content hashes for SVG sprite files.
9-
* Creates a sprite-hashes.json (or sprite-hashes.asset.php) file in the dist folder.
7+
* Creates a sprite-hashes.php file in the dist folder.
108
*
119
* @param {Object} options Plugin options.
12-
* @param {string} [options.returnFormat='json'] Output format: 'json' or 'php'.
13-
* When 'php', generates a .asset.php file (outputFilename .json → .asset.php).
1410
* @param {string} [options.outputPath='dist'] Output directory.
1511
* @param {string} [options.spritePath='dist/icons'] Sprite SVG directory.
16-
* @param {string} [options.outputFilename='sprite-hashes.json'] Output file name.
12+
* @param {string} [options.outputFilename='sprite-hashes.php'] Output file name.
1713
* @param {number} [options.hashLength=8] Hash length in characters.
1814
*/
1915
class SpriteHashPlugin {
2016
constructor(options = {}) {
21-
const returnFormat = options.returnFormat || 'json'
22-
if (!ALLOWED_RETURN_FORMATS.includes(returnFormat)) {
23-
throw new Error(`SpriteHashPlugin: returnFormat must be one of ${ALLOWED_RETURN_FORMATS.join(', ')}`)
24-
}
25-
2617
this.options = {
2718
outputPath: options.outputPath || 'dist',
2819
spritePath: options.spritePath || 'dist/icons',
29-
outputFilename: options.outputFilename || 'sprite-hashes.' + returnFormat,
20+
outputFilename: options.outputFilename || 'sprite-hashes.asset.php',
3021
hashLength: options.hashLength || 8,
31-
returnFormat,
3222
}
3323
}
3424

@@ -50,11 +40,7 @@ class SpriteHashPlugin {
5040
apply(compiler) {
5141
compiler.hooks.afterEmit.tapAsync('SpriteHashPlugin', (compilation, callback) => {
5242
const spriteDir = path.resolve(compiler.options.context, this.options.spritePath)
53-
const outputFilename =
54-
this.options.returnFormat === 'php'
55-
? this.options.outputFilename.replace(/\.json$/i, '.asset.php')
56-
: this.options.outputFilename
57-
const outputFile = path.resolve(compiler.options.context, this.options.outputPath, outputFilename)
43+
const outputFile = path.resolve(compiler.options.context, this.options.outputPath, this.options.outputFilename)
5844

5945
if (!fs.existsSync(spriteDir)) {
6046
console.warn(`SpriteHashPlugin: Sprite directory not found: ${spriteDir}`)
@@ -75,22 +61,20 @@ class SpriteHashPlugin {
7561
hashes[relativePath] = hash
7662
})
7763

78-
if (this.options.returnFormat === 'php') {
79-
const phpLines = [
80-
'<?php',
81-
'/**',
82-
' * Sprite file hashes. Generated by SpriteHashPlugin.',
83-
' *',
84-
' * @return array<string, string> Path => hash.',
85-
' */',
86-
'return ' + this.formatPhpArray(hashes) + ';',
87-
'',
88-
]
89-
fs.writeFileSync(outputFile, phpLines.join('\n'))
90-
} else {
91-
fs.writeFileSync(outputFile, JSON.stringify(hashes, null, 2))
92-
}
93-
console.log(`SpriteHashPlugin: Generated ${outputFilename} with ${Object.keys(hashes).length} sprites`)
64+
const phpLines = [
65+
'<?php',
66+
'/**',
67+
' * Sprite file hashes. Generated by SpriteHashPlugin.',
68+
' *',
69+
' * @return array<string, string> Path => hash.',
70+
' */',
71+
'return ' + this.formatPhpArray(hashes) + ';',
72+
'',
73+
]
74+
fs.writeFileSync(outputFile, phpLines.join('\n'))
75+
console.log(
76+
`SpriteHashPlugin: Generated ${this.options.outputFilename} with ${Object.keys(hashes).length} sprites`
77+
)
9478

9579
callback()
9680
})

inc/Services/Svg.php

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -120,23 +120,17 @@ public function get_sprite_hash( string $sprite_name ): ?string {
120120
static $sprite_hashes = null;
121121

122122
if ( null === $sprite_hashes ) {
123-
$php_file = get_theme_file_path( '/dist/sprite-hashes.php' );
124-
$json_file = get_theme_file_path( '/dist/sprite-hashes.json' );
125-
126-
if ( is_readable( $php_file ) ) {
127-
$sprite_hash = require $php_file;
128-
$sprite_hash = \is_array( $sprite_hashes ) ? $sprite_hashes : [];
129-
} elseif ( is_readable( $json_file ) ) {
130-
$sprite_hash = file_get_contents( $json_file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
131-
try {
132-
$sprite_hash = json_decode( $json_content, true, 512, JSON_THROW_ON_ERROR );
133-
$sprite_hash = \is_array( $sprite_hashes ) ? $sprite_hashes : [];
134-
} catch ( \JsonException $e ) {
135-
$sprite_hashes = [];
136-
137-
return null;
138-
}
139-
} else {
123+
$sprite_hash_file = get_theme_file_path( '/dist/sprite-hashes.asset.php' );
124+
125+
if ( ! is_readable( $sprite_hash_file ) ) {
126+
$sprite_hashes = [];
127+
128+
return null;
129+
}
130+
131+
$sprite_hash = require $sprite_hash_file;
132+
133+
if ( ! is_array( $sprite_hash ) ) {
140134
$sprite_hashes = [];
141135

142136
return null;
@@ -145,6 +139,6 @@ public function get_sprite_hash( string $sprite_name ): ?string {
145139
$sprite_hashes = $sprite_hash;
146140
}
147141

148-
return $sprite_hash[ sprintf( 'icons/%s.svg', $sprite_name ) ] ?? null;
142+
return $sprite_hashes[ sprintf( 'icons/%s.svg', $sprite_name ) ] ?? null;
149143
}
150144
}

0 commit comments

Comments
 (0)