Skip to content

Commit

Permalink
chore: change tag overrides (#37)
Browse files Browse the repository at this point in the history
Co-authored-by: Mat Groves <mat.groves@play.co>
  • Loading branch information
Zyie and GoodBoyDigital authored Jun 25, 2024
1 parent 83d4add commit aad9bc0
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 221 deletions.
11 changes: 6 additions & 5 deletions src/core/pipes/AssetPipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,22 @@ export type DeepRequired<T> = T extends Primitive
? DeepRequired<U2>
: DeepRequired<T[P]>
};
export interface PluginOptions<T extends string>
{
tags?: Partial<Record<T, string>>;
}
export interface PluginOptions {}

export interface AssetPipe<OPTIONS=Record<string, any>>
export interface AssetPipe<OPTIONS=Record<string, any>, TAGS extends string = string>
{
/** Whether the process runs on a folder */
folder?: boolean;

/** Name of the plugin used to tell the manifest parsers which one to use */
name: string;

/** Default options for the plugin */
defaultOptions: OPTIONS;

/** Tags that can be used to control the plugin */
tags?: Record<TAGS, string>;

/**
* Called once at the start.
* @param asser - the root asset
Expand Down
2 changes: 1 addition & 1 deletion src/core/pipes/mergePipeOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { merge } from '../utils/merge.js';
import type { Asset } from '../Asset.js';
import type { AssetPipe, PluginOptions } from './AssetPipe.js';

export function mergePipeOptions<T extends PluginOptions<any>>(pipe: AssetPipe<T>, asset: Asset): T | false
export function mergePipeOptions<T extends PluginOptions>(pipe: AssetPipe<T>, asset: Asset): T | false
{
if (!asset.settings) return pipe.defaultOptions;

Expand Down
23 changes: 10 additions & 13 deletions src/image/compress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type CompressWebpOptions = Omit<WebpOptions, 'force'>;
type CompressAvifOptions = Omit<AvifOptions, 'force'>;
type CompressPngOptions = Omit<PngOptions, 'force'>;

export interface CompressOptions extends PluginOptions<'nc'>
export interface CompressOptions extends PluginOptions
{
png?: CompressPngOptions | boolean;
webp?: CompressWebpOptions | boolean;
Expand All @@ -26,7 +26,7 @@ export interface CompressImageData
sharpImage: sharp.Sharp;
}

export function compress(options: CompressOptions = {}): AssetPipe<CompressOptions>
export function compress(options: CompressOptions = {}): AssetPipe<CompressOptions, 'nc'>
{
const compress = resolveOptions<CompressOptions>(options, {
png: true,
Expand All @@ -52,25 +52,22 @@ export function compress(options: CompressOptions = {}): AssetPipe<CompressOptio
});
}

const defaultOptions = {
...compress,
tags: {
nc: 'nc',
...options.tags,
}
};

return {
folder: true,
name: 'compress',
defaultOptions,
defaultOptions: {
...compress,
},
tags: {
nc: 'nc',
},
test(asset: Asset, options)
{
return options && checkExt(asset.path, '.png', '.jpg', '.jpeg') && !asset.allMetaData[options.tags.nc as any];
return options && checkExt(asset.path, '.png', '.jpg', '.jpeg') && !asset.allMetaData[this.tags!.nc];
},
async transform(asset: Asset, options)
{
const shouldCompress = compress && !asset.metaData[options.tags.nc as any];
const shouldCompress = compress && !asset.metaData.nc;

if (!shouldCompress)
{
Expand Down
28 changes: 11 additions & 17 deletions src/image/mipmap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { resolveOptions } from './utils/resolveOptions.js';
import type { Asset, AssetPipe, PluginOptions } from '../core/index.js';
import type { CompressImageData } from './compress.js';

export interface MipmapOptions<T extends string = ''> extends PluginOptions<'fix' | T>
export interface MipmapOptions extends PluginOptions
{
/** A template for denoting the resolution of the images. */
template?: string;
Expand All @@ -20,34 +20,28 @@ const defaultMipmapOptions: Required<MipmapOptions> = {
template: '@%%x',
resolutions: { default: 1, low: 0.5 },
fixedResolution: 'default',
tags: {
fix: 'fix',
}
};

export function mipmap(_options: MipmapOptions = {}): AssetPipe<MipmapOptions>
export function mipmap(_options: MipmapOptions = {}): AssetPipe<MipmapOptions, 'fix'>
{
const mipmap = resolveOptions(_options, defaultMipmapOptions);

const defaultOptions = {
...mipmap,
tags: {
fix: 'fix',
..._options.tags,
}
};

return {
folder: true,
name: 'mipmap',
defaultOptions,
defaultOptions: {
...mipmap,
},
tags: {
fix: 'fix',
},
test(asset: Asset, options)
{
return options && checkExt(asset.path, '.png', '.jpg', '.jpeg');
},
async transform(asset: Asset, options)
{
const shouldMipmap = mipmap && !asset.metaData[options.tags.fix as any];
const shouldMipmap = mipmap && !asset.metaData[this.tags!.fix];

let processedImages: CompressImageData[];

Expand All @@ -62,13 +56,13 @@ export function mipmap(_options: MipmapOptions = {}): AssetPipe<MipmapOptions>
if (shouldMipmap)
{
const { resolutions, fixedResolution } = options as Required<MipmapOptions>
|| defaultOptions;
|| this.defaultOptions;

const fixedResolutions: {[x: string]: number} = {};

fixedResolutions[fixedResolution] = resolutions[fixedResolution];

const resolutionHash = asset.allMetaData[options.tags.fix as any]
const resolutionHash = asset.allMetaData[this.tags!.fix]
? fixedResolutions
: resolutions;

Expand Down
23 changes: 8 additions & 15 deletions src/json/index.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import { checkExt, createNewAssetAt, Logger } from '../core/index.js';

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

export type JsonOptions = PluginOptions<'nc'>;

export function json(_options: JsonOptions = {}): AssetPipe
export function json(): AssetPipe<any, 'nc'>
{
const defaultOptions = {
tags: {
nc: 'nc',
..._options?.tags
}

};

return {
name: 'json',
folder: false,
defaultOptions,
test(asset: Asset, options)
defaultOptions: null,
tags: {
nc: 'nc',
},
test(asset: Asset)
{
return !asset.metaData[options.tags.nc] && checkExt(asset.path, '.json');
return !asset.metaData[this.tags!.nc] && checkExt(asset.path, '.json');
},
async transform(asset: Asset)
{
Expand Down
51 changes: 30 additions & 21 deletions src/manifest/pixiManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,30 @@ export interface PixiManifestEntry
};
}

export interface PixiManifestOptions extends PluginOptions<'mIgnore' | 'manifest'>
export interface PixiManifestOptions extends PluginOptions
{
output?: string;
createShortcuts?: boolean;
trimExtensions?: boolean;
includeMetaData?: boolean;
}

export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<PixiManifestOptions>
export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<PixiManifestOptions, 'manifest' | 'mIgnore'>
{
const defaultOptions: PixiManifestOptions = {
output: 'manifest.json',
createShortcuts: false,
trimExtensions: false,
includeMetaData: true,
..._options,
return {
name: 'pixi-manifest',
defaultOptions: {
output: 'manifest.json',
createShortcuts: false,
trimExtensions: false,
includeMetaData: true,
..._options,
},
tags: {
manifest: 'm',
mIgnore: 'mIgnore'
}
};

return {
name: 'pixi-manifest',
defaultOptions,
finish: async (asset: Asset, options, pipeSystem: PipeSystem) =>
},
async finish(asset: Asset, options, pipeSystem: PipeSystem)
{
const newFileName = path.dirname(options.output) === '.'
? path.joinSafe(pipeSystem.outputPath, options.output) : options.output;
Expand All @@ -67,7 +65,15 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
bundles: [defaultBundle]
};

collectAssets(asset, options, pipeSystem.outputPath, pipeSystem.entryPath, manifest.bundles, defaultBundle);
collectAssets(
asset,
options,
pipeSystem.outputPath,
pipeSystem.entryPath,
manifest.bundles,
defaultBundle,
this.tags!
);
filterUniqueNames(manifest);
await fs.writeJSON(newFileName, manifest, { spaces: 2 });
}
Expand Down Expand Up @@ -102,7 +108,8 @@ function collectAssets(
outputPath = '',
entryPath = '',
bundles: PixiBundle[],
bundle: PixiBundle
bundle: PixiBundle,
tags: AssetPipe<null, 'manifest' | 'mIgnore'>['tags']
)
{
if (asset.skip) return;
Expand All @@ -111,7 +118,7 @@ function collectAssets(

let localBundle = bundle;

if (asset.metaData[options.tags!.manifest!])
if (asset.metaData[tags!.manifest!])
{
localBundle = {
name: stripTags(asset.filename),
Expand All @@ -124,9 +131,11 @@ function collectAssets(
const bundleAssets = localBundle.assets;
const finalAssets = asset.getFinalTransformedChildren();

if (asset.transformChildren.length > 0 && !asset.inheritedMetaData[options.tags!.mIgnore!])
if (asset.transformChildren.length > 0 && !asset.inheritedMetaData[tags!.mIgnore!])
{
const nonIgnored = finalAssets.filter((finalAsset) => !finalAsset.inheritedMetaData[options.tags!.mIgnore!]);
const nonIgnored = finalAssets.filter((finalAsset) => !finalAsset.inheritedMetaData[tags!.mIgnore!]);

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

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

Expand All @@ -143,7 +152,7 @@ function collectAssets(

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

// for all assets.. check for atlas and remove them from the bundle..
Expand Down
3 changes: 2 additions & 1 deletion src/pixi/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { texturePackerCacheBuster } from '../texture-packer/texturePackerCacheBu
import { texturePackerCompress } from '../texture-packer/texturePackerCompress.js';
import { webfont } from '../webfont/webfont.js';

import type { AssetPipe } from '../core/index.js';
import type { FfmpegOptions } from '../ffmpeg/ffmpeg.js';
import type { CompressOptions } from '../image/compress.js';
import type { PixiManifestOptions } from '../manifest/pixiManifest.js';
Expand Down Expand Up @@ -80,7 +81,7 @@ export function pixiAssetPackPipes(config: PixiAssetPack)
mipmap({
resolutions: apConfig.resolutions as Record<string, number>,
}),
];
] as AssetPipe[];

if (apConfig.compression !== false)
{
Expand Down
33 changes: 15 additions & 18 deletions src/spine/spineAtlasCompress.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
import { checkExt, createNewAssetAt, swapExt } from '../core/index.js';
import { AtlasView } from './AtlasView.js';

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

export type SpineAtlasCompressOptions = PluginOptions<'nc'> & Omit<CompressOptions, 'jpg'>;
export type SpineAtlasCompressOptions = Omit<CompressOptions, 'jpg'>;

export function spineAtlasCompress(_options?: SpineAtlasCompressOptions): AssetPipe<SpineAtlasCompressOptions>
export function spineAtlasCompress(_options?: SpineAtlasCompressOptions): AssetPipe<SpineAtlasCompressOptions, 'nc'>
{
const defaultOptions = {
...{
png: true,
webp: true,
avif: false,
return {
name: 'spine-atlas-compress',
defaultOptions: {
...{
png: true,
webp: true,
avif: false,
},
..._options,
},
..._options,
tags: {
nc: 'nc',
..._options?.tags
}
};

return {
name: 'spine-atlas-compress',
defaultOptions,
test(asset: Asset, options)
},
test(asset: Asset)
{
return !asset.allMetaData[options.tags.nc]
return !asset.allMetaData[this.tags!.nc]
&& checkExt(asset.path, '.atlas');
},
async transform(asset: Asset, options)
Expand Down
Loading

0 comments on commit aad9bc0

Please sign in to comment.