Skip to content

Commit

Permalink
feat: allow tags to be added manifest data directly (#82)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie authored Sep 3, 2024
1 parent 80d7e77 commit 4c98058
Show file tree
Hide file tree
Showing 18 changed files with 150 additions and 29 deletions.
35 changes: 35 additions & 0 deletions packages/assetpack/src/core/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,5 +253,40 @@ export class Asset
this.children[i].releaseChildrenBuffers();
}
}

/**
* Get the public meta data for this asset
* This will exclude any internal data
*/
getPublicMetaData(internalPipeData: Record<string, any>)
{
const internalKeys = new Set(Object.keys(internalPipeData));
const metaData = Object.keys(this.allMetaData).reduce((result: Record<string, any>, key) =>
{
if (!internalKeys.has(key))
{
result[key] = this.allMetaData[key];
}

return result;
}, {} as Record<string, any>);

return metaData;
}

getInternalMetaData(internalPipeData: Record<string, any>)
{
const res: Record<string, any> = {};

Object.keys(internalPipeData).forEach((key) =>
{
if (this.allMetaData[key])
{
res[key] = this.allMetaData[key];
}
});

return res;
}
}

7 changes: 6 additions & 1 deletion packages/assetpack/src/core/pipes/AssetPipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type DeepRequired<T> = T extends Primitive
};
export interface PluginOptions {}

export interface AssetPipe<OPTIONS=Record<string, any>, TAGS extends string = string>
export interface AssetPipe<OPTIONS=Record<string, any>, TAGS extends string = string, INTERNAL_TAGS extends string = string>
{
/** Whether the process runs on a folder */
folder?: boolean;
Expand All @@ -31,6 +31,11 @@ export interface AssetPipe<OPTIONS=Record<string, any>, TAGS extends string = st
/** Tags that can be used to control the plugin */
tags?: Record<TAGS, string>;

/**
* Any tags here will not be placed in the manifest data.
*/
internalTags?: Record<INTERNAL_TAGS, string>;

/**
* Called once at the start.
* @param asser - the root asset
Expand Down
6 changes: 6 additions & 0 deletions packages/assetpack/src/core/pipes/PipeSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export class PipeSystem

assetSettings: AssetSettings[] = [];

internalMetaData: Record<string, any> = {
copy: 'copy',
ignore: 'ignore',
};

constructor(options: PipeSystemOptions)
{
const pipes = [];
Expand All @@ -49,6 +54,7 @@ export class PipeSystem
options.pipes.flat().forEach((pipe) =>
{
this.pipeHash[pipe.name] = pipe;
this.internalMetaData = { ...this.internalMetaData, ...Object.values(pipe.internalTags ?? pipe.tags ?? {}).reduce((acc, tag) => ({ ...acc, [tag]: true }), {}) };
});

this.pipes = pipes;
Expand Down
4 changes: 2 additions & 2 deletions packages/assetpack/src/image/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function compress(options: CompressOptions = {}): AssetPipe<CompressOptio
},
async transform(asset: Asset, options)
{
const shouldCompress = compress && !asset.metaData.nc;
const shouldCompress = compress && !asset.metaData[this.tags!.nc];

if (!shouldCompress)
{
Expand Down Expand Up @@ -137,8 +137,8 @@ export function compress(options: CompressOptions = {}): AssetPipe<CompressOptio
if ((image.format === '.png' && !options.png) || (((image.format === '.jpg') || (image.format === '.jpeg')) && !options.jpg))
{
newAssets.push(asset);

}

return newAssets;
}
catch (error)
Expand Down
6 changes: 3 additions & 3 deletions packages/assetpack/src/image/utils/compressGpuTextures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ export async function compressGpuTextures(
options: CompressOptions,
): Promise<CompressImageDataResult[]>
{
let compressed: CompressImageDataResult[] = [];
let compressed: (Partial<CompressImageDataResult> & {image: Promise<string>})[] = [];

if (!options.astc && !options.bc7 && !options.basis)
{
return compressed;
return compressed as CompressImageDataResult[];
}

const tmpDir = await fs.mkdtemp(join(tmpdir(), 'assetpack-tex-'));
Expand Down Expand Up @@ -87,7 +87,7 @@ export async function compressGpuTextures(
await fs.rm(tmpDir, { recursive: true, force: true });
}

return compressed;
return compressed as CompressImageDataResult[];
}

async function adjustImageSize(sharpImage: sharp.Sharp, imagePath: string): Promise<string>
Expand Down
62 changes: 56 additions & 6 deletions packages/assetpack/src/manifest/pixiManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,49 @@ export interface PixiManifestEntry

export interface PixiManifestOptions extends PluginOptions
{
/**
* The output location for the manifest file.
*/
output?: string;
/**
* if true, the alias will be created with the basename of the file.
*/
createShortcuts?: boolean;
/**
* if true, the extensions will be removed from the alias names.
*/
trimExtensions?: boolean;
/**
* if true, the metaData will be outputted in the data field of the manifest.
*/
includeMetaData?: boolean;
/**
* if true, the all tags will be outputted in the data.tags field of the manifest.
* If false, only internal tags will be outputted to the data.tags field. All other tags will be outputted to the data field directly.
* @example
* ```json
* {
* "bundles": [
* {
* "name": "default",
* "assets": [
* {
* "alias": ["test"],
* "src": ["test.png"],
* "data": {
* "tags": {
* "nc": true,
* "customTag": true // this tag will be outputted to the data field directly instead of the data.tags field
* }
* }
* }
* ]
* }
* ]
* }
* @default true
*/
legacyMetaDataOutput?: boolean;
}

export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<PixiManifestOptions, 'manifest' | 'mIgnore'>
Expand All @@ -45,6 +84,7 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
createShortcuts: false,
trimExtensions: false,
includeMetaData: true,
legacyMetaDataOutput: true,
..._options,
},
tags: {
Expand Down Expand Up @@ -72,7 +112,8 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
pipeSystem.entryPath,
manifest.bundles,
defaultBundle,
this.tags!
this.tags!,
pipeSystem.internalMetaData
);
filterUniqueNames(manifest);
await fs.writeJSON(newFileName, manifest, { spaces: 2 });
Expand Down Expand Up @@ -109,7 +150,8 @@ function collectAssets(
entryPath = '',
bundles: PixiBundle[],
bundle: PixiBundle,
tags: AssetPipe<null, 'manifest' | 'mIgnore'>['tags']
tags: AssetPipe<null, 'manifest' | 'mIgnore'>['tags'],
internalTags: Record<string, any>
)
{
if (asset.skip) return;
Expand Down Expand Up @@ -137,20 +179,28 @@ function collectAssets(

if (finalManifestAssets.length === 0) return;

const metadata = {
tags: { ...asset.getInternalMetaData(internalTags) },
...asset.getPublicMetaData(internalTags)
} as Record<string, any>;

if (options.legacyMetaDataOutput)
{
metadata.tags = asset.allMetaData;
}

bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, asset.path)), options),
src: finalManifestAssets
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a)),
data: options.includeMetaData ? {
tags: asset.allMetaData
} : undefined
data: options.includeMetaData ? metadata : undefined
});
}

asset.children.forEach((child) =>
{
collectAssets(child, options, outputPath, entryPath, bundles, localBundle, tags);
collectAssets(child, options, outputPath, entryPath, bundles, localBundle, tags, internalTags);
});

// for all assets.. check for atlas and remove them from the bundle..
Expand Down
4 changes: 3 additions & 1 deletion packages/assetpack/src/webfont/sdf.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs from 'fs-extra';
import generateBMFont from 'msdf-bmfont-xml';
import { checkExt, createNewAssetAt, stripTags } from '../core/index.js';
import { checkExt, createNewAssetAt, path, stripTags } from '../core/index.js';

