Skip to content

Commit

Permalink
Turbopack: make a require stub available in ESM (#70255)
Browse files Browse the repository at this point in the history
Closes PACK-3265
Closes #70186

We did detect `require` calls in ESM at build time, but that variable
was not available *at runtime*
  • Loading branch information
mischnic authored Sep 23, 2024
1 parent 49688e8 commit 1718798
Show file tree
Hide file tree
Showing 77 changed files with 319 additions and 295 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
k: refresh,
R: createResolvePathFromModule(r),
b: getWorkerBlobURL,
z: requireStub,
__dirname: typeof module.id === "string" ? module.id.replace(/(^|\/)\/+$/, "") : module.id
})
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ function loadWebAssemblyModule(chunkPath: ChunkPath) {
return compileWebAssemblyFromPath(resolved);
}

function getWorkerBlobURL(_chunks: ChunkPath[]) {
function getWorkerBlobURL(_chunks: ChunkPath[]): never {
throw new Error("Worker blobs are not implemented yet for Node.js");
}

Expand Down Expand Up @@ -255,6 +255,7 @@ function instantiateModule(id: ModuleId, source: SourceInfo): Module {
U: relativeURL,
R: createResolvePathFromModule(r),
b: getWorkerBlobURL,
z: requireStub,
__dirname: typeof module.id === "string" ? module.id.replace(/(^|\/)\/+$/, "") : module.id
});
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ interface TurbopackBaseContext {
P: ResolveAbsolutePath;
U: RelativeURL;
b: GetWorkerBlobURL,
z: CommonJsRequire
__dirname: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,10 @@ relativeURL.prototype = URL.prototype;
function invariant(never: never, computeMessage: (arg: any) => string): never {
throw new Error(`Invariant: ${computeMessage(never)}`);
}

/**
* A stub function to make `require` available but non-functional in ESM.
*/
function requireStub(_moduleId: ModuleId): never {
throw new Error("dynamic usage of require is not supported");
}
12 changes: 7 additions & 5 deletions turbopack/crates/turbopack-ecmascript/src/chunk/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ impl EcmascriptChunkItemContent {
refresh,
externals,
async_module,
stub_require: true,
..Default::default()
}
} else {
Expand All @@ -67,7 +68,6 @@ impl EcmascriptChunkItemContent {
// These things are not available in ESM
module: true,
exports: true,
require: true,
this: true,
..Default::default()
}
Expand Down Expand Up @@ -115,7 +115,9 @@ impl EcmascriptChunkItemContent {
if this.options.exports {
args.push("e: exports");
}
if this.options.require {
if this.options.stub_require {
args.push("z: require");
} else {
args.push("t: require");
}
if this.options.wasm {
Expand Down Expand Up @@ -173,9 +175,9 @@ pub struct EcmascriptChunkItemOptions {
/// Whether this chunk item's module factory should include an `exports`
/// argument.
pub exports: bool,
/// Whether this chunk item's module factory should include a `require`
/// argument.
pub require: bool,
/// Whether this chunk item's module factory should include an argument for the real `require`,
/// or just a throwing stub (for ESM)
pub stub_require: bool,
/// Whether this chunk item's module factory should include a
/// `__turbopack_external_require__` argument.
pub externals: bool,
Expand Down

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

Loading

0 comments on commit 1718798

Please sign in to comment.