Skip to content

Commit

Permalink
Merge branch 'feedthejim/bundle-react' of github.com:vercel/next.js i…
Browse files Browse the repository at this point in the history
…nto feedthejim/bundle-react
  • Loading branch information
ijjk committed Sep 15, 2023
2 parents 485f0ae + f9f6945 commit e2db79b
Show file tree
Hide file tree
Showing 26 changed files with 847 additions and 457 deletions.
539 changes: 390 additions & 149 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@ next-transform-dynamic = { path = "packages/next-swc/crates/next-transform-dynam
next-transform-strip-page-exports = { path = "packages/next-swc/crates/next-transform-strip-page-exports" }

# SWC crates
swc_core = { version = "=0.79.55", features = ["ecma_loader_lru", "ecma_loader_parking_lot"] }
testing = { version = "0.33.21" }
swc_core = { version = "0.83.12", features = [
"ecma_loader_lru",
"ecma_loader_parking_lot",
] }
testing = { version = "0.34.1" }

# Turbo crates
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230914.5" }
turbopack-binding = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230915.2" }
# [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-230914.5" }
turbo-tasks = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230915.2" }
# [TODO]: need to refactor embed_directory! macro usage in next-core
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230914.5" }
turbo-tasks-fs = { git = "https://github.com/vercel/turbo.git", tag = "turbopack-230915.2" }

# General Deps

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,8 @@
"@picocss/pico": "1.5.10",
"@svgr/webpack": "5.5.0",
"@swc/cli": "0.1.55",
"@swc/core": "1.3.55",
"@swc/helpers": "0.5.1",
"@swc/core": "1.3.85",
"@swc/helpers": "0.5.2",
"@testing-library/jest-dom": "6.1.2",
"@testing-library/react": "13.0.0",
"@types/cheerio": "0.22.16",
Expand Down
5 changes: 3 additions & 2 deletions packages/next-swc/crates/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub struct TransformOptions {
pub server_components: Option<react_server_components::Config>,

#[serde(default)]
pub styled_jsx: bool,
pub styled_jsx: Option<turbopack_binding::swc::custom_transform::styled_jsx::visitor::Config>,

#[serde(default)]
pub styled_components:
Expand Down Expand Up @@ -202,11 +202,12 @@ where
)),
_ => Either::Right(noop()),
},
if opts.styled_jsx {
if let Some(config) = opts.styled_jsx {
Either::Left(
turbopack_binding::swc::custom_transform::styled_jsx::visitor::styled_jsx(
cm.clone(),
file.name.clone(),
config,
),
)
} else {
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/core/src/optimize_barrel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ impl Fold for OptimizeBarrel {
.into(),
raw: None,
}),
asserts: None,
with: None,
type_only: false,
})));
}
Expand Down
6 changes: 3 additions & 3 deletions packages/next-swc/crates/core/src/server_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
raw: None,
}),
type_only: false,
asserts: None,
with: None,
})));
}

Expand Down Expand Up @@ -1001,7 +1001,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
raw: None,
}),
type_only: false,
asserts: None,
with: None,
})));
new.push(ModuleItem::Stmt(Stmt::Expr(ExprStmt {
span: DUMMY_SP,
Expand Down Expand Up @@ -1076,7 +1076,7 @@ impl<C: Comments> VisitMut for ServerActions<C> {
raw: None,
}),
type_only: false,
asserts: None,
with: None,
})));
// Make it the first item
new.rotate_right(1);
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/core/tests/full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn test(input: &Path, minify: bool) {
is_server: false,
server_components: None,
styled_components: Some(assert_json("{}")),
styled_jsx: true,
styled_jsx: Some(assert_json("{}")),
remove_console: None,
react_remove_properties: None,
relay: None,
Expand Down
2 changes: 1 addition & 1 deletion packages/next-swc/crates/core/tests/full/example/output.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ import t from "other";
}())[0];
export var __N_SSG = !0;
export default function e() {
return React.createElement("div", null);
return /*#__PURE__*/ React.createElement("div", null);
}
181 changes: 98 additions & 83 deletions packages/next-swc/crates/next-api/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ use turbopack_binding::{
turbopack::{
core::{
asset::{Asset, AssetContent},
changed::any_content_changed_of_output_assets,
chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets},
file_source::FileSource,
output::{OutputAsset, OutputAssets},
Expand Down Expand Up @@ -487,15 +486,19 @@ impl AppEndpoint {
async fn output(self: Vc<Self>) -> Result<Vc<AppEndpointOutput>> {
let this = self.await?;

let (app_entry, ty) = match this.ty {
AppEndpointType::Page { ty: _, loader_tree } => {
(self.app_page_entry(loader_tree), "page")
}
let (app_entry, ty, ssr_and_client) = match this.ty {
AppEndpointType::Page { ty, loader_tree } => (
self.app_page_entry(loader_tree),
"page",
matches!(ty, AppPageEndpointType::Html),
),
// NOTE(alexkirsz) For routes, technically, a lot of the following code is not needed,
// as we know we won't have any client references. However, for now, for simplicity's
// sake, we just do the same thing as for pages.
AppEndpointType::Route { path } => (self.app_route_entry(path), "route"),
AppEndpointType::Metadata { metadata } => (self.app_metadata_entry(metadata), "route"),
AppEndpointType::Route { path } => (self.app_route_entry(path), "route", false),
AppEndpointType::Metadata { metadata } => {
(self.app_metadata_entry(metadata), "route", false)
}
};

let node_root = this.app_project.project().node_root();
Expand Down Expand Up @@ -562,82 +565,86 @@ impl AppEndpoint {
.entry(Vc::upcast(app_entry.rsc_entry))
.await?;

let client_references_chunks = get_app_client_references_chunks(
client_reference_types,
this.app_project.project().client_chunking_context(),
this.app_project.project().ssr_chunking_context(),
);
let client_references_chunks_ref = client_references_chunks.await?;

let mut entry_client_chunks = vec![];
// TODO(alexkirsz) In which manifest does this go?
let mut entry_ssr_chunks = vec![];
for client_reference in app_entry_client_references.iter() {
let client_reference_chunks = client_references_chunks_ref
.get(client_reference.ty())
.expect("client reference should have corresponding chunks");
entry_client_chunks
.extend(client_reference_chunks.client_chunks.await?.iter().copied());
entry_ssr_chunks.extend(client_reference_chunks.ssr_chunks.await?.iter().copied());
}
if ssr_and_client {
let client_references_chunks = get_app_client_references_chunks(
client_reference_types,
this.app_project.project().client_chunking_context(),
this.app_project.project().ssr_chunking_context(),
);
let client_references_chunks_ref = client_references_chunks.await?;

let mut entry_client_chunks = vec![];
// TODO(alexkirsz) In which manifest does this go?
let mut entry_ssr_chunks = vec![];
for client_reference in app_entry_client_references.iter() {
let client_reference_chunks = client_references_chunks_ref
.get(client_reference.ty())
.expect("client reference should have corresponding chunks");
entry_client_chunks
.extend(client_reference_chunks.client_chunks.await?.iter().copied());
entry_ssr_chunks.extend(client_reference_chunks.ssr_chunks.await?.iter().copied());
}

client_assets.extend(entry_client_chunks.iter().copied());
server_assets.extend(entry_ssr_chunks.iter().copied());
client_assets.extend(entry_client_chunks.iter().copied());
server_assets.extend(entry_ssr_chunks.iter().copied());

let entry_client_chunks_paths = entry_client_chunks
.iter()
.map(|chunk| chunk.ident().path())
.try_join()
.await?;
let mut entry_client_chunks_paths: Vec<_> = entry_client_chunks_paths
.iter()
.map(|path| {
client_relative_path_ref
.get_path_to(path)
.expect("asset path should be inside client root")
.to_string()
})
.collect();
entry_client_chunks_paths.extend(client_shared_chunks_paths.iter().cloned());
let entry_client_chunks_paths = entry_client_chunks
.iter()
.map(|chunk| chunk.ident().path())
.try_join()
.await?;
let mut entry_client_chunks_paths: Vec<_> = entry_client_chunks_paths
.iter()
.map(|path| {
client_relative_path_ref
.get_path_to(path)
.expect("asset path should be inside client root")
.to_string()
})
.collect();
entry_client_chunks_paths.extend(client_shared_chunks_paths.iter().cloned());

let app_build_manifest = AppBuildManifest {
pages: [(app_entry.original_name.clone(), entry_client_chunks_paths)]
.into_iter()
.collect(),
};
let manifest_path_prefix = get_asset_prefix_from_pathname(&app_entry.pathname);
let app_build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/app{manifest_path_prefix}/{ty}/app-build-manifest.json",
)),
AssetContent::file(
File::from(serde_json::to_string_pretty(&app_build_manifest)?).into(),
),
));
server_assets.push(app_build_manifest_output);
let app_build_manifest = AppBuildManifest {
pages: [(app_entry.original_name.clone(), entry_client_chunks_paths)]
.into_iter()
.collect(),
};
let manifest_path_prefix = get_asset_prefix_from_pathname(&app_entry.pathname);
let app_build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/app{manifest_path_prefix}/{ty}/app-build-manifest.json",
)),
AssetContent::file(
File::from(serde_json::to_string_pretty(&app_build_manifest)?).into(),
),
));
server_assets.push(app_build_manifest_output);

