Skip to content

Commit

Permalink
feat: add OutputAsset (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Nov 13, 2023
1 parent 8fcfda3 commit 42420d7
Show file tree
Hide file tree
Showing 16 changed files with 163 additions and 73 deletions.
7 changes: 0 additions & 7 deletions crates/rolldown/src/bundler/bundle/asset.rs

This file was deleted.

9 changes: 5 additions & 4 deletions crates/rolldown/src/bundler/bundle/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use std::{borrow::Cow, hash::BuildHasherDefault};

use super::asset::OutputChunk;
use super::output::Output;
use crate::bundler::{
bundle::output::OutputChunk,
chunk::{
chunk::{Chunk, ChunkSymbolExporter, CrossChunkImportItem},
ChunkId, ChunksVec,
Expand Down Expand Up @@ -264,7 +265,7 @@ impl<'a> Bundle<'a> {
ChunkGraph { chunks, module_to_chunk }
}

pub fn generate(&mut self, _input_options: &'a InputOptions) -> Vec<OutputChunk> {
pub fn generate(&mut self, _input_options: &'a InputOptions) -> Vec<Output> {
use rayon::prelude::*;
let mut chunk_graph = self.generate_chunks();

Expand All @@ -287,14 +288,14 @@ impl<'a> Bundle<'a> {
.map(|(_chunk_id, c)| {
let content = c.render(self.graph, &chunk_graph, self.output_options).unwrap();

OutputChunk {
Output::Chunk(Box::new(OutputChunk {
file_name: c.file_name.clone().unwrap(),
code: content,
is_entry: c.entry_module.is_some(),
facade_module_id: c
.entry_module
.map(|id| self.graph.modules[id].expect_normal().resource_id.prettify().to_string()),
}
}))
})
.collect::<Vec<_>>();

Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/bundle/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod asset;
#[allow(clippy::module_inception)]
pub mod bundle;
pub mod output;
35 changes: 35 additions & 0 deletions crates/rolldown/src/bundler/bundle/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#[derive(Debug)]
pub struct OutputChunk {
pub file_name: String,
pub code: String,
pub is_entry: bool,
pub facade_module_id: Option<String>,
}

#[derive(Debug)]
pub struct OutputAsset {
pub file_name: String,
pub source: String,
}

#[derive(Debug)]
pub enum Output {
Chunk(Box<OutputChunk>),
Asset(Box<OutputAsset>),
}

impl Output {
pub fn file_name(&self) -> &str {
match self {
Self::Chunk(chunk) => &chunk.file_name,
Self::Asset(asset) => &asset.file_name,
}
}

pub fn content(&self) -> &str {
match self {
Self::Chunk(chunk) => &chunk.code,
Self::Asset(asset) => &asset.source,
}
}
}
18 changes: 7 additions & 11 deletions crates/rolldown/src/bundler/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rolldown_fs::FileSystemExt;
use sugar_path::AsPath;

use super::{
bundle::asset::OutputChunk,
bundle::output::Output,
graph::graph::Graph,
plugin_driver::{PluginDriver, SharedPluginDriver},
};
Expand All @@ -32,7 +32,7 @@ impl<T: FileSystemExt + Default + 'static> Bundler<T> {
Self { input_options, plugin_driver: Arc::new(PluginDriver::new(plugins)), fs: Arc::new(fs) }
}

pub async fn write(&mut self, output_options: OutputOptions) -> BuildResult<Vec<OutputChunk>> {
pub async fn write(&mut self, output_options: OutputOptions) -> BuildResult<Vec<Output>> {
let dir =
self.input_options.cwd.as_path().join(&output_options.dir).to_string_lossy().to_string();

Expand All @@ -46,29 +46,25 @@ impl<T: FileSystemExt + Default + 'static> Bundler<T> {
)
});
for chunk in &assets {
let dest = dir.as_path().join(&chunk.file_name);
let dest = dir.as_path().join(chunk.file_name());
if let Some(p) = dest.parent() {
if !self.fs.exists(p) {
self.fs.create_dir_all(p).unwrap();
}
};
self.fs.write(dest.as_path(), chunk.code.as_bytes()).unwrap_or_else(|_| {
panic!("Failed to write file in {:?}", dir.as_path().join(&chunk.file_name))
self.fs.write(dest.as_path(), chunk.content().as_bytes()).unwrap_or_else(|_| {
panic!("Failed to write file in {:?}", dir.as_path().join(chunk.file_name()))
});
}

Ok(assets)
}

pub async fn generate(&mut self, output_options: OutputOptions) -> BuildResult<Vec<OutputChunk>> {
pub async fn generate(&mut self, output_options: OutputOptions) -> BuildResult<Vec<Output>> {
self.build(output_options, Arc::clone(&self.fs)).await
}

async fn build(
&mut self,
output_options: OutputOptions,
fs: Arc<T>,
) -> BuildResult<Vec<OutputChunk>> {
async fn build(&mut self, output_options: OutputOptions, fs: Arc<T>) -> BuildResult<Vec<Output>> {
tracing::trace!("InputOptions {:#?}", self.input_options);
tracing::trace!("OutputOptions: {output_options:#?}",);

Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub(crate) type SharedResolver<T> = Arc<Resolver<T>>;

pub use crate::{
bundler::{
bundle::asset::OutputChunk,
bundle::output::{Output, OutputAsset, OutputChunk},
bundler::Bundler,
options::{
file_name_template::FileNameTemplate,
Expand Down
12 changes: 6 additions & 6 deletions crates/rolldown/tests/common/case.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{borrow::Cow, path::Path};

use rolldown::OutputChunk;
use rolldown::Output;
use rolldown_error::BuildError;
use string_wizard::MagicString;

Expand Down Expand Up @@ -40,18 +40,18 @@ impl Case {
self.fixture.exec();
}

fn render_assets_to_snapshot(&mut self, mut assets: Vec<OutputChunk>) {
fn render_assets_to_snapshot(&mut self, mut assets: Vec<Output>) {
self.snapshot.append("# Assets\n\n");
assets.sort_by_key(|c| c.file_name.clone());
assets.sort_by_key(|c| c.file_name().to_string());
let artifacts = assets
.iter()
// FIXME: should render the runtime module while tree shaking being supported
.filter(|asset| !asset.file_name.contains("rolldown_runtime"))
.filter(|asset| !asset.file_name().contains("rolldown_runtime"))
.flat_map(|asset| {
[
Cow::Owned(format!("## {}\n", asset.file_name)),
Cow::Owned(format!("## {}\n", asset.file_name())),
"```js".into(),
Cow::Borrowed(asset.code.trim()),
Cow::Borrowed(asset.content().trim()),
"```".into(),
]
})
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/tests/common/fixture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
process::Command,
};

use rolldown::{Bundler, FileNameTemplate, InputOptions, OutputChunk, OutputOptions};
use rolldown::{Bundler, FileNameTemplate, InputOptions, Output, OutputOptions};
use rolldown_error::BuildError;
use rolldown_fs::FileSystemOs;
use rolldown_testing::TestConfig;
Expand Down Expand Up @@ -81,7 +81,7 @@ impl Fixture {
}
}

pub async fn compile(&mut self) -> Result<Vec<OutputChunk>, Vec<BuildError>> {
pub async fn compile(&mut self) -> Result<Vec<Output>, Vec<BuildError>> {
let fixture_path = self.dir_path();

let mut test_config = self.test_config();
Expand Down
12 changes: 10 additions & 2 deletions crates/rolldown_binding/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ export interface OutputChunk {
isEntry: boolean
facadeModuleId?: string
}
export interface OutputAsset {
fileName: string
source: string
}
export interface Outputs {
chunks: Array<OutputChunk>
assets: Array<OutputAsset>
}
export class Bundler {
constructor(inputOpts: InputOptions)
write(opts: OutputOptions): Promise<Array<OutputChunk>>
generate(opts: OutputOptions): Promise<Array<OutputChunk>>
write(opts: OutputOptions): Promise<Outputs>
generate(opts: OutputOptions): Promise<Outputs>
}
16 changes: 7 additions & 9 deletions crates/rolldown_binding/src/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rolldown_fs::FileSystemOs;
use tracing::instrument;

use crate::{
options::InputOptions, options::OutputOptions, output_chunk::OutputChunk,
options::InputOptions, options::OutputOptions, output::Outputs,
utils::init_custom_trace_subscriber, NAPI_ENV,
};

Expand All @@ -23,12 +23,12 @@ impl Bundler {
}

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

#[napi]
pub async fn generate(&self, opts: OutputOptions) -> napi::Result<Vec<OutputChunk>> {
pub async fn generate(&self, opts: OutputOptions) -> napi::Result<Outputs> {
self.generate_impl(opts).await
}
}
Expand All @@ -50,7 +50,7 @@ impl Bundler {

#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn write_impl(&self, output_opts: OutputOptions) -> napi::Result<Vec<OutputChunk>> {
pub async fn write_impl(&self, output_opts: OutputOptions) -> napi::Result<Outputs> {
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 @@ -68,13 +68,12 @@ impl Bundler {
}
};

let output_chunks = outputs.into_iter().map(Into::into).collect::<Vec<_>>();
Ok(output_chunks)
Ok(outputs.into())
}

#[instrument(skip_all)]
#[allow(clippy::significant_drop_tightening)]
pub async fn generate_impl(&self, output_opts: OutputOptions) -> napi::Result<Vec<OutputChunk>> {
pub async fn generate_impl(&self, output_opts: OutputOptions) -> napi::Result<Outputs> {
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 @@ -92,7 +91,6 @@ impl Bundler {
}
};

let output_chunks = outputs.into_iter().map(Into::into).collect::<Vec<_>>();
Ok(output_chunks)
Ok(outputs.into())
}
}
2 changes: 1 addition & 1 deletion crates/rolldown_binding/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ static ALLOC: mimalloc_rust::GlobalMiMalloc = mimalloc_rust::GlobalMiMalloc;

pub mod bundler;
pub mod options;
pub mod output_chunk;
pub mod output;
pub mod utils;
scoped_tls::scoped_thread_local!(static NAPI_ENV: napi::Env);
62 changes: 62 additions & 0 deletions crates/rolldown_binding/src/output.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use derivative::Derivative;
use serde::Deserialize;

#[napi_derive::napi(object)]
#[derive(Deserialize, Default, Derivative)]
#[serde(rename_all = "camelCase")]
#[derivative(Debug)]
pub struct OutputChunk {
pub code: String,
pub file_name: String,
pub is_entry: bool,
pub facade_module_id: Option<String>,
}

impl From<Box<rolldown::OutputChunk>> for OutputChunk {
fn from(chunk: Box<rolldown::OutputChunk>) -> Self {
Self {
code: chunk.code,
file_name: chunk.file_name,
is_entry: chunk.is_entry,
facade_module_id: chunk.facade_module_id,
}
}
}

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

impl From<Box<rolldown::OutputAsset>> for OutputAsset {
fn from(chunk: Box<rolldown::OutputAsset>) -> Self {
Self { source: chunk.source, file_name: chunk.file_name }
}
}

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

impl From<Vec<rolldown::Output>> for Outputs {
fn from(outputs: Vec<rolldown::Output>) -> Self {
let mut chunks: Vec<OutputChunk> = vec![];
let mut assets: Vec<OutputAsset> = vec![];

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

Self { chunks, assets }
}
}
24 changes: 0 additions & 24 deletions crates/rolldown_binding/src/output_chunk.rs

This file was deleted.

5 changes: 4 additions & 1 deletion crates/rolldown_binding_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ pub fn bundle(file_list: Vec<FileItem>) -> Vec<AssetItem> {
match bundler.write(OutputOptions::default()).await {
Ok(assets) => assets
.into_iter()
.map(|item| AssetItem { name: item.file_name, content: item.code })
.map(|item| AssetItem {
name: item.file_name().to_string(),
content: item.content().to_owned(),
})
.collect::<Vec<_>>(),
Err(err) => {
panic!("{err:?}",);
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/rollup-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ export type {
OutputPlugin,
OutputChunk,
NormalizedInputOptions,
OutputAsset,
} from 'rollup'

0 comments on commit 42420d7

Please sign in to comment.