Skip to content

Commit

Permalink
add cache buster
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyDigital committed Apr 15, 2024
1 parent c481042 commit 1659228
Show file tree
Hide file tree
Showing 16 changed files with 2,095 additions and 3 deletions.
438 changes: 436 additions & 2 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
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": [
Expand Down
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.
543 changes: 543 additions & 0 deletions packages/cache-buster/.asset-pack/sdf-font/sdf.fnt

Large diffs are not rendered by default.

44 changes: 44 additions & 0 deletions packages/cache-buster/README.md
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(),
},
};
```

10 changes: 10 additions & 0 deletions packages/cache-buster/jest.config.js
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',
},
};
47 changes: 47 additions & 0 deletions packages/cache-buster/package.json
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"
}
}
7 changes: 7 additions & 0 deletions packages/cache-buster/rollup.config.mjs
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'))
});
53 changes: 53 additions & 0 deletions packages/cache-buster/src/cacheBuster.ts
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');
}
1 change: 1 addition & 0 deletions packages/cache-buster/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cacheBuster';
49 changes: 49 additions & 0 deletions packages/cache-buster/test/cacheBuster.test.ts
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.
Loading

0 comments on commit 1659228

Please sign in to comment.