Skip to content

Commit

Permalink
extract RewrittenPackages
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed May 18, 2023
1 parent 18fd6a9 commit 11f723b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 23 deletions.
32 changes: 9 additions & 23 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<R extends ModuleRequest>(reason: string, before: R, after: R = before): R {
Expand Down Expand Up @@ -143,7 +143,11 @@ export type SyncResolverFunction<R extends ModuleRequest = ModuleRequest, Res ex
) => Res;

export class Resolver {
constructor(private options: Options) {}
private rewrittenPackages: RewrittenPackages;

constructor(private options: Options) {
this.rewrittenPackages = new RewrittenPackages(this.options.appRoot);
}

beforeResolve<R extends ModuleRequest>(request: R): R {
if (request.specifier === '@embroider/macros') {
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -644,24 +648,6 @@ export class Resolver {
return request;
}

@Memoize()
private get legacyAddonsIndex(): { v1ToV2: Map<string, string>; v2toV1: Map<string, string> } {
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<string, string> };
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<R extends ModuleRequest>(request: R): R {
if (request.isVirtual) {
return request;
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/rewritten-packages.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>; v2ToV1: Map<string, string> } {
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<string, string> };
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() };
}
}

0 comments on commit 11f723b

Please sign in to comment.