Skip to content

Commit

Permalink
combine mipmap and compress into image
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie committed Apr 24, 2024
1 parent a5e3365 commit ca29674
Show file tree
Hide file tree
Showing 48 changed files with 102 additions and 223 deletions.
41 changes: 27 additions & 14 deletions package-lock.json

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

34 changes: 0 additions & 34 deletions packages/compress/README.md

This file was deleted.

47 changes: 0 additions & 47 deletions packages/compress/package.json

This file was deleted.

42 changes: 42 additions & 0 deletions packages/image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# @assetpack/plugin-image

AssetPack plugin for compressing and mipmapping images into different formats.

## Installation

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

## Basic Usage

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

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

## Options

### compress

- `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)

### mipmap

- `template`: A template for denoting the resolution of the images. Defaults to `@%%x`. Note you must use `%%` to denote the resolution.
- `resolutions`: An object containing the resolutions that the images will be resized to. Defaults to `{ default: 1, low: 0.5 }`.
- `fixedResolution`: A resolution used if the fix tag is applied e.g. `path/to/image{fix}.png` or `path/to{fix}`. Resolution must match one found in resolutions. Defaults to `default`.
- `tags` - An object containing the tags to use for the plugin. Defaults to `{ fix: "fix" }`.
File renamed without changes.
6 changes: 3 additions & 3 deletions packages/mipmap/package.json → packages/image/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@play-co/assetpack-plugin-mipmap",
"name": "@play-co/assetpack-plugin-image",
"version": "1.2.2",
"description": "",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/mipmap/#readme",
"homepage": "https://github.com/pixijs/assetpack/tree/master/packages/image/#readme",
"bugs": "https://github.com/pixijs/assetpack/issues",
"repository": {
"url": "pixijs/assetpack",
"directory": "packages/mipmap"
"directory": "packages/image"
},
"license": "MIT",
"author": "Zyie",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface CompressOptions extends PluginOptions<'nc'>
jpg?: CompressJpgOptions | boolean;
}

export interface MipmapCompressImageData
export interface CompressImageData
{
format: '.avif' | '.png' | '.webp' | '.jpg' | '.jpeg';
resolution: number;
Expand Down Expand Up @@ -77,8 +77,8 @@ export function compress(options: CompressOptions = {}): AssetPipe<CompressOptio

try
{
const image: MipmapCompressImageData = {
format: asset.extension as MipmapCompressImageData['format'],
const image: CompressImageData = {
format: asset.extension as CompressImageData['format'],
resolution: 1,
sharpImage: sharp(asset.buffer),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './compress';
export * from './mipmap';
14 changes: 4 additions & 10 deletions packages/mipmap/src/mipmap.ts → packages/image/src/mipmap.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Asset, AssetPipe, PluginOptions } from '@play-co/assetpack-core';
import { checkExt, createNewAssetAt } from '@play-co/assetpack-core';
import sharp from 'sharp';
import type { CompressImageData } from './compress';
import { mipmapSharp } from './utils/mipmapSharp';
import { resolveOptions } from './utils/resolveOptions';

Expand All @@ -14,13 +15,6 @@ export interface MipmapOptions<T extends string = ''> extends PluginOptions<'fix
fixedResolution?: string;
}

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

const defaultMipmapOptions: Required<MipmapOptions> = {
template: '@%%x',
resolutions: { default: 1, low: 0.5 },
Expand Down Expand Up @@ -54,10 +48,10 @@ export function mipmap(_options: MipmapOptions = {}): AssetPipe<MipmapOptions>
{
const shouldMipmap = mipmap && !asset.metaData[options.tags.fix as any];

let processedImages: MipmapCompressImageData[];
let processedImages: CompressImageData[];

const image: MipmapCompressImageData = {
format: asset.extension as MipmapCompressImageData['format'],
const image: CompressImageData = {
format: asset.extension as CompressImageData['format'],
resolution: 1,
sharpImage: sharp(asset.buffer),
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type { AvifOptions, WebpOptions, JpegOptions, PngOptions } from 'sharp';
import type { MipmapCompressImageData, CompressOptions } from '../compress';
import type { CompressImageData, CompressOptions } from '../compress';

export async function compressSharp(
image: MipmapCompressImageData,
image: CompressImageData,
options: CompressOptions
): Promise<MipmapCompressImageData[]>
): Promise<CompressImageData[]>
{
const compressed: MipmapCompressImageData[] = [];
const compressed: CompressImageData[] = [];

const sharpImage = image.sharpImage;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import type { MipmapCompressImageData } from '../mipmap';
import type { CompressImageData } from '../compress';

export async function mipmapSharp(
image: MipmapCompressImageData,
image: CompressImageData,
resolutionHash: {[x: string]: number},
largestResolution: number

): Promise<MipmapCompressImageData[]>
): Promise<CompressImageData[]>
{
const sharpImage = image.sharpImage;

const metadata = await sharpImage.metadata();

const { width, height } = metadata;

const output: MipmapCompressImageData[] = [];
const output: CompressImageData[] = [];

if (width && height)
{
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync } from 'fs-extra';
import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test';
import { compress } from '../src/compress';

const pkg = 'compress';
const pkg = 'image';

describe('Compress', () =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { existsSync } from 'fs-extra';
import { assetPath, createFolder, getInputDir, getOutputDir } from '../../../shared/test';
import { mipmap } from '../src/mipmap';

const pkg = 'mipmap';
const pkg = 'image';

describe('Mipmap', () =>
{
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
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes.
3 changes: 1 addition & 2 deletions packages/manifest/test/Manifest.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AssetPack } from '@play-co/assetpack-core';
import { audio } from '@play-co/assetpack-plugin-ffmpeg';
import { compress, mipmap } from '@play-co/assetpack-plugin-image';
import { pixiManifest } from '@play-co/assetpack-plugin-manifest';
import { spineAtlasMipmap } from '@play-co/assetpack-plugin-spine';
import { compress } from '@play-co/assetpack-plugin-compress';
import { texturePacker } from '@play-co/assetpack-plugin-texture-packer';
import { existsSync, readJSONSync } from 'fs-extra';
import type { File } from '../../../shared/test';
Expand All @@ -12,7 +12,6 @@ import {
getInputDir,
getOutputDir,
} from '../../../shared/test';
import { mipmap } from '@play-co/assetpack-plugin-mipmap';

const pkg = 'manifest';

Expand Down
Loading

0 comments on commit ca29674

Please sign in to comment.