Skip to content

Commit

Permalink
feat: wasm (#178)
Browse files Browse the repository at this point in the history
* init project

* tweak variable

* init project

* pass fs into oxc_resolver

* bundle with vfs

* make it run in browser

* chore: lint

* chore: bump version

* chore: rename crate

* chore: install

* clean up playground

* chore:recover

* clean up the code

* clean up dep

* add package wasm

* chore: rebase

* chore: update lock file

* chore: use workspace instead
  • Loading branch information
houyunlu authored Nov 6, 2023
1 parent 042f832 commit d8d0b2a
Show file tree
Hide file tree
Showing 39 changed files with 5,415 additions and 516 deletions.
84 changes: 37 additions & 47 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ repository = "https://github.com/rolldown-rs/rolldown"

[workspace.dependencies]
oxc = { version = "0.3.0", features = ["semantic"] }
oxc_resolver = { version = "0.5.0" }
oxc_resolver = { version = "0.5.1" }
sugar_path = "0.0.12"
tokio = { version = "1.33.0" }
tokio = { version = "1.33.0", default-features = false }
hashbrown = { version = "0.14.2", features = ["rayon"] }
derivative = "2.2.0"
rustc-hash = "1.1.0"
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ sugar_path = { workspace = true }
oxc = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
tokio = { workspace = true, features = ["fs", "rt", "macros", "rt-multi-thread", "sync"] }
tokio = { workspace = true, features = ["rt", "macros", "sync"] }
rustc-hash = { workspace = true }
tracing = { workspace = true }
anyhow = { workspace = true }
Expand Down
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler/bundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct Bundler<T: FileSystemExt> {
fs: Arc<T>,
}

impl<T: FileSystemExt + 'static> Bundler<T> {
impl<T: FileSystemExt + Default + '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);
Expand Down Expand Up @@ -53,7 +53,7 @@ impl<T: FileSystemExt + 'static> Bundler<T> {
for chunk in &assets {
let dest = dir.as_path().join(&chunk.file_name);
if let Some(p) = dest.parent() {
if !p.exists() {
if !self.fs.exists(p) {
self.fs.create_dir_all(p).unwrap();
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/graph/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct Graph {
}

impl Graph {
pub async fn generate_module_graph<T: FileSystemExt + 'static>(
pub async fn generate_module_graph<T: FileSystemExt + Default + 'static>(
&mut self,
input_options: &NormalizedInputOptions,
fs: Arc<T>,
Expand Down
27 changes: 15 additions & 12 deletions crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ 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};

use super::normal_module_task::NormalModuleTask;
Expand All @@ -23,11 +22,11 @@ use crate::bundler::utils::resolve_id::{resolve_id, ResolvedRequestInfo};
use crate::error::{BatchedErrors, BatchedResult};
use crate::SharedResolver;

pub struct ModuleLoader<'a, T> {
pub struct ModuleLoader<'a, T: FileSystemExt + Default> {
ctx: ModuleLoaderContext,
input_options: &'a NormalizedInputOptions,
graph: &'a mut Graph,
resolver: SharedResolver,
resolver: SharedResolver<T>,
tx: tokio::sync::mpsc::UnboundedSender<Msg>,
rx: tokio::sync::mpsc::UnboundedReceiver<Msg>,
fs: Arc<T>,
Expand All @@ -41,7 +40,10 @@ pub struct ModuleLoaderContext {
}

impl ModuleLoaderContext {
fn try_spawn_runtime_normal_module_task(&mut self, task_context: &ModuleTaskContext) -> ModuleId {
fn try_spawn_runtime_normal_module_task<T: FileSystemExt + Default + 'static>(
&mut self,
task_context: &ModuleTaskContext<T>,
) -> ModuleId {
match self.visited.entry(RUNTIME_PATH.to_string().into()) {
std::collections::hash_map::Entry::Occupied(visited) => *visited.get(),
std::collections::hash_map::Entry::Vacant(not_visited) => {
Expand All @@ -59,9 +61,9 @@ impl ModuleLoaderContext {
}
}

fn try_spawn_new_task(
fn try_spawn_new_task<T: FileSystemExt + Default + 'static>(
&mut self,
module_task_context: &ModuleTaskContext,
module_task_context: &ModuleTaskContext<T>,
info: &ResolvedRequestInfo,
is_entry: bool,
graph: &mut Graph,
Expand Down Expand Up @@ -100,14 +102,14 @@ impl ModuleLoaderContext {
}
}

impl<'a, T: FileSystemExt + 'static> ModuleLoader<'a, T> {
impl<'a, T: FileSystemExt + 'static + Default> 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,
rx,
input_options,
resolver: Resolver::with_cwd(input_options.cwd.clone(), false).into(),
resolver: Resolver::with_cwd_and_fs(input_options.cwd.clone(), false, Arc::clone(&fs)).into(),
graph,
fs,
ctx: ModuleLoaderContext::default(),
Expand All @@ -117,7 +119,7 @@ impl<'a, T: FileSystemExt + 'static> ModuleLoader<'a, T> {
pub async fn fetch_all_modules(mut self) -> BatchedResult<()> {
assert!(!self.input_options.input.is_empty(), "You must supply options.input to rolldown");

let resolved_entries = self.resolve_entries()?;
let resolved_entries = self.resolve_entries().await?;

self.ctx.intermediate_modules.reserve(resolved_entries.len() + 1 /* runtime */);

Expand Down Expand Up @@ -188,11 +190,11 @@ impl<'a, T: FileSystemExt + 'static> ModuleLoader<'a, T> {
}

#[allow(clippy::collection_is_never_read)]
fn resolve_entries(&mut self) -> BatchedResult<Vec<(Option<String>, ResolvedRequestInfo)>> {
async fn resolve_entries(&mut self) -> BatchedResult<Vec<(Option<String>, ResolvedRequestInfo)>> {
let resolver = &self.resolver;

let resolved_ids =
block_on_spawn_all(self.input_options.input.iter().map(|input_item| async move {
futures::future::join_all(self.input_options.input.iter().map(|input_item| async move {
let specifier = &input_item.import;
let resolve_id = resolve_id(resolver, specifier, None, false).await?;

Expand All @@ -205,7 +207,8 @@ impl<'a, T: FileSystemExt + 'static> ModuleLoader<'a, T> {
}

Ok((input_item.name.clone(), info))
}));
}))
.await;

let mut errors = BatchedErrors::default();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use crate::{bundler::options::normalized_input_options::NormalizedInputOptions,
use super::Msg;

/// Used to store common data shared between all tasks.
pub struct ModuleTaskContext<'task> {
pub struct ModuleTaskContext<'task, T: FileSystemExt + Default> {
pub input_options: &'task NormalizedInputOptions,
pub tx: &'task tokio::sync::mpsc::UnboundedSender<Msg>,
pub resolver: &'task SharedResolver,
pub resolver: &'task SharedResolver<T>,
pub fs: &'task dyn FileSystemExt,
}

impl<'task> ModuleTaskContext<'task> {
pub unsafe fn assume_static(&self) -> &'static ModuleTaskContext<'static> {
impl<'task, T: FileSystemExt + Default> ModuleTaskContext<'task, T> {
pub unsafe fn assume_static(&self) -> &'static ModuleTaskContext<'static, T> {
std::mem::transmute(self)
}
}
13 changes: 7 additions & 6 deletions crates/rolldown/src/bundler/module_loader/normal_module_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use index_vec::IndexVec;
use oxc::{ast::Visit, span::SourceType};
use rolldown_common::{ImportRecord, ImportRecordId, ModuleId, ModuleType, ResourceId, SymbolRef};
use rolldown_error::BuildError;
use rolldown_fs::FileSystemExt;
use rolldown_oxc::{OxcCompiler, OxcProgram};
use rolldown_resolver::Resolver;
use sugar_path::AsPath;
Expand All @@ -20,8 +21,8 @@ use crate::bundler::{
},
visitors::scanner::{self, ScanResult},
};
pub struct NormalModuleTask<'task> {
ctx: &'task ModuleTaskContext<'task>,
pub struct NormalModuleTask<'task, T: FileSystemExt + Default> {
ctx: &'task ModuleTaskContext<'task, T>,
module_id: ModuleId,
path: ResourceId,
module_type: ModuleType,
Expand All @@ -30,9 +31,9 @@ pub struct NormalModuleTask<'task> {
is_entry: bool,
}

impl<'task> NormalModuleTask<'task> {
impl<'task, T: FileSystemExt + Default + 'static> NormalModuleTask<'task, T> {
pub fn new(
ctx: &'task ModuleTaskContext<'task>,
ctx: &'task ModuleTaskContext<'task, T>,
id: ModuleId,
is_entry: bool,
path: ResourceId,
Expand Down Expand Up @@ -148,8 +149,8 @@ impl<'task> NormalModuleTask<'task> {
}

#[allow(clippy::option_if_let_else)]
pub(crate) async fn resolve_id(
resolver: &Resolver,
pub(crate) async fn resolve_id<F: FileSystemExt + Default>(
resolver: &Resolver<F>,
importer: &ResourceId,
specifier: &str,
) -> Result<ResolvedRequestInfo, BuildError> {
Expand Down
Loading

0 comments on commit d8d0b2a

Please sign in to comment.