From 0fb4f1e778ba358b15553fbc07d19aa5b0dbceeb Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:13:58 +0100 Subject: [PATCH 1/9] fix: ensure allMetaData is up to date --- packages/assetpack/src/core/Asset.ts | 11 ++++++++--- packages/assetpack/src/webfont/webfont.ts | 3 +-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/packages/assetpack/src/core/Asset.ts b/packages/assetpack/src/core/Asset.ts index 9c5888e..c831c78 100644 --- a/packages/assetpack/src/core/Asset.ts +++ b/packages/assetpack/src/core/Asset.ts @@ -30,7 +30,7 @@ export class Asset metaData: Record = {}; inheritedMetaData: Record = {}; - allMetaData: Record = {}; + private _allMetaData: Record = {}; transformData: Record = {}; settings?: Record; @@ -63,7 +63,7 @@ export class Asset asset.parent = this; asset.inheritedMetaData = { ...this.inheritedMetaData, ...this.metaData }; - asset.allMetaData = { ...asset.inheritedMetaData, ...asset.metaData }; + asset._allMetaData = { ...asset.inheritedMetaData, ...asset.metaData }; asset.transformData = { ...this.transformData, ...asset.transformData }; } @@ -87,12 +87,17 @@ export class Asset asset.transformParent = this; asset.inheritedMetaData = { ...this.inheritedMetaData, ...this.metaData }; - asset.allMetaData = { ...asset.inheritedMetaData, ...asset.metaData }; + asset._allMetaData = { ...asset.inheritedMetaData, ...asset.metaData }; asset.transformData = { ...this.transformData, ...asset.transformData }; asset.settings = this.settings; } + get allMetaData() + { + return { ...this.inheritedMetaData, ...this.metaData }; + } + get state() { return this._state; diff --git a/packages/assetpack/src/webfont/webfont.ts b/packages/assetpack/src/webfont/webfont.ts index 9ec7481..3f56165 100644 --- a/packages/assetpack/src/webfont/webfont.ts +++ b/packages/assetpack/src/webfont/webfont.ts @@ -34,8 +34,7 @@ export function webfont(): AssetPipe buffer = fonts.svg.to.woff2(asset.path); break; default: - throw new Error(`{Assetpack] Unsupported font type: ${ext}`); - break; + throw new Error(`{AssetPack] Unsupported font type: ${ext}`); } const newFileName = asset.filename.replace(/\.(otf|ttf|svg)$/i, '.woff2'); From 3b36f33102de895a028c0dae0c7324e1c7058919 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Wed, 28 Aug 2024 14:21:18 +0100 Subject: [PATCH 2/9] remove unused --- packages/assetpack/src/core/Asset.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/assetpack/src/core/Asset.ts b/packages/assetpack/src/core/Asset.ts index c831c78..52aa791 100644 --- a/packages/assetpack/src/core/Asset.ts +++ b/packages/assetpack/src/core/Asset.ts @@ -30,7 +30,6 @@ export class Asset metaData: Record = {}; inheritedMetaData: Record = {}; - private _allMetaData: Record = {}; transformData: Record = {}; settings?: Record; @@ -63,7 +62,6 @@ export class Asset asset.parent = this; asset.inheritedMetaData = { ...this.inheritedMetaData, ...this.metaData }; - asset._allMetaData = { ...asset.inheritedMetaData, ...asset.metaData }; asset.transformData = { ...this.transformData, ...asset.transformData }; } @@ -87,7 +85,6 @@ export class Asset asset.transformParent = this; asset.inheritedMetaData = { ...this.inheritedMetaData, ...this.metaData }; - asset._allMetaData = { ...asset.inheritedMetaData, ...asset.metaData }; asset.transformData = { ...this.transformData, ...asset.transformData }; asset.settings = this.settings; From 0d3d68811c0261864a6c895db1ee8012268a2553 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Wed, 28 Aug 2024 15:11:10 +0100 Subject: [PATCH 3/9] feat: add data tags --- packages/assetpack/src/core/Asset.ts | 18 ++++++++++++++++++ packages/assetpack/src/core/pipes/AssetPipe.ts | 8 +++++++- .../assetpack/src/core/pipes/PipeSystem.ts | 2 ++ .../assetpack/src/manifest/pixiManifest.ts | 3 ++- packages/assetpack/src/webfont/sdf.ts | 9 +++++++-- packages/assetpack/src/webfont/webfont.ts | 8 +++++++- .../assetpack/test/webfont/Webfont.test.ts | 12 ++++++++---- packages/docs/docs/guide/pipes/overview.mdx | 11 +++++++++-- packages/docs/docs/guide/pipes/webfont.mdx | 16 +++++++++------- 9 files changed, 69 insertions(+), 18 deletions(-) diff --git a/packages/assetpack/src/core/Asset.ts b/packages/assetpack/src/core/Asset.ts index 52aa791..7c23b41 100644 --- a/packages/assetpack/src/core/Asset.ts +++ b/packages/assetpack/src/core/Asset.ts @@ -31,6 +31,7 @@ export class Asset metaData: Record = {}; inheritedMetaData: Record = {}; transformData: Record = {}; + manifestData: Record = {}; settings?: Record; @@ -253,5 +254,22 @@ export class Asset this.children[i].releaseChildrenBuffers(); } } + + /** + * Update the manifest data with certain keys from the metaData + * @param keys - keys to apply from the metaData to the manifestData + */ + applyManifestData(keys: string[]) + { + for (let i = 0; i < keys.length; i++) + { + const key = keys[i]; + + if (this.metaData[key] !== undefined) + { + this.manifestData[key] = this.metaData[key]; + } + } + } } diff --git a/packages/assetpack/src/core/pipes/AssetPipe.ts b/packages/assetpack/src/core/pipes/AssetPipe.ts index 6a2feb6..26e203d 100644 --- a/packages/assetpack/src/core/pipes/AssetPipe.ts +++ b/packages/assetpack/src/core/pipes/AssetPipe.ts @@ -17,7 +17,7 @@ export type DeepRequired = T extends Primitive }; export interface PluginOptions {} -export interface AssetPipe, TAGS extends string = string> +export interface AssetPipe, TAGS extends string = string, DATA_TAGS extends string = string> { /** Whether the process runs on a folder */ folder?: boolean; @@ -31,6 +31,12 @@ export interface AssetPipe, TAGS extends string = st /** Tags that can be used to control the plugin */ tags?: Record; + /** + * Any tags here will be placed in the manifests `data` object + * This can be used to pass data to loaders more easily + */ + dataTags?: Record; + /** * Called once at the start. * @param asser - the root asset diff --git a/packages/assetpack/src/core/pipes/PipeSystem.ts b/packages/assetpack/src/core/pipes/PipeSystem.ts index 39b528c..470e61e 100644 --- a/packages/assetpack/src/core/pipes/PipeSystem.ts +++ b/packages/assetpack/src/core/pipes/PipeSystem.ts @@ -88,6 +88,8 @@ export class PipeSystem { asset.transformName = pipe.name; asset.transformChildren = []; + // apply the dataTags of the pipe to the asset so it can be used by pixi and other loaders + asset.applyManifestData(Object.values(pipe.dataTags ?? {})); const assets = await pipe.transform(asset, options, this); diff --git a/packages/assetpack/src/manifest/pixiManifest.ts b/packages/assetpack/src/manifest/pixiManifest.ts index 127525f..6eed7e6 100644 --- a/packages/assetpack/src/manifest/pixiManifest.ts +++ b/packages/assetpack/src/manifest/pixiManifest.ts @@ -143,7 +143,8 @@ function collectAssets( .map((finalAsset) => path.relative(outputPath, finalAsset.path)) .sort((a, b) => b.localeCompare(a)), data: options.includeMetaData ? { - tags: asset.allMetaData + tags: asset.allMetaData, + ...asset.manifestData } : undefined }); } diff --git a/packages/assetpack/src/webfont/sdf.ts b/packages/assetpack/src/webfont/sdf.ts index 9b16132..8bfc490 100644 --- a/packages/assetpack/src/webfont/sdf.ts +++ b/packages/assetpack/src/webfont/sdf.ts @@ -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'; @@ -14,7 +14,7 @@ export interface SDFFontOptions extends PluginOptions function signedFont( defaultOptions: SDFFontOptions -): AssetPipe +): AssetPipe { return { folder: false, @@ -25,6 +25,9 @@ function signedFont( nc: 'nc', fix: 'fix', }, + dataTags: { + family: 'family', + }, test(asset: Asset) { return asset.allMetaData[this.tags!.font] && checkExt(asset.path, '.ttf'); @@ -33,6 +36,8 @@ function signedFont( { const newFileName = stripTags(asset.filename.replace(/\.(ttf)$/i, '')); + // set the family name to the filename if it doesn't exist + asset.manifestData.family = asset.metaData.family ?? path.trimExt(asset.filename); const { font, textures } = await GenerateFont(asset.path, { ...options.font, filename: newFileName, diff --git a/packages/assetpack/src/webfont/webfont.ts b/packages/assetpack/src/webfont/webfont.ts index 3f56165..ed9b663 100644 --- a/packages/assetpack/src/webfont/webfont.ts +++ b/packages/assetpack/src/webfont/webfont.ts @@ -3,7 +3,7 @@ import { fonts } from './fonts.js'; import type { Asset, AssetPipe } from '../core/index.js'; -export function webfont(): AssetPipe +export function webfont(): AssetPipe { return { folder: false, @@ -12,6 +12,9 @@ export function webfont(): AssetPipe tags: { wf: 'wf', }, + dataTags: { + family: 'family', + }, test(asset: Asset) { return asset.allMetaData[this.tags!.wf] && checkExt(asset.path, '.otf', '.ttf', '.svg'); @@ -43,6 +46,9 @@ export function webfont(): AssetPipe newAsset.buffer = buffer; + // set the family name to the filename if it doesn't exist + asset.manifestData.family = asset.metaData.family ?? path.trimExt(asset.filename); + return [newAsset]; } }; diff --git a/packages/assetpack/test/webfont/Webfont.test.ts b/packages/assetpack/test/webfont/Webfont.test.ts index b924fc9..1593a7c 100644 --- a/packages/assetpack/test/webfont/Webfont.test.ts +++ b/packages/assetpack/test/webfont/Webfont.test.ts @@ -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, - } + }, } }, ], diff --git a/packages/docs/docs/guide/pipes/overview.mdx b/packages/docs/docs/guide/pipes/overview.mdx index be273fc..5c5d6dd 100644 --- a/packages/docs/docs/guide/pipes/overview.mdx +++ b/packages/docs/docs/guide/pipes/overview.mdx @@ -57,5 +57,12 @@ Each plugin has its own set of tags, so be sure to check the documentation for t ### Other Tag Examples - You can also add multiple tags to a single asset, like this `asset{tag1}{tag2}`. -- Tags can have data appended to them, like this `asset{tag1:myData}`. -- Tags can have multiple data values, like this `asset{tag1:100,200}`. +- Tags can have data appended to them, like this `asset{tag1=myData}`. +- Tags can have multiple data values, like this `asset{tag1=100&200}`. + +### Data Tags + +Data tags are a special type of tag that a plugin can specify to allow for the value of that tag to be passed to the manifest. +This can be useful for PixiJS as in the manifest you can specify certain properties for each asset. + +For example, the webfont plugin has a `family` tag that can be used to specify the font family name of the font file. diff --git a/packages/docs/docs/guide/pipes/webfont.mdx b/packages/docs/docs/guide/pipes/webfont.mdx index 0bd9bad..3907127 100644 --- a/packages/docs/docs/guide/pipes/webfont.mdx +++ b/packages/docs/docs/guide/pipes/webfont.mdx @@ -30,9 +30,10 @@ export default { ### Tags -| Tag | Folder/File | Description | -| ---- | ----------- | -------------------------------------------------------------- | -| `wf` | `both` | If present the file(s) will be converted to a webfont (woff2). | +| Tag | Folder/File | Description | +| -------- | ----------- | -------------------------------------------------------------- | +| `wf` | `both` | If present the file(s) will be converted to a webfont (woff2). | +| `family` | `file` | The font family name. | --- @@ -80,7 +81,8 @@ export default { ### Tags -| Tag | Folder/File | Description | -| ------ | ----------- | -------------------------------------------------------- | -| `sdf` | `both` | If present the file(s) will be converted to a SDF font. | -| `msdf` | `both` | If present the file(s) will be converted to a MSDF font. | +| Tag | Folder/File | Description | +| -------- | ----------- | -------------------------------------------------------- | +| `sdf` | `both` | If present the file(s) will be converted to a SDF font. | +| `msdf` | `both` | If present the file(s) will be converted to a MSDF font. | +| `family` | `file` | The font family name. | From 90ffd6673fc9cd850ce9d6ffac0a552d6d779411 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:09:05 +0100 Subject: [PATCH 4/9] update --- packages/assetpack/src/core/Asset.ts | 35 ++++++++--- .../assetpack/src/core/pipes/AssetPipe.ts | 7 +-- .../assetpack/src/core/pipes/PipeSystem.ts | 8 ++- packages/assetpack/src/image/compress.ts | 2 +- .../assetpack/src/manifest/pixiManifest.ts | 63 ++++++++++++++++--- packages/assetpack/src/webfont/sdf.ts | 7 +-- packages/assetpack/src/webfont/webfont.ts | 7 +-- .../assetpack/test/manifest/Manifest.test.ts | 1 + .../test/spine/spineAtlasManifest.test.ts | 3 + .../assetpack/test/webfont/Webfont.test.ts | 2 +- 10 files changed, 101 insertions(+), 34 deletions(-) diff --git a/packages/assetpack/src/core/Asset.ts b/packages/assetpack/src/core/Asset.ts index 7c23b41..e60fee5 100644 --- a/packages/assetpack/src/core/Asset.ts +++ b/packages/assetpack/src/core/Asset.ts @@ -31,7 +31,6 @@ export class Asset metaData: Record = {}; inheritedMetaData: Record = {}; transformData: Record = {}; - manifestData: Record = {}; settings?: Record; @@ -256,20 +255,38 @@ export class Asset } /** - * Update the manifest data with certain keys from the metaData - * @param keys - keys to apply from the metaData to the manifestData + * Get the public meta data for this asset + * This will exclude any internal data */ - applyManifestData(keys: string[]) + getPublicMetaData(internalPipeData: Record) { - for (let i = 0; i < keys.length; i++) + const internalKeys = new Set(Object.keys(internalPipeData)); + const metaData = Object.keys(this.allMetaData).reduce((result: Record, key) => { - const key = keys[i]; + if (!internalKeys.has(key)) + { + result[key] = this.allMetaData[key]; + } + + return result; + }, {} as Record); + + return metaData; + } - if (this.metaData[key] !== undefined) + getInternalMetaData(internalPipeData: Record) + { + const res: Record = {}; + + Object.keys(internalPipeData).forEach((key) => + { + if (this.allMetaData[key]) { - this.manifestData[key] = this.metaData[key]; + res[key] = this.allMetaData[key]; } - } + }); + + return res; } } diff --git a/packages/assetpack/src/core/pipes/AssetPipe.ts b/packages/assetpack/src/core/pipes/AssetPipe.ts index 26e203d..1662ba3 100644 --- a/packages/assetpack/src/core/pipes/AssetPipe.ts +++ b/packages/assetpack/src/core/pipes/AssetPipe.ts @@ -17,7 +17,7 @@ export type DeepRequired = T extends Primitive }; export interface PluginOptions {} -export interface AssetPipe, TAGS extends string = string, DATA_TAGS extends string = string> +export interface AssetPipe, TAGS extends string = string, INTERNAL_TAGS extends string = string> { /** Whether the process runs on a folder */ folder?: boolean; @@ -32,10 +32,9 @@ export interface AssetPipe, TAGS extends string = st tags?: Record; /** - * Any tags here will be placed in the manifests `data` object - * This can be used to pass data to loaders more easily + * Any tags here will not be placed in the manifest data. */ - dataTags?: Record; + internalTags?: Record; /** * Called once at the start. diff --git a/packages/assetpack/src/core/pipes/PipeSystem.ts b/packages/assetpack/src/core/pipes/PipeSystem.ts index 470e61e..6b14e85 100644 --- a/packages/assetpack/src/core/pipes/PipeSystem.ts +++ b/packages/assetpack/src/core/pipes/PipeSystem.ts @@ -28,6 +28,11 @@ export class PipeSystem assetSettings: AssetSettings[] = []; + internalMetaData: Record = { + copy: 'copy', + ignore: 'ignore', + }; + constructor(options: PipeSystemOptions) { const pipes = []; @@ -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; @@ -88,8 +94,6 @@ export class PipeSystem { asset.transformName = pipe.name; asset.transformChildren = []; - // apply the dataTags of the pipe to the asset so it can be used by pixi and other loaders - asset.applyManifestData(Object.values(pipe.dataTags ?? {})); const assets = await pipe.transform(asset, options, this); diff --git a/packages/assetpack/src/image/compress.ts b/packages/assetpack/src/image/compress.ts index 92ba323..12be771 100644 --- a/packages/assetpack/src/image/compress.ts +++ b/packages/assetpack/src/image/compress.ts @@ -67,7 +67,7 @@ export function compress(options: CompressOptions = {}): AssetPipe @@ -45,6 +84,7 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe['tags'] + tags: AssetPipe['tags'], + internalTags: Record ) { if (asset.skip) return; @@ -137,21 +179,28 @@ function collectAssets( if (finalManifestAssets.length === 0) return; + const metadata = { + tags: { ...asset.getInternalMetaData(internalTags) }, + ...asset.getPublicMetaData(internalTags) + } as Record; + + 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, - ...asset.manifestData - } : 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.. diff --git a/packages/assetpack/src/webfont/sdf.ts b/packages/assetpack/src/webfont/sdf.ts index 8bfc490..5749f09 100644 --- a/packages/assetpack/src/webfont/sdf.ts +++ b/packages/assetpack/src/webfont/sdf.ts @@ -14,7 +14,7 @@ export interface SDFFontOptions extends PluginOptions function signedFont( defaultOptions: SDFFontOptions -): AssetPipe +): AssetPipe { return { folder: false, @@ -25,9 +25,6 @@ function signedFont( nc: 'nc', fix: 'fix', }, - dataTags: { - family: 'family', - }, test(asset: Asset) { return asset.allMetaData[this.tags!.font] && checkExt(asset.path, '.ttf'); @@ -37,7 +34,7 @@ function signedFont( const newFileName = stripTags(asset.filename.replace(/\.(ttf)$/i, '')); // set the family name to the filename if it doesn't exist - asset.manifestData.family = asset.metaData.family ?? path.trimExt(asset.filename); + asset.metaData.family ??= path.trimExt(asset.filename); const { font, textures } = await GenerateFont(asset.path, { ...options.font, filename: newFileName, diff --git a/packages/assetpack/src/webfont/webfont.ts b/packages/assetpack/src/webfont/webfont.ts index ed9b663..4ed2471 100644 --- a/packages/assetpack/src/webfont/webfont.ts +++ b/packages/assetpack/src/webfont/webfont.ts @@ -3,7 +3,7 @@ import { fonts } from './fonts.js'; import type { Asset, AssetPipe } from '../core/index.js'; -export function webfont(): AssetPipe +export function webfont(): AssetPipe { return { folder: false, @@ -12,9 +12,6 @@ export function webfont(): AssetPipe tags: { wf: 'wf', }, - dataTags: { - family: 'family', - }, test(asset: Asset) { return asset.allMetaData[this.tags!.wf] && checkExt(asset.path, '.otf', '.ttf', '.svg'); @@ -47,7 +44,7 @@ export function webfont(): AssetPipe newAsset.buffer = buffer; // set the family name to the filename if it doesn't exist - asset.manifestData.family = asset.metaData.family ?? path.trimExt(asset.filename); + asset.metaData.family ??= path.trimExt(asset.filename); return [newAsset]; } diff --git a/packages/assetpack/test/manifest/Manifest.test.ts b/packages/assetpack/test/manifest/Manifest.test.ts index cd29400..bfbae6d 100644 --- a/packages/assetpack/test/manifest/Manifest.test.ts +++ b/packages/assetpack/test/manifest/Manifest.test.ts @@ -218,6 +218,7 @@ describe('Manifest', () => alias: ['spine/dragon.atlas'], src: ['spine/dragon@0.5x.atlas', 'spine/dragon.atlas'], data: { + spine: true, tags: { spine: true, }, diff --git a/packages/assetpack/test/spine/spineAtlasManifest.test.ts b/packages/assetpack/test/spine/spineAtlasManifest.test.ts index 2a24b3d..f546e62 100644 --- a/packages/assetpack/test/spine/spineAtlasManifest.test.ts +++ b/packages/assetpack/test/spine/spineAtlasManifest.test.ts @@ -78,6 +78,7 @@ describe('Atlas Manifest', () => 'dragon.atlas' ], data: { + spine: true, tags: { spine: true } @@ -182,6 +183,7 @@ describe('Atlas Manifest', () => 'dragon.atlas' ], data: { + spine: true, tags: { spine: true } @@ -206,6 +208,7 @@ describe('Atlas Manifest', () => 'nested/dragon.atlas' ], data: { + spine: true, tags: { spine: true } diff --git a/packages/assetpack/test/webfont/Webfont.test.ts b/packages/assetpack/test/webfont/Webfont.test.ts index 1593a7c..ea7e695 100644 --- a/packages/assetpack/test/webfont/Webfont.test.ts +++ b/packages/assetpack/test/webfont/Webfont.test.ts @@ -293,7 +293,7 @@ describe('Webfont', () => msdfFont(), mipmap(), compress(), - pixiManifest(), + pixiManifest({ legacyMetaDataOutput: false }), ] }); From 089c3882e6a9953cc979669054eb9f26c482668c Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Fri, 30 Aug 2024 10:42:42 +0100 Subject: [PATCH 5/9] feat: add frame names to spritesheet manifest data --- .../assetpack/src/texture-packer/texturePacker.ts | 11 +++++++++++ packages/docs/docs/guide/pipes/texture-packer.mdx | 2 ++ 2 files changed, 13 insertions(+) diff --git a/packages/assetpack/src/texture-packer/texturePacker.ts b/packages/assetpack/src/texture-packer/texturePacker.ts index 9e93f02..2edd856 100644 --- a/packages/assetpack/src/texture-packer/texturePacker.ts +++ b/packages/assetpack/src/texture-packer/texturePacker.ts @@ -19,6 +19,8 @@ export interface TexturePackerOptions extends PluginOptions /** The maximum size a sprite sheet can be before its split out */ maximumTextureSize?: number; } + /** If true, the frame names for the sprite sheet will be added to the asset meta data. */ + addFrameNames?: boolean; } function checkForTexturePackerShortcutClashes( @@ -67,6 +69,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe b - a).forEach((resolution) => { @@ -160,6 +164,8 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipeDefaults to `{ default: 1, low: 0.5 }`. | | resolutionOptions.fixedResolution | `string` | A resolution used if the fix tag is applied. Resolution must match one found in resolutions.
Defaults to `default`. | | resolutionOptions.maximumTextureSize | `number` | The maximum size a sprite sheet can be before its split out.
Defaults to `4096`. | +| | | | +| addFrameNames | `boolean` | Whether to add frame names to the data in the manifest
Defaults to `false`. | ## Tags From 54518cbc121b510e98ef99de3637daaeebcbdab9 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Tue, 3 Sep 2024 09:38:39 +0100 Subject: [PATCH 6/9] add test --- .../src/texture-packer/texturePacker.ts | 16 ++++++---- .../assetpack/test/manifest/Manifest.test.ts | 29 +++++++++++++++++-- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/packages/assetpack/src/texture-packer/texturePacker.ts b/packages/assetpack/src/texture-packer/texturePacker.ts index 2edd856..95f7d63 100644 --- a/packages/assetpack/src/texture-packer/texturePacker.ts +++ b/packages/assetpack/src/texture-packer/texturePacker.ts @@ -69,7 +69,7 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe b - a).forEach((resolution) => { @@ -151,6 +151,14 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe + { + Object.keys(json.frames).forEach((frame) => imageNames.add(frame)); + }); + } + const outPromises: Promise[] = []; for (let i = 0; i < out.textures.length; i++) @@ -164,8 +172,6 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe resolutionOptions: { maximumTextureSize: 512, }, + addFrameNames: true, }), mipmap(), compress({ @@ -183,10 +184,34 @@ describe('Manifest', () => 'bundle/tps-0.png.json'], data: { tags: { - tps: true, m: true, + tps: true, + frameNames: [ + 'sprite9.png', + 'sprite8.png', + 'sprite7.png', + 'sprite6.png', + 'sprite5.png', + 'sprite4.png', + 'sprite3.png', + 'sprite2.png', + 'sprite1.png', + 'sprite0.png' + ] }, - }, + frameNames: [ + 'sprite9.png', + 'sprite8.png', + 'sprite7.png', + 'sprite6.png', + 'sprite5.png', + 'sprite4.png', + 'sprite3.png', + 'sprite2.png', + 'sprite1.png', + 'sprite0.png' + ] + } }, ], }); From d96eee4dca20f6b3d9ec20dcb25b7ca9377acca2 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Tue, 3 Sep 2024 10:13:21 +0100 Subject: [PATCH 7/9] fix up --- packages/assetpack/src/image/compress.ts | 2 +- packages/assetpack/src/image/utils/compressGpuTextures.ts | 6 +++--- packages/assetpack/test/spine/spineAtlasAll.test.ts | 1 + .../assetpack/test/texture-packer/texturePackerAll.test.ts | 1 + 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/packages/assetpack/src/image/compress.ts b/packages/assetpack/src/image/compress.ts index 50f9693..aa1acc3 100644 --- a/packages/assetpack/src/image/compress.ts +++ b/packages/assetpack/src/image/compress.ts @@ -137,8 +137,8 @@ export function compress(options: CompressOptions = {}): AssetPipe { - let compressed: CompressImageDataResult[] = []; + let compressed: (Partial & {image: Promise})[] = []; if (!options.astc && !options.bc7 && !options.basis) { - return compressed; + return compressed as CompressImageDataResult[]; } const tmpDir = await fs.mkdtemp(join(tmpdir(), 'assetpack-tex-')); @@ -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 diff --git a/packages/assetpack/test/spine/spineAtlasAll.test.ts b/packages/assetpack/test/spine/spineAtlasAll.test.ts index eefc4d5..2d29ff9 100644 --- a/packages/assetpack/test/spine/spineAtlasAll.test.ts +++ b/packages/assetpack/test/spine/spineAtlasAll.test.ts @@ -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; diff --git a/packages/assetpack/test/texture-packer/texturePackerAll.test.ts b/packages/assetpack/test/texture-packer/texturePackerAll.test.ts index 3d39700..494921c 100644 --- a/packages/assetpack/test/texture-packer/texturePackerAll.test.ts +++ b/packages/assetpack/test/texture-packer/texturePackerAll.test.ts @@ -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; From bedf79b79fbc018e03b47e783883988ba5760115 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Tue, 3 Sep 2024 10:18:32 +0100 Subject: [PATCH 8/9] longer tests --- packages/assetpack/vitest.config.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/assetpack/vitest.config.js b/packages/assetpack/vitest.config.js index 558d2fc..95696c1 100644 --- a/packages/assetpack/vitest.config.js +++ b/packages/assetpack/vitest.config.js @@ -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: From 11277bc198934f1ac9fbc4235a1a1116cf493eb8 Mon Sep 17 00:00:00 2001 From: Zyie <24736175+Zyie@users.noreply.github.com> Date: Tue, 3 Sep 2024 10:26:22 +0100 Subject: [PATCH 9/9] longer tests --- .../assetpack/test/texture-packer/texturePackerCompress.test.ts | 2 +- .../assetpack/test/texture-packer/texturePackerManifest.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/assetpack/test/texture-packer/texturePackerCompress.test.ts b/packages/assetpack/test/texture-packer/texturePackerCompress.test.ts index d9e867f..9ca4964 100644 --- a/packages/assetpack/test/texture-packer/texturePackerCompress.test.ts +++ b/packages/assetpack/test/texture-packer/texturePackerCompress.test.ts @@ -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 }); }); diff --git a/packages/assetpack/test/texture-packer/texturePackerManifest.test.ts b/packages/assetpack/test/texture-packer/texturePackerManifest.test.ts index 7245c96..12932fb 100644 --- a/packages/assetpack/test/texture-packer/texturePackerManifest.test.ts +++ b/packages/assetpack/test/texture-packer/texturePackerManifest.test.ts @@ -110,5 +110,5 @@ describe('Texture Packer Compression', () => } }, ]); - }); + }, { timeout: 20000 }); });