Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor self-resolution #1355

Merged
merged 2 commits into from
Feb 13, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,29 @@ export class Resolver {
// packages get this help, v2 packages are natively supposed to make their
// own modules resolvable, and we want to push them all to do that
// correctly.
return request.alias(this.resolveWithinPackage(request.specifier, pkg));
return this.resolveWithinPackage(request, pkg);
}

let originalPkg = this.originalPackage(request.fromFile);
if (originalPkg && pkg.meta['auto-upgraded'] && originalPkg.name === packageName) {
// A file that was relocated out of a package is importing that package's
// name, it should find its own original copy.
return request.alias(this.resolveWithinPackage(request.specifier, originalPkg));
return this.resolveWithinPackage(request, originalPkg);
}

return request;
}

private resolveWithinPackage(specifier: string, pkg: Package): string {
private resolveWithinPackage<R extends ModuleRequest>(request: R, pkg: Package): R {
if ('exports' in pkg.packageJSON) {
// this is the easy case -- a package that uses exports can safely resolve
// its own name
return require.resolve(specifier, { paths: [pkg.root] });
// its own name, so it's enough to let it resolve the (self-targeting)
// sepcifier from its own package root.
return request.rehome(resolve(pkg.root, 'package.json'));
} else {
// otherwise we need to just assume that internal naming is simple
return request.alias(request.specifier.replace(pkg.name, '.'), resolve(pkg.root, 'package.json'));
}
return specifier.replace(pkg.name, pkg.root);
}

private preHandleExternal<R extends ModuleRequest>(request: R): R {
Expand Down Expand Up @@ -382,11 +385,9 @@ export class Resolver {
// v2 packages can fall back to the set of known active addons only to find
// themselves (which is needed due to app tree merging)
if ((pkg.meta['auto-upgraded'] || packageName === pkg.name) && this.options.activeAddons[packageName]) {
return request.alias(
this.resolveWithinPackage(
specifier,
PackageCache.shared('embroider-stage3', this.options.appRoot).get(this.options.activeAddons[packageName])
)
return this.resolveWithinPackage(
request,
PackageCache.shared('embroider-stage3', this.options.appRoot).get(this.options.activeAddons[packageName])
);
}

Expand Down