diff --git a/.gitignore b/.gitignore index 341b505a..f21e6b30 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ build *.o # Auto-generated by lib/image-worker-plugin.js -src/features/worker/index.ts -src/features/worker/tsconfig.json -src/features/worker/bridge/meta.ts -src/features/worker/bridge/tsconfig.json +src/features-worker/index.ts +src/features-worker/tsconfig.json +src/client/lazy-app/worker-bridge/meta.ts diff --git a/lib/image-worker-plugin.js b/lib/image-worker-plugin.js index faca947a..0515f8d7 100644 --- a/lib/image-worker-plugin.js +++ b/lib/image-worker-plugin.js @@ -24,17 +24,34 @@ export default function () { return { name: 'image-worker-plugin', async buildStart() { - const base = path.join(process.cwd(), 'src', 'features', 'worker'); + const featuresWorkerBase = path.join( + process.cwd(), + 'src', + 'features-worker', + ); + const featuresWorkerBridgeBase = path.join( + process.cwd(), + 'src', + 'client', + 'lazy-app', + 'worker-bridge', + ); + const tsImports = ( - await globP('../*/**/worker/*.ts', { - cwd: base, + await globP('src/features/*/**/worker/*.ts', { + absolute: true, }) ) .filter((tsFile) => !tsFile.endsWith('.d.ts')) .map((tsFile) => tsFile.slice(0, -'.ts'.length)); - const tsNames = tsImports.map((tsImport) => [ - tsImport, + const featuresWorkerTsNames = tsImports.map((tsImport) => [ + path.relative(featuresWorkerBase, tsImport), + path.basename(tsImport), + ]); + + const featuresWorkerBridgeTsNames = tsImports.map((tsImport) => [ + path.relative(featuresWorkerBridgeBase, tsImport), path.basename(tsImport), ]); @@ -42,9 +59,11 @@ export default function () { `// This file is autogenerated by lib/image-worker-plugin.js`, `import { expose } from 'comlink';`, `import { timed } from './util';`, - tsNames.map(([path, name]) => `import ${name} from './${path}';`), + featuresWorkerTsNames.map( + ([path, name]) => `import ${name} from './${path}';`, + ), `const exports = {`, - tsNames.map(([_, name]) => [ + featuresWorkerTsNames.map(([_, name]) => [ ` ${name}(`, ` ...args: Parameters`, ` ): ReturnType {`, @@ -62,40 +81,28 @@ export default function () { if (previousWorkerContent === workerFile) return; previousWorkerContent = workerFile; - const tsConfigReferences = tsImports.map((tsImport) => ({ - path: path.dirname(tsImport), - })); - const workerTsConfig = { - extends: '../../../generic-tsconfig.json', + extends: '../../generic-tsconfig.json', compilerOptions: { lib: ['webworker', 'esnext'], }, - references: tsConfigReferences, - }; - - const bridgeTsConfig = { - extends: '../../../../generic-tsconfig.json', - compilerOptions: { - lib: ['esnext', 'dom', 'dom.iterable'], - types: [], - }, - include: ['../../../client/lazy-app/util.ts', '**/*.ts'], - references: tsConfigReferences.map((ref) => ({ - path: path.join('..', ref.path), + references: featuresWorkerTsNames.map(([tsImport]) => ({ + path: path.dirname(tsImport), })), }; const bridgeMeta = [ `// This file is autogenerated by lib/image-worker-plugin.js`, - tsNames.map(([path, name]) => `import type ${name} from '../${path}';`), + featuresWorkerBridgeTsNames.map( + ([path, name]) => `import type ${name} from '${path}';`, + ), `export const methodNames = ${JSON.stringify( - tsNames.map(([_, name]) => name), + featuresWorkerBridgeTsNames.map(([_, name]) => name), null, ' ', )} as const;`, `export interface BridgeMethods {`, - tsNames.map(([_, name]) => [ + featuresWorkerBridgeTsNames.map(([_, name]) => [ ` ${name}(`, ` signal: AbortSignal,`, ` ...args: Parameters`, @@ -108,15 +115,14 @@ export default function () { await Promise.all([ fsp.writeFile( - path.join(base, 'tsconfig.json'), + path.join(featuresWorkerBase, 'tsconfig.json'), JSON.stringify(workerTsConfig, null, ' '), ), + fsp.writeFile(path.join(featuresWorkerBase, 'index.ts'), workerFile), fsp.writeFile( - path.join(base, 'bridge', 'tsconfig.json'), - JSON.stringify(bridgeTsConfig, null, ' '), + path.join(featuresWorkerBridgeBase, 'meta.ts'), + bridgeMeta, ), - fsp.writeFile(path.join(base, 'index.ts'), workerFile), - fsp.writeFile(path.join(base, 'bridge', 'meta.ts'), bridgeMeta), ]); }, }; diff --git a/rollup.config.js b/rollup.config.js index aca0b5f3..f443133c 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -75,6 +75,8 @@ export default async function ({ watch }) { 'src/client', 'src/shared', 'src/features', + 'src/features-worker', + 'src/features-worker-worker-bridge', 'src/sw', 'codecs', ]), diff --git a/src/features/worker/bridge/index.ts b/src/client/lazy-app/worker-bridge/index.ts similarity index 91% rename from src/features/worker/bridge/index.ts rename to src/client/lazy-app/worker-bridge/index.ts index db716afa..bf678dbf 100644 --- a/src/features/worker/bridge/index.ts +++ b/src/client/lazy-app/worker-bridge/index.ts @@ -1,8 +1,8 @@ import { wrap } from 'comlink'; import { BridgeMethods, methodNames } from './meta'; -import workerURL from 'omt:../index'; -import type { ProcessorWorkerApi } from '../'; -import { abortable } from '../../../client/lazy-app/util'; +import workerURL from 'omt:../features-worker'; +import type { ProcessorWorkerApi } from '../../../features-worker'; +import { abortable } from '../util'; /** How long the worker should be idle before terminating. */ const workerTimeout = 10000; diff --git a/src/client/tsconfig.json b/src/client/tsconfig.json index 47bc2e7f..d2b51875 100644 --- a/src/client/tsconfig.json +++ b/src/client/tsconfig.json @@ -4,5 +4,5 @@ "lib": ["esnext", "dom", "dom.iterable"], "types": [] }, - "references": [{ "path": "../features/worker" }, { "path": "../shared" }] + "references": [{ "path": "../features-worker" }, { "path": "../shared" }] } diff --git a/src/features/worker/missing-types.d.ts b/src/features-worker/missing-types.d.ts similarity index 91% rename from src/features/worker/missing-types.d.ts rename to src/features-worker/missing-types.d.ts index e0f384ad..27b1d41c 100644 --- a/src/features/worker/missing-types.d.ts +++ b/src/features-worker/missing-types.d.ts @@ -10,4 +10,4 @@ * 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 similarity index 100% rename from src/features/worker/util.ts rename to src/features-worker/util.ts diff --git a/src/features/worker/bridge/missing-types.d.ts b/src/features/worker/bridge/missing-types.d.ts deleted file mode 100644 index e8e80d3a..00000000 --- a/src/features/worker/bridge/missing-types.d.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * 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. - */ -///