Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 9, 2022
1 parent eedf8d9 commit 94bfd10
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,31 @@ import loadConfig from "next/dist/server/config";
import { PHASE_DEVELOPMENT_SERVER } from "next/dist/shared/lib/constants";

type IpcIncomingMessage = {
type: "loadNextConfig" | "loadPostcssConfig";
type: "loadNextConfig";
path: string;
};

type IpcOutgoingMessage = {
type: "javaScriptValue";
data: number[];
data: string;
};

const ipc = IPC as Ipc<IpcIncomingMessage, IpcOutgoingMessage>;

const CACHED_CONFIGS = new Map<string, any>();

(async () => {
while (true) {
const msg = await ipc.recv();

switch (msg.type) {
case "loadNextConfig": {
const nextConfig = await loadConfig(PHASE_DEVELOPMENT_SERVER, msg.path);
CACHED_CONFIGS.set(msg.path, nextConfig);
// @ts-expect-error
nextConfig.rewrites = await nextConfig.rewrites?.();
// @ts-expect-error
nextConfig.redirects = await nextConfig.redirects?.();
await ipc.send({
type: "javaScriptValue",
data: Array.from(Buffer.from(JSON.stringify(nextConfig))),
data: JSON.stringify(nextConfig),
});
break;
}
Expand Down
33 changes: 17 additions & 16 deletions crates/next-core/src/next_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,27 @@ impl NextConfigLoaderVc {
context: AssetContextVc,
chunking_context: ChunkingContextVc,
project_root: FileSystemPathVc,
output_root: FileSystemPathVc,
intermediate_output_path: FileSystemPathVc,
) -> Self {
let entry_module = EcmascriptModuleAssetVc::new(
VirtualAssetVc::new(
intermediate_output_path.join("next.js"),
next_js_file("entry/config/next.ts").into(),
)
.into(),
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::React { refresh: false },
EcmascriptInputTransform::TypeScript,
]),
context.environment(),
);
NextConfigLoader {
path: project_root,
entry_module: EcmascriptModuleAssetVc::new(
VirtualAssetVc::new(
output_root.join("next.js"),
next_js_file("read-config/next.ts").into(),
)
.into(),
context,
Value::new(EcmascriptModuleAssetType::Typescript),
EcmascriptInputTransformsVc::cell(vec![
EcmascriptInputTransform::React { refresh: false },
EcmascriptInputTransform::TypeScript,
]),
context.environment(),
),
entry_module,
chunking_context,
intermediate_output_path: output_root,
intermediate_output_path,
}
.cell()
}
Expand Down
13 changes: 0 additions & 13 deletions crates/turbopack-node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -646,19 +646,6 @@ async fn proxy_error(
.cell())
}

async fn eval_js_operation(
operation: &mut NodeJsOperation,
content: EvalJavaScriptOutgoingMessage,
) -> Result<Vec<u8>> {
operation.send(content).await?;
match operation.recv().await? {
EvalJavaScriptIncomingMessage::Error(err) => {
bail!(err.print(Default::default(), None).await?);
}
EvalJavaScriptIncomingMessage::JavaScriptValue { data } => Ok(data),
}
}

pub fn register() {
turbo_tasks::register();
turbo_tasks_fs::register();
Expand Down
27 changes: 19 additions & 8 deletions crates/turbopack-node/src/read_config.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,35 @@
use std::collections::HashMap;

use anyhow::{anyhow, Result};
use anyhow::{anyhow, bail, Result};
use turbo_tasks_fs::{rope::Rope, to_sys_path, FileSystemPathVc};
use turbopack_core::chunk::{ChunkGroupVc, ChunkingContextVc};
use turbopack_ecmascript::EcmascriptModuleAssetVc;

use crate::{
bootstrap::NodeJsBootstrapAsset, emit, eval_js_operation, pool::NodeJsPool,
EvalJavaScriptOutgoingMessage,
bootstrap::NodeJsBootstrapAsset, emit, pool::NodeJsPool, EvalJavaScriptIncomingMessage,
EvalJavaScriptOutgoingMessage, NodeJsOperation,
};

#[turbo_tasks::value(shared)]
#[derive(Clone)]
pub enum JavaScriptValue {
Value(Rope),
// TODO, support stream in the future
Stream(#[turbo_tasks(trace_ignore)] Vec<u8>),
}

#[turbo_tasks::value(transparent)]
pub struct JavaScriptValueArray(Vec<JavaScriptValueVc>);
async fn eval_js_operation(
operation: &mut NodeJsOperation,
content: EvalJavaScriptOutgoingMessage,
) -> Result<Vec<u8>> {
operation.send(content).await?;
match operation.recv().await? {
EvalJavaScriptIncomingMessage::Error(err) => {
bail!(err.print(Default::default(), None).await?);
}
EvalJavaScriptIncomingMessage::JavaScriptValue { data } => Ok(data),
}
}

#[turbo_tasks::value_trait]
pub trait JavaScriptConfig {
Expand All @@ -32,7 +43,6 @@ pub trait JavaScriptConfig {
to_sys_path(self.intermediate_output_path()).await?,
to_sys_path(self.intermediate_output_path().join("read-config.js")).await?,
) {
let pool = NodeJsPool::new(cwd, entrypoint, HashMap::new(), 1);
let bootstrap = NodeJsBootstrapAsset {
path: self.intermediate_output_path().join("read-config.js"),
chunk_group: ChunkGroupVc::from_chunk(
Expand All @@ -41,20 +51,21 @@ pub trait JavaScriptConfig {
),
};
emit(bootstrap.cell().into(), self.intermediate_output_path()).await?;
let pool = NodeJsPool::new(cwd, entrypoint, HashMap::new(), 1);
let mut operation = pool.operation().await?;
let output = eval_js_operation(
&mut operation,
EvalJavaScriptOutgoingMessage::LoadNextConfig {
path: to_sys_path(self.path())
.await?
.and_then(|p| p.to_str().map(|s| s.to_string()))
.ok_or_else(|| anyhow!("Invalid next.config.js path"))?,
.ok_or_else(|| anyhow!("Invalid config path"))?,
},
)
.await?;
Ok(JavaScriptValue::Value(output.into()).cell())
} else {
Err(anyhow!("can only render from a disk filesystem"))
panic!("can only render from a disk filesystem");
}
}
}

0 comments on commit 94bfd10

Please sign in to comment.