Skip to content

Commit

Permalink
fix: async_scope_block_on (#210)
Browse files Browse the repository at this point in the history
  • Loading branch information
houyunlu committed Nov 9, 2023
1 parent 0a40bbd commit 51f1be1
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 11 deletions.
34 changes: 34 additions & 0 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion crates/rolldown/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ rustc-hash = { workspace = true }
tracing = { workspace = true }
anyhow = { workspace = true }
index_vec = { workspace = true }
futures = "0.3.25"
futures = { workspace = true }
rayon = "1.6.0"
string_wizard = { workspace = true }
async-trait = { workspace = true }
Expand Down
10 changes: 5 additions & 5 deletions crates/rolldown/src/bundler/module_loader/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 Down Expand Up @@ -127,7 +128,7 @@ impl<'a, T: FileSystemExt + 'static + Default> 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().await?;
let resolved_entries = self.resolve_entries()?;

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

Expand Down Expand Up @@ -199,12 +200,12 @@ impl<'a, T: FileSystemExt + 'static + Default> ModuleLoader<'a, T> {
}

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

let resolved_ids =
futures::future::join_all(self.input_options.input.iter().map(|input_item| async move {
block_on_spawn_all(self.input_options.input.iter().map(|input_item| async move {
let specifier = &input_item.import;
match resolve_id(resolver, plugin_driver, specifier, None, false).await {
Ok(r) => {
Expand All @@ -220,8 +221,7 @@ impl<'a, T: FileSystemExt + 'static + Default> ModuleLoader<'a, T> {
}
Err(e) => Err(e),
}
}))
.await;
}));

let mut errors = BatchedErrors::default();

Expand Down
6 changes: 6 additions & 0 deletions crates/rolldown_utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,9 @@ repository.workspace = true

[dependencies]
string_wizard = { workspace = true }

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
async-scoped = { workspace = true, features = ["use-tokio"] }

[target.'cfg(target_arch = "wasm32")'.dependencies]
futures = { workspace = true, features = ["executor"]}
27 changes: 27 additions & 0 deletions crates/rolldown_utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,30 @@ mod magic_string_ext;
pub mod reserved_word;

pub use crate::magic_string_ext::MagicStringExt;
use std::future::Future;

#[cfg(not(target_arch = "wasm32"))]
pub fn block_on_spawn_all<Iter, Out>(iter: Iter) -> Vec<Out>
where
Iter: Iterator,
Out: Send + 'static,
Iter::Item: Future<Output = Out> + Send,
{
use async_scoped::TokioScope;
let (_ret, collections) =
async_scoped::Scope::scope_and_block(|scope: &mut TokioScope<'_, _>| {
iter.into_iter().for_each(|fut| scope.spawn(fut));
});
collections.into_iter().map(Result::unwrap).collect()
}

#[cfg(target_arch = "wasm32")]
pub fn block_on_spawn_all<Iter, Out>(iter: Iter) -> Vec<Out>
where
Iter: Iterator,
Out: Send + 'static,
Iter::Item: Future<Output = Out> + Send,
{
use futures::{executor::block_on, future};
block_on(future::join_all(iter))
}
2 changes: 1 addition & 1 deletion web/wasm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"sideEffects": [
"./snippets/*"
]
}
}
8 changes: 4 additions & 4 deletions web/wasm/rolldown_wasm.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,15 @@ async function __wbg_load(module, imports) {
function __wbg_get_imports() {
const imports = {};
imports.wbg = {};
imports.wbg.__wbg_assetitem_new = function() { return logError(function (arg0) {
const ret = AssetItem.__wrap(arg0);
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_fileitem_unwrap = function() { return logError(function (arg0) {
const ret = FileItem.__unwrap(takeObject(arg0));
_assertNum(ret);
return ret;
}, arguments) };
imports.wbg.__wbg_assetitem_new = function() { return logError(function (arg0) {
const ret = AssetItem.__wrap(arg0);
return addHeapObject(ret);
}, arguments) };
imports.wbg.__wbg_error_f851667af71bcfc6 = function() { return logError(function (arg0, arg1) {
let deferred0_0;
let deferred0_1;
Expand Down

0 comments on commit 51f1be1

Please sign in to comment.