Unverified Commit 35d300e5 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

Add dedicated client entry for Turbopack builds (#65824)

Previously with Turbopack, we would attempt to establish a websocket
connection to the HMR server, even when running production builds. This
adds `next-turbopack.ts`, which includes the necessary runtime code for
Turbopack, but does not include HMR code.

Test Plan: `TURBOPACK=1 TURBOPACK_BUILD=1 pnpm next build
test/e2e/app-dir/app-middleware`

---------
Co-authored-by: 's avatarTim Neutkens <tim@timneutkens.nl>
parent 22f822eb
......@@ -3,6 +3,7 @@ use indexmap::IndexMap;
use next_core::{
all_assets_from_entries, create_page_loader_entry_module, get_asset_path_from_pathname,
get_edge_resolve_options_context,
mode::NextMode,
next_client::{
get_client_module_options_context, get_client_resolve_options_context,
get_client_runtime_entries, ClientContextType, RuntimeEntries,
......@@ -645,7 +646,11 @@ impl PageEndpoint {
.join("_".to_string()),
)),
Request::parse(Value::new(Pattern::Constant(
"next/dist/client/next-dev-turbopack.js".to_string(),
match *this.pages_project.project().next_mode().await? {
NextMode::Development => "next/dist/client/next-dev-turbopack.js",
NextMode::Build => "next/dist/client/next-turbopack.js",
}
.to_string(),
))),
Value::new(EcmaScriptModulesReferenceSubType::Undefined),
IssueSeverity::Error.cell(),
......@@ -653,7 +658,7 @@ impl PageEndpoint {
)
.first_module()
.await?
.context("expected next/dist/client/next-dev-turbopack.js to resolve to a module")?;
.context("expected Next.js client runtime to resolve to a module")?;
let Some(client_main_module) =
Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(client_main_module).await?
......
// A client-side entry point for Turbopack builds. Includes logic to load chunks,
// but does not include development-time features like hot module reloading.
// TODO: Remove use of `any` type.
import { initialize, version, router, emitter, hydrate } from './'
// TODO: This seems necessary, but is a module in the `dev` directory.
import { displayContent } from './dev/fouc'
window.next = {
version: `${version}-turbo`,
// router is initialized later so it has to be live-binded
get router() {
return router
},
emitter,
}
;(self as any).__next_set_public_path__ = () => {}
;(self as any).__webpack_hash__ = ''
// for the page loader
declare let __turbopack_load__: any
initialize({})
.then(() => {
// for the page loader
;(self as any).__turbopack_load_page_chunks__ = (
page: string,
chunksData: any
) => {
const chunkPromises = chunksData.map(__turbopack_load__)
Promise.all(chunkPromises).catch((err) =>
console.error('failed to load chunks for page ' + page, err)
)
}
return hydrate({ beforeRender: displayContent })
})
.catch((err) => {
console.error('Error was not caught', err)
})
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