mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-18 19:49:04 +00:00
Moving worker and worker bridge
This commit is contained in:
@@ -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<typeof ${name}>`,
|
||||
` ): ReturnType<typeof ${name}> {`,
|
||||
@@ -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<typeof ${name}>`,
|
||||
@@ -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),
|
||||
]);
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user