forked from pixijs/assetpack
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
51 changed files
with
553 additions
and
483 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
6 changes: 3 additions & 3 deletions
6
packages/mipmap-compress/package.json → packages/compress/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
}, | ||
|
||
}; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './compress'; |
2 changes: 1 addition & 1 deletion
2
...ipmap-compress/src/utils/compressSharp.ts → packages/compress/src/utils/compressSharp.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["src/**/*", "types/**/*", "test/**/*"], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.