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 = [ ...@@ -37,11 +37,11 @@ swc_core = { version = "0.87.16", features = [
testing = { version = "0.35.14" } testing = { version = "0.35.14" }
# Turbo crates # 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.. # [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 # [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 # General Deps
......
...@@ -65,41 +65,89 @@ impl EcmascriptClientReferenceProxyModule { ...@@ -65,41 +65,89 @@ impl EcmascriptClientReferenceProxyModule {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn proxy_module(self: Vc<Self>) -> Result<Vc<EcmascriptModuleAsset>> { async fn proxy_module(&self) -> Result<Vc<EcmascriptModuleAsset>> {
let this = self.await?;
let mut code = CodeBuilder::default(); let mut code = CodeBuilder::default();
// Adapted from let server_module_path = &*self.server_module_ident.path().to_string().await?;
// 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'
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. if !exports.dynamic_exports.is_empty() {
// The __esModule getter forces the proxy target to create the default export // TODO: throw? warn?
// and the $$typeof value is for rendering logic to determine if the module }
// is a client boundary.
const {{ __esModule, $$typeof }} = proxy;
export {{ __esModule, $$typeof }}; writedoc!(
export default proxy; code,
"#, r#"
server_module_path = StringifyJs(&this.server_module_ident.path().to_string().await?) 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 code = code.build();
let proxy_module_content = let proxy_module_content =
AssetContent::file(File::from(code.source_code().clone()).into()); AssetContent::file(File::from(code.source_code().clone()).into());
let proxy_source = VirtualSource::new( 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, proxy_module_content,
); );
let proxy_module = this let proxy_module = self
.server_asset_context .server_asset_context
.process( .process(
Vc::upcast(proxy_source), Vc::upcast(proxy_source),
...@@ -168,7 +216,7 @@ impl ChunkableModule for EcmascriptClientReferenceProxyModule { ...@@ -168,7 +216,7 @@ impl ChunkableModule for EcmascriptClientReferenceProxyModule {
async fn as_chunk_item( async fn as_chunk_item(
self: Vc<Self>, self: Vc<Self>,
chunking_context: Vc<Box<dyn ChunkingContext>>, 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 item = self.proxy_module().as_chunk_item(chunking_context);
let ecmascript_item = Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkItem>>(item) let ecmascript_item = Vc::try_resolve_downcast::<Box<dyn EcmascriptChunkItem>>(item)
.await? .await?
......
use std::collections::BTreeMap;
use anyhow::{bail, Context, Result}; use anyhow::{bail, Context, Result};
use indoc::formatdoc; use indoc::formatdoc;
use turbo_tasks::Vc; use turbo_tasks::Vc;
...@@ -10,7 +12,10 @@ use turbopack_binding::turbopack::{ ...@@ -10,7 +12,10 @@ use turbopack_binding::turbopack::{
module::Module, module::Module,
reference::ModuleReferences, reference::ModuleReferences,
}, },
ecmascript::chunk::EcmascriptChunkType, ecmascript::{
chunk::EcmascriptChunkType,
references::esm::{EsmExport, EsmExports},
},
turbopack::ecmascript::{ turbopack::ecmascript::{
chunk::{ chunk::{
EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable, EcmascriptChunkItem, EcmascriptChunkItemContent, EcmascriptChunkPlaceable,
...@@ -97,8 +102,21 @@ impl ChunkableModule for NextServerComponentModule { ...@@ -97,8 +102,21 @@ impl ChunkableModule for NextServerComponentModule {
impl EcmascriptChunkPlaceable for NextServerComponentModule { impl EcmascriptChunkPlaceable for NextServerComponentModule {
#[turbo_tasks::function] #[turbo_tasks::function]
fn get_exports(&self) -> Vc<EcmascriptExports> { fn get_exports(&self) -> Vc<EcmascriptExports> {
// TODO This should be EsmExports let exports = BTreeMap::from([(
EcmascriptExports::Value.cell() "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 @@ ...@@ -194,7 +194,7 @@
"@types/ws": "8.2.0", "@types/ws": "8.2.0",
"@vercel/ncc": "0.34.0", "@vercel/ncc": "0.34.0",
"@vercel/nft": "0.26.2", "@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", "acorn": "8.5.0",
"amphtml-validator": "1.0.35", "amphtml-validator": "1.0.35",
"anser": "1.4.9", "anser": "1.4.9",
......
...@@ -1083,8 +1083,8 @@ importers: ...@@ -1083,8 +1083,8 @@ importers:
specifier: 0.26.2 specifier: 0.26.2
version: 0.26.2 version: 0.26.2
'@vercel/turbopack-ecmascript-runtime': '@vercel/turbopack-ecmascript-runtime':
specifier: https://gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.3 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.3(react-refresh@0.12.0)(webpack@5.86.0)' 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: acorn:
specifier: 8.5.0 specifier: 8.5.0
version: 8.5.0 version: 8.5.0
...@@ -25688,9 +25688,9 @@ packages: ...@@ -25688,9 +25688,9 @@ packages:
/zwitch@2.0.4: /zwitch@2.0.4:
resolution: {integrity: sha512-bXE4cR/kVZhKZX/RjPEflHaKVhUVl85noU3v6b8apfQEc1x4A+zBxjZ4lN8LqGd6WZ3dl98pY4o717VFmoPp+A==} 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)': '@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.3} 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.3' id: '@gitpkg-fork.vercel.sh/vercel/turbo/crates/turbopack-ecmascript-runtime/js?turbopack-240110.4'
name: '@vercel/turbopack-ecmascript-runtime' name: '@vercel/turbopack-ecmascript-runtime'
version: 0.0.0 version: 0.0.0
dependencies: 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