Skip to content

Commit

Permalink
Merge branch 'main' into patch-2
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie authored Nov 19, 2024
2 parents ed371f4 + abc1ae4 commit 150656d
Show file tree
Hide file tree
Showing 15 changed files with 411 additions and 47 deletions.
3 changes: 2 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions packages/assetpack/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "@assetpack/core",
"version": "1.1.1",
"license": "MIT",
"type": "module",
"version": "1.2.1",
"keywords": [],
"homepage": "https://pixijs.io/assetpack/",
"bugs": "https://github.com/pixijs/assetpack/issues",
"repository": {
"type": "git",
"url": "https://github.com/pixijs/assetpack.git"
},
"keywords": [],
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/core/index.js",
"./cache-buster": "./dist/cache-buster/index.js",
Expand All @@ -35,8 +35,8 @@
],
"scripts": {
"build": "tsc",
"release": "xs bump,git-push",
"publish-ci": "xs publish",
"release": "xs bump,git-push",
"watch": "tsc -w"
},
"husky": {
Expand Down Expand Up @@ -70,6 +70,7 @@
"fs-extra": "^11.2.0",
"glob": "^10.4.1",
"gpu-tex-enc": "^1.2.5",
"json5": "^2.2.3",
"maxrects-packer": "^2.7.3",
"merge": "^2.1.1",
"minimatch": "9.0.4",
Expand Down
33 changes: 21 additions & 12 deletions packages/assetpack/src/core/AssetCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,7 @@ export class AssetCache

private _serializeAsset(asset: Asset, schema: AssetCacheData['assets'], saveHash = false)
{
const serializeAsset: CachedAsset = {
isFolder: asset.isFolder,
parent: asset.parent?.path,
transformParent: asset.transformParent?.path,
metaData: asset.metaData,
transformData: asset.transformData
};

if (!asset.isFolder && saveHash)
{
serializeAsset.hash = asset.hash;
}
const serializeAsset: CachedAsset = this.toCacheData(asset, saveHash);

schema[asset.path] = serializeAsset;

Expand All @@ -77,6 +66,25 @@ export class AssetCache
this._serializeAsset(child, schema);
});
}

private toCacheData(asset: Asset, saveHash: boolean): CachedAsset
{
const data: CachedAsset = {
isFolder: asset.isFolder,
parent: asset.parent?.path,
transformParent: asset.transformParent?.path,
metaData: { ...asset.metaData },
inheritedMetaData: { ...asset.inheritedMetaData },
transformData: { ...asset.transformData }
};

if (!asset.isFolder && saveHash)
{
data.hash = asset.hash;
}

return data;
}
}

export interface CachedAsset
Expand All @@ -85,6 +93,7 @@ export interface CachedAsset
hash?: string;
parent: string | undefined;
metaData: Record<string, any>;
inheritedMetaData: Record<string, any>;
transformData: Record<string, any>;
transformParent: string | undefined;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/assetpack/src/core/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class AssetPack
if (cache)
{
// write back to the cache...
await (assetCache as AssetCache).write(root);
(assetCache as AssetCache).write(root);

// release the buffers from the cache
root.releaseChildrenBuffers();
Expand Down
2 changes: 1 addition & 1 deletion packages/assetpack/src/core/AssetWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface AssetWatcherOptions
assetSettingsData?: AssetSettings[];
ignore?: string | string[];
onUpdate: (root: Asset) => Promise<void>;
onComplete: (root: Asset) => Promise<void>;
onComplete: (root: Asset) => void;
}

interface ChangeData
Expand Down
20 changes: 12 additions & 8 deletions packages/assetpack/src/core/utils/syncAssetsWithCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ function syncAssetsFromCache(assetHash: Record<string, Asset>, cachedData: Recor
});

assetToDelete.metaData = cachedAsset.metaData;
assetToDelete.transformData = cachedAsset.transformData;
assetToDelete.inheritedMetaData = cachedAsset.inheritedMetaData;

assetToDelete.state = 'deleted';

Expand Down Expand Up @@ -87,22 +89,24 @@ function syncTransformedAssetsFromCache(assetHash: Record<string, Asset>, cached
for (const i in cachedData)
{
const cachedAssetData = cachedData[i];
let asset = assetHash[i];

if (cachedAssetData.transformParent)
if (!asset)
{
const transformedAsset = new Asset({
asset = new Asset({
path: i,
isFolder: cachedAssetData.isFolder
});

transformedAsset.metaData = cachedAssetData.metaData;
transformedAsset.transformData = cachedAssetData.transformData;
transformedAssets[i] = asset;
assetHash[i] = asset;

transformedAssets[i] = transformedAsset;
assetHash[i] = transformedAsset;

transformedAsset.transformParent = assetHash[cachedAssetData.transformParent];
asset.transformParent = assetHash[cachedAssetData.transformParent!];
}

asset.inheritedMetaData = cachedAssetData.inheritedMetaData;
asset.transformData = cachedAssetData.transformData;
asset.metaData = cachedAssetData.metaData;
}

for (const i in transformedAssets)
Expand Down
11 changes: 8 additions & 3 deletions packages/assetpack/src/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json5 from 'json5';
import { checkExt, createNewAssetAt, Logger } from '../core/index.js';

import type { Asset, AssetPipe } from '../core/index.js';
Expand All @@ -13,14 +14,18 @@ export function json(): AssetPipe<any, 'nc'>
},
test(asset: Asset)
{
return !asset.metaData[this.tags!.nc] && checkExt(asset.path, '.json');
return !asset.metaData[this.tags!.nc] && checkExt(asset.path, '.json', '.json5');
},
async transform(asset: Asset)
{
try
{
const json = JSON.parse(asset.buffer.toString());
const compressedJsonAsset = createNewAssetAt(asset, asset.filename);
const json = json5.parse(asset.buffer.toString());

// replace the json5 with json
const filename = asset.filename.replace('.json5', '.json');

const compressedJsonAsset = createNewAssetAt(asset, filename);

compressedJsonAsset.buffer = Buffer.from(JSON.stringify(json));

Expand Down
72 changes: 67 additions & 5 deletions packages/assetpack/src/manifest/pixiManifest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import fs from 'fs-extra';
import { path, stripTags } from '../core/index.js';
import { Logger, path, stripTags } from '../core/index.js';

import type {
Asset,
Expand All @@ -11,6 +11,7 @@ export interface PixiBundle
{
name: string;
assets: PixiManifestEntry[];
relativeName?: string;
}

export interface PixiManifest
Expand Down Expand Up @@ -46,6 +47,11 @@ export interface PixiManifestOptions extends PluginOptions
* if true, the metaData will be outputted in the data field of the manifest.
*/
includeMetaData?: boolean;
/**
* The name style for asset bundles in the manifest file.
* When set to relative, asset bundles will use their relative paths as names.
*/
nameStyle?: 'short' | 'relative';
/**
* 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.
Expand Down Expand Up @@ -85,6 +91,7 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
trimExtensions: false,
includeMetaData: true,
legacyMetaDataOutput: true,
nameStyle: 'short',
..._options,
},
tags: {
Expand Down Expand Up @@ -115,25 +122,52 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
this.tags!,
pipeSystem.internalMetaData
);
filterUniqueNames(manifest);
filterUniqueNames(manifest, options);
await fs.writeJSON(newFileName, manifest, { spaces: 2 });
}
};
}

function filterUniqueNames(manifest: PixiManifest)
function filterUniqueNames(manifest: PixiManifest, options: PixiManifestOptions)
{
const nameMap = new Map<PixiManifestEntry, string[]>();
const isNameStyleShort = options.nameStyle !== 'relative';
const bundleNames = new Set<string>();
const duplicateBundleNames = new Set<string>();

manifest.bundles.forEach((bundle) =>
bundle.assets.forEach((asset) => nameMap.set(asset, asset.alias as string[])));
{
if (isNameStyleShort)
{
if (bundleNames.has(bundle.name))
{
duplicateBundleNames.add(bundle.name);
Logger.warn(`[AssetPack][manifest] Duplicate bundle name '${bundle.name}'. All bundles with that name will be renamed to their relative name instead.`);
}
else
{
bundleNames.add(bundle.name);
}
}

bundle.assets.forEach((asset) => nameMap.set(asset, asset.alias as string[]));
});

const arrays = Array.from(nameMap.values());
const sets = arrays.map((arr) => new Set(arr));
const uniqueArrays = arrays.map((arr, i) => arr.filter((x) => sets.every((set, j) => j === i || !set.has(x))));

manifest.bundles.forEach((bundle) =>
{
if (isNameStyleShort)
{
// Switch to relative bundle name to avoid duplications
if (duplicateBundleNames.has(bundle.name))
{
bundle.name = bundle.relativeName ?? bundle.name;
}
}

bundle.assets.forEach((asset) =>
{
const names = nameMap.get(asset) as string[];
Expand All @@ -143,6 +177,21 @@ function filterUniqueNames(manifest: PixiManifest)
});
}

function getRelativeBundleName(asset: Asset, entryPath: string): string
{
let name = asset.filename;
let parent = asset.parent;

// Exclude assets the paths of which equal to the entry path
while (parent && parent.path !== entryPath)
{
name = `${parent.filename}/${name}`;
parent = parent.parent;
}

return stripTags(name);
}

function collectAssets(
asset: Asset,
options: PixiManifestOptions,
Expand All @@ -163,10 +212,23 @@ function collectAssets(
if (asset.metaData[tags!.manifest!])
{
localBundle = {
name: stripTags(asset.filename),
name: options.nameStyle === 'relative' ? getRelativeBundleName(asset, entryPath) : stripTags(asset.filename),
assets: []
};

// This property helps rename duplicate bundle declarations
// Also, mark it as non-enumerable to prevent fs from including it into output
if (options.nameStyle !== 'relative')
{
Object.defineProperty(localBundle, 'relativeName', {
enumerable: false,
get()
{
return getRelativeBundleName(asset, entryPath);
}
});
}

bundles.push(localBundle);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/assetpack/src/webfont/webfont.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { checkExt, createNewAssetAt, path } from '../core/index.js';
import { checkExt, createNewAssetAt, path, stripTags } from '../core/index.js';
import { fonts } from './fonts.js';

import type { Asset, AssetPipe } from '../core/index.js';
Expand Down Expand Up @@ -44,7 +44,7 @@ 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);
asset.metaData.family ??= stripTags(path.trimExt(asset.filename));

return [newAsset];
}
Expand Down
4 changes: 3 additions & 1 deletion packages/assetpack/test/core/AssetCache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,18 @@ describe('AssetCache', () =>

expect(cachedAssetData).toEqual({
test: {
inheritedMetaData: {},
isFolder: true,
metaData: {},
transformData: {},
},
'test/test.json': {
isFolder: false,
hash: '12345',
inheritedMetaData: {},
parent: 'test',
transformData: {},
metaData: {}
metaData: {},
}
});
});
Expand Down
Loading

0 comments on commit 150656d

Please sign in to comment.