Skip to content

Commit

Permalink
feat: add rolldown_fs crate (#173)
Browse files Browse the repository at this point in the history
* feat: file system crate init

* more generic method

* add more method

* replace all with std path

* lint

* ck point

* update version

* chore: rename

* fmt

* use generic param instead dyn trait

* add extra bound
  • Loading branch information
houyunlu authored Nov 6, 2023
1 parent 71a838a commit 583c858
Show file tree
Hide file tree
Showing 25 changed files with 317 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .github/actions/rustup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,4 @@ runs:
if: ${{ inputs.restore-cache == 'true' }}
with:
shared-key: ${{ inputs.shared-key }}
save-if: ${{ inputs.save-cache == 'true' }}
save-if: ${{ inputs.save-cache == 'true' }}
4 changes: 1 addition & 3 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"recommendations": [
"editorconfig.editorconfig"
]
"recommendations": ["editorconfig.editorconfig"]
}
24 changes: 22 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/rolldown-rs/rolldown"

[workspace.dependencies]
oxc = { version = "0.2.0", features = ["semantic", "formatter"] }
oxc_resolver = { version = "0.5.0" }
sugar_path = "0.0.12"
tokio = { version = "1.33.0" }
hashbrown = { version = "0.14.2", features = ["rayon"] }
Expand Down
16 changes: 8 additions & 8 deletions crates/bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ name = "bench"
version = "0.1.0"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
homepage.workspace = true
license.workspace = true
edition.workspace = true
repository.workspace = true
homepage.workspace = true
license.workspace = true
edition.workspace = true
repository.workspace = true

[lib]
bench = false

[dependencies]
criterion = "0.4.0"
rolldown = { path = "../rolldown" }
tokio = { workspace = true, features = ["full"] }

criterion = "0.4.0"
rolldown = { path = "../rolldown" }
tokio = { workspace = true, features = ["full"] }
rolldown_fs = { path = "../rolldown_fs/" }
[[bench]]
name = "threejs"
harness = false
Expand Down
4 changes: 2 additions & 2 deletions crates/bench/benches/threejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ fn criterion_benchmark(c: &mut Criterion) {
group
.sample_size(20)
.bench_function("threejs", |b| {
b.iter(|| tokio::runtime::Runtime::new().unwrap().block_on(threejs()))
b.iter(|| tokio::runtime::Runtime::new().unwrap().block_on(threejs()));
})
.bench_function("threejs10x", |b| {
b.iter(|| tokio::runtime::Runtime::new().unwrap().block_on(threejs10x()))
b.iter(|| tokio::runtime::Runtime::new().unwrap().block_on(threejs10x()));
});
}

Expand Down
18 changes: 11 additions & 7 deletions crates/bench/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use std::path::PathBuf;

use rolldown::{Bundler, InputItem, InputOptions, OutputOptions};
use rolldown_fs::FileSystemOs;

