Skip to content

Commit

Permalink
fix(turbopack): add no-op resolveAbsolutePath to browser runtime (v…
Browse files Browse the repository at this point in the history
…ercel/turborepo#8549)

### Description

Reading `import.meta.url` should work in the browser, it can just return
a `/ROOT/...` path.

Closes PACK-3095
Fixes #66005
  • Loading branch information
ForsakenHarmony authored Jun 26, 2024
1 parent cfc3e6b commit e57b23c
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
w: loadWebAssembly.bind(null, sourceInfo),
u: loadWebAssemblyModule.bind(null, sourceInfo),
g: globalThis,
P: resolveAbsolutePath,
U: relativeURL,
k: refresh,
R: createResolvePathFromModule(r),
Expand All @@ -386,6 +387,14 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
return module;
}

/**
* no-op for browser
* @param modulePath
*/
function resolveAbsolutePath(modulePath?: string): string {
return `/ROOT/${modulePath ?? ""}`;
}

/**
* NOTE(alexkirsz) Webpack has a "module execution" interception hook that
* Next.js' React Refresh runtime hooks into to add module context to the
Expand Down
2 changes: 0 additions & 2 deletions crates/turbopack-ecmascript-runtime/js/src/nodejs/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ function stringifySourceInfo(source: SourceInfo): string {

type ExternalRequire = (id: ModuleId) => Exports | EsmNamespaceObject;
type ExternalImport = (id: ModuleId) => Promise<Exports | EsmNamespaceObject>;
type ResolveAbsolutePath = (modulePath?: string) => string;

interface TurbopackNodeBuildContext extends TurbopackBaseContext {
P: ResolveAbsolutePath;
R: ResolvePathFromModule;
x: ExternalRequire;
y: ExternalImport;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ type AsyncModule = (
hasAwait: boolean
) => void;

type ResolveAbsolutePath = (modulePath?: string) => string;

interface TurbopackBaseContext {
a: AsyncModule;
e: Module["exports"];
Expand All @@ -71,6 +73,7 @@ interface TurbopackBaseContext {
w: LoadWebAssembly;
u: LoadWebAssemblyModule;
g: typeof globalThis;
P: ResolveAbsolutePath;
U: RelativeURL;
__dirname: string;
}

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

0 comments on commit e57b23c

Please sign in to comment.