diff --git a/.gitignore b/.gitignore index 19d6cfbc..63646f64 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ build *.o # Auto-generated by lib/image-worker-plugin.js -src/image-worker/index.ts +src/features/worker/index.ts +src/features/worker/tsconfig.json diff --git a/generic-tsconfig.json b/generic-tsconfig.json index 9731d183..9b13e884 100644 --- a/generic-tsconfig.json +++ b/generic-tsconfig.json @@ -15,8 +15,7 @@ "allowSyntheticDefaultImports": true, "paths": { "static-build/*": ["src/static-build/*"], - "image-worker/*": ["src/image-worker/*"], - "worker-main-shared/*": ["src/worker-main-shared/*"] + "features/*": ["src/features/*"] } } } diff --git a/lib/image-worker-plugin.js b/lib/image-worker-plugin.js index b6499392..40d7ed74 100644 --- a/lib/image-worker-plugin.js +++ b/lib/image-worker-plugin.js @@ -22,24 +22,31 @@ export default function () { return { name: 'image-worker-plugin', async buildStart() { - const base = path.join(process.cwd(), 'src', 'image-worker'); - const dirs = ( - await globP('*/', { + const base = path.join(process.cwd(), 'src', 'features', 'worker'); + const tsImports = ( + await globP('../*/**/worker/*.ts', { cwd: base, }) - ).map((dir) => dir.slice(0, -1)); + ) + .filter((tsFile) => !tsFile.endsWith('.d.ts')) + .map((tsFile) => tsFile.slice(0, -'.ts'.length)); - const file = [ + const tsNames = tsImports.map((tsImport) => [ + tsImport, + path.basename(tsImport), + ]); + + const workerFile = [ `// This file is autogenerated by lib/image-worker-plugin.js`, `import { expose } from 'comlink';`, `import { timed } from './util';`, - dirs.map((dir) => `import ${dir} from './${dir}';`), + tsNames.map(([path, name]) => `import ${name} from './${path}';`), `const exports = {`, - dirs.map((dir) => [ - ` ${dir}(`, - ` ...args: Parameters`, - ` ): ReturnType {`, - ` return timed('${dir}', () => ${dir}(...args));`, + tsNames.map(([_, name]) => [ + ` ${name}(`, + ` ...args: Parameters`, + ` ): ReturnType {`, + ` return timed('${name}', () => ${name}(...args));`, ` },`, ]), `};`, @@ -49,7 +56,23 @@ export default function () { .flat(Infinity) .join('\n'); - await fsp.writeFile(path.join(base, 'index.ts'), file); + const tsConfig = { + extends: '../../../generic-tsconfig.json', + compilerOptions: { + lib: ['webworker', 'esnext'], + }, + references: tsImports.map((tsImport) => ({ + path: path.dirname(tsImport), + })), + }; + + await Promise.all([ + fsp.writeFile( + path.join(base, 'tsconfig.json'), + JSON.stringify(tsConfig, null, ' '), + ), + fsp.writeFile(path.join(base, 'index.ts'), workerFile), + ]); }, }; } diff --git a/package.json b/package.json index c4d09428..335e6012 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,7 @@ "license": "apache-2.0", "scripts": { "build": "rollup -c && node lib/move-output.js", + "debug": "node --inspect-brk node_modules/.bin/rollup -c", "dev": "rollup -cw & npm run serve", "serve": "serve --config server.json .tmp/build/static" }, diff --git a/rollup.config.js b/rollup.config.js index e1c61146..3b916e39 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -54,8 +54,7 @@ export default async function ({ watch }) { resolveDirsPlugin([ 'src/static-build', 'src/client', - 'src/image-worker', - 'src/worker-main-shared', + 'src/features', 'codecs', ]), assetPlugin(), diff --git a/src/client/index.tsx b/src/client/index.tsx index 5f5b9068..f6ddfc3a 100644 --- a/src/client/index.tsx +++ b/src/client/index.tsx @@ -11,10 +11,10 @@ * limitations under the License. */ import { wrap } from 'comlink'; -import workerURL from 'omt:image-worker'; +import workerURL from 'omt:features/worker'; import imgURL from 'url:./tmp.png'; -import type { ProcessorWorkerApi } from 'image-worker/index'; +import type { ProcessorWorkerApi } from 'features/worker'; const worker = new Worker(workerURL); const api = wrap(worker); diff --git a/src/client/tsconfig.json b/src/client/tsconfig.json index 3ef4d4a0..4a164d4a 100644 --- a/src/client/tsconfig.json +++ b/src/client/tsconfig.json @@ -4,5 +4,5 @@ "lib": ["esnext", "dom", "dom.iterable"], "types": [] }, - "references": [{ "path": "../image-worker" }] + "references": [{ "path": "../features/worker" }] } diff --git a/src/image-worker/avifDecode/index.ts b/src/features/decoders/avif/worker/avifDecode.ts similarity index 95% rename from src/image-worker/avifDecode/index.ts rename to src/features/decoders/avif/worker/avifDecode.ts index cdf04b11..68809b2b 100644 --- a/src/image-worker/avifDecode/index.ts +++ b/src/features/decoders/avif/worker/avifDecode.ts @@ -12,7 +12,7 @@ */ import avifDecoder, { AVIFModule } from 'codecs/avif/dec/avif_dec'; import wasmUrl from 'url:codecs/avif/dec/avif_dec.wasm'; -import { initEmscriptenModule } from '../util'; +import { initEmscriptenModule } from 'features/util'; let emscriptenModule: Promise; diff --git a/src/features/decoders/avif/worker/missing-types.d.ts b/src/features/decoders/avif/worker/missing-types.d.ts new file mode 100644 index 00000000..c729fd74 --- /dev/null +++ b/src/features/decoders/avif/worker/missing-types.d.ts @@ -0,0 +1,13 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/// diff --git a/src/features/decoders/avif/worker/tsconfig.json b/src/features/decoders/avif/worker/tsconfig.json new file mode 100644 index 00000000..bea39d16 --- /dev/null +++ b/src/features/decoders/avif/worker/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../../../generic-tsconfig.json", + "compilerOptions": { + "lib": ["webworker", "esnext"] + }, + "references": [{ "path": "../../../" }] +} diff --git a/src/features/decoders/webp/worker/missing-types.d.ts b/src/features/decoders/webp/worker/missing-types.d.ts new file mode 100644 index 00000000..c729fd74 --- /dev/null +++ b/src/features/decoders/webp/worker/missing-types.d.ts @@ -0,0 +1,13 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/// diff --git a/src/features/decoders/webp/worker/tsconfig.json b/src/features/decoders/webp/worker/tsconfig.json new file mode 100644 index 00000000..bea39d16 --- /dev/null +++ b/src/features/decoders/webp/worker/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../../../generic-tsconfig.json", + "compilerOptions": { + "lib": ["webworker", "esnext"] + }, + "references": [{ "path": "../../../" }] +} diff --git a/src/image-worker/webpDecode/index.ts b/src/features/decoders/webp/worker/webpDecode.ts similarity index 95% rename from src/image-worker/webpDecode/index.ts rename to src/features/decoders/webp/worker/webpDecode.ts index c31e2ad2..8fa97993 100644 --- a/src/image-worker/webpDecode/index.ts +++ b/src/features/decoders/webp/worker/webpDecode.ts @@ -12,7 +12,7 @@ */ import webpDecoder, { WebPModule } from 'codecs/webp/dec/webp_dec'; import wasmUrl from 'url:codecs/webp/dec/webp_dec.wasm'; -import { initEmscriptenModule } from '../util'; +import { initEmscriptenModule } from 'features/util'; let emscriptenModule: Promise; diff --git a/src/image-worker/avifEncode/index.ts b/src/features/encoders/avif/worker/avifEncode.ts similarity index 95% rename from src/image-worker/avifEncode/index.ts rename to src/features/encoders/avif/worker/avifEncode.ts index 143bbb94..31e75b0c 100644 --- a/src/image-worker/avifEncode/index.ts +++ b/src/features/encoders/avif/worker/avifEncode.ts @@ -15,7 +15,7 @@ import avifEncoder, { EncodeOptions, } from 'codecs/avif/enc/avif_enc'; import wasmUrl from 'url:codecs/avif/enc/avif_enc.wasm'; -import { initEmscriptenModule } from '../util'; +import { initEmscriptenModule } from 'features/util'; let emscriptenModule: Promise; diff --git a/src/features/encoders/avif/worker/missing-types.d.ts b/src/features/encoders/avif/worker/missing-types.d.ts new file mode 100644 index 00000000..c729fd74 --- /dev/null +++ b/src/features/encoders/avif/worker/missing-types.d.ts @@ -0,0 +1,13 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/// diff --git a/src/features/encoders/avif/worker/tsconfig.json b/src/features/encoders/avif/worker/tsconfig.json new file mode 100644 index 00000000..bea39d16 --- /dev/null +++ b/src/features/encoders/avif/worker/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../../../generic-tsconfig.json", + "compilerOptions": { + "lib": ["webworker", "esnext"] + }, + "references": [{ "path": "../../../" }] +} diff --git a/src/features/encoders/mozjpeg/worker/missing-types.d.ts b/src/features/encoders/mozjpeg/worker/missing-types.d.ts new file mode 100644 index 00000000..c729fd74 --- /dev/null +++ b/src/features/encoders/mozjpeg/worker/missing-types.d.ts @@ -0,0 +1,13 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/// diff --git a/src/image-worker/mozjpegEncode/index.ts b/src/features/encoders/mozjpeg/worker/mozjpegEncode.ts similarity index 95% rename from src/image-worker/mozjpegEncode/index.ts rename to src/features/encoders/mozjpeg/worker/mozjpegEncode.ts index cd593de6..fb6ede6f 100644 --- a/src/image-worker/mozjpegEncode/index.ts +++ b/src/features/encoders/mozjpeg/worker/mozjpegEncode.ts @@ -15,7 +15,7 @@ import mozjpeg_enc, { EncodeOptions, } from 'codecs/mozjpeg_enc/mozjpeg_enc'; import wasmUrl from 'url:codecs/mozjpeg_enc/mozjpeg_enc.wasm'; -import { initEmscriptenModule } from '../util'; +import { initEmscriptenModule } from 'features/util'; let emscriptenModule: Promise; diff --git a/src/features/encoders/mozjpeg/worker/tsconfig.json b/src/features/encoders/mozjpeg/worker/tsconfig.json new file mode 100644 index 00000000..bea39d16 --- /dev/null +++ b/src/features/encoders/mozjpeg/worker/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../../../generic-tsconfig.json", + "compilerOptions": { + "lib": ["webworker", "esnext"] + }, + "references": [{ "path": "../../../" }] +} diff --git a/src/features/encoders/webp/worker/missing-types.d.ts b/src/features/encoders/webp/worker/missing-types.d.ts new file mode 100644 index 00000000..c729fd74 --- /dev/null +++ b/src/features/encoders/webp/worker/missing-types.d.ts @@ -0,0 +1,13 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/// diff --git a/src/features/encoders/webp/worker/tsconfig.json b/src/features/encoders/webp/worker/tsconfig.json new file mode 100644 index 00000000..bea39d16 --- /dev/null +++ b/src/features/encoders/webp/worker/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../../../generic-tsconfig.json", + "compilerOptions": { + "lib": ["webworker", "esnext"] + }, + "references": [{ "path": "../../../" }] +} diff --git a/src/image-worker/webpEncode/index.ts b/src/features/encoders/webp/worker/webpEncode.ts similarity index 95% rename from src/image-worker/webpEncode/index.ts rename to src/features/encoders/webp/worker/webpEncode.ts index 1fe35e87..a2dfb272 100644 --- a/src/image-worker/webpEncode/index.ts +++ b/src/features/encoders/webp/worker/webpEncode.ts @@ -15,7 +15,7 @@ import webpEncoder, { EncodeOptions, } from 'codecs/webp/enc/webp_enc'; import wasmUrl from 'url:codecs/webp/enc/webp_enc.wasm'; -import { initEmscriptenModule } from '../util'; +import { initEmscriptenModule } from 'features/util'; let emscriptenModule: Promise; diff --git a/src/image-worker/missing-types.d.ts b/src/features/missing-types.d.ts similarity index 100% rename from src/image-worker/missing-types.d.ts rename to src/features/missing-types.d.ts diff --git a/src/features/processors/quantize/worker/missing-types.d.ts b/src/features/processors/quantize/worker/missing-types.d.ts new file mode 100644 index 00000000..c729fd74 --- /dev/null +++ b/src/features/processors/quantize/worker/missing-types.d.ts @@ -0,0 +1,13 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/// diff --git a/src/image-worker/quantize/index.ts b/src/features/processors/quantize/worker/quantize.ts similarity index 96% rename from src/image-worker/quantize/index.ts rename to src/features/processors/quantize/worker/quantize.ts index 6782aa3e..41377a59 100644 --- a/src/image-worker/quantize/index.ts +++ b/src/features/processors/quantize/worker/quantize.ts @@ -12,7 +12,7 @@ */ import imagequant, { QuantizerModule } from 'codecs/imagequant/imagequant'; import wasmUrl from 'url:codecs/imagequant/imagequant.wasm'; -import { initEmscriptenModule } from '../util'; +import { initEmscriptenModule } from 'features/util'; export interface QuantizeOptions { zx: number; diff --git a/src/features/processors/quantize/worker/tsconfig.json b/src/features/processors/quantize/worker/tsconfig.json new file mode 100644 index 00000000..bea39d16 --- /dev/null +++ b/src/features/processors/quantize/worker/tsconfig.json @@ -0,0 +1,7 @@ +{ + "extends": "../../../../../generic-tsconfig.json", + "compilerOptions": { + "lib": ["webworker", "esnext"] + }, + "references": [{ "path": "../../../" }] +} diff --git a/src/image-worker/tsconfig.json b/src/features/tsconfig.json similarity index 78% rename from src/image-worker/tsconfig.json rename to src/features/tsconfig.json index efb17003..790bfa66 100644 --- a/src/image-worker/tsconfig.json +++ b/src/features/tsconfig.json @@ -3,5 +3,6 @@ "compilerOptions": { "lib": ["webworker", "esnext"] }, + "include": ["util.ts", "*.d.ts"], "references": [] } diff --git a/src/image-worker/util.ts b/src/features/util.ts similarity index 88% rename from src/image-worker/util.ts rename to src/features/util.ts index 5c94f995..d1aa569e 100644 --- a/src/image-worker/util.ts +++ b/src/features/util.ts @@ -32,8 +32,3 @@ export function clamp( ): number { return Math.min(Math.max(num, min), max); } - -export function timed(name: string, func: () => Promise) { - console.time(name); - return func().finally(() => console.timeEnd(name)); -} diff --git a/src/features/worker/missing-types.d.ts b/src/features/worker/missing-types.d.ts new file mode 100644 index 00000000..e0f384ad --- /dev/null +++ b/src/features/worker/missing-types.d.ts @@ -0,0 +1,13 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/// diff --git a/src/features/worker/util.ts b/src/features/worker/util.ts new file mode 100644 index 00000000..8b913406 --- /dev/null +++ b/src/features/worker/util.ts @@ -0,0 +1,16 @@ +/** + * Copyright 2020 Google Inc. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * http://www.apache.org/licenses/LICENSE-2.0 + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +export function timed(name: string, func: () => Promise) { + console.time(name); + return func().finally(() => console.timeEnd(name)); +} diff --git a/tsconfig.json b/tsconfig.json index f89adff0..629e708f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -4,6 +4,6 @@ "references": [ { "path": "./src/client" }, { "path": "./src/static-build" }, - { "path": "./src/image-worker" } + { "path": "./src/features/worker" } ] }