From e9abd9f7fd41096c01527387d0b1a2ebde002d04 Mon Sep 17 00:00:00 2001 From: chentsulin Date: Wed, 23 Feb 2022 23:45:03 +0800 Subject: [PATCH] feat(jest-haste-map): add DependencyExtractor type --- packages/jest-haste-map/src/index.ts | 3 ++- packages/jest-haste-map/src/types.ts | 5 +++++ packages/jest-haste-map/src/worker.ts | 4 ++-- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index 244e37eb80cc..9e8844b9049e 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -30,6 +30,7 @@ import normalizePathSep from './lib/normalizePathSep'; import type { ChangeEvent, CrawlerOptions, + DependencyExtractor, EventsQueue, FileData, FileMetaData, @@ -316,7 +317,7 @@ export default class HasteMap extends EventEmitter { } if (options.dependencyExtractor) { - const dependencyExtractor = await requireOrImportModule( + const dependencyExtractor = await requireOrImportModule( options.dependencyExtractor, false, ); diff --git a/packages/jest-haste-map/src/types.ts b/packages/jest-haste-map/src/types.ts index 314348955655..07260b184b08 100644 --- a/packages/jest-haste-map/src/types.ts +++ b/packages/jest-haste-map/src/types.ts @@ -154,3 +154,8 @@ export type ChangeEvent = { hasteFS: HasteFS; moduleMap: ModuleMap; }; + +export type DependencyExtractor = { + extract: (code: string, filePath: string, defaultExtract: DependencyExtractor['extract']) => Set; + getCacheKey?: () => string; +}; diff --git a/packages/jest-haste-map/src/worker.ts b/packages/jest-haste-map/src/worker.ts index 3f584ca18eab..74747c4cc66c 100644 --- a/packages/jest-haste-map/src/worker.ts +++ b/packages/jest-haste-map/src/worker.ts @@ -12,7 +12,7 @@ import {requireOrImportModule} from 'jest-util'; import blacklist from './blacklist'; import H from './constants'; import * as dependencyExtractor from './lib/dependencyExtractor'; -import type {HasteImpl, WorkerMessage, WorkerMetadata} from './types'; +import type {DependencyExtractor, HasteImpl, WorkerMessage, WorkerMetadata} from './types'; const PACKAGE_JSON = path.sep + 'package.json'; @@ -75,7 +75,7 @@ export async function worker(data: WorkerMessage): Promise { dependencies = Array.from( data.dependencyExtractor ? ( - await requireOrImportModule(data.dependencyExtractor, false) + await requireOrImportModule(data.dependencyExtractor, false) ).extract(content, filePath, dependencyExtractor.extract) : dependencyExtractor.extract(content), );