Skip to content
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

Chore/object-hash-options #15

Merged
merged 4 commits into from
Apr 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}

32 changes: 32 additions & 0 deletions packages/core/src/utils/generateCacheName.ts
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);
}
47 changes: 47 additions & 0 deletions 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 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');
});
});
Loading