pub async fn run_fixture(fixture_path: PathBuf) {
let mut bundler = Bundler::new(InputOptions {
input: Some(vec![InputItem {
name: Some("main".to_string()),
import: "./main.js".to_string(),
}]),
cwd: Some(fixture_path.clone()),
});
let mut bundler = Bundler::new(
InputOptions {
input: Some(vec![InputItem {
name: Some("main".to_string()),
import: "./main.js".to_string(),
}]),
cwd: Some(fixture_path.clone()),
},
FileSystemOs,
);

if fixture_path.join("dist").is_dir() {
std::fs::remove_dir_all(fixture_path.join("dist")).unwrap();
Expand Down
3 changes: 3 additions & 0 deletions crates/rolldown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rolldown_utils = { path = "../rolldown_utils" }
rolldown_resolver = { path = "../rolldown_resolver" }
rolldown_error = { path = "../rolldown_error" }
rolldown_oxc = { path = "../rolldown_oxc" }
rolldown_fs = { path = "../rolldown_fs" }
derivative = { workspace = true }
sugar_path = { workspace = true }
oxc = { workspace = true }
Expand All @@ -32,6 +33,8 @@ rayon = "1.6.0"
string_wizard = { workspace = true }
async-trait = { workspace = true }
smallvec = { workspace = true }
oxc_resolver = { workspace = true }


[dev_dependencies]
insta = { workspace = true }
Expand Down
20 changes: 12 additions & 8 deletions crates/rolldown/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
use std::path::PathBuf;
use std::{path::PathBuf, sync::Arc};

use rolldown::{Bundler, InputItem, InputOptions};
use rolldown_fs::FileSystemOs;
use sugar_path::SugarPathBuf;

#[tokio::main]
async fn main() {
let root = PathBuf::from(&std::env::var("CARGO_MANIFEST_DIR").unwrap());
let cwd = root.join("./examples").into_normalize();
let mut bundler = Bundler::new(InputOptions {
input: Some(vec![InputItem {
name: Some("basic".to_string()),
import: "./index.js".to_string(),
}]),
cwd: Some(cwd),
});
let mut bundler = Bundler::new(
InputOptions {
input: Some(vec![InputItem {
name: Some("basic".to_string()),
import: "./index.js".to_string(),
}]),
cwd: Some(cwd),
},
FileSystemOs,
);

let outputs = bundler.write(Default::default()).await.unwrap();
println!("{outputs:#?}");
Expand Down
34 changes: 21 additions & 13 deletions crates/rolldown/src/bundler/bundler.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::sync::Arc;

use rolldown_error::BuildError;
use rolldown_fs::FileSystemExt;
use sugar_path::AsPath;

use super::{
Expand All @@ -13,22 +16,23 @@ use crate::{bundler::bundle::bundle::Bundle, plugin::plugin::BoxPlugin, InputOpt

type BuildResult<T> = Result<T, Vec<BuildError>>;

pub struct Bundler {
pub struct Bundler<T: FileSystemExt> {
input_options: NormalizedInputOptions,
_plugins: Vec<BoxPlugin>,
fs: Arc<T>,
}

impl Bundler {
pub fn new(input_options: InputOptions) -> Self {
impl<T: FileSystemExt + 'static> Bundler<T> {
pub fn new(input_options: InputOptions, fs: T) -> Self {
// rolldown_tracing::enable_tracing_on_demand();
let normalized = NormalizedInputOptions::from_input_options(input_options);
Self { input_options: normalized, _plugins: vec![] }
Self { input_options: normalized, _plugins: vec![], fs: Arc::new(fs) }
}

pub fn with_plugins(input_options: InputOptions, plugins: Vec<BoxPlugin>) -> Self {
pub fn with_plugins(input_options: InputOptions, plugins: Vec<BoxPlugin>, fs: T) -> Self {
// rolldown_tracing::enable_tracing_on_demand();
let normalized = NormalizedInputOptions::from_input_options(input_options);
Self { input_options: normalized, _plugins: plugins }
Self { input_options: normalized, _plugins: plugins, fs: Arc::new(fs) }
}

pub async fn write(&mut self, output_options: crate::OutputOptions) -> BuildResult<Vec<Asset>> {
Expand All @@ -37,9 +41,9 @@ impl Bundler {
});
let normalized = NormalizedOutputOptions::from_output_options(output_options);

let assets = self.build(normalized).await?;
let assets = self.build(normalized, Arc::clone(&self.fs)).await?;

std::fs::create_dir_all(&dir).unwrap_or_else(|_| {
self.fs.create_dir_all(dir.as_path()).unwrap_or_else(|_| {
panic!(
"Could not create directory for output chunks: {:?} \ncwd: {}",
dir.as_path(),
Expand All @@ -50,10 +54,10 @@ impl Bundler {
let dest = dir.as_path().join(&chunk.file_name);
if let Some(p) = dest.parent() {
if !p.exists() {
std::fs::create_dir_all(p).unwrap();
self.fs.create_dir_all(p).unwrap();
}
};
std::fs::write(dest, &chunk.content).unwrap_or_else(|_| {
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))
});
}
Expand All @@ -66,15 +70,19 @@ impl Bundler {
output_options: crate::OutputOptions,
) -> BuildResult<Vec<Asset>> {
let normalized = NormalizedOutputOptions::from_output_options(output_options);
self.build(normalized).await
self.build(normalized, Arc::clone(&self.fs)).await
}

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

let mut graph = Graph::default();
graph.generate_module_graph(&self.input_options).await?;
graph.generate_module_graph(&self.input_options, fs).await?;

let mut bundle = Bundle::new(&mut graph, &output_options);
let assets = bundle.generate(&self.input_options);
Expand Down
8 changes: 6 additions & 2 deletions crates/rolldown/src/bundler/graph/graph.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::sync::Arc;

use super::{linker::Linker, linker_info::LinkingInfoVec, symbols::Symbols};
use crate::{
bundler::{
Expand All @@ -7,6 +9,7 @@ use crate::{
error::BatchedResult,
};
use rolldown_common::ModuleId;
use rolldown_fs::FileSystemExt;
use rustc_hash::FxHashSet;

#[derive(Default, Debug)]
Expand All @@ -20,11 +23,12 @@ pub struct Graph {
}

impl Graph {
pub async fn generate_module_graph(
pub async fn generate_module_graph<T: FileSystemExt + 'static>(
&mut self,
input_options: &NormalizedInputOptions,
fs: Arc<T>,
) -> BatchedResult<()> {
ModuleLoader::new(input_options, self).fetch_all_modules().await?;
ModuleLoader::new(input_options, self, fs).fetch_all_modules().await?;

tracing::trace!("{:#?}", self);

Expand Down
10 changes: 7 additions & 3 deletions crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::sync::Arc;
use index_vec::IndexVec;
use rolldown_common::{ImportKind, ModuleId, RawPath, ResourceId};
use rolldown_error::BuildError;
use rolldown_fs::FileSystemExt;
use rolldown_resolver::Resolver;
use rolldown_utils::block_on_spawn_all;
use rustc_hash::{FxHashMap, FxHashSet};
Expand All @@ -21,18 +22,19 @@ use crate::bundler::utils::resolve_id::{resolve_id, ResolvedRequestInfo};
use crate::error::{BatchedErrors, BatchedResult};
use crate::SharedResolver;

pub struct ModuleLoader<'a> {
pub struct ModuleLoader<'a, T> {
input_options: &'a NormalizedInputOptions,
graph: &'a mut Graph,
resolver: SharedResolver,
visited: FxHashMap<RawPath, ModuleId>,
remaining: u32,
tx: tokio::sync::mpsc::UnboundedSender<Msg>,
rx: tokio::sync::mpsc::UnboundedReceiver<Msg>,
fs: Arc<T>,
}

impl<'a> ModuleLoader<'a> {
pub fn new(input_options: &'a NormalizedInputOptions, graph: &'a mut Graph) -> Self {
impl<'a, T: FileSystemExt + 'static> ModuleLoader<'a, T> {
pub fn new(input_options: &'a NormalizedInputOptions, graph: &'a mut Graph, fs: Arc<T>) -> Self {
let (tx, rx) = tokio::sync::mpsc::unbounded_channel::<Msg>();
Self {
tx,
Expand All @@ -42,6 +44,7 @@ impl<'a> ModuleLoader<'a> {
visited: FxHashMap::default(),
remaining: u32::default(),
graph,
fs,
}
}

Expand Down Expand Up @@ -169,6 +172,7 @@ impl<'a> ModuleLoader<'a> {
module_path,
info.module_type,
self.tx.clone(),
Arc::clone(&self.fs),
);
tokio::spawn(async move { task.run().await });
}
Expand Down
Loading

0 comments on commit 583c858

Please sign in to comment.