forked from external-repos/squoosh
Making processor-worker a real worker (to TypeScript) (#351)
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
import { expose } from 'comlink';
|
||||
import { EncodeOptions as MozJPEGEncoderOptions } from './mozjpeg/encoder-meta';
|
||||
import { QuantizeOptions } from './imagequant/processor-meta';
|
||||
import { EncodeOptions as OptiPNGEncoderOptions } from './optipng/encoder-meta';
|
||||
import { EncodeOptions as WebPEncoderOptions } from './webp/encoder-meta';
|
||||
import { EncodeOptions as MozJPEGEncoderOptions } from '../mozjpeg/encoder-meta';
|
||||
import { QuantizeOptions } from '../imagequant/processor-meta';
|
||||
import { EncodeOptions as OptiPNGEncoderOptions } from '../optipng/encoder-meta';
|
||||
import { EncodeOptions as WebPEncoderOptions } from '../webp/encoder-meta';
|
||||
|
||||
async function mozjpegEncode(
|
||||
data: ImageData, options: MozJPEGEncoderOptions,
|
||||
): Promise<ArrayBuffer> {
|
||||
const { encode } = await import(
|
||||
/* webpackChunkName: "process-mozjpeg-enc" */
|
||||
'./mozjpeg/encoder',
|
||||
'../mozjpeg/encoder',
|
||||
);
|
||||
return encode(data, options);
|
||||
}
|
||||
@@ -17,7 +17,7 @@ async function mozjpegEncode(
|
||||
async function quantize(data: ImageData, opts: QuantizeOptions): Promise<ImageData> {
|
||||
const { process } = await import(
|
||||
/* webpackChunkName: "process-imagequant" */
|
||||
'./imagequant/processor',
|
||||
'../imagequant/processor',
|
||||
);
|
||||
return process(data, opts);
|
||||
}
|
||||
@@ -27,7 +27,7 @@ async function optiPngEncode(
|
||||
): Promise<ArrayBuffer> {
|
||||
const { compress } = await import(
|
||||
/* webpackChunkName: "process-optipng" */
|
||||
'./optipng/encoder',
|
||||
'../optipng/encoder',
|
||||
);
|
||||
return compress(data, options);
|
||||
}
|
||||
@@ -37,7 +37,7 @@ async function webpEncode(
|
||||
): Promise<ArrayBuffer> {
|
||||
const { encode } = await import(
|
||||
/* webpackChunkName: "process-webp-enc" */
|
||||
'./webp/encoder',
|
||||
'../webp/encoder',
|
||||
);
|
||||
return encode(data, options);
|
||||
}
|
||||
@@ -45,7 +45,7 @@ async function webpEncode(
|
||||
async function webpDecode(data: ArrayBuffer): Promise<ImageData> {
|
||||
const { decode } = await import(
|
||||
/* webpackChunkName: "process-webp-dec" */
|
||||
'./webp/decoder',
|
||||
'../webp/decoder',
|
||||
);
|
||||
return decode(data);
|
||||
}
|
||||
18
src/codecs/processor-worker/tsconfig.json
Normal file
18
src/codecs/processor-worker/tsconfig.json
Normal file
@@ -0,0 +1,18 @@
|
||||
{
|
||||
"compileOnSave": false,
|
||||
"compilerOptions": {
|
||||
"strict": true,
|
||||
"target": "esnext",
|
||||
"module": "esnext",
|
||||
"lib": [
|
||||
"webworker",
|
||||
"esnext"
|
||||
],
|
||||
"moduleResolution": "node",
|
||||
"experimentalDecorators": true,
|
||||
"noUnusedLocals": true,
|
||||
"sourceMap": true,
|
||||
"allowJs": false,
|
||||
"baseUrl": "."
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import { proxy } from 'comlink';
|
||||
import { QuantizeOptions } from './imagequant/processor-meta';
|
||||
import { ProcessorWorkerApi } from './processor-worker';
|
||||
import { canvasEncode, blobToArrayBuffer } from '../lib/util';
|
||||
import { EncodeOptions as MozJPEGEncoderOptions } from './mozjpeg/encoder-meta';
|
||||
import { EncodeOptions as OptiPNGEncoderOptions } from './optipng/encoder-meta';
|
||||
@@ -18,6 +17,8 @@ import * as browserTIFF from './browser-tiff/encoder';
|
||||
import * as browserJP2 from './browser-jp2/encoder';
|
||||
import * as browserPDF from './browser-pdf/encoder';
|
||||
|
||||
type ProcessorWorkerApi = import('./processor-worker').ProcessorWorkerApi;
|
||||
|
||||
/** How long the worker should be idle before terminating. */
|
||||
const workerTimeout = 1000;
|
||||
|
||||
@@ -62,7 +63,7 @@ export default class Processor {
|
||||
// @ts-ignore - Typescript doesn't know about the 2nd param to new Worker, and the
|
||||
// definition can't be overwritten.
|
||||
this._worker = new Worker(
|
||||
'./processor-worker.ts',
|
||||
'./processor-worker',
|
||||
{ name: 'processor-worker', type: 'module' },
|
||||
) as Worker;
|
||||
// Need to do some TypeScript trickery to make the type match.
|
||||
|
||||
@@ -13,5 +13,8 @@
|
||||
"allowJs": false,
|
||||
"baseUrl": "."
|
||||
},
|
||||
"exclude": ["src/sw/**/*"]
|
||||
"exclude": [
|
||||
"src/sw/**/*",
|
||||
"src/codecs/processor-worker/**/*"
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user