Skip to content

Commit

Permalink
add enable_edge_node_externals (vercel/turborepo#6562)
Browse files Browse the repository at this point in the history
### Description

Some edge runtimes allow additional node.js builtins

### Testing Instructions

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


Closes PACK-2030
  • Loading branch information
sokra authored Nov 24, 2023
1 parent 25e85f5 commit 4b3ebfa
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
13 changes: 13 additions & 0 deletions crates/turbopack-core/src/environment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,19 @@ impl Environment {
})
}

#[turbo_tasks::function]
pub async fn supports_commonjs_externals(self: Vc<Self>) -> Result<Vc<bool>> {
let this = self.await?;
Ok(match this.execution {
ExecutionEnvironment::NodeJsBuildTime(..) | ExecutionEnvironment::NodeJsLambda(_) => {
Vc::cell(true)
}
ExecutionEnvironment::Browser(_) => Vc::cell(false),
ExecutionEnvironment::EdgeWorker(_) => Vc::cell(true),
ExecutionEnvironment::Custom(_) => todo!(),
})
}

#[turbo_tasks::function]
pub async fn resolve_extensions(self: Vc<Self>) -> Result<Vc<Vec<String>>> {
let env = self.await?;
Expand Down
10 changes: 7 additions & 3 deletions crates/turbopack-ecmascript/src/references/esm/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,14 @@ impl CodeGenerateable for EsmAssetReference {
}));
}
ReferencedAsset::OriginalReferenceTypeExternal(request) => {
if !*chunking_context.environment().node_externals().await? {
if !*chunking_context
.environment()
.supports_commonjs_externals()
.await?
{
bail!(
"the chunking context does not support Node.js external modules \
(request: {})",
"the chunking context does not support external modules (request: \
{})",
request
);
}
Expand Down
14 changes: 14 additions & 0 deletions crates/turbopack/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const NODE_EXTERNALS: [&str; 51] = [
"pnpapi",
];

const EDGE_NODE_EXTERNALS: [&str; 5] = ["buffer", "events", "assert", "util", "async_hooks"];

#[turbo_tasks::function]
async fn base_resolve_options(
resolve_path: Vc<FileSystemPath>,
Expand Down Expand Up @@ -100,6 +102,18 @@ async fn base_resolve_options(
);
}
}
if opt.enable_edge_node_externals {
for req in EDGE_NODE_EXTERNALS {
direct_mappings.insert(
AliasPattern::exact(req),
ImportMapping::External(Some(format!("node:{req}"))).into(),
);
direct_mappings.insert(
AliasPattern::exact(format!("node:{req}")),
ImportMapping::External(None).into(),
);
}
}

let mut import_map = ImportMap::new(direct_mappings);
if let Some(additional_import_map) = opt.import_map {
Expand Down
3 changes: 3 additions & 0 deletions crates/turbopack/src/resolve_options_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ pub struct ResolveOptionsContext {
/// Mark well-known Node.js modules as external imports and load them using
/// native `require`. e.g. url, querystring, os
pub enable_node_externals: bool,
/// Mark well-known Edge modules as external imports and load them using
/// native `require`. e.g. buffer, events, assert
pub enable_edge_node_externals: bool,
#[serde(default)]
/// Enables the "browser" field and export condition in package.json
pub browser: bool,
Expand Down

0 comments on commit 4b3ebfa

Please # to comment.