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: Allow ESM externals in SSR #70258

Merged
merged 6 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
26 changes: 15 additions & 11 deletions crates/next-core/src/next_manifests/client_reference_manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,6 @@ impl ClientReferenceManifest {
(Vec::new(), false)
};

entry_manifest.client_modules.module_exports.insert(
get_client_reference_module_key(&server_path, "*"),
ManifestNodeEntry {
name: "*".into(),
id: (&*client_module_id).into(),
chunks: client_chunks_paths,
r#async: client_is_async,
},
);

if let Some(ssr_chunking_context) = ssr_chunking_context {
let ssr_chunk_item = ecmascript_client_reference
.ssr_module
Expand Down Expand Up @@ -154,14 +144,28 @@ impl ClientReferenceManifest {
(Vec::new(), false)
};

entry_manifest.client_modules.module_exports.insert(
get_client_reference_module_key(&server_path, "*"),
ManifestNodeEntry {
name: "*".into(),
id: (&*client_module_id).into(),
chunks: client_chunks_paths,
// This should of course be client_is_async, but SSR can become async
// due to ESM externals, and the ssr_manifest_node is currently ignored
// by React.
r#async: client_is_async || ssr_is_async,
},
);

let mut ssr_manifest_node = ManifestNode::default();
ssr_manifest_node.module_exports.insert(
"*".into(),
ManifestNodeEntry {
name: "*".into(),
id: (&*ssr_module_id).into(),
chunks: ssr_chunks_paths,
r#async: ssr_is_async,
// See above
r#async: client_is_async || ssr_is_async,
},
);

Expand Down
3 changes: 1 addition & 2 deletions crates/next-core/src/next_server/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ pub async fn get_server_resolve_options_context(
project_path,
project_path.root(),
ExternalPredicate::Only(Vc::cell(external_packages)).cell(),
// app-ssr can't have esm externals as that would make the module async on the server only
*next_config.import_externals().await? && !matches!(ty, ServerContextType::AppSSR { .. }),
*next_config.import_externals().await?,
);

let mut custom_conditions = vec![mode.await?.condition().to_string().into()];
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-external/app-external.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,11 @@ describe('app dir - external dependency', () => {
expect(html).toContain('hello')
})

it('should support client module references with SSR-only ESM externals', async () => {
const html = await next.render('/esm-client-ref-external')
expect(html).toContain('client external-pure-esm-lib')
})

it('should support exporting multiple star re-exports', async () => {
const html = await next.render('/wildcard')
expect(html).toContain('Foo')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use client'

import name from 'esm'

export function Client() {
return <div>{`client ${name}`}</div>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Client } from './client'

export default function Page() {
return (
<h1>
<Client />
</h1>
)
}
1 change: 1 addition & 0 deletions test/e2e/app-dir/app-external/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
'conditional-exports-optout',
'dual-pkg-optout',
'transitive-external',
'esm',
],
}
1 change: 1 addition & 0 deletions test/e2e/app-dir/app-external/node_modules/esm/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions test/e2e/app-dir/app-external/node_modules/esm/package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions test/e2e/esm-externals/esm-externals.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ describe('esm-externals', () => {
// App dir
describe.each(['/server', '/client'])('app dir url %s', (url) => {
const expectedHtml = isTurbopack
? url === '/client'
? 'Hello Wrong+Wrong+Alternative'
: 'Hello World+World+World'
? 'Hello World+World+World'
: 'Hello World+World+Alternative'

const expectedText = isTurbopack
Expand Down
Loading