Skip to content

Commit

Permalink
Merge branch 'main' into chore/clean
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Apr 23, 2024
2 parents f20f95b + 7e8c7bd commit 30a8669
Show file tree
Hide file tree
Showing 53 changed files with 3,523 additions and 15,603 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
- run: npm run lint
- run: npx nx affected --target=test:types --parallel=3
- uses: FedericoCarboni/setup-ffmpeg@v2
- run: npx nx affected --target=test --parallel=3 --ci
- run: npx nx affected --target=build --parallel=3
- run: npx nx affected --target=test --parallel=3 --ci
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "node_modules/lerna/schemas/lerna-schema.json",
"useWorkspaces": true,
"version": "1.1.3",
"version": "1.2.2",
"command": {
"publish": {
"graphType": "all"
Expand Down
16,550 changes: 1,600 additions & 14,950 deletions package-lock.json

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions packages/cache-buster/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@play-co/assetpack-plugin-cache-buster",
"version": "1.1.3",
"version": "1.2.2",
"description": "",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/cache-buster/#readme",
"bugs": "https://github.com/pixijs/assetpack/issues",
Expand Down Expand Up @@ -29,11 +29,10 @@
"test:types": "tsc --noEmit"
},
"dependencies": {
"@node-rs/crc32": "^1.10.0",
"fs-extra": "^11.1.0"
},
"devDependencies": {
"@play-co/assetpack-core": "1.1.3"
"@play-co/assetpack-core": "1.2.2"
},
"peerDependencies": {
"@play-co/assetpack-core": ">=0.0.0"
Expand Down
19 changes: 2 additions & 17 deletions packages/cache-buster/src/cacheBuster.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import type { AssetPipe, Asset } from '@play-co/assetpack-core';
import { createNewAssetAt, swapExt } from '@play-co/assetpack-core';
import fs from 'fs-extra';

import nodeCrc32 from '@node-rs/crc32';

/**
* Cache buster asset pipe. This pipe will add a hash to the end of the filename
Expand All @@ -28,26 +25,14 @@ export function cacheBuster(): AssetPipe
},
async transform(asset: Asset)
{
const buffer = asset.buffer ?? fs.readFileSync(asset.path);

const hash = crc32(buffer);
const hash = asset.hash;
const newFileName = swapExt(asset.filename, `-${hash}${asset.extension}`);

const newAsset = createNewAssetAt(asset, newFileName);

// by attaching the buffer - we can avoid reading the file again
// and the final copy op will use the buffer, rather than the file path!
newAsset.buffer = buffer;
newAsset.buffer = asset.buffer;

return [newAsset];
}
};
}

/** Calculate a CRC32 checksum. */
export function crc32(input: string | Buffer): string
{
const checksumHex = nodeCrc32.crc32(input).toString(16);

return Buffer.from(checksumHex, 'hex').toString('base64url');
}
14 changes: 7 additions & 7 deletions packages/cache-buster/test/cacheBuster.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AssetPack, path } from '@play-co/assetpack-core';
import { existsSync, readFileSync } from 'fs-extra';
import { Asset, AssetPack, path } from '@play-co/assetpack-core';
import { existsSync } from 'fs-extra';
import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test';
import { cacheBuster, crc32 } from '../src';
import { cacheBuster } from '../src';

const pkg = 'cache-buster';

Expand Down Expand Up @@ -40,10 +40,10 @@ describe('CacheBuster', () =>

const originalPath = path.joinSafe('.testInput', testName, 'ttf.ttf');

const buffer = readFileSync(originalPath);

const hash = crc32(buffer);
const asset = new Asset({
path: originalPath,
});

expect(existsSync(path.joinSafe('.testOutput', testName, `ttf-${hash}.ttf`))).toBe(true);
expect(existsSync(path.joinSafe('.testOutput', testName, `ttf-${asset.hash}.ttf`))).toBe(true);
});
});
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@play-co/assetpack-cli",
"version": "1.1.3",
"version": "1.2.2",
"description": "A simple CLI to run AssetPack",
"keywords": [
"assetpack",
Expand Down
9 changes: 2 additions & 7 deletions packages/compress/src/utils/compressSharp.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { AvifOptions, WebpOptions, JpegOptions, PngOptions } from 'sharp';
import type { MipmapCompressImageData, CompressOptions } from '../compress';
import sharp from 'sharp';

export async function compressSharp(
image: MipmapCompressImageData,
Expand All @@ -9,18 +8,14 @@ export async function compressSharp(
{
const compressed: MipmapCompressImageData[] = [];

let sharpImage = image.sharpImage;
const sharpImage = image.sharpImage;

if (image.format === '.png' && options.png)
{
// 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, force: true }).toBuffer());

compressed.push({
format: '.png',
resolution: image.resolution,
sharpImage: sharpImage.clone(),
sharpImage: sharpImage.clone().png({ ...options.png as PngOptions, force: true }),
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@play-co/assetpack-core",
"version": "1.1.3",
"version": "1.2.2",
"description": "",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/core/#readme",
"bugs": "https://github.com/pixijs/assetpack/issues",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ export class Asset
Logger.warn('[Assetpack] folders should not have hashes. Contact the developer of the Assetpack');
}

this._hash ??= getHash(this.buffer ?? this.path);
this._hash ??= getHash(this.buffer);

return this._hash;
}
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ export * from './utils/merge';
export * from './utils/path';
export * from './utils/stripTags';
export * from './utils/swapExt';
export * from './utils/findAssetsWithFileName';
30 changes: 30 additions & 0 deletions packages/core/src/utils/findAssetsWithFileName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Asset } from '../Asset';

export function findAssetsWithFileName(
test: (asset: Asset) => boolean,
asset: Asset,
searchTransform: boolean,
out: Asset[] = []
): Asset[]
{
if (test(asset))
{
out.push(asset);
}

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

findAssetsWithFileName(test, child, searchTransform, out);
}

for (let i = 0; i < asset.transformChildren.length; i++)
{
const transformChild = asset.transformChildren[i];

findAssetsWithFileName(test, transformChild, searchTransform, out);
}

return out;
}
6 changes: 3 additions & 3 deletions packages/ffmpeg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@play-co/assetpack-plugin-ffmpeg",
"version": "1.1.3",
"version": "1.2.2",
"description": "",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/ffmpeg/#readme",
"bugs": "https://github.com/pixijs/assetpack/issues",
Expand Down Expand Up @@ -35,8 +35,8 @@
"fs-extra": "^11.1.0"
},
"devDependencies": {
"@play-co/assetpack-core": "1.1.3",
"@play-co/assetpack-plugin-manifest": "1.1.3"
"@play-co/assetpack-core": "1.2.2",
"@play-co/assetpack-plugin-manifest": "1.2.2"
},
"peerDependencies": {
"@play-co/assetpack-core": ">=0.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/json/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@play-co/assetpack-plugin-json",
"version": "1.1.3",
"version": "1.2.2",
"description": "",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/json/#readme",
"bugs": "https://github.com/pixijs/assetpack/issues",
Expand Down Expand Up @@ -32,7 +32,7 @@
"fs-extra": "^11.1.0"
},
"devDependencies": {
"@play-co/assetpack-core": "1.1.3"
"@play-co/assetpack-core": "1.2.2"
},
"peerDependencies": {
"@play-co/assetpack-core": ">=0.0.0"
Expand Down
4 changes: 2 additions & 2 deletions packages/manifest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@play-co/assetpack-plugin-manifest",
"version": "1.1.3",
"version": "1.2.2",
"description": "",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/manifest/#readme",
"bugs": "https://github.com/pixijs/assetpack/issues",
Expand Down Expand Up @@ -33,7 +33,7 @@
"upath": "^2.0.1"
},
"devDependencies": {
"@play-co/assetpack-core": "1.1.3"
"@play-co/assetpack-core": "1.2.2"
},
"peerDependencies": {
"@play-co/assetpack-core": ">=0.0.0"
Expand Down
79 changes: 76 additions & 3 deletions packages/manifest/src/pixiManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
type Asset,
type AssetPipe,
type PipeSystem,
path
path,
findAssetsWithFileName
} from '@play-co/assetpack-core';

import fs from 'fs-extra';
Expand All @@ -29,6 +30,37 @@ export interface PixiManifestOptions
output?: string;
createShortcuts?: boolean;
trimExtensions?: boolean;
includeMetaData?: boolean;
}

// TODO EXPORT this out! But don't want to create a dependency on the atlas plugin just yet..
export class AtlasView
{
public rawAtlas: string;

constructor(buffer: Buffer)
{
this.rawAtlas = buffer.toString();
}

getTextures(): string[]
{
const regex = /^.+?(?:\.png|\.jpg|\.jpeg|\.webp|\.avif)$/gm;

const matches = this.rawAtlas.match(regex);

return matches as string[];
}

replaceTexture(filename: string, newFilename: string)
{
this.rawAtlas = this.rawAtlas.replace(filename, newFilename);
}

get buffer()
{
return Buffer.from(this.rawAtlas);
}
}

export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<PixiManifestOptions>
Expand All @@ -37,6 +69,7 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
output: 'manifest.json',
createShortcuts: false,
trimExtensions: false,
includeMetaData: true,
..._options
};

Expand All @@ -45,6 +78,8 @@ export function pixiManifest(_options: PixiManifestOptions = {}): AssetPipe<Pixi
defaultOptions,
finish: async (asset: Asset, options, pipeSystem: PipeSystem) =>
{
removeAtlasTextures(asset);

const newFileName = path.dirname(options.output) === '.'
? path.joinSafe(pipeSystem.outputPath, options.output) : options.output;

Expand Down Expand Up @@ -100,15 +135,25 @@ function collectAssets(
{
bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, `${asset.path}-${pageIndex}`)), options),
src: pages.map((finalAsset) => path.relative(outputPath, finalAsset.path))
src: pages
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a)),
data: options.includeMetaData ? {
tags: asset.allMetaData
} : undefined
});
});
}
else
{
bundleAssets.push({
alias: getShortNames(stripTags(path.relative(entryPath, asset.path)), options),
src: finalAssets.map((finalAsset) => path.relative(outputPath, finalAsset.path))
src: finalAssets
.map((finalAsset) => path.relative(outputPath, finalAsset.path))
.sort((a, b) => b.localeCompare(a)),
data: options.includeMetaData ? {
tags: asset.allMetaData
} : undefined
});
}
}
Expand All @@ -117,6 +162,34 @@ function collectAssets(
{
collectAssets(child, options, outputPath, entryPath, bundles, localBundle);
});

// for all assets.. check for atlas and remove them from the bundle..
}

function removeAtlasTextures(asset: Asset)
{
// do a pass to remove what we don't want..

const atlasAssets = findAssetsWithFileName((asset) =>
asset.extension === '.atlas' && asset.transformChildren.length === 0, asset, true);

atlasAssets.forEach((atlasAsset) =>
{
const view = new AtlasView(atlasAsset.buffer);

const textureNames = view.getTextures();

textureNames.forEach((texture) =>
{
const textureAssets = findAssetsWithFileName((asset) =>
asset.filename === texture, asset, true);

textureAssets.forEach((textureAsset) =>
{
textureAsset.skip = true;
});
});
});
}

function getTexturePackedAssets(assets: Asset[])
Expand Down
Loading

0 comments on commit 30a8669

Please sign in to comment.