Skip to content

Commit

Permalink
cache id is now takes into account all pipe options
Browse files Browse the repository at this point in the history
add test
  • Loading branch information
GoodBoyDigital committed Apr 25, 2024
1 parent 0c2d6d6 commit e957baa
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 4 deletions.
7 changes: 4 additions & 3 deletions packages/core/src/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import type { AssetSettings } from './pipes/PipeSystem';
import { PipeSystem } from './pipes/PipeSystem';
import { finalCopyPipe } from './pipes/finalCopyPipe';
import type { AssetPackConfig } from './config';
import objectHash from 'object-hash';
import { Logger } from './logger/Logger';
import { promiseAllConcurrent } from './utils/promiseAllConcurrent';
import { path } from './utils/path';
import merge from 'merge';
import { generateCacheName } from './utils/generateCacheName';

export class AssetPack
{
Expand Down Expand Up @@ -41,10 +41,10 @@ export class AssetPack
level: this.config.logLevel || 'info',
});

const { pipes, cache, ...configWithoutPlugins } = this.config;
const { pipes, cache } = this.config;

// make a hash..
const cacheName = [objectHash(configWithoutPlugins), ...(pipes as AssetPipe[]).map((pipe) => pipe.name)].join('-');
const cacheName = generateCacheName(this.config);

let assetCacheData = null;
let assetCache: AssetCache | null = null;
Expand Down Expand Up @@ -243,3 +243,4 @@ function normalizePath(pth: string)

return pth;
}

21 changes: 21 additions & 0 deletions packages/core/src/utils/generateCacheName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import type { AssetPackConfig } from '../config';
import objectHash from 'object-hash';

export function generateCacheName(options: AssetPackConfig)
{
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { pipes, cache, ...configWithoutPlugins } = options;

const optionsToHash: any = {
...configWithoutPlugins,
};

// get pipes
pipes?.flat().forEach((pipe) =>
{
optionsToHash[pipe.name] = pipe.defaultOptions;
});

// make a hash..
return objectHash(optionsToHash);
}
49 changes: 48 additions & 1 deletion packages/core/test/Utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { createFolder, createAssetPipe, getInputDir, getOutputDir } from '../../
import type { Asset } from '../src/Asset';
import { AssetPack } from '../src/AssetPack';
import { extractTagsFromFileName } from '../src/utils/extractTagsFromFileName';
import { generateCacheName } from '../src/utils/generateCacheName';

describe('Utils', () =>
{
Expand All @@ -24,7 +25,7 @@ describe('Utils', () =>
expect(extractTagsFromFileName('test{tag1}{tag2=1&2}.json')).toEqual({ tag1: true, tag2: [1, 2] });
});

it.only('should allow for tags to be overridden', async () =>
it('should allow for tags to be overridden', async () =>
{
const testName = 'tag-override';
const inputDir = getInputDir(pkg, testName);
Expand Down Expand Up @@ -82,4 +83,50 @@ describe('Utils', () =>

await assetpack.run();
});

it('should create a unique cache name', async () =>
{
const cacheName = generateCacheName({
entry: 'test',
output: 'out',
pipes: [
{
name: 'test',
defaultOptions: { hi: 'there' }
},
],
});

expect(cacheName).toEqual('9782a5400ded95c60849cf955508938b7efdc8a0');

// change the settings:

const cacheName2 = generateCacheName({
entry: 'test',
output: 'out',
pipes: [
{
name: 'test-2',
defaultOptions: { hi: 'there' }
},
],
});

expect(cacheName2).toEqual('abdf0d02db2c221346e31f61331e5880deff6f4e');

// change the settings:

const cacheName3 = generateCacheName({
entry: 'test',
output: 'out',
pipes: [
{
name: 'test-2',
defaultOptions: { hi: 'bye!' }
},
],
});

expect(cacheName3).toEqual('ab900fa81d7121ea46bd2eafe9e826633c1c48a0');
});
});

0 comments on commit e957baa

Please sign in to comment.