Skip to content

Commit

Permalink
Merge pull request #1355 from embroider-build/refactor-self-resolution
Browse files Browse the repository at this point in the history
Refactor self-resolution
  • Loading branch information
ef4 authored Feb 13, 2023
2 parents b869b2c + 237fa4b commit 4f583c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
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

0 comments on commit 4f583c9

Please sign in to comment.