Skip to content

Commit

Permalink
fix: panic when passing test option to SourceMapDevToolPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
inottn committed Oct 15, 2024
1 parent 4ea491e commit 89f86bc
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
7 changes: 4 additions & 3 deletions crates/rspack_binding_options/src/options/raw_devtool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use rspack_plugin_devtool::{
Append, EvalDevToolModulePluginOptions, ModuleFilenameTemplate, ModuleFilenameTemplateFnCtx,
SourceMapDevToolPluginOptions, TestFn,
};
use tokio::runtime::Handle;

type RawAppend = Either3<String, bool, ThreadsafeFunction<RawPathData, String>>;

Expand Down Expand Up @@ -95,8 +94,10 @@ fn normalize_raw_module_filename_template(
}

fn normalize_raw_test(raw: ThreadsafeFunction<String, bool>) -> TestFn {
let handle = Handle::current();
Box::new(move |ctx| handle.block_on(raw.call(ctx)))
Box::new(move |ctx| {
let raw = raw.clone();
Box::pin(async move { raw.call(ctx).await })
})
}

#[napi(object, object_to_js = false)]
Expand Down
14 changes: 10 additions & 4 deletions crates/rspack_plugin_devtool/src/source_map_dev_tool_plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum Append {
Disabled,
}

pub type TestFn = Box<dyn Fn(String) -> Result<bool> + Sync + Send>;
pub type TestFn = Box<dyn Fn(String) -> BoxFuture<'static, Result<bool>> + Sync + Send>;

#[derive(Derivative)]
#[derivative(Debug)]
Expand Down Expand Up @@ -173,11 +173,11 @@ impl SourceMapDevToolPlugin {
let output_options = &compilation.options.output;
let map_options = MapOptions::new(self.columns);

let mut mapped_sources = raw_assets
let futures = raw_assets
.into_par_iter()
.filter_map(|(file, asset)| {
.map(|(file, asset)| async {
let is_match = match &self.test {
Some(test) => match test(file.to_owned()) {
Some(test) => match test(file.to_owned()).await {
Ok(val) => val,
Err(e) => return Some(Err(e)),
},
Expand All @@ -193,6 +193,12 @@ impl SourceMapDevToolPlugin {
};
source.map(Ok)
})
.collect::<Vec<_>>();

let mut mapped_sources = join_all(futures)
.await
.into_iter()
.flatten()
.collect::<Result<Vec<_>>>()?;

let source_map_modules = mapped_sources
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
},
plugins: [
new rspack.SourceMapDevToolPlugin({
test: /\.js/,
filename: "[file].map",
sourceRoot: path.join(__dirname, "folder") + "/"
}),
Expand Down

0 comments on commit 89f86bc

Please sign in to comment.