Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Aug 29, 2024
1 parent c74abf4 commit b28b33e
Show file tree
Hide file tree
Showing 10 changed files with 106 additions and 37 deletions.
43 changes: 31 additions & 12 deletions packages/assetpack/src/core/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ export class Asset

metaData: Record<string, any> = {};
inheritedMetaData: Record<string, any> = {};
allMetaData: Record<string, any> = {};
transformData: Record<string, any> = {};
manifestData: Record<string, any> = {};

settings?: Record<string, any>;

Expand Down Expand Up @@ -64,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 };
}

Expand All @@ -88,12 +85,16 @@ 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;
}

get allMetaData()
{
return { ...this.inheritedMetaData, ...this.metaData };
}

get state()
{
return this._state;
Expand Down Expand Up @@ -254,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<string, any>)
{
for (let i = 0; i < keys.length; i++)
const internalKeys = new Set(Object.keys(internalPipeData));
const metaData = Object.keys(this.allMetaData).reduce((result: Record<string, any>, key) =>
{
const key = keys[i];
if (!internalKeys.has(key))
{
result[key] = this.allMetaData[key];
}

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

if (this.metaData[key] !== undefined)
return metaData;
}

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

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

return res;
}
}

7 changes: 3 additions & 4 deletions 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, DATA_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 @@ -32,10 +32,9 @@ export interface AssetPipe<OPTIONS=Record<string, any>, TAGS extends string = st
tags?: Record<TAGS, string>;

/**
* 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<DATA_TAGS, string>;
internalTags?: Record<INTERNAL_TAGS, string>;

/**
* Called once at the start.
Expand Down
8 changes: 6 additions & 2 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 Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion packages/assetpack/src/image/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,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
63 changes: 56 additions & 7 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,21 +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,
...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..
Expand Down
7 changes: 2 additions & 5 deletions packages/assetpack/src/webfont/sdf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface SDFFontOptions extends PluginOptions

function signedFont(
defaultOptions: SDFFontOptions
): AssetPipe<SDFFontOptions, 'font' | 'nc' | 'fix', 'family'>
): AssetPipe<SDFFontOptions, 'font' | 'nc' | 'fix'>
{
return {
folder: false,
Expand All @@ -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');
Expand All @@ -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,
Expand Down
7 changes: 2 additions & 5 deletions packages/assetpack/src/webfont/webfont.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fonts } from './fonts.js';

import type { Asset, AssetPipe } from '../core/index.js';

export function webfont(): AssetPipe<any, 'wf', 'family'>
export function webfont(): AssetPipe<any, 'wf'>
{
return {
folder: false,
Expand All @@ -12,9 +12,6 @@ export function webfont(): AssetPipe<any, 'wf', 'family'>
tags: {
wf: 'wf',
},
dataTags: {
family: 'family',
},
test(asset: Asset)
{
return asset.allMetaData[this.tags!.wf] && checkExt(asset.path, '.otf', '.ttf', '.svg');
Expand Down Expand Up @@ -48,7 +45,7 @@ export function webfont(): AssetPipe<any, 'wf', 'family'>
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];
}
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 @@ -218,6 +218,7 @@ describe('Manifest', () =>
alias: ['spine/dragon.atlas'],
src: ['spine/[email protected]', 'spine/dragon.atlas'],
data: {
spine: true,
tags: {
spine: true,
},
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
2 changes: 1 addition & 1 deletion 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 Down

0 comments on commit b28b33e

Please sign in to comment.