-
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4ee0928
feat(svg): adds sprite hash manifest for cache busting
firestar300 f1042f0
feat(Svg): add cache system
firestar300 d341e94
feat(Svg): add query args
firestar300 811aa1f
fix(Svg): phpcs issue
firestar300 729e0d5
feat(Svg): allow width and height attributes to Svg elements
firestar300 2d10d9c
feat(webpack-svg-sprite-hashes): add returnFormat option to output ph…
firestar300 97a49f8
fix(Svg): psalm issue
firestar300 847082c
refactor(svg): remove json return format
firestar300 ce82a75
Potential fix for code scanning alert no. 5: Incomplete string escapi…
firestar300 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| 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.json file in the dist folder. | ||
| */ | ||
| class SpriteHashPlugin { | ||
| constructor(options = {}) { | ||
| this.options = { | ||
| outputPath: options.outputPath || 'dist', | ||
| spritePath: options.spritePath || 'dist/icons', | ||
| outputFilename: options.outputFilename || 'sprite-hashes.json', | ||
| hashLength: options.hashLength || 8, | ||
| }; | ||
| } | ||
|
|
||
| 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; | ||
| }); | ||
|
|
||
| fs.writeFileSync(outputFile, JSON.stringify(hashes, null, 2)); | ||
| console.log( | ||
| `SpriteHashPlugin: Generated ${ | ||
| this.options.outputFilename | ||
| } with ${Object.keys(hashes).length} sprites` | ||
| ); | ||
|
|
||
| callback(); | ||
| } | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| module.exports = SpriteHashPlugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.