-
Notifications
You must be signed in to change notification settings - Fork 18
feat(svg): adds sprite hash manifest for cache busting #471
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
4ee0928
f1042f0
d341e94
811aa1f
729e0d5
2d10d9c
97a49f8
847082c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,84 @@ | |||||||||||||||||||||||||||||||||||
| const fs = require('fs') | |||||||||||||||||||||||||||||||||||
| const path = require('path') | |||||||||||||||||||||||||||||||||||
| const crypto = require('crypto') | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| /** | |||||||||||||||||||||||||||||||||||
| * Webpack plugin to generate content hashes for SVG sprite files. | |||||||||||||||||||||||||||||||||||
| * Creates a sprite-hashes.php file in the dist folder. | |||||||||||||||||||||||||||||||||||
| * | |||||||||||||||||||||||||||||||||||
| * @param {Object} options Plugin options. | |||||||||||||||||||||||||||||||||||
| * @param {string} [options.outputPath='dist'] Output directory. | |||||||||||||||||||||||||||||||||||
| * @param {string} [options.spritePath='dist/icons'] Sprite SVG directory. | |||||||||||||||||||||||||||||||||||
| * @param {string} [options.outputFilename='sprite-hashes.php'] Output file name. | |||||||||||||||||||||||||||||||||||
| * @param {number} [options.hashLength=8] Hash length in characters. | |||||||||||||||||||||||||||||||||||
| */ | |||||||||||||||||||||||||||||||||||
| class SpriteHashPlugin { | |||||||||||||||||||||||||||||||||||
| constructor(options = {}) { | |||||||||||||||||||||||||||||||||||
| this.options = { | |||||||||||||||||||||||||||||||||||
| outputPath: options.outputPath || 'dist', | |||||||||||||||||||||||||||||||||||
| spritePath: options.spritePath || 'dist/icons', | |||||||||||||||||||||||||||||||||||
| outputFilename: options.outputFilename || 'sprite-hashes.asset.php', | |||||||||||||||||||||||||||||||||||
| hashLength: options.hashLength || 8, | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| /** | |||||||||||||||||||||||||||||||||||
| * Formats a plain object as a PHP associative array string. | |||||||||||||||||||||||||||||||||||
| * | |||||||||||||||||||||||||||||||||||
| * @param {Record<string, string>} obj Key-value pairs. | |||||||||||||||||||||||||||||||||||
| * @return {string} PHP array literal. | |||||||||||||||||||||||||||||||||||
| */ | |||||||||||||||||||||||||||||||||||
| formatPhpArray(obj) { | |||||||||||||||||||||||||||||||||||
| const entries = Object.entries(obj).map(([key, value]) => { | |||||||||||||||||||||||||||||||||||
| const escapedKey = key.replace(/'/g, "\\'") | |||||||||||||||||||||||||||||||||||
| const escapedValue = String(value).replace(/'/g, "\\'") | |||||||||||||||||||||||||||||||||||
Check failureCode scanning / CodeQL Incomplete string escaping or encoding High
This does not escape backslash characters in the input.
Copilot AutofixAI about 1 hour ago To correctly encode strings for a PHP single-quoted literal, both backslashes and single quotes must be escaped. In PHP, this is done by replacing The best fix is to update the
No new imports or methods are necessary; this uses only built-in string operations. All other behavior (file paths, hash generation, output file format) remains unchanged.
Suggested changeset
1
config/webpack-sprite-hash-plugin.js
Copilot is powered by AI and may make mistakes. Always verify output.
Refresh and try again.
|
|||||||||||||||||||||||||||||||||||
| return `\t'${escapedKey}' => '${escapedValue}'` | |||||||||||||||||||||||||||||||||||
| }) | |||||||||||||||||||||||||||||||||||
| return `array(\n${entries.join(',\n')}\n)` | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| apply(compiler) { | |||||||||||||||||||||||||||||||||||
| compiler.hooks.afterEmit.tapAsync('SpriteHashPlugin', (compilation, callback) => { | |||||||||||||||||||||||||||||||||||
| const spriteDir = path.resolve(compiler.options.context, this.options.spritePath) | |||||||||||||||||||||||||||||||||||
| const outputFile = path.resolve(compiler.options.context, this.options.outputPath, this.options.outputFilename) | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| if (!fs.existsSync(spriteDir)) { | |||||||||||||||||||||||||||||||||||
| console.warn(`SpriteHashPlugin: Sprite directory not found: ${spriteDir}`) | |||||||||||||||||||||||||||||||||||
| callback() | |||||||||||||||||||||||||||||||||||
| return | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| const hashes = {} | |||||||||||||||||||||||||||||||||||
| const files = fs.readdirSync(spriteDir).filter((file) => file.endsWith('.svg')) | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| files.forEach((file) => { | |||||||||||||||||||||||||||||||||||
| const filePath = path.join(spriteDir, file) | |||||||||||||||||||||||||||||||||||
| const content = fs.readFileSync(filePath) | |||||||||||||||||||||||||||||||||||
| const hash = crypto.createHash('md5').update(content).digest('hex').substring(0, this.options.hashLength) | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| // Store with relative path as key | |||||||||||||||||||||||||||||||||||
| const relativePath = `icons/${file}` | |||||||||||||||||||||||||||||||||||
| hashes[relativePath] = hash | |||||||||||||||||||||||||||||||||||
| }) | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| const phpLines = [ | |||||||||||||||||||||||||||||||||||
| '<?php', | |||||||||||||||||||||||||||||||||||
| '/**', | |||||||||||||||||||||||||||||||||||
| ' * Sprite file hashes. Generated by SpriteHashPlugin.', | |||||||||||||||||||||||||||||||||||
| ' *', | |||||||||||||||||||||||||||||||||||
| ' * @return array<string, string> Path => hash.', | |||||||||||||||||||||||||||||||||||
| ' */', | |||||||||||||||||||||||||||||||||||
| 'return ' + this.formatPhpArray(hashes) + ';', | |||||||||||||||||||||||||||||||||||
| '', | |||||||||||||||||||||||||||||||||||
| ] | |||||||||||||||||||||||||||||||||||
| fs.writeFileSync(outputFile, phpLines.join('\n')) | |||||||||||||||||||||||||||||||||||
| console.log( | |||||||||||||||||||||||||||||||||||
| `SpriteHashPlugin: Generated ${this.options.outputFilename} with ${Object.keys(hashes).length} sprites` | |||||||||||||||||||||||||||||||||||
| ) | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| callback() | |||||||||||||||||||||||||||||||||||
| }) | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
| } | |||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||
| module.exports = SpriteHashPlugin | |||||||||||||||||||||||||||||||||||
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Copilot Autofix
AI about 1 hour ago
In general, when building string literals with manual escaping, backslashes must be escaped before escaping other meta-characters like single quotes. For PHP single-quoted strings, every backslash (
\) should become\\, and every single quote (') should become\'.The best fix here is to replace the ad-hoc
replace(/'/g, "\\'")logic with a small helper that correctly escapes both backslashes and single quotes for PHP single-quoted strings. This helper should be used for both keys and values informatPhpArray. The order of replacements matters: first replace\with\\, then replace'with\', so that newly added backslashes from the quote-escaping step are not re-escaped.Concretely, in
config/webpack-sprite-hash-plugin.js:_escapePhpSingleQuoted) to theSpriteHashPluginclass, aboveformatPhpArray.return String(str).replace(/\\/g, '\\\\').replace(/'/g, "\\'").formatPhpArrayto use this helper for bothkeyandvalueinstead of the current.replace(/'/g, "\\'")calls.No new imports are needed; only standard JavaScript string methods and regexes are used.