Skip to content

Commit

Permalink
allow to use full urls in browser runtime (vercel/turborepo#7712)
Browse files Browse the repository at this point in the history
### Description

<!--
  ✍️ Write a short summary of your work.
  If necessary, include relevant screenshots.
-->

### Testing Instructions

<!--
  Give a quick description of steps to test your changes.
-->


Closes PACK-2724
  • Loading branch information
sokra authored Mar 12, 2024
1 parent dda49ed commit fb3d3cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type RefreshHelpers = RefreshRuntimeGlobals["$RefreshHelpers$"];

interface TurbopackDevBaseContext extends TurbopackBaseContext {
k: RefreshContext;
R: ResolvePathFromModule;
}

interface TurbopackDevContext extends TurbopackDevBaseContext {}
Expand Down Expand Up @@ -259,6 +260,18 @@ async function loadChunkPath(
}
}

/**
* Returns an absolute url to an asset.
*/
function createResolvePathFromModule(
resolver: (moduleId: string) => Exports
): (moduleId: string) => string {
return function resolvePathFromModule(moduleId: string): string {
const exported = resolver(moduleId);
return exported?.default ?? exported;
};
}

function instantiateModule(id: ModuleId, source: SourceInfo): Module {
const moduleFactory = moduleFactories[id];
if (typeof moduleFactory !== "function") {
Expand Down Expand Up @@ -319,6 +332,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
const sourceInfo: SourceInfo = { type: SourceType.Parent, parentId: id };

runModuleExecutionHooks(module, (refresh) => {
const r = commonJsRequire.bind(null, module);
moduleFactory.call(
module.exports,
augmentContext({
Expand All @@ -341,6 +355,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
g: globalThis,
U: relativeURL,
k: refresh,
R: createResolvePathFromModule(r),
__dirname: module.id.replace(/(^|\/)\/+$/, ""),
})
);
Expand Down

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

Loading

0 comments on commit fb3d3cd

Please sign in to comment.