forked from external-repos/squoosh
Generating processor metadata
This commit is contained in:
@@ -22,7 +22,7 @@ const autoGenComment =
|
||||
|
||||
export default function () {
|
||||
let previousWorkerImports;
|
||||
let previousEncoderMetas;
|
||||
let previousJoinedMetas;
|
||||
|
||||
/**
|
||||
* Generates the worker file & tsconfig for all features
|
||||
@@ -134,7 +134,7 @@ export default function () {
|
||||
const joinedWorkerImports = workerImports.join();
|
||||
|
||||
// Avoid regenerating if nothing's changed.
|
||||
// This also prevents an infinite look in the watcher.
|
||||
// This also prevents an infinite loop in the watcher.
|
||||
if (joinedWorkerImports === previousWorkerImports) return;
|
||||
|
||||
previousWorkerImports = joinedWorkerImports;
|
||||
@@ -153,6 +153,14 @@ export default function () {
|
||||
.filter((tsFile) => !tsFile.endsWith('.d.ts'))
|
||||
.map((tsFile) => tsFile.slice(0, -'.ts'.length));
|
||||
|
||||
const processorMetas = (
|
||||
await globP('src/features/processors/*/shared/meta.ts', {
|
||||
absolute: true,
|
||||
})
|
||||
)
|
||||
.filter((tsFile) => !tsFile.endsWith('.d.ts'))
|
||||
.map((tsFile) => tsFile.slice(0, -'.ts'.length));
|
||||
|
||||
const featureMetaBasePath = path.join(
|
||||
process.cwd(),
|
||||
'src',
|
||||
@@ -161,20 +169,24 @@ export default function () {
|
||||
'feature-meta',
|
||||
);
|
||||
|
||||
const joinedEncoderMetas = encoderMetas.join();
|
||||
const joinedMetas = [...encoderMetas, ...processorMetas].join();
|
||||
|
||||
// Avoid regenerating if nothing's changed.
|
||||
// This also prevents an infinite look in the watcher.
|
||||
if (joinedEncoderMetas === previousEncoderMetas) return;
|
||||
previousEncoderMetas = joinedEncoderMetas;
|
||||
// This also prevents an infinite loop in the watcher.
|
||||
if (joinedMetas === previousJoinedMetas) return;
|
||||
previousJoinedMetas = joinedMetas;
|
||||
|
||||
const encoderMetaTsNames = encoderMetas.map((tsImport) => [
|
||||
const getTsName = (tsImport) => [
|
||||
path.relative(featureMetaBasePath, tsImport),
|
||||
path.basename(tsImport.slice(0, -'/shared/meta'.length)),
|
||||
]);
|
||||
];
|
||||
|
||||
const encoderMetaTsNames = encoderMetas.map(getTsName);
|
||||
const processorMetaTsNames = processorMetas.map(getTsName);
|
||||
|
||||
const featureMeta = [
|
||||
autoGenComment,
|
||||
// Encoder stuff
|
||||
encoderMetaTsNames.map(
|
||||
([path, name]) => `import * as ${name}EncoderMeta from '${path}';`,
|
||||
),
|
||||
@@ -194,6 +206,22 @@ export default function () {
|
||||
`};`,
|
||||
`export type EncoderType = keyof typeof encoderMap`,
|
||||
`export const encoders = [...Object.values(encoderMap)];`,
|
||||
// Processor stuff
|
||||
processorMetaTsNames.map(
|
||||
([path, name]) => `import * as ${name}ProcessorMeta from '${path}';`,
|
||||
),
|
||||
`interface Enableable { enabled: boolean; }`,
|
||||
`export interface ProcessorState {`,
|
||||
processorMetaTsNames.map(
|
||||
([_, name]) => ` ${name}: Enableable & ${name}ProcessorMeta.Options;`,
|
||||
),
|
||||
`}`,
|
||||
`export const defaultProcessorState: ProcessorState = {`,
|
||||
processorMetaTsNames.map(
|
||||
([_, name]) =>
|
||||
` ${name}: { enabled: false, ...${name}ProcessorMeta.defaultOptions },`,
|
||||
),
|
||||
`}`,
|
||||
]
|
||||
.flat(Infinity)
|
||||
.join('\n');
|
||||
|
||||
Reference in New Issue
Block a user