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

Rehome moved requests to real on-disk files #1547

Merged
merged 1 commit into from
Jul 18, 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
35 changes: 24 additions & 11 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,28 +107,38 @@ type MergeMap = Map</* engine root dir */ string, Map</* withinEngineModuleName
const compatPattern = /#embroider_compat\/(?<type>[^\/]+)\/(?<rest>.*)/;

export interface ModuleRequest {
specifier: string;
fromFile: string;
isVirtual: boolean;
readonly specifier: string;
readonly fromFile: string;
readonly isVirtual: boolean;
readonly meta: Record<string, unknown> | undefined;
alias(newSpecifier: string): this;
rehome(newFromFile: string): this;
virtualize(virtualFilename: string): this;
withMeta(meta: Record<string, any> | undefined): this;
}

class NodeModuleRequest implements ModuleRequest {
constructor(readonly specifier: string, readonly fromFile: string, readonly isVirtual = false) {}
constructor(
readonly specifier: string,
readonly fromFile: string,
readonly isVirtual: boolean,
readonly meta: Record<string, any> | undefined
) {}
alias(specifier: string): this {
return new NodeModuleRequest(specifier, this.fromFile) as this;
return new NodeModuleRequest(specifier, this.fromFile, false, this.meta) as this;
}
rehome(fromFile: string): this {
if (this.fromFile === fromFile) {
return this;
} else {
return new NodeModuleRequest(this.specifier, fromFile) as this;
return new NodeModuleRequest(this.specifier, fromFile, false, this.meta) as this;
}
}
virtualize(filename: string) {
return new NodeModuleRequest(filename, this.fromFile, true) as this;
virtualize(filename: string): this {
return new NodeModuleRequest(filename, this.fromFile, true, this.meta) as this;
}
withMeta(meta: Record<string, any> | undefined): this {
return new NodeModuleRequest(this.specifier, this.fromFile, this.isVirtual, meta) as this;
}
}

Expand Down Expand Up @@ -247,7 +257,7 @@ export class Resolver {
| { type: 'virtual'; filename: string; content: string }
| { type: 'real'; filename: string }
| { type: 'not_found'; err: Error } {
let resolution = this.resolveSync(new NodeModuleRequest(specifier, fromFile), request => {
let resolution = this.resolveSync(new NodeModuleRequest(specifier, fromFile, false, undefined), request => {
if (request.isVirtual) {
return {
type: 'found',
Expand Down Expand Up @@ -744,7 +754,7 @@ export class Resolver {
return logTransition(
'outbound request from moved package',
request,
request.rehome(resolve(originalRequestingPkg.root, request.fromFile.slice(requestingPkg.root.length + 1)))
request.withMeta({ wasMovedTo: request.fromFile }).rehome(resolve(originalRequestingPkg.root, 'package.json'))
);
}

Expand Down Expand Up @@ -953,7 +963,10 @@ export class Resolver {
// isV2Ember()
let movedPkg = this.packageCache.maybeMoved(pkg);
if (movedPkg !== pkg) {
fromFile = resolve(movedPkg.root, request.fromFile.slice(pkg.root.length + 1));
if (!request.meta?.wasMovedTo) {
throw new Error(`bug: embroider resolver's meta is not propagating`);
}
fromFile = request.meta.wasMovedTo as string;
pkg = movedPkg;
}

Expand Down
11 changes: 9 additions & 2 deletions packages/webpack/src/webpack-resolver-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,9 @@ function getAdaptedResolve(
}

class WebpackModuleRequest implements ModuleRequest {
specifier: string;
fromFile: string;
readonly specifier: string;
readonly fromFile: string;
readonly meta: Record<string, any> | undefined;

static from(state: any, babelLoaderPrefix: string, appRoot: string): WebpackModuleRequest | undefined {
// when the files emitted from our virtual-loader try to import things,
Expand Down Expand Up @@ -146,6 +147,7 @@ class WebpackModuleRequest implements ModuleRequest {
context: string;
contextInfo: {
issuer: string;
_embroiderMeta?: Record<string, any> | undefined;
};
},
public isVirtual = false
Expand All @@ -157,6 +159,7 @@ class WebpackModuleRequest implements ModuleRequest {
// that can actually be handed back to webpack)
this.specifier = state.request;
this.fromFile = state.contextInfo.issuer;
this.meta = state.contextInfo._embroiderMeta ? { ...state.contextInfo._embroiderMeta } : undefined;
}

alias(newSpecifier: string) {
Expand All @@ -180,4 +183,8 @@ class WebpackModuleRequest implements ModuleRequest {
next.isVirtual = true;
return next;
}
withMeta(meta: Record<string, any> | undefined): this {
this.state.contextInfo._embroiderMeta = meta;
return new WebpackModuleRequest(this.babelLoaderPrefix, this.appRoot, this.state) as this;
}
}