import type { BitmapFontOptions } from 'msdf-bmfont-xml';
import type { Asset, AssetPipe, PluginOptions } from '../core/index.js';
Expand Down Expand Up @@ -33,6 +33,8 @@ function signedFont(
{
const newFileName = stripTags(asset.filename.replace(/\.(ttf)$/i, ''));

// set the family name to the filename if it doesn't exist
asset.metaData.family ??= path.trimExt(asset.filename);
const { font, textures } = await GenerateFont(asset.path, {
...options.font,
filename: newFileName,
Expand Down
3 changes: 3 additions & 0 deletions packages/assetpack/src/webfont/webfont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ export function webfont(): AssetPipe<any, 'wf'>

newAsset.buffer = buffer;

// set the family name to the filename if it doesn't exist
asset.metaData.family ??= path.trimExt(asset.filename);

return [newAsset];
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/assetpack/test/manifest/Manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ describe('Manifest', () =>
alias: ['spine/dragon.atlas'],
src: ['spine/[email protected]', 'spine/dragon.atlas'],
data: {
spine: true,
tags: {
spine: true,
},
Expand Down
1 change: 1 addition & 0 deletions packages/assetpack/test/spine/spineAtlasAll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ describe('Spine Atlas All', () =>
// remove the outputDir
file = file.replace(`${outputDir}/`, '');
const isFileHalfSize = file.includes('@0.5x');
// eslint-disable-next-line no-nested-ternary
const isFileFileType = file.includes(isWebp ? '.webp' : isAstc ? '.astc' : '.png');
const shouldExist = isHalfSize === isFileHalfSize && isFileType === isFileFileType;

Expand Down
3 changes: 3 additions & 0 deletions packages/assetpack/test/spine/spineAtlasManifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ describe('Atlas Manifest', () =>
'dragon.atlas'
],
data: {
spine: true,
tags: {
spine: true
}
Expand Down Expand Up @@ -182,6 +183,7 @@ describe('Atlas Manifest', () =>
'dragon.atlas'
],
data: {
spine: true,
tags: {
spine: true
}
Expand All @@ -206,6 +208,7 @@ describe('Atlas Manifest', () =>
'nested/dragon.atlas'
],
data: {
spine: true,
tags: {
spine: true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ describe('Texture Packer All', () =>
// remove the outputDir
file = file.replace(`${outputDir}/`, '');
const isFileHalfSize = file.includes('@0.5x');
// eslint-disable-next-line no-nested-ternary
const isFileFileType = file.includes(isWebp ? '.webp' : isAstc ? '.astc.ktx' : '.png');
const shouldExist = isHalfSize === isFileHalfSize && isFileType === isFileFileType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,5 @@ describe('Texture Packer Compression', () =>
expect(sheetWebp.meta.image).toEqual(`sprites.webp`);
expect(sheetAstc.meta.image).toEqual(`sprites.astc.ktx`);
expect(sheetBasis.meta.image).toEqual(`sprites.basis.ktx2`);
});
}, { timeout: 20000 });
});
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,5 @@ describe('Texture Packer Compression', () =>
}
},
]);
});
}, { timeout: 20000 });
});
14 changes: 9 additions & 5 deletions packages/assetpack/test/webfont/Webfont.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ describe('Webfont', () =>
msdfFont(),
mipmap(),
compress(),
pixiManifest(),
pixiManifest({ legacyMetaDataOutput: false }),
]
});

Expand All @@ -309,36 +309,40 @@ describe('Webfont', () =>
alias: ['defaultFolder/ttf.ttf'],
src: ['defaultFolder/ttf.woff2'],
data: {
family: 'ttf',
tags: {
wf: true,
}
},
}
},
{
alias: ['msdfFolder/ttf.ttf'],
src: ['msdfFolder/ttf.fnt'],
data: {
family: 'ttf',
tags: {
msdf: true,
}
},
}
},
{
alias: ['sdfFolder/ttf.ttf'],
src: ['sdfFolder/ttf.fnt'],
data: {
family: 'ttf',
tags: {
sdf: true,
}
},
}
},
{
alias: ['svgFolder/svg.svg'],
src: ['svgFolder/svg.woff2'],
data: {
family: 'svg',
tags: {
wf: true,
}
},
}
},
],
Expand Down
1 change: 1 addition & 0 deletions packages/assetpack/vitest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineConfig } from 'vitest/config';

export default defineConfig({
test: {
testTimeout: 10000,
poolOptions: {
threads: {
// Due to the cpu-features and vitest threads incompatibility, see:
Expand Down
Loading

0 comments on commit 4c98058

Please sign in to comment.