Skip to content

Commit

Permalink
Fix Next.js 11 React version (vercel/turborepo#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz authored Sep 8, 2022
1 parent 82e0ae4 commit 9ae093c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 29 deletions.
86 changes: 62 additions & 24 deletions crates/next-dev/benches/bundlers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::{
fmt::Display,
fs::{self, File},
io::{self, BufRead, BufReader, Write},
path::Path,
Expand All @@ -7,13 +8,15 @@ use std::{

use anyhow::{anyhow, Context, Result};
use regex::Regex;
use tempfile::TempDir;

pub trait Bundler {
fn get_name(&self) -> &str;
fn get_path(&self) -> &str {
"/"
}
fn react_version(&self) -> &str {
"^18.2.0"
}
fn prepare(&self, _template_dir: &Path) -> Result<()> {
Ok(())
}
Expand Down Expand Up @@ -128,19 +131,11 @@ impl Bundler for Parcel {
}
}

struct Vite {
install_dir: TempDir,
}
struct Vite {}

impl Vite {
fn new() -> Result<Self> {
// Manage our own installation and avoid `npm exec`, `npx`, etc. to avoid their
// overhead influencing benchmarks.
let install_dir = tempfile::tempdir()?;
install_from_npm(install_dir.path(), "vite", "3.0.9")
.context("failed to install `vite` module")?;

Ok(Vite { install_dir })
fn new() -> Self {
Vite {}
}
}

Expand All @@ -149,12 +144,16 @@ impl Bundler for Vite {
"Vite CSR"
}

fn prepare(&self, install_dir: &Path) -> Result<()> {
install_from_npm(install_dir, "vite", "3.0.9")
.context("failed to install `vite` module")?;
Ok(())
}

fn start_server(&self, test_dir: &Path) -> Result<(Child, String)> {
let mut proc = Command::new("node")
.args([
(self
.install_dir
.path()
(test_dir
.join("node_modules")
.join("vite")
.join("bin")
Expand Down Expand Up @@ -183,16 +182,17 @@ impl Bundler for Vite {
}
}

#[derive(Debug)]
struct NextJs {
version: u32,
version: NextJsVersion,
name: String,
}

impl NextJs {
fn new(version: u32) -> Self {
fn new(version: NextJsVersion) -> Self {
Self {
name: format!("{version} SSR"),
version,
name: format!("Next.js {version} SSR"),
}
}
}
Expand All @@ -206,8 +206,12 @@ impl Bundler for NextJs {
"/page"
}

fn react_version(&self) -> &str {
self.version.react_version()
}

fn prepare(&self, install_dir: &Path) -> Result<()> {
install_from_npm(install_dir, "next", &format!("^{}", self.version))
install_from_npm(install_dir, "next", self.version.version())
.context("failed to install `next` module")?;
Ok(())
}
Expand Down Expand Up @@ -251,7 +255,41 @@ impl Bundler for NextJs {
}
}

pub fn get_bundlers() -> Result<Vec<Box<dyn Bundler>>> {
#[derive(Debug)]
enum NextJsVersion {
V11,
V12,
}

impl Display for NextJsVersion {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
NextJsVersion::V11 => write!(f, "Next.js 11"),
NextJsVersion::V12 => write!(f, "Next.js 12"),
}
}
}

impl NextJsVersion {
/// Returns the version of Next.js to install from npm.
pub fn version(&self) -> &'static str {
match self {
NextJsVersion::V11 => "^11",
NextJsVersion::V12 => "^12",
}
}

/// Returns the version of React to install from npm alongside this version
/// of Next.js.
pub fn react_version(&self) -> &'static str {
match self {
NextJsVersion::V11 => "^17.0.2",
NextJsVersion::V12 => "^18.2.0",
}
}
}

pub fn get_bundlers() -> Vec<Box<dyn Bundler>> {
let config = std::env::var("TURBOPACK_BENCH_BUNDLERS").ok();
let mut turbopack = false;
let mut others = false;
Expand All @@ -274,12 +312,12 @@ pub fn get_bundlers() -> Result<Vec<Box<dyn Bundler>>> {

if others {
bundlers.push(Box::new(Parcel {}));
bundlers.push(Box::new(Vite::new()?));
bundlers.push(Box::new(NextJs::new(12)));
bundlers.push(Box::new(NextJs::new(11)));
bundlers.push(Box::new(Vite::new()));
bundlers.push(Box::new(NextJs::new(NextJsVersion::V12)));
bundlers.push(Box::new(NextJs::new(NextJsVersion::V11)));
}

Ok(bundlers)
bundlers
}

fn wait_for_match(stdout: &mut ChildStdout, re: Regex) -> Option<String> {
Expand Down
12 changes: 7 additions & 5 deletions crates/next-dev/benches/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use tokio::{
time::{sleep, timeout},
};
use tungstenite::{error::ProtocolError::ResetWithoutClosingHandshake, Error::Protocol};
use turbopack_create_test_app::test_app_builder::{TestApp, TestAppBuilder};
use turbopack_create_test_app::test_app_builder::{PackageJsonConfig, TestApp, TestAppBuilder};
use url::Url;

mod bundlers;
Expand Down Expand Up @@ -135,7 +135,7 @@ fn bench_startup_internal(mut g: BenchmarkGroup<WallTime>, wait_for_hydration: b
let runtime = Runtime::new().unwrap();
let browser = &runtime.block_on(create_browser());

for bundler in retry_default((), |_| get_bundlers()).expect("failed to get bundlers") {
for bundler in get_bundlers() {
for module_count in get_module_counts() {
let input = (bundler.as_ref(), module_count);
g.bench_with_input(
Expand Down Expand Up @@ -178,7 +178,7 @@ fn bench_simple_file_change(c: &mut Criterion) {
let runtime = Runtime::new().unwrap();
let browser = &runtime.block_on(create_browser());

for bundler in retry_default((), |_| get_bundlers()).expect("failed to get bundlers") {
for bundler in get_bundlers() {
for module_count in get_module_counts() {
let input = (bundler.as_ref(), module_count);
g.bench_with_input(
Expand Down Expand Up @@ -265,7 +265,7 @@ fn bench_restart(c: &mut Criterion) {
let runtime = Runtime::new().unwrap();
let browser = &runtime.block_on(create_browser());

for bundler in retry_default((), |_| get_bundlers()).expect("failed to get bundlers") {
for bundler in get_bundlers() {
for module_count in get_module_counts() {
let input = (bundler.as_ref(), module_count);

Expand Down Expand Up @@ -512,7 +512,9 @@ fn build_test(module_count: usize, bundler: &dyn Bundler) -> TestApp {
let test_app = TestAppBuilder {
module_count,
directories_count: module_count / 20,
package_json: true,
package_json: Some(PackageJsonConfig {
react_version: bundler.react_version().to_string(),
}),
..Default::default()
}
.build()
Expand Down

0 comments on commit 9ae093c

Please sign in to comment.