-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c481042
commit 1659228
Showing
16 changed files
with
2,095 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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,5 +1,5 @@ | ||
{ | ||
"name": "assetpack", | ||
"name": "@play-co/assetpack", | ||
"private": true, | ||
"license": "MIT", | ||
"workspaces": [ | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Large diffs are not rendered by default.
Oops, something went wrong.
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,44 @@ | ||
# @assetpack/plugin-cache-buster | ||
|
||
AssetPack plugin for generating hashes and appending them to the file names. | ||
Super useful for when assets change and they need to be re-downloaded. | ||
|
||
Note that order matters with AssetPack plugins and its best to have this pipe transform happen at. | ||
|
||
When combining this with `texturePacker` you add the `texturePackerCacheBuster` pipe right after | ||
the `cacheBuster` pipe. `texturePackerCacheBuster` will ensure that the json files internanlly update their | ||
asset names to accommodate the new file names. | ||
|
||
## Example transform | ||
|
||
``` | ||
|- assets | ||
|- mySprite.png | ||
|- myJson.json | ||
``` | ||
transforms to: | ||
``` | ||
|- assets | ||
|- mySprite-dfs3e.png | ||
|- myJson-aw3dsf.json | ||
``` | ||
## Installation | ||
|
||
```sh | ||
npm install --save-dev @assetpack/plugin-cache-buster | ||
``` | ||
|
||
## Basic Usage | ||
|
||
```js | ||
import { cacheBuster } from "@assetpack/plugin-cache-buster"; | ||
|
||
export default { | ||
... | ||
pipes: { | ||
... | ||
cacheBuster(), | ||
}, | ||
}; | ||
``` | ||
|
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,10 @@ | ||
const sharedConfig = require('../../jest.config'); | ||
|
||
module.exports = { | ||
...sharedConfig, | ||
rootDir: './', | ||
moduleNameMapper: { | ||
'^@assetpack/plugin-(.*)$': '<rootDir>/../$1/src', | ||
'^@assetpack/(.*)$': '<rootDir>/../$1/src', | ||
}, | ||
}; |
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,47 @@ | ||
{ | ||
"name": "@assetpack/plugin-cache-buster", | ||
"version": "0.7.0", | ||
"description": "", | ||
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/cache-buster/#readme", | ||
"bugs": "https://github.com/pixijs/assetpack/issues", | ||
"repository": { | ||
"url": "pixijs/assetpack", | ||
"directory": "packages/cache-buster" | ||
}, | ||
"license": "MIT", | ||
"author": "Zyie", | ||
"exports": { | ||
"import": "./dist/es/index.js", | ||
"types": "./dist/types/index.d.ts", | ||
"default": "./dist/cjs/index.js" | ||
}, | ||
"main": "./dist/cjs/index.js", | ||
"module": "./dist/es/index.js", | ||
"types": "./dist/types/index.d.ts", | ||
"files": [ | ||
"dist", | ||
"*.d.ts" | ||
], | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "rollup -c", | ||
"test": "npx jest --config ./jest.config.js", | ||
"test:types": "tsc --noEmit" | ||
}, | ||
"dependencies": { | ||
"@node-rs/crc32": "^1.10.0", | ||
"fs-extra": "^11.1.0" | ||
}, | ||
"devDependencies": { | ||
"@assetpack/core": "0.7.0" | ||
}, | ||
"peerDependencies": { | ||
"@assetpack/core": ">=0.0.0" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
} | ||
} |
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,7 @@ | ||
import { readFileSync } from 'fs'; | ||
|
||
import { createConfig } from '../../shared/rollup.config.mjs'; | ||
|
||
export default createConfig({ | ||
pkg: JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')) | ||
}); |
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,53 @@ | ||
import type { AssetPipe, Asset } from '@assetpack/core'; | ||
import { createNewAssetAt, swapExt } from '@assetpack/core'; | ||
import { readFileSync } from 'fs-extra'; | ||
|
||
import { crc32 as calculateCrc32 } from '@node-rs/crc32'; | ||
|
||
/** | ||
* Cache buster asset pipe. This pipe will add a hash to the end of the filename | ||
* the hash is calculated from the contents of the file. | ||
* | ||
* worth noting that when combined with the texture packer plugin, an additional | ||
* plugin is required to update the texture packer json files to point to the new | ||
* file names (`texturePackerCacheBuster.ts`) | ||
* | ||
* @returns the cache buster asset pipe | ||
*/ | ||
export function cacheBuster(): AssetPipe | ||
{ | ||
const defaultOptions = {}; | ||
|
||
return { | ||
folder: false, | ||
name: 'cache-buster', | ||
defaultOptions, | ||
test(asset: Asset) | ||
{ | ||
return !asset.isFolder; | ||
}, | ||
async transform(asset: Asset) | ||
{ | ||
const buffer = asset.buffer ?? readFileSync(asset.path); | ||
|
||
const hash = crc32(buffer); | ||
const newFileName = swapExt(asset.filename, `-${hash}${asset.extension}`); | ||
|
||
const newAsset = createNewAssetAt(asset, newFileName); | ||
|
||
// by attaching the buffer - we can avoid reading the file again | ||
// and the final copy op will use the buffer, rather than the file path! | ||
newAsset.buffer = buffer; | ||
|
||
return [newAsset]; | ||
} | ||
}; | ||
} | ||
|
||
/** Calculate a CRC32 checksum. */ | ||
export function crc32(input: string | Buffer): string | ||
{ | ||
const checksumHex = calculateCrc32(input).toString(16); | ||
|
||
return Buffer.from(checksumHex, 'hex').toString('base64url'); | ||
} |
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 @@ | ||
export * from './cacheBuster'; |
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,49 @@ | ||
import { AssetPack, joinSafe } from '@assetpack/core'; | ||
import { existsSync, readFileSync } from 'fs-extra'; | ||
import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test'; | ||
import { cacheBuster, crc32 } from '../src'; | ||
|
||
const pkg = 'cache-buster'; | ||
|
||
describe('CacheBuster', () => | ||
{ | ||
it('should hash a file', async () => | ||
{ | ||
const testName = 'cache-buster'; | ||
const inputDir = getInputDir(pkg, testName); | ||
const outputDir = getOutputDir(pkg, testName); | ||
|
||
createFolder( | ||
pkg, | ||
{ | ||
name: testName, | ||
files: [ | ||
{ | ||
name: 'ttf.ttf', | ||
content: assetPath(pkg, 'Roboto-Regular.ttf'), | ||
}, | ||
], | ||
folders: [], | ||
} | ||
); | ||
|
||
const assetpack = new AssetPack({ | ||
entry: inputDir, | ||
output: outputDir, | ||
cache: true, | ||
pipes: [ | ||
cacheBuster() | ||
] | ||
}); | ||
|
||
await assetpack.run(); | ||
|
||
const originalPath = joinSafe('.testInput', testName, 'ttf.ttf'); | ||
|
||
const buffer = readFileSync(originalPath); | ||
|
||
const hash = crc32(buffer); | ||
|
||
expect(existsSync(joinSafe('.testOutput', testName, `ttf-${hash}.ttf`))).toBe(true); | ||
}); | ||
}); |
Binary file not shown.
Oops, something went wrong.