rspack_loader_testing/
lib.rs

1use async_trait::async_trait;
2use rspack_cacheable::{cacheable, cacheable_dyn};
3use rspack_core::{Loader, LoaderContext, RunnerContext};
4use rspack_error::Result;
5use rspack_loader_runner::{DisplayWithSuffix, Identifier};
6use serde_json::json;
7
8#[cacheable]
9pub struct SimpleLoader;
10#[cacheable_dyn]
11#[async_trait]
12impl Loader<RunnerContext> for SimpleLoader {
13  fn identifier(&self) -> Identifier {
14    SIMPLE_LOADER_IDENTIFIER.into()
15  }
16
17  async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
18    let Some(content) = loader_context.take_content() else {
19      return Ok(());
20    };
21    let export = format!("{}-simple", content.try_into_string()?);
22    loader_context.finish_with(format!("module.exports = {}", json!(export)));
23    Ok(())
24  }
25}
26pub const SIMPLE_LOADER_IDENTIFIER: &str = "builtin:test-simple-loader";
27
28#[cacheable]
29pub struct SimpleAsyncLoader;
30#[cacheable_dyn]
31#[async_trait]
32impl Loader<RunnerContext> for SimpleAsyncLoader {
33  fn identifier(&self) -> Identifier {
34    SIMPLE_ASYNC_LOADER_IDENTIFIER.into()
35  }
36
37  async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
38    let Some(content) = loader_context.take_content() else {
39      return Ok(());
40    };
41    loader_context.finish_with(format!("{}-async-simple", content.try_into_string()?));
42    Ok(())
43  }
44}
45pub const SIMPLE_ASYNC_LOADER_IDENTIFIER: &str = "builtin:test-simple-async-loader";
46
47#[cacheable]
48pub struct PitchingLoader;
49#[cacheable_dyn]
50#[async_trait]
51impl Loader<RunnerContext> for PitchingLoader {
52  fn identifier(&self) -> Identifier {
53    PITCHING_LOADER_IDENTIFIER.into()
54  }
55
56  async fn pitch(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
57    loader_context.finish_with(
58      [
59        loader_context
60          .remaining_request()
61          .display_with_suffix(loader_context.resource()),
62        loader_context.previous_request().to_string(),
63      ]
64      .join(":"),
65    );
66    Ok(())
67  }
68}
69pub const PITCHING_LOADER_IDENTIFIER: &str = "builtin:test-pitching-loader";
70
71#[cacheable]
72pub struct PassthroughLoader;
73#[cacheable_dyn]
74#[async_trait]
75impl Loader<RunnerContext> for PassthroughLoader {
76  fn identifier(&self) -> Identifier {
77    PASS_THROUGH_LOADER_IDENTIFIER.into()
78  }
79
80  async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
81    let patch_data = loader_context.take_all();
82    loader_context.finish_with(patch_data);
83    Ok(())
84  }
85}
86pub const PASS_THROUGH_LOADER_IDENTIFIER: &str = "builtin:test-passthrough-loader";
87
88#[cacheable]
89pub struct NoPassthroughLoader;
90#[cacheable_dyn]
91#[async_trait]
92impl Loader<RunnerContext> for NoPassthroughLoader {
93  fn identifier(&self) -> Identifier {
94    NO_PASS_THROUGH_LOADER_IDENTIFIER.into()
95  }
96
97  async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
98    let (content, _, _) = loader_context.take_all();
99    loader_context.finish_with(content);
100    Ok(())
101  }
102}
103pub const NO_PASS_THROUGH_LOADER_IDENTIFIER: &str = "builtin:test-no-passthrough-loader";