Skip to content

Commit

Permalink
Make require stub available in ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Sep 19, 2024
1 parent 8e1a92b commit ed1030b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 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

0 comments on commit ed1030b

Please sign in to comment.