Skip to content

Commit

Permalink
feat: support expanding urls in nexturbo (and fix static files)
Browse files Browse the repository at this point in the history
  • Loading branch information
ForsakenHarmony committed Sep 8, 2023
1 parent b5d7526 commit 1b8c152
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/next-swc/crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ pub struct NapiProjectOptions {

/// A map of environment variables to use when compiling code.
pub env: Vec<NapiEnvVar>,

/// The address of the dev server.
pub server_addr: String,
}

#[napi(object)]
Expand All @@ -90,6 +93,7 @@ impl From<NapiProjectOptions> for ProjectOptions {
.into_iter()
.map(|NapiEnvVar { name, value }| (name, value))
.collect(),
server_addr: val.server_addr,
}
}
}
Expand Down
24 changes: 16 additions & 8 deletions packages/next-swc/crates/next-api/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::path::MAIN_SEPARATOR;
use std::{net::SocketAddr, path::MAIN_SEPARATOR};

use anyhow::Result;
use indexmap::{map::Entry, IndexMap};
Expand Down Expand Up @@ -81,6 +81,9 @@ pub struct ProjectOptions {

/// Whether to watch the filesystem for file changes.
pub watch: bool,

/// The address of the dev server.
pub server_addr: String,
}

#[derive(Serialize, Deserialize, TraceRawVcs, PartialEq, Eq, ValueDebugFormat)]
Expand Down Expand Up @@ -122,6 +125,7 @@ impl ProjectContainer {
root_path: options.root_path.clone(),
project_path: options.project_path.clone(),
watch: options.watch,
server_addr: options.server_addr.parse()?,
next_config,
js_config,
env: Vc::upcast(env),
Expand Down Expand Up @@ -159,6 +163,10 @@ pub struct Project {
/// Whether to watch the filesystem for file changes.
watch: bool,

/// The address of the dev server.
#[turbo_tasks(trace_ignore)]
server_addr: SocketAddr,

/// Next config.
next_config: Vc<NextConfig>,

Expand Down Expand Up @@ -222,6 +230,11 @@ impl Project {
Ok(Vc::upcast(disk_fs))
}

#[turbo_tasks::function]
fn server_addr(&self) -> Vc<ServerAddr> {
ServerAddr::new(self.server_addr).cell()
}

#[turbo_tasks::function]
pub(super) fn node_root(self: Vc<Self>) -> Vc<FileSystemPath> {
self.node_fs().root().join(".next".to_string())
Expand Down Expand Up @@ -302,18 +315,13 @@ impl Project {
Ok(get_server_compile_time_info(
this.mode,
self.env(),
// TODO(alexkirsz) Fill this out.
ServerAddr::empty(),
self.server_addr(),
))
}

#[turbo_tasks::function]
pub(super) fn edge_compile_time_info(self: Vc<Self>) -> Vc<CompileTimeInfo> {
get_edge_compile_time_info(
self.project_path(),
// TODO(alexkirsz) Fill this out.
ServerAddr::empty(),
)
get_edge_compile_time_info(self.project_path(), self.server_addr())
}

#[turbo_tasks::function]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ pub fn get_server_chunking_context(
project_path,
node_root,
node_root.join("server/chunks".to_string()),
client_root.join("static/media".to_string()),
client_root.join("_next/static/media".to_string()),
environment,
)
.minify_type(MinifyType::NoMinify)
Expand Down
7 changes: 6 additions & 1 deletion packages/next/src/build/swc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -399,9 +399,14 @@ interface ProjectOptions {
env: Record<string, string>

/**
* Whether to watch he filesystem for file changes.
* Whether to watch the filesystem for file changes.
*/
watch: boolean

/**
* The address of the dev server.
*/
serverAddr: string
}

interface TurboEngineOptions {
Expand Down
1 change: 1 addition & 0 deletions packages/next/src/server/lib/router-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ export async function initialize(opts: {
nextConfig: config,
isCustomServer: opts.customServer,
turbo: !!process.env.TURBOPACK,
port: opts.port,
})
}

Expand Down
2 changes: 2 additions & 0 deletions packages/next/src/server/lib/router-utils/setup-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ type SetupOpts = {
ReturnType<typeof import('./filesystem').setupFsCheck>
>
nextConfig: NextConfigComplete
port: number
}

async function verifyTypeScript(opts: SetupOpts) {
Expand Down Expand Up @@ -203,6 +204,7 @@ async function startWatcher(opts: SetupOpts) {
jsConfig,
watch: true,
env: process.env as Record<string, string>,
serverAddr: `127.0.0.1:${opts.port}`,
})
const iter = project.entrypointsSubscribe()
const curEntries: Map<string, Route> = new Map()
Expand Down
1 change: 1 addition & 0 deletions test/development/basic/next-rs-api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ describe('next.rs api', () => {
? path.resolve(__dirname, '../../..')
: next.testDir,
watch: true,
serverAddr: `127.0.0.1:${next.appPort}`,
})
projectUpdateSubscription = project.updateInfoSubscribe()
})
Expand Down

0 comments on commit 1b8c152

Please sign in to comment.