forked from pixijs/assetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cachebuster): add cache buster plugins for atlas and texture pac…
…ker (#14) * - add spine cache buster - add sprite sheet cache buster - tests * Update packages/spine/test/spineAtlasAll.test.ts * remove only * fix test and merge bits * rename * fix options * fix options * Chore/object-hash-options (#15) * cache id is now takes into account all pipe options add test * docs --------- Co-authored-by: Zyie <[email protected]>
- Loading branch information
1 parent
332914d
commit 87703d7
Showing
15 changed files
with
573 additions
and
10 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -33,6 +33,6 @@ export function cacheBuster(): AssetPipe | |
newAsset.buffer = asset.buffer; | ||
|
||
return [newAsset]; | ||
} | ||
}, | ||
}; | ||
} |
This file contains 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 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,32 @@ | ||
import type { AssetPackConfig } from '../config'; | ||
import objectHash from 'object-hash'; | ||
|
||
/** | ||
* Returns a unique name based on the hash generated from the config | ||
* this takes into account the following: | ||
* - pipes and their options, | ||
* - entry and output paths. | ||
* - assetSettings options | ||
* - ignore options | ||
* | ||
* @param options - The asset pack config | ||
* @returns - A unique name based on the hash generated from the config | ||
*/ | ||
export function generateCacheName(options: AssetPackConfig) | ||
{ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { pipes, cache, logLevel, ...configWithoutPlugins } = options; | ||
|
||
const optionsToHash: any = { | ||
...configWithoutPlugins, | ||
}; | ||
|
||
// get pipes | ||
pipes?.flat().forEach((pipe) => | ||
{ | ||
optionsToHash[pipe.name] = pipe.defaultOptions; | ||
}); | ||
|
||
// make a hash.. | ||
return objectHash(optionsToHash); | ||
} |
This file contains 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 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 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 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 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './spineAtlasMipmap'; | ||
export * from './spineAtlasCompress'; | ||
export * from './spineAtlasCacheBuster'; |
This file contains 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,83 @@ | ||
import type { Asset } from '@play-co/assetpack-core'; | ||
import { checkExt, type AssetPipe, findAssetsWithFileName } from '@play-co/assetpack-core'; | ||
import { AtlasView } from './AtlasView'; | ||
import { removeSync, writeFileSync } from 'fs-extra'; | ||
|
||
/** | ||
* This should be used after the cache buster plugin in the pipes. | ||
* As it relies on the cache buster plugin to have already cache busted all files. | ||
* This corrects the atlas files to point to the new cache busted textures. | ||
* At the same time it updates the hash of the files. | ||
* | ||
* As this pipe needs to know about all the textures in the atlas files most of the work is done | ||
* in the finish method. | ||
* | ||
* Kind of like applying a patch at the end of the transform process. | ||
* | ||
* @param _options | ||
* @returns | ||
*/ | ||
export function spineAtlasCacheBuster(): AssetPipe | ||
{ | ||
const defaultOptions = {}; | ||
|
||
const atlasFileToFix: Asset[] = []; | ||
|
||
return { | ||
folder: false, | ||
name: 'spine-cache-buster', | ||
defaultOptions, | ||
test(asset: Asset, _options) | ||
{ | ||
return checkExt(asset.path, '.atlas'); | ||
}, | ||
|
||
async transform(asset: Asset, _options) | ||
{ | ||
atlasFileToFix.push(asset); | ||
|
||
return [asset]; | ||
}, | ||
|
||
async finish(asset: Asset) | ||
{ | ||
// first we retrieve the final transformed children - so the atlas files that have been copied | ||
// to the output folder. | ||
const atlasAssets = atlasFileToFix.map((asset) => asset.getFinalTransformedChildren()[0]); | ||
|
||
atlasAssets.forEach((atlasAsset) => | ||
{ | ||
// we are going to replace the textures in the atlas file with the new cache busted textures | ||
// as we do this, the hash of the atlas file will change, so we need to update the path | ||
// and also remove the original file. | ||
|
||
const originalHash = atlasAsset.hash; | ||
const originalPath = atlasAsset.path; | ||
|
||
const atlasView = new AtlasView(atlasAsset.buffer); | ||
|
||
atlasView.getTextures().forEach((texture) => | ||
{ | ||
const textureAssets = findAssetsWithFileName((asset) => | ||
asset.filename === texture, asset, true); | ||
|
||
// last transformed child is the renamed texture | ||
const cacheBustedTexture = textureAssets[0].getFinalTransformedChildren()[0]; | ||
|
||
atlasView.replaceTexture(texture, cacheBustedTexture.filename); | ||
}); | ||
|
||
atlasAsset.buffer = atlasView.buffer; | ||
|
||
atlasAsset.path = atlasAsset.path.replace(originalHash, atlasAsset.hash); | ||
|
||
removeSync(originalPath); | ||
|
||
// rewrite.. | ||
writeFileSync(atlasAsset.path, atlasAsset.buffer); | ||
}); | ||
|
||
atlasFileToFix.length = 0; | ||
} | ||
}; | ||
} |
This file contains 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 |
---|---|---|
@@ -1,15 +1,17 @@ | ||
import { AssetPack } from '@play-co/assetpack-core'; | ||
import { compress, mipmap } from '@play-co/assetpack-plugin-image'; | ||
import { readFileSync } from 'fs-extra'; | ||
import { existsSync, readFileSync } from 'fs-extra'; | ||
import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test'; | ||
import { spineAtlasCompress } from '../src/spineAtlasCompress'; | ||
import { spineAtlasMipmap } from '../src/spineAtlasMipmap'; | ||
import { cacheBuster } from '@play-co/assetpack-plugin-cache-buster'; | ||
import { spineAtlasCacheBuster } from '../src/spineAtlasCacheBuster'; | ||
|
||
const pkg = 'spine'; | ||
|
||
describe('Spine Atlas All', () => | ||
{ | ||
it.only('should correctly create files when Mipmap and Compress are used', async () => | ||
it('should correctly create files when Mipmap and Compress are used', async () => | ||
{ | ||
const testName = 'spine-atlas-compress-mip'; | ||
const inputDir = getInputDir(pkg, testName); | ||
|
@@ -71,4 +73,92 @@ describe('Spine Atlas All', () => | |
expect(rawAtlasWebpHalf.includes('[email protected]')).toBeTruthy(); | ||
expect(rawAtlasWebpHalf.includes('[email protected]')).toBeTruthy(); | ||
}); | ||
|
||
it('should correctly create files when Mipmap and CacheBuster are used', async () => | ||
{ | ||
const testName = 'spine-atlas-mip-cache-buster'; | ||
const inputDir = getInputDir(pkg, testName); | ||
const outputDir = getOutputDir(pkg, testName); | ||
|
||
createFolder( | ||
pkg, | ||
{ | ||
name: testName, | ||
files: [ | ||
{ | ||
name: 'dragon{spine}.atlas', | ||
content: assetPath(pkg, 'dragon.atlas'), | ||
}, | ||
{ | ||
name: 'dragon.png', | ||
content: assetPath(pkg, 'dragon.png'), | ||
}, | ||
{ | ||
name: 'dragon2.png', | ||
content: assetPath(pkg, 'dragon2.png'), | ||
}, | ||
], | ||
folders: [], | ||
}); | ||
|
||
const assetpack = new AssetPack({ | ||
entry: inputDir, | ||
output: outputDir, | ||
cache: false, | ||
pipes: [ | ||
mipmap({ | ||
resolutions: { default: 1, low: 0.5 }, | ||
}), | ||
compress({ | ||
png: true, | ||
webp: true, | ||
jpg: true, | ||
}), | ||
spineAtlasMipmap({ | ||
resolutions: { default: 1, low: 0.5 }, | ||
}), | ||
spineAtlasCompress({ | ||
png: true, | ||
jpg: true, | ||
webp: true, | ||
}), | ||
cacheBuster(), | ||
spineAtlasCacheBuster() | ||
] | ||
}); | ||
|
||
await assetpack.run(); | ||
|
||
[ | ||
{ | ||
atlas: `[email protected]`, | ||
png1: `[email protected]`, | ||
png2: `[email protected]_22pw.webp` | ||
}, | ||
{ | ||
atlas: `dragon.webp-spj8.atlas`, | ||
png1: `dragon-rSwKOg.webp`, | ||
png2: `dragon2-ws3uhw.webp` | ||
}, | ||
{ | ||
atlas: `[email protected]`, | ||
png1: `[email protected]`, | ||
png2: `[email protected]` | ||
}, | ||
{ | ||
atlas: `dragon.png-O471eg.atlas`, | ||
png1: `dragon-vezElA.png`, | ||
png2: `dragon2-3UnJNw.png` | ||
} | ||
].forEach(({ atlas, png1, png2 }) => | ||
{ | ||
const rawAtlas = readFileSync(`${outputDir}/${atlas}`); | ||
|
||
expect(rawAtlas.includes(png1)).toBeTruthy(); | ||
expect(rawAtlas.includes(png2)).toBeTruthy(); | ||
|
||
expect(existsSync(`${outputDir}/${png1}`)).toBeTruthy(); | ||
expect(existsSync(`${outputDir}/${png2}`)).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.