From 11f723bd3ed45d6dbe26c626ae9cc21be522df2a Mon Sep 17 00:00:00 2001 From: Edward Faulkner Date: Tue, 21 Mar 2023 16:55:09 -0400 Subject: [PATCH] extract RewrittenPackages --- packages/core/src/module-resolver.ts | 32 +++++++----------------- packages/core/src/rewritten-packages.ts | 33 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 packages/core/src/rewritten-packages.ts diff --git a/packages/core/src/module-resolver.ts b/packages/core/src/module-resolver.ts index 08e0fa6450..2140a27507 100644 --- a/packages/core/src/module-resolver.ts +++ b/packages/core/src/module-resolver.ts @@ -18,8 +18,8 @@ import { } from './virtual-content'; import { Memoize } from 'typescript-memoize'; import { describeExports } from './describe-exports'; -import { existsSync, readFileSync } from 'fs'; -import { readJSONSync } from 'fs-extra'; +import { readFileSync } from 'fs'; +import { RewrittenPackages } from './rewritten-packages'; const debug = makeDebug('embroider:resolver'); function logTransition(reason: string, before: R, after: R = before): R { @@ -143,7 +143,11 @@ export type SyncResolverFunction Res; export class Resolver { - constructor(private options: Options) {} + private rewrittenPackages: RewrittenPackages; + + constructor(private options: Options) { + this.rewrittenPackages = new RewrittenPackages(this.options.appRoot); + } beforeResolve(request: R): R { if (request.specifier === '@embroider/macros') { @@ -608,7 +612,7 @@ export class Resolver { if (!pkg) { return request; } - let originalRoot = this.legacyAddonsIndex.v2toV1.get(pkg.root); + let originalRoot = this.rewrittenPackages.rewrittenFrom(pkg.root); if (originalRoot) { request = logTransition( 'outbound from moved v1 addon', @@ -625,7 +629,7 @@ export class Resolver { try { let target = PackageCache.shared('embroider-stage3', this.options.appRoot).resolve(packageName, pkg); if (target) { - let movedRoot = this.legacyAddonsIndex.v1ToV2.get(target.root); + let movedRoot = this.rewrittenPackages.rewrittenTo(target.root); if (movedRoot) { request = logTransition( 'inbound to moved v1 addon', @@ -644,24 +648,6 @@ export class Resolver { return request; } - @Memoize() - private get legacyAddonsIndex(): { v1ToV2: Map; v2toV1: Map } { - let addonsDir = resolve(this.options.appRoot, 'node_modules', '.embroider', 'addons'); - let indexFile = resolve(addonsDir, 'v1-addon-index.json'); - if (existsSync(indexFile)) { - let { v1Addons } = readJSONSync(indexFile) as { v1Addons: Record }; - return { - v1ToV2: new Map( - Object.entries(v1Addons).map(([oldRoot, relativeNewRoot]) => [oldRoot, resolve(addonsDir, relativeNewRoot)]) - ), - v2toV1: new Map( - Object.entries(v1Addons).map(([oldRoot, relativeNewRoot]) => [resolve(addonsDir, relativeNewRoot), oldRoot]) - ), - }; - } - return { v1ToV2: new Map(), v2toV1: new Map() }; - } - private handleRenaming(request: R): R { if (request.isVirtual) { return request; diff --git a/packages/core/src/rewritten-packages.ts b/packages/core/src/rewritten-packages.ts new file mode 100644 index 0000000000..373800454e --- /dev/null +++ b/packages/core/src/rewritten-packages.ts @@ -0,0 +1,33 @@ +import { Memoize } from 'typescript-memoize'; +import { readJSONSync, existsSync } from 'fs-extra'; +import { resolve } from 'path'; + +export class RewrittenPackages { + constructor(private appRoot: string) {} + + rewrittenTo(packageRoot: string): string | undefined { + return this.rewrittenPackagesIndex.v1ToV2.get(packageRoot); + } + + rewrittenFrom(packageRoot: string): string | undefined { + return this.rewrittenPackagesIndex.v2ToV1.get(packageRoot); + } + + @Memoize() + private get rewrittenPackagesIndex(): { v1ToV2: Map; v2ToV1: Map } { + let addonsDir = resolve(this.appRoot, 'node_modules', '.embroider', 'addons'); + let indexFile = resolve(addonsDir, 'v1-addon-index.json'); + if (existsSync(indexFile)) { + let { v1Addons } = readJSONSync(indexFile) as { v1Addons: Record }; + return { + v1ToV2: new Map( + Object.entries(v1Addons).map(([oldRoot, relativeNewRoot]) => [oldRoot, resolve(addonsDir, relativeNewRoot)]) + ), + v2ToV1: new Map( + Object.entries(v1Addons).map(([oldRoot, relativeNewRoot]) => [resolve(addonsDir, relativeNewRoot), oldRoot]) + ), + }; + } + return { v1ToV2: new Map(), v2ToV1: new Map() }; + } +}