rspack_loader_react_refresh/
lib.rs1mod plugin;
2
3pub use plugin::ReactRefreshLoaderPlugin;
4use rspack_cacheable::{cacheable, cacheable_dyn};
5use rspack_core::RunnerContext;
6use rspack_error::Result;
7use rspack_loader_runner::{Identifier, Loader, LoaderContext};
8
9#[cacheable]
10pub struct ReactRefreshLoader {
11 identifier: Identifier,
12}
13
14impl Default for ReactRefreshLoader {
15 fn default() -> Self {
16 Self {
17 identifier: REACT_REFRESH_LOADER_IDENTIFIER.into(),
18 }
19 }
20}
21
22impl ReactRefreshLoader {
23 pub fn with_identifier(mut self, identifier: Identifier) -> Self {
26 debug_assert!(identifier.starts_with(REACT_REFRESH_LOADER_IDENTIFIER));
27 self.identifier = identifier;
28 self
29 }
30}
31
32#[cacheable_dyn]
33#[async_trait::async_trait]
34impl Loader<RunnerContext> for ReactRefreshLoader {
35 fn identifier(&self) -> Identifier {
36 self.identifier
37 }
38
39 async fn run(&self, loader_context: &mut LoaderContext<RunnerContext>) -> Result<()> {
40 let Some(content) = loader_context.take_content() else {
41 return Ok(());
42 };
43 let supports_arrow_function = loader_context
44 .context
45 .options
46 .output
47 .environment
48 .supports_arrow_function();
49
50 let mut source = content.try_into_string()?;
51
52 if supports_arrow_function {
53 source += r#"
54function $RefreshSig$() { return $ReactRefreshRuntime$.createSignatureFunctionForTransform() }
55function $RefreshReg$(type, id) { $ReactRefreshRuntime$.register(type, __webpack_module__.id + "_" + id) }
56Promise.resolve().then(() => { $ReactRefreshRuntime$.refresh(__webpack_module__.id, __webpack_module__.hot) });
57"#;
58 } else {
59 source += r#"
60function $RefreshSig$() { return $ReactRefreshRuntime$.createSignatureFunctionForTransform() }
61function $RefreshReg$(type, id) { $ReactRefreshRuntime$.register(type, __webpack_module__.id + "_" + id) }
62Promise.resolve().then(function() { $ReactRefreshRuntime$.refresh(__webpack_module__.id, __webpack_module__.hot) });
63"#;
64 }
65 let sm = loader_context.take_source_map();
66 loader_context.finish_with((source, sm));
67 Ok(())
68 }
69}
70
71pub const REACT_REFRESH_LOADER_IDENTIFIER: &str = "builtin:react-refresh-loader";