Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack: make a require stub available in ESM #70255

Merged
merged 4 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading