2022-09-10 01:35:12 +02:00
|
|
|
use std::{
|
|
|
|
path::Path,
|
|
|
|
process::{Child, Command, Stdio},
|
|
|
|
};
|
|
|
|
|
|
|
|
use anyhow::{anyhow, Context, Result};
|
|
|
|
use regex::Regex;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
bundlers::Bundler,
|
2022-09-12 21:34:26 +02:00
|
|
|
util::{
|
|
|
|
npm::{self, NpmPackage},
|
|
|
|
wait_for_match,
|
|
|
|
},
|
2022-09-10 01:35:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
pub struct Turbopack {
|
|
|
|
name: String,
|
|
|
|
path: String,
|
|
|
|
has_server_rendered_html: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Turbopack {
|
|
|
|
pub fn new(name: &str, path: &str, has_server_rendered_html: bool) -> Self {
|
|
|
|
Turbopack {
|
|
|
|
name: name.to_owned(),
|
|
|
|
path: path.to_owned(),
|
|
|
|
has_server_rendered_html,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Bundler for Turbopack {
|
|
|
|
fn get_name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get_path(&self) -> &str {
|
|
|
|
&self.path
|
|
|
|
}
|
|
|
|
|
|
|
|
fn has_server_rendered_html(&self) -> bool {
|
|
|
|
self.has_server_rendered_html
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prepare(&self, install_dir: &Path) -> Result<()> {
|
2022-09-12 21:34:26 +02:00
|
|
|
npm::install(
|
|
|
|
install_dir,
|
|
|
|
&[
|
|
|
|
NpmPackage::new("react-refresh", "^0.12.0"),
|
|
|
|
NpmPackage::new("@next/react-refresh-utils", "^12.2.5"),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
.context("failed to install from npm")?;
|
2022-09-10 01:35:12 +02:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn start_server(&self, test_dir: &Path) -> Result<(Child, String)> {
|
|
|
|
let mut proc = Command::new(std::env!("CARGO_BIN_EXE_next-dev"))
|
|
|
|
.args([
|
|
|
|
test_dir
|
|
|
|
.to_str()
|
|
|
|
.ok_or_else(|| anyhow!("failed to convert test directory path to string"))?,
|
|
|
|
"--no-open",
|
|
|
|
"--port",
|
|
|
|
"0",
|
|
|
|
])
|
|
|
|
.stdout(Stdio::piped())
|
|
|
|
.spawn()?;
|
|
|
|
|
|
|
|
// Wait for the devserver address to appear in stdout.
|
|
|
|
let addr = wait_for_match(
|
|
|
|
proc.stdout
|
|
|
|
.as_mut()
|
|
|
|
.ok_or_else(|| anyhow!("missing stdout"))?,
|
|
|
|
Regex::new("server listening on: (.*)")?,
|
|
|
|
)
|
|
|
|
.ok_or_else(|| anyhow!("failed to find devserver address"))?;
|
|
|
|
|
|
|
|
Ok((proc, addr))
|
|
|
|
}
|
|
|
|
}
|