let build_manifest = BuildManifest {
root_main_files: client_shared_chunks_paths,
..Default::default()
};
let build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/app{manifest_path_prefix}/{ty}/build-manifest.json",
)),
AssetContent::file(File::from(serde_json::to_string_pretty(&build_manifest)?).into()),
));
server_assets.push(build_manifest_output);

let entry_manifest = ClientReferenceManifest::build_output(
node_root,
client_relative_path,
app_entry.original_name.clone(),
client_references,
client_references_chunks,
this.app_project.project().client_chunking_context(),
Vc::upcast(this.app_project.project().ssr_chunking_context()),
);
server_assets.push(entry_manifest);
let build_manifest = BuildManifest {
root_main_files: client_shared_chunks_paths,
..Default::default()
};
let build_manifest_output = Vc::upcast(VirtualOutputAsset::new(
node_root.join(format!(
"server/app{manifest_path_prefix}/{ty}/build-manifest.json",
)),
AssetContent::file(
File::from(serde_json::to_string_pretty(&build_manifest)?).into(),
),
));
server_assets.push(build_manifest_output);

let entry_manifest = ClientReferenceManifest::build_output(
node_root,
client_relative_path,
app_entry.original_name.clone(),
client_references,
client_references_chunks,
this.app_project.project().client_chunking_context(),
Vc::upcast(this.app_project.project().ssr_chunking_context()),
);
server_assets.push(entry_manifest);
}

fn create_app_paths_manifest(
node_root: Vc<FileSystemPath>,
Expand Down Expand Up @@ -897,13 +904,21 @@ impl Endpoint for AppEndpoint {
}

#[turbo_tasks::function]
fn server_changed(self: Vc<Self>) -> Vc<Completion> {
any_content_changed_of_output_assets(self.output().server_assets())
async fn server_changed(self: Vc<Self>) -> Result<Vc<Completion>> {
Ok(self
.await?
.app_project
.project()
.server_changed(self.output().server_assets()))
}

#[turbo_tasks::function]
fn client_changed(self: Vc<Self>) -> Vc<Completion> {
any_content_changed_of_output_assets(self.output().client_assets())
async fn client_changed(self: Vc<Self>) -> Result<Vc<Completion>> {
Ok(self
.await?
.app_project
.project()
.client_changed(self.output().client_assets()))
}
}

Expand Down
5 changes: 2 additions & 3 deletions packages/next-swc/crates/next-api/src/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use turbopack_binding::{
turbopack::{
core::{
asset::AssetContent,
changed::any_content_changed_of_output_assets,
chunk::{ChunkableModule, ChunkingContext},
context::AssetContext,
module::Module,
Expand Down Expand Up @@ -209,8 +208,8 @@ impl Endpoint for MiddlewareEndpoint {
}

#[turbo_tasks::function]
fn server_changed(self: Vc<Self>) -> Vc<Completion> {
any_content_changed_of_output_assets(self.output_assets())
async fn server_changed(self: Vc<Self>) -> Result<Vc<Completion>> {
Ok(self.await?.project.server_changed(self.output_assets()))
}

#[turbo_tasks::function]
Expand Down
17 changes: 12 additions & 5 deletions packages/next-swc/crates/next-api/src/pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ use turbopack_binding::{
build::BuildChunkingContext,
core::{
asset::AssetContent,
changed::any_content_changed_of_output_assets,
chunk::{ChunkableModule, ChunkingContext, EvaluatableAssets},
context::AssetContext,
file_source::FileSource,
Expand Down Expand Up @@ -944,13 +943,21 @@ impl Endpoint for PageEndpoint {
}

#[turbo_tasks::function]
fn server_changed(self: Vc<Self>) -> Vc<Completion> {
any_content_changed_of_output_assets(self.output().server_assets())
async fn server_changed(self: Vc<Self>) -> Result<Vc<Completion>> {
Ok(self
.await?
.pages_project
.project()
.server_changed(self.output().server_assets()))
}

#[turbo_tasks::function]
fn client_changed(self: Vc<Self>) -> Vc<Completion> {
any_content_changed_of_output_assets(self.output().client_assets())
async fn client_changed(self: Vc<Self>) -> Result<Vc<Completion>> {
Ok(self
.await?
.pages_project
.project()
.client_changed(self.output().client_assets()))
}
}

Expand Down
Loading

0 comments on commit e2db79b

Please sign in to comment.