@@ -2,33 +2,23 @@ const fs = require('fs')
22const path = require ( 'path' )
33const 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 */
1915class 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 ( / \. j s o n $ / 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 } )
0 commit comments