Skip to content

Commit

Permalink
chore: move output.rs to the types folder (#506)
Browse files Browse the repository at this point in the history
<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info about the "what" this PR is solving -->

### Test Plan

<!-- e.g. is there anything you'd like reviewers to focus on? -->

---
  • Loading branch information
hyf0 committed Mar 9, 2024
1 parent 505aa84 commit 558008b
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 88 deletions.
14 changes: 8 additions & 6 deletions crates/rolldown_binding/src/bundler.rs
Expand Up @@ -5,8 +5,10 @@ use rolldown_fs::OsFileSystem;
use tracing::instrument;

use crate::{
options::InputOptions, options::OutputOptions, output::Outputs,
utils::try_init_custom_trace_subscriber, NAPI_ENV,
options::{InputOptions, OutputOptions},
types::binding_outputs::BindingOutputs,
utils::try_init_custom_trace_subscriber,
NAPI_ENV,
};

#[napi]
Expand All @@ -23,12 +25,12 @@ impl Bundler {
}

#[napi]
pub async fn write(&self, opts: OutputOptions) -> napi::Result<Outputs> {
pub async fn write(&self, opts: OutputOptions) -> napi::Result<BindingOutputs> {
self.write_impl(opts).await
}

#[napi]
pub async fn generate(&self, opts: OutputOptions) -> napi::Result<Outputs> {
pub async fn generate(&self, opts: OutputOptions) -> napi::Result<BindingOutputs> {
self.generate_impl(opts).await
}

Expand Down Expand Up @@ -67,7 +69,7 @@ impl Bundler {

#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn write_impl(&self, output_opts: OutputOptions) -> napi::Result<Outputs> {
pub async fn write_impl(&self, output_opts: OutputOptions) -> napi::Result<BindingOutputs> {
let mut bundler_core = self.inner.try_lock().map_err(|_| {
napi::Error::from_reason("Failed to lock the bundler. Is another operation in progress?")
})?;
Expand All @@ -88,7 +90,7 @@ impl Bundler {

#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn generate_impl(&self, output_opts: OutputOptions) -> napi::Result<Outputs> {
pub async fn generate_impl(&self, output_opts: OutputOptions) -> napi::Result<BindingOutputs> {
let mut bundler_core = self.inner.try_lock().map_err(|_| {
napi::Error::from_reason("Failed to lock the bundler. Is another operation in progress?")
})?;
Expand Down
1 change: 0 additions & 1 deletion crates/rolldown_binding/src/lib.rs
Expand Up @@ -4,7 +4,6 @@ static ALLOC: mimalloc::MiMalloc = mimalloc::MiMalloc;

pub mod bundler;
pub mod options;
pub mod output;
pub mod types;
pub mod utils;
scoped_tls::scoped_thread_local!(static NAPI_ENV: napi::Env);
6 changes: 3 additions & 3 deletions crates/rolldown_binding/src/options/plugin/plugin_adapter.rs
@@ -1,7 +1,7 @@
use std::borrow::Cow;

use crate::utils::JsCallback;
use crate::{output::Outputs, utils::napi_error_ext::NapiErrorExt};
use crate::{types::binding_outputs::BindingOutputs, utils::napi_error_ext::NapiErrorExt};
use derivative::Derivative;
use rolldown_plugin::Plugin;

Expand All @@ -17,8 +17,8 @@ pub type LoadCallback = JsCallback<(String,), Option<SourceResult>>;
pub type TransformCallback = JsCallback<(String, String), Option<SourceResult>>;
pub type BuildEndCallback = JsCallback<(Option<String>,), ()>;
pub type RenderChunkCallback = JsCallback<(String, RenderedChunk), Option<HookRenderChunkOutput>>;
pub type GenerateBundleCallback = JsCallback<(Outputs, bool), Option<HookRenderChunkOutput>>;
pub type WriteBundleCallback = JsCallback<(Outputs,), ()>;
pub type GenerateBundleCallback = JsCallback<(BindingOutputs, bool), Option<HookRenderChunkOutput>>;
pub type WriteBundleCallback = JsCallback<(BindingOutputs,), ()>;

#[derive(Derivative)]
#[derivative(Debug)]
Expand Down
78 changes: 0 additions & 78 deletions crates/rolldown_binding/src/output.rs

This file was deleted.

17 changes: 17 additions & 0 deletions crates/rolldown_binding/src/types/binding_output_asset.rs
@@ -0,0 +1,17 @@
use derivative::Derivative;
use serde::Deserialize;

#[napi_derive::napi(object)]
#[derive(Deserialize, Default, Derivative)]
#[serde(rename_all = "camelCase")]
#[derivative(Debug)]
pub struct BindingOutputAsset {
pub file_name: String,
pub source: String,
}

impl From<Box<rolldown_common::OutputAsset>> for BindingOutputAsset {
fn from(chunk: Box<rolldown_common::OutputAsset>) -> Self {
Self { source: chunk.source, file_name: chunk.file_name }
}
}
40 changes: 40 additions & 0 deletions crates/rolldown_binding/src/types/binding_output_chunk.rs
@@ -0,0 +1,40 @@
use std::collections::HashMap;

use derivative::Derivative;
use serde::Deserialize;

use crate::types::binding_rendered_module::BindingRenderedModule;

#[napi_derive::napi(object)]
#[derive(Deserialize, Default, Derivative)]
#[serde(rename_all = "camelCase")]
#[derivative(Debug)]
pub struct BindingOutputChunk {
// PreRenderedChunk
pub is_entry: bool,
pub is_dynamic_entry: bool,
pub facade_module_id: Option<String>,
pub module_ids: Vec<String>,
pub exports: Vec<String>,
// RenderedChunk
pub file_name: String,
#[serde(skip_deserializing)]
pub modules: HashMap<String, BindingRenderedModule>,
// OutputChunk
pub code: String,
}

impl From<Box<rolldown_common::OutputChunk>> for BindingOutputChunk {
fn from(chunk: Box<rolldown_common::OutputChunk>) -> Self {
Self {
code: chunk.code,
file_name: chunk.file_name,
is_entry: chunk.is_entry,
is_dynamic_entry: chunk.is_dynamic_entry,
facade_module_id: chunk.facade_module_id,
modules: chunk.modules.into_iter().map(|(key, value)| (key, value.into())).collect(),
exports: chunk.exports,
module_ids: chunk.module_ids,
}
}
}
27 changes: 27 additions & 0 deletions crates/rolldown_binding/src/types/binding_outputs.rs
@@ -0,0 +1,27 @@
use derivative::Derivative;
use serde::Deserialize;

use super::{binding_output_asset::BindingOutputAsset, binding_output_chunk::BindingOutputChunk};

#[napi_derive::napi(object)]
#[derive(Deserialize, Default, Derivative)]
#[serde(rename_all = "camelCase")]
#[derivative(Debug)]
pub struct BindingOutputs {
pub chunks: Vec<BindingOutputChunk>,
pub assets: Vec<BindingOutputAsset>,
}

impl From<Vec<rolldown_common::Output>> for BindingOutputs {
fn from(outputs: Vec<rolldown_common::Output>) -> Self {
let mut chunks: Vec<BindingOutputChunk> = vec![];
let mut assets: Vec<BindingOutputAsset> = vec![];

outputs.into_iter().for_each(|o| match o {
rolldown_common::Output::Chunk(chunk) => chunks.push(chunk.into()),
rolldown_common::Output::Asset(asset) => assets.push(asset.into()),
});

Self { chunks, assets }
}
}
3 changes: 3 additions & 0 deletions crates/rolldown_binding/src/types/mod.rs
@@ -1 +1,4 @@
pub mod binding_output_asset;
pub mod binding_output_chunk;
pub mod binding_outputs;
pub mod binding_rendered_module;

0 comments on commit 558008b

Please sign in to comment.