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
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 13 additions & 12 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface ModuleRequest {
specifier: string;
fromFile: string;
isVirtual: boolean;
alias(newSpecifier: string, newFromFile?: string): this;
alias(newSpecifier: string): this;
rehome(newFromFile: string): this;
virtualize(virtualFilename: string): this;
}
Expand Down 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, '.')).rehome(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
6 changes: 1 addition & 5 deletions packages/webpack/src/webpack-resolver-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,8 @@ class WebpackModuleRequest implements ModuleRequest {
this.fromFile = state.contextInfo.issuer;
}

alias(newSpecifier: string, newFromFile?: string) {
alias(newSpecifier: string) {
this.state.request = newSpecifier;
if (newFromFile) {
this.state.contextInfo.issuer = newFromFile;
this.state.context = dirname(newFromFile);
}
return new WebpackModuleRequest(this.state) as this;
}
rehome(newFromFile: string) {
Expand Down