Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(core): misc fixes #8

Merged
merged 4 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/core/src/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class Asset
// file based..
parent: Asset | null = null;
children: Asset[] = [];
ignoreChildren = false;

// transform based..
transformParent: Asset | null = null;
Expand All @@ -37,6 +36,7 @@ export class Asset

isFolder: boolean;
path = '';
skip = false;

private _state: 'deleted' | 'added' | 'modified' | 'normal' = 'added';
private _buffer?: Buffer | null = null;
Expand Down Expand Up @@ -178,6 +178,17 @@ export class Asset
return asset;
}

skipChildren()
{
for (let i = 0; i < this.children.length; i++)
{
const child = this.children[i];

child.skip = true;
child.skipChildren();
}
}

getFinalTransformedChildren(asset: Asset = this, finalChildren: Asset[] = []): Asset[]
{
if (asset.transformChildren.length > 0)
Expand Down
16 changes: 11 additions & 5 deletions packages/core/src/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ export class AssetPack
{
Logger.info('cache found.');
}
else
{
Logger.warn('cache not found, clearing output folder');

// to be safe - lets nuke the folder as the cache is empty
fs.removeSync(this._outputPath);
Zyie marked this conversation as resolved.
Show resolved Hide resolved
}
}

// make sure the output folders exists
Expand Down Expand Up @@ -170,6 +177,8 @@ export class AssetPack
const all = assetsToTransform.map((asset) =>
(async () =>
{
if (asset.skip) return;

await this._pipeSystem.transform(asset);
index++;

Expand Down Expand Up @@ -200,12 +209,9 @@ export class AssetPack
output.push(asset);
}

if (!asset.ignoreChildren)
for (let i = 0; i < asset.children.length; i++)
{
for (let i = 0; i < asset.children.length; i++)
{
this.deleteAndCollectAssetsToTransform(asset.children[i], output);
}
this.deleteAndCollectAssetsToTransform(asset.children[i], output);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/json/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export function json(_options: JsonOptions = {}): AssetPipe
const json = JSON.parse(asset.buffer.toString());
const compressedJsonAsset = createNewAssetAt(asset, asset.filename);

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

return [compressedJsonAsset];
}
Expand Down
2 changes: 1 addition & 1 deletion packages/json/test/Json.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,6 @@ describe('Json', () =>

const data = readFileSync(`${outputDir}/json/json.json`, 'utf8');

expect(data.replace(/\\/g, '').trim()).toEqual(`"{"hello":"world","Im":"not broken"}"`);
expect(data.replace(/\\/g, '').trim()).toEqual(`{"hello":"world","Im":"not broken"}`);
});
});
11 changes: 5 additions & 6 deletions packages/manifest/src/pixiManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ function collectAssets(
bundle: PixiManifest,
)
{
if (asset.skip) return;

let localBundle = bundle;

if (asset.metaData.m || asset.metaData.manifest)
Expand Down Expand Up @@ -111,13 +113,10 @@ function collectAssets(
}
}

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

function getTexturePackedAssets(assets: Asset[])
Expand Down
4 changes: 2 additions & 2 deletions packages/mipmap-compress/src/utils/compressSharp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ export async function compressSharp(
{
// optimising the PNG image and using that as the source of the WebP and AVIF images
// will result in a smaller file size and increase the speed of the compression.
sharpImage = sharp(await image.sharpImage.png(options.png as PngOptions).toBuffer());
sharpImage = sharp(await image.sharpImage.png({ ...options.png as PngOptions, force: true }).toBuffer());

compressed.push({
format: '.png',
resolution: image.resolution,
sharpImage
sharpImage: sharpImage.clone(),
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/texture-packer/src/packer/createJsons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function createJsons(
w: rect.width,
h: rect.height
},
rotated: false,
rotated: rect.rot,
trimmed: rect.textureData.trimmed,
spriteSourceSize: {
x: rect.textureData.trimOffsetLeft,
Expand Down
3 changes: 2 additions & 1 deletion packages/texture-packer/src/texturePacker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ export function texturePacker(_options: TexturePackerOptions = {}): AssetPipe<Te
// eslint-disable-next-line max-len
fixedResolutions[resolutionOptions.fixedResolution as any] = resolutionOptions.resolutions[resolutionOptions.fixedResolution];

asset.ignoreChildren = true;
// skip the children so that they do not get processed!
asset.skipChildren();

const largestResolution = Math.max(...Object.values(resolutionOptions.resolutions));
const resolutionHash = asset.allMetaData[tags.fix as any] ? fixedResolutions : resolutionOptions.resolutions;
Expand Down
6 changes: 4 additions & 2 deletions packages/texture-packer/test/texturePacker.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AssetPack } from '@play-co/assetpack-core';
import { AssetPack, Logger } from '@play-co/assetpack-core';
import { existsSync, readJSONSync } from 'fs-extra';
import sharp from 'sharp';
import type { File } from '../../../shared/test/index';
Expand Down Expand Up @@ -70,6 +70,8 @@ describe('Texture Packer', () =>
const sheet2Exists = existsSync(`${outputDir}/sprites-1.json`);

expect(sheet2Exists).toBe(false);

expect(existsSync(`${outputDir}/sprites`)).toBe(false);
});

it('should adjust the size of the textures outputted based on maximumTextureSize', async () =>
Expand Down Expand Up @@ -567,7 +569,7 @@ describe('Texture Packer', () =>
});

// Mock console.warn
const mockWarn = jest.spyOn(console, 'warn').mockImplementation();
const mockWarn = jest.spyOn(Logger, 'warn').mockImplementation();

await assetpack.run();

Expand Down
Loading