Skip to content

Commit

Permalink
move module-request to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
ef4 committed Nov 27, 2024
1 parent 8d218f9 commit 614989c
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 36 deletions.
3 changes: 2 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export { default as Options, optionsWithDefaults } from './options';
export { default as WaitForTrees, OutputPaths } from './wait-for-trees';
export { compile as jsHandlebarsCompile } from './js-handlebars';
export { todo, unsupported, warn, debug, expectWarning, throwOnWarnings } from './messages';
export { Resolver, ModuleRequest, Resolution } from './module-resolver';
export { Resolver } from './module-resolver';
export type { ModuleRequest, Resolution } from './module-request';
export type { Options as ResolverOptions } from './module-resolver-options';
export { ResolverLoader } from './resolver-loader';
export { virtualContent } from './virtual-content';
Expand Down
33 changes: 33 additions & 0 deletions packages/core/src/module-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// This is generic because different build systems have different ways of
// representing a found module, and we just pass those values through.
export type Resolution<T = unknown, E = unknown> =
| { type: 'found'; filename: string; isVirtual: boolean; result: T }

// used for requests that are special and don't represent real files that
// embroider can possibly do anything custom with.
//
// the motivating use case for introducing this is Vite's depscan which marks
// almost everything as "external" as a way to tell esbuild to stop traversing
// once it has been seen the first time.
| { type: 'ignored'; result: T }

// the important thing about this Resolution is that embroider should do its
// fallback behaviors here.
| { type: 'not_found'; err: E };

export interface ModuleRequest<Res extends Resolution = Resolution> {
readonly specifier: string;
readonly fromFile: string;
readonly isVirtual: boolean;
readonly meta: Record<string, unknown> | undefined;
readonly debugType: string;
readonly isNotFound: boolean;
readonly resolvedTo: Res | undefined;
alias(newSpecifier: string): this;
rehome(newFromFile: string): this;
virtualize(virtualFilename: string): this;
withMeta(meta: Record<string, any> | undefined): this;
notFound(): this;
defaultResolve(): Promise<Res>;
resolveTo(resolution: Res): this;
}
35 changes: 1 addition & 34 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { nodeResolve } from './node-resolve';
import { decodePublicRouteEntrypoint, encodeRouteEntrypoint } from './virtual-route-entrypoint';
import type { Options, EngineConfig } from './module-resolver-options';
import { satisfies } from 'semver';
import type { ModuleRequest, Resolution } from './module-request';

const debug = makeDebug('embroider:resolver');

Expand Down Expand Up @@ -110,40 +111,6 @@ type MergeMap = Map</* engine root dir */ string, Map</* withinEngineModuleName

const compatPattern = /@embroider\/virtual\/(?<type>[^\/]+)\/(?<rest>.*)/;

export interface ModuleRequest<Res extends Resolution = Resolution> {
readonly specifier: string;
readonly fromFile: string;
readonly isVirtual: boolean;
readonly meta: Record<string, unknown> | undefined;
readonly debugType: string;
readonly isNotFound: boolean;
readonly resolvedTo: Res | undefined;
alias(newSpecifier: string): this;
rehome(newFromFile: string): this;
virtualize(virtualFilename: string): this;
withMeta(meta: Record<string, any> | undefined): this;
notFound(): this;
defaultResolve(): Promise<Res>;
resolveTo(resolution: Res): this;
}

// This is generic because different build systems have different ways of
// representing a found module, and we just pass those values through.
export type Resolution<T = unknown, E = unknown> =
| { type: 'found'; filename: string; isVirtual: boolean; result: T }

// used for requests that are special and don't represent real files that
// embroider can possibly do anything custom with.
//
// the motivating use case for introducing this is Vite's depscan which marks
// almost everything as "external" as a way to tell esbuild to stop traversing
// once it has been seen the first time.
| { type: 'ignored'; result: T }

// the important thing about this Resolution is that embroider should do its
// fallback behaviors here.
| { type: 'not_found'; err: E };

export class Resolver {
constructor(readonly options: Options) {}

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/node-resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import { explicitRelative } from '@embroider/shared-internals';
import assertNever from 'assert-never';

// these would be circular, but they're type-only so it's fine
import type { ModuleRequest, Resolution, Resolver } from './module-resolver';
import type { ModuleRequest, Resolution } from './module-request';
import type { Resolver } from './module-resolver';

export class NodeModuleRequest implements ModuleRequest {
constructor(
Expand Down

0 comments on commit 614989c

Please sign in to comment.