mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-11 16:26:20 +00:00
Expose Typescript types in libSquoosh (#1142)
* Expose type declarations in libSquoosh npm package * Add comment on why we remove the tsbuildinfo * Fix PreprocessOptions type Resize should require at least one of the width, height. The other options are optional for all preprocessors * Update libSquoosh README to reflect encode changes I also removed requiring `await image.decoded` call before calling preprocess or encode since they decode the image before the operation
This commit is contained in:
@@ -43,19 +43,15 @@ The returned `image` object is a representation of the original image, that you
|
|||||||
When an image has been ingested, you can start preprocessing it and encoding it to other formats. This example will resize the image and then encode it to a `.jpg` and `.jxl` image:
|
When an image has been ingested, you can start preprocessing it and encoding it to other formats. This example will resize the image and then encode it to a `.jpg` and `.jxl` image:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
await image.decoded; //Wait until the image is decoded before running preprocessors.
|
|
||||||
|
|
||||||
const preprocessOptions = {
|
const preprocessOptions = {
|
||||||
//When both width and height are specified, the image resized to specified size.
|
//When both width and height are specified, the image resized to specified size.
|
||||||
resize: {
|
resize: {
|
||||||
enabled: true,
|
|
||||||
width: 100,
|
width: 100,
|
||||||
height: 50,
|
height: 50,
|
||||||
},
|
},
|
||||||
/*
|
/*
|
||||||
//When either width or height is specified, the image resized to specified size keeping aspect ratio.
|
//When either width or height is specified, the image resized to specified size keeping aspect ratio.
|
||||||
resize: {
|
resize: {
|
||||||
enabled: true,
|
|
||||||
width: 100,
|
width: 100,
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
@@ -68,7 +64,7 @@ const encodeOptions = {
|
|||||||
quality: 90,
|
quality: 90,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
await image.encode(encodeOptions);
|
const result = await image.encode(encodeOptions);
|
||||||
```
|
```
|
||||||
|
|
||||||
The default values for each option can be found in the [`codecs.ts`][codecs.ts] file under `defaultEncoderOptions`. Every unspecified value will use the default value specified there. _Better documentation is needed here._
|
The default values for each option can be found in the [`codecs.ts`][codecs.ts] file under `defaultEncoderOptions`. Every unspecified value will use the default value specified there. _Better documentation is needed here._
|
||||||
@@ -92,7 +88,7 @@ When you have encoded an image, you normally want to write it to a file.
|
|||||||
This example takes an image that has been encoded as a `jpg` and writes it to a file:
|
This example takes an image that has been encoded as a `jpg` and writes it to a file:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const rawEncodedImage = (await image.encodedWith.mozjpeg).binary;
|
const rawEncodedImage = image.encodedWith.mozjpeg.binary;
|
||||||
|
|
||||||
fs.writeFile('/path/to/new/image.jpg', rawEncodedImage);
|
fs.writeFile('/path/to/new/image.jpg', rawEncodedImage);
|
||||||
```
|
```
|
||||||
@@ -103,10 +99,7 @@ This example iterates through all encoded versions of the image and writes them
|
|||||||
const newImagePath = '/path/to/image.'; //extension is added automatically
|
const newImagePath = '/path/to/image.'; //extension is added automatically
|
||||||
|
|
||||||
for (const encodedImage of Object.values(image.encodedWith)) {
|
for (const encodedImage of Object.values(image.encodedWith)) {
|
||||||
fs.writeFile(
|
fs.writeFile(newImagePath + encodedImage.extension, encodedImage.binary);
|
||||||
newImagePath + (await encodedImage).extension,
|
|
||||||
(await encodedImage).binary,
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -135,7 +128,7 @@ console.log(await image.decoded);
|
|||||||
Information about an encoded image can be found at `Image.encodedWith[encoderName]`. It looks something like this:
|
Information about an encoded image can be found at `Image.encodedWith[encoderName]`. It looks something like this:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
console.log(await image.encodedWith.jxl);
|
console.log(image.encodedWith.jxl);
|
||||||
// Returns:
|
// Returns:
|
||||||
{
|
{
|
||||||
optionsUsed: {
|
optionsUsed: {
|
||||||
|
|||||||
26
libsquoosh/lib/move-d-ts.js
Normal file
26
libsquoosh/lib/move-d-ts.js
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const del = require('del');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const tsOutputDir = path.resolve('..', '.tmp', 'ts', 'libsquoosh');
|
||||||
|
const tsOutputSourceDir = path.join(tsOutputDir, 'src');
|
||||||
|
const buildDir = path.resolve('build');
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
await del(path.join(buildDir, '*.d.ts'));
|
||||||
|
const files = await fs.promises.readdir(tsOutputSourceDir);
|
||||||
|
|
||||||
|
const movePromises = [];
|
||||||
|
for (const file of files) {
|
||||||
|
if (file.endsWith('d.ts') || file.endsWith('d.ts.map')) {
|
||||||
|
movePromises.push(
|
||||||
|
fs.promises.rename(
|
||||||
|
path.join(tsOutputSourceDir, file),
|
||||||
|
path.join(buildDir, file),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// We need to remove `tsconfig.tsbuildinfo` otherwise TS does not emit unchanged `.d.ts` files
|
||||||
|
await del([path.join(tsOutputDir, 'tsconfig.tsbuildinfo')], { force: true });
|
||||||
|
})();
|
||||||
@@ -4,11 +4,12 @@
|
|||||||
"description": "A Node library for Squoosh",
|
"description": "A Node library for Squoosh",
|
||||||
"public": true,
|
"public": true,
|
||||||
"main": "./build/index.js",
|
"main": "./build/index.js",
|
||||||
|
"types": "./build/index.d.ts",
|
||||||
"files": [
|
"files": [
|
||||||
"/build/*"
|
"/build/*"
|
||||||
],
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"build": "rollup -c"
|
"build": "rollup -c && node lib/move-d-ts.js"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "Google Chrome Developers <chromium-dev@google.com>",
|
"author": "Google Chrome Developers <chromium-dev@google.com>",
|
||||||
|
|||||||
@@ -22,9 +22,10 @@ type EncoderKey = keyof typeof encoders;
|
|||||||
type PreprocessorKey = keyof typeof preprocessors;
|
type PreprocessorKey = keyof typeof preprocessors;
|
||||||
|
|
||||||
type PreprocessOptions = {
|
type PreprocessOptions = {
|
||||||
resize?: ResizeOptions;
|
resize?: Partial<Omit<ResizeOptions, 'width' | 'height'>> &
|
||||||
quant?: QuantOptions;
|
(Pick<ResizeOptions, 'width'> | Pick<ResizeOptions, 'height'>);
|
||||||
rotate?: RotateOptions;
|
quant?: Partial<QuantOptions>;
|
||||||
|
rotate?: Partial<RotateOptions>;
|
||||||
};
|
};
|
||||||
type EncodeResult = {
|
type EncodeResult = {
|
||||||
optionsUsed: object;
|
optionsUsed: object;
|
||||||
|
|||||||
@@ -3,7 +3,8 @@
|
|||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"lib": ["esnext"],
|
"lib": ["esnext"],
|
||||||
"types": ["node"],
|
"types": ["node"],
|
||||||
"allowJs": true
|
"allowJs": true,
|
||||||
|
"declaration": true
|
||||||
},
|
},
|
||||||
"include": ["src/**/*", "../codecs/**/*"]
|
"include": ["src/**/*", "../codecs/**/*"]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user