Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Apr 19, 2024
1 parent a7a646e commit 8fa3241
Show file tree
Hide file tree
Showing 51 changed files with 553 additions and 483 deletions.
323 changes: 148 additions & 175 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@
"@rollup/plugin-typescript": "^11.0.0",
"@types/fs-extra": "^11.0.1",
"@types/jest": "^29.4.0",
"chokidar": "^3.5.3",
"eslint": "^8.33.0",
"eventemitter3": "^5.0.1",
"find-up": "^5.0.0",
"fs-extra": "^11.1.0",
"husky": "^8.0.3",
Expand Down
34 changes: 34 additions & 0 deletions packages/compress/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# @assetpack/plugin-compress

AssetPack plugin for compressing images into different formats.

## Installation

```sh
npm install --save-dev @assetpack/plugin-compress
```

## Basic Usage

```js
import { compress } from "@assetpack/plugin-compress";

export default {
...
plugins: {
...
compress: compress(),
},
};
```

## Options

### compressJpg

- `tags` - An object containing the tags to use for the plugin. Defaults to `{ nc: "nc" }`.
- `nc` - The tag used to denote that the image should not be compressed. Can be placed on a folder or file.
- jpg: Any settings supported by [sharp](https://sharp.pixelplumbing.com/api-output#jpeg)
- png: Any settings supported by [sharp](https://sharp.pixelplumbing.com/api-output#png)
- webp: Any settings supported by [sharp](https://sharp.pixelplumbing.com/api-output#webp)
- avif: Any settings supported by [sharp](https://sharp.pixelplumbing.com/api-output#avif)
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@play-co/assetpack-plugin-mipmap-compress",
"name": "@play-co/assetpack-plugin-compress",
"version": "1.1.3",
"description": "",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/mipmap-compress/#readme",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/compress/#readme",
"bugs": "https://github.com/pixijs/assetpack/issues",
"repository": {
"url": "pixijs/assetpack",
"directory": "packages/mipmap-compress"
"directory": "packages/compress"
},
"license": "MIT",
"author": "Zyie",
Expand Down
File renamed without changes.
119 changes: 119 additions & 0 deletions packages/compress/src/compress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import type { PluginOptions, Asset, AssetPipe } from '@play-co/assetpack-core';
import { checkExt, createNewAssetAt } from '@play-co/assetpack-core';
import type { AvifOptions, JpegOptions, PngOptions, WebpOptions } from 'sharp';
import sharp from 'sharp';
import { resolveOptions } from './utils/resolveOptions';
import { compressSharp } from './utils/compressSharp';

type CompressJpgOptions = Omit<JpegOptions, 'force'>;
type CompressWebpOptions = Omit<WebpOptions, 'force'>;
type CompressAvifOptions = Omit<AvifOptions, 'force'>;
type CompressPngOptions = Omit<PngOptions, 'force'>;

export interface CompressOptions extends PluginOptions<'nc'>
{
png?: CompressPngOptions | boolean;
webp?: CompressWebpOptions | boolean;
avif?: CompressAvifOptions | boolean;
jpg?: CompressJpgOptions | boolean;
}

export interface MipmapCompressImageData
{
format: '.avif' | '.png' | '.webp' | '.jpg' | '.jpeg';
resolution: number;
sharpImage: sharp.Sharp;
}

export function compress(options: CompressOptions = {}): AssetPipe<CompressOptions>
{
const compress = resolveOptions<CompressOptions>(options, {
png: true,
jpg: true,
webp: false,
avif: false,
});

if (compress)
{
compress.jpg = resolveOptions<CompressJpgOptions>(compress.jpg, {

});
compress.png = resolveOptions<CompressPngOptions>(compress.png, {
quality: 90,
});
compress.webp = resolveOptions<CompressWebpOptions>(compress.webp, {
quality: 80,
});
compress.avif = resolveOptions<CompressAvifOptions>(compress.avif, {

});
}

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

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

if (!shouldCompress)
{
return [];
}

try
{
const image: MipmapCompressImageData = {
format: asset.extension as MipmapCompressImageData['format'],
resolution: 1,
sharpImage: sharp(asset.buffer),
};

const processedImages = await compressSharp(image, options);

const newAssets = processedImages.map((data) =>
{
const end = `${data.format}`;
const filename = asset.filename
.replace(/\.[^/.]+$/, end);

const newAsset = createNewAssetAt(
asset,
filename
);

return newAsset;
});

const promises = processedImages.map((image, i) => image.sharpImage.toBuffer().then((buffer) =>
{
newAssets[i].buffer = buffer;
}));

await Promise.all(promises);

return newAssets;
}
catch (error)
{
throw new Error(`[AssetPack] Failed to compress image: ${asset.path} - ${error}`);
}
},

};
}

1 change: 1 addition & 0 deletions packages/compress/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './compress';
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AvifOptions, WebpOptions, JpegOptions, PngOptions } from 'sharp';
import type { MipmapCompressImageData, CompressOptions } from '../mipmapCompress';
import type { MipmapCompressImageData, CompressOptions } from '../compress';
import sharp from 'sharp';

export async function compressSharp(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { AssetPack } from '@play-co/assetpack-core';
import { existsSync } from 'fs-extra';
import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test';
import { mipmapCompress } from '../src/mipmapCompress';
import { compress } from '../src/compress';

const pkg = 'mipmap-compress';
const pkg = 'compress';

describe('Compress', () =>
{
Expand Down Expand Up @@ -35,14 +35,11 @@ describe('Compress', () =>
output: outputDir,
cache: false,
pipes: [
mipmapCompress({
mipmap: false,
compress: {
png: true,
webp: true,
avif: true,
jpg: true,
},
compress({
png: true,
webp: true,
avif: true,
jpg: true,
}),
]
});
Expand All @@ -58,7 +55,7 @@ describe('Compress', () =>
expect(existsSync(`${outputDir}/testJpg.png`)).toBe(false);
});

it('should compress png with 1 plugin', async () =>
it.only('should compress png with 1 plugin', async () =>
{
const testName = 'compress-png-1-plugin';
const inputDir = getInputDir(pkg, testName);
Expand All @@ -85,14 +82,11 @@ describe('Compress', () =>
output: outputDir,
cache: false,
pipes: [
mipmapCompress({
mipmap: false,
compress: {
png: true,
webp: true,
jpg: true,
avif: true,
},
compress({
png: true,
webp: true,
jpg: true,
avif: true,
}),
]
});
Expand Down Expand Up @@ -135,11 +129,9 @@ describe('Compress', () =>
output: outputDir,
cache: false,
pipes: [
mipmapCompress({
compress: {
webp: false,
avif: false,
}
compress({
webp: false,
avif: false,
})
]
});
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
4 changes: 4 additions & 0 deletions packages/compress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": "../../tsconfig.json",
"include": ["src/**/*", "types/**/*", "test/**/*"],
}
5 changes: 3 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"merge": "^2.1.1",
"minimatch": "5.1.1",
"object-hash": "3.0.0",
"ora": "^5.4.1",
"cli-progress": "^3.12.0",
"readline": "^1.3.0",
"string-width": "^4.2.3",
"strip-ansi": "^6.0.0",
Expand All @@ -47,7 +47,8 @@
},
"devDependencies": {
"@types/clone": "^2.1.1",
"@types/object-hash": "^3.0.0"
"@types/object-hash": "^3.0.0",
"@types/cli-progress": "3.11.5"
},
"engines": {
"node": ">=16.0.0"
Expand Down
26 changes: 12 additions & 14 deletions packages/core/src/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import objectHash from 'object-hash';
import { Logger } from './logger/Logger';
import { promiseAllConcurrent } from './utils/promiseAllConcurrent';
import { path } from './utils/path';
import merge from 'merge';

export class AssetPack
{
public static defaultConfig: AssetPackConfig = {
private _defaultConfig: AssetPackConfig = {
entry: './static',
output: './dist',
ignore: [],
cache: true,
logLevel: 'info',
pipes: [],
// files: []
};

readonly config: AssetPackConfig;
Expand All @@ -33,13 +33,12 @@ export class AssetPack

constructor(config: AssetPackConfig = {})
{
config = { ...AssetPack.defaultConfig, ...config };
this.config = config;
this._entryPath = normalizePath(config.entry as string);
this._outputPath = normalizePath(config.output as string);
this.config = merge.recursive(true, this._defaultConfig, config);
this._entryPath = normalizePath(this.config.entry as string);
this._outputPath = normalizePath(this.config.output as string);

Logger.init({
level: config.logLevel || 'info'
level: this.config.logLevel || 'info',
});

const { pipes, cache, ...configWithoutPlugins } = config;
Expand Down Expand Up @@ -98,8 +97,8 @@ export class AssetPack
this._assetWatcher = new AssetWatcher({
entryPath: this._entryPath,
assetCacheData,
ignore: config.ignore,
assetSettingsData: config.assetSettings as AssetSettings[] || [],
ignore: this.config.ignore,
assetSettingsData: this.config.assetSettings as AssetSettings[] || [],
onUpdate: async (root: Asset) =>
{
Logger.report({
Expand Down Expand Up @@ -128,11 +127,6 @@ export class AssetPack
}
}
});

Logger.report({
type: 'buildStart',
message: config.entry,
});
}

/**
Expand Down Expand Up @@ -180,6 +174,10 @@ export class AssetPack
phase: 'transform',
message: percent.toString()
});

// wait for 2 seconds before starting the next asset
// this is to prevent the CPU from being overloaded
await new Promise((resolve) => setTimeout(resolve, 4000));
}));

await promiseAllConcurrent(all, 5);
Expand Down
Loading

0 comments on commit 8fa3241

Please sign in to comment.