Unverified Commit ee6cc6d9 authored by Leah's avatar Leah Committed by GitHub

feat(turbopack): support named client references properly (#59578)

### What?
Makes sure `import * as mod from 'client-mod'` works properly by
actually adding the exports to the emitted module.

Depends on: https://github.com/vercel/turbo/pull/6787

### Turbopack Updates

* https://github.com/vercel/turbo/pull/6965 <!-- Tobias Koppers -
recycle trace buffers -->
* https://github.com/vercel/turbo/pull/6974 <!-- Chris Olszewski -
feat(lockfiles): support Yarn 4 patches -->
* https://github.com/vercel/turbo/pull/6787 <!-- Leah -
feat(turbopack-ecmascript): support named client references -->


Closes PACK-2110
parent 8f7637c8
This diff is collapsed.
......@@ -37,11 +37,11 @@ swc_core = { version = "0.87.16", features = [
testing = { version = "0.35.14" }
# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240110.3" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240110.4" }
# [TODO]: need to refactor embed_directory! macro usages, as well as resolving turbo_tasks::function, macros..
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240110.3" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240110.4" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240110.3" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-240110.4" }
# General Deps
......
......@@ -65,41 +65,89 @@ impl EcmascriptClientReferenceProxyModule {
}
#[turbo_tasks::function]
async fn proxy_module(self: Vc<Self>) -> Result<Vc<EcmascriptModuleAsset>> {
let this = self.await?;
async fn proxy_module(&self) -> Result<Vc<EcmascriptModuleAsset>> {
let mut code = CodeBuilder::default();
// Adapted from
// next.js/packages/next/src/build/webpack/loaders/next-flight-loader/index.ts
writedoc!(
code,
r#"
import {{ createProxy }} from 'next/dist/build/webpack/loaders/next-flight-loader/module-proxy'
let server_module_path = &*self.server_module_ident.path().to_string().await?;
const proxy = createProxy({server_module_path})
// Adapted from https://github.com/facebook/react/blob/c5b9375767e2c4102d7e5559d383523736f1c902/packages/react-server-dom-webpack/src/ReactFlightWebpackNodeLoader.js#L323-L354
if let EcmascriptExports::EsmExports(exports) = &*self.client_module.get_exports().await? {
let exports = exports.expand_exports().await?;
// Accessing the __esModule property and exporting $$typeof are required here.
// The __esModule getter forces the proxy target to create the default export
// and the $$typeof value is for rendering logic to determine if the module
// is a client boundary.
const {{ __esModule, $$typeof }} = proxy;
if !exports.dynamic_exports.is_empty() {
// TODO: throw? warn?
}
export {{ __esModule, $$typeof }};
export default proxy;
"#,
server_module_path = StringifyJs(&this.server_module_ident.path().to_string().await?)
)?;
writedoc!(
code,
r#"
import {{ registerClientReference }} from "react-server-dom-turbopack/server.edge";
"#,
)?;
for export_name in exports.exports.keys() {
if export_name == "default" {
writedoc!(
code,
r#"
export default registerClientReference(
function() {{ throw new Error({call_err}); }},
{server_module_path},
"default",
);
"#,
call_err = StringifyJs(&format!(
"Attempted to call the default export of {server_module_path} from \
the server, but it's on the client. It's not possible to invoke a \
client function from the server, it can only be rendered as a \
Component or passed to props of a Client Component."
)),
server_module_path = StringifyJs(server_module_path),
)?;
} else {
writedoc!(
code,
r#"
export const {export_name} = registerClientReference(
function() {{ throw new Error({call_err}); }},
{server_module_path},
{export_name_str},
);
"#,
export_name = export_name,
call_err = StringifyJs(&format!(
"Attempted to call {export_name}() from the server but {export_name} \
is on the client. It's not possible to invoke a client function from \
the server, it can only be rendered as a Component or passed to \
props of a Client Component."
)),
server_module_path = StringifyJs(server_module_path),
export_name_str = StringifyJs(export_name),
)?;
}
}
} else {
writedoc!(
code,
r#"
const {{ createClientModuleProxy }} = require("react-server-dom-turbopack/server.edge");
__turbopack_export_namespace__(createClientModuleProxy({server_module_path}));
"#,
server_module_path = StringifyJs(server_module_path)
)?;
};
let code = code.build();
let proxy_module_content =
AssetContent::file(File::from(code.source_code().clone()).into());
let proxy_source = VirtualSource::new(
this.server_module_ident.path().join("proxy.ts".to_string()),
self.server_module_ident.path().join("proxy.js".to_string()),
proxy_module_content,
);
let proxy_module = this
let proxy_module = self
.server_asset_context
.process(
Vc::upcast(proxy_source),
......@@ -168,7 +216,7 @@ impl ChunkableModule for EcmascriptClientReferenceProxyModule {
async fn as_chunk_item(
self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Vc<Box<dyn turbopack_binding::turbopack::core::chunk::ChunkItem>>> {
) -> Result<Vc<Box<dyn ChunkItem>>> {
let item = self.proxy_module().as_chunk_item(chunking_context);
let ecmascript_item = Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkItem>>(item)
.await?
......
use std::collections::BTreeMap;
use anyhow::{bail, Context, Result};
use indoc::formatdoc;
use turbo_tasks::Vc;
......@@ -10,7 +12,10 @@ use turbopack_binding::turbopack::{
module::Module,
reference::ModuleReferences,
},
ecmascript::chunk::EcmascriptChunkType,
ecmascript::{
chunk::EcmascriptChunkType,
references::esm::{EsmExport, EsmExports},
},
turbopack::ecmascript::{
chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
......@@ -97,8 +102,21 @@ impl ChunkableModule for NextServerComponentModule {
impl EcmascriptChunkPlaceable for NextServerComponentModule {
#[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> {
// TODO This should be EsmExports
EcmascriptExports::Value.cell()
let exports = BTreeMap::from([(
"default".to_string(),
EsmExport::ImportedNamespace(Vc::upcast(NextServerComponentModuleReference::new(
Vc::upcast(self.module),
))),
)]);
EcmascriptExports::EsmExports(
EsmExports {
exports,
star_exports: Default::default(),
}
.cell(),
)
.cell()
}
}
......
......@@ -194,7 +194,7 @@
"@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0",
"@vercel/nft": "0.26.2",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.3",
"@vercel/turbopack-ecmascript-runtime": "https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.4",
"acorn": "8.5.0",
"amphtml-validator": "1.0.35",
"anser": "1.4.9",
......
......@@ -1083,8 +1083,8 @@ importers:
specifier: 0.26.2
version: 0.26.2
'@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.3
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.3(react-refresh@0.12.0)(webpack@5.86.0)'
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.4
version: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.4(react-refresh@0.12.0)(webpack@5.86.0)'
acorn:
specifier: 8.5.0
version: 8.5.0
......@@ -25688,9 +25688,9 @@ packages:
/zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==}
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.3(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.3}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.3'
'@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.4(react-refresh@0.12.0)(webpack@5.86.0)':
resolution: {tarball: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.4}
id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.4'
name: '@vercel/turbopack-ecmascript-runtime'
version: 0.0.0
dependencies:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment