diff --git a/.changeset/twenty-fireants-camp.md b/.changeset/twenty-fireants-camp.md new file mode 100644 index 000000000..789dd7ebe --- /dev/null +++ b/.changeset/twenty-fireants-camp.md @@ -0,0 +1,7 @@ +--- +"@farmfe/runtime-plugin-hmr": patch +"@farmfe/plugin-react": patch +"@farmfe/core": patch +--- + +fix #1755 and support useAbsolutePath for plugin-react diff --git a/crates/node/src/lib.rs b/crates/node/src/lib.rs index ac5a0425f..193a1796e 100644 --- a/crates/node/src/lib.rs +++ b/crates/node/src/lib.rs @@ -391,10 +391,18 @@ impl JsCompiler { } else { resolved_path.into() }; + let current_exec_order = module_graph + .module(&module_id) + .map(|m| m.execution_order) + .unwrap_or(usize::MAX); let parents = module_graph.dependents_ids(&module_id); parents .into_iter() + .filter(|id| { + let module = module_graph.module(id).unwrap(); + module.execution_order > current_exec_order + }) .map(|p| p.resolved_path_with_query(&context.config.root)) .collect() } diff --git a/crates/plugin_runtime/src/lib.rs b/crates/plugin_runtime/src/lib.rs index 0a4bc66ee..528742a9e 100644 --- a/crates/plugin_runtime/src/lib.rs +++ b/crates/plugin_runtime/src/lib.rs @@ -385,7 +385,7 @@ impl Plugin for FarmPluginRuntime { remap_source: Some(Box::new(move |src| { format!("/{}", farmfe_utils::relative(&root, src)) })), - hires: if matches!(context.config.mode, Mode::Production) { + hires: if context.config.minify.enabled() { Some(MappingsOptionHires::Boundary) } else { None diff --git a/packages/runtime-plugin-hmr/src/hmr-client.ts b/packages/runtime-plugin-hmr/src/hmr-client.ts index 8b9a6cdf4..6e28e33e9 100644 --- a/packages/runtime-plugin-hmr/src/hmr-client.ts +++ b/packages/runtime-plugin-hmr/src/hmr-client.ts @@ -160,8 +160,6 @@ export class HmrClient { const disposer = this.disposeMap.get(acceptedId); if (disposer) await disposer(acceptHotContext.data); - // clear accept callbacks, it will be re-registered in the accepted module when the module is required - acceptHotContext.acceptCallbacks = []; const acceptedExports = moduleSystem.require(acceptedId); diff --git a/packages/runtime-plugin-hmr/src/hot-module-state.ts b/packages/runtime-plugin-hmr/src/hot-module-state.ts index 5674d45eb..1fbd3f06d 100644 --- a/packages/runtime-plugin-hmr/src/hot-module-state.ts +++ b/packages/runtime-plugin-hmr/src/hot-module-state.ts @@ -102,7 +102,9 @@ export class HotModuleState { export function createHotContext(id: string, hmrClient: HmrClient) { if (hmrClient.registeredHotModulesMap.has(id)) { - return hmrClient.registeredHotModulesMap.get(id); + const hotModuleState = hmrClient.registeredHotModulesMap.get(id); + hotModuleState.acceptCallbacks = []; // clear the accept callbacks when hot reloading + return hotModuleState; } const state = new HotModuleState(id, hmrClient); diff --git a/rust-plugins/react/index.d.ts b/rust-plugins/react/index.d.ts index 7f93d9bdb..9a4f57f60 100644 --- a/rust-plugins/react/index.d.ts +++ b/rust-plugins/react/index.d.ts @@ -21,8 +21,7 @@ export interface ReactConfig { */ throwIfNamespace?: boolean; /** - * Toggles plugins that aid in development, such as @swc/plugin-transform-react-jsx-self - * and @swc/plugin-transform-react-jsx-source. + * Toggles debug props __self and __source on elements generated from JSX, which are used by development tooling such as React Developer Tools. * * Defaults to `false`, * @@ -48,6 +47,11 @@ export interface ReactConfig { * Declares the module specifier to be used for importing the `jsx` and `jsxs` factory functions when using `runtime` 'automatic' */ importSource?: string; + + /** + * Inject absolute path for generated filename + */ + useAbsolutePath?: boolean; } declare const binPath: (options?:ReactConfig)=>[string, ReactConfig]; export default binPath; diff --git a/rust-plugins/react/src/lib.rs b/rust-plugins/react/src/lib.rs index 708a3055a..090b6dd3a 100644 --- a/rust-plugins/react/src/lib.rs +++ b/rust-plugins/react/src/lib.rs @@ -23,6 +23,7 @@ const GLOBAL_INJECT_MODULE_ID: &str = "farmfe_plugin_react_global_inject"; #[serde(rename_all = "camelCase", default)] struct SwcTransformReactOptions { pub refresh: Option, + pub use_absolute_path: Option, } #[farm_plugin] @@ -30,6 +31,7 @@ pub struct FarmPluginReact { core_lib: Library, options: String, enable_react_refresh: bool, + use_absolute_path: bool, } impl FarmPluginReact { @@ -41,6 +43,7 @@ impl FarmPluginReact { core_lib: load_core_lib(config.core_lib_path.as_ref().unwrap()), options, enable_react_refresh: is_dev && react_options.refresh.unwrap_or(true), + use_absolute_path: react_options.use_absolute_path.unwrap_or(false), } } } @@ -117,11 +120,13 @@ impl Plugin for FarmPluginReact { let unresolved_mark = param.meta.as_script().unresolved_mark; let ast = &mut param.meta.as_script_mut().ast; - let (cm, _) = create_swc_source_map( - &self.core_lib, - ¶m.module_id.to_string(), - param.content.clone(), - )?; + let file_name = if self.use_absolute_path { + param.module_id.resolved_path(&context.config.root) + } else { + param.module_id.to_string() + }; + + let (cm, _) = create_swc_source_map(&self.core_lib, &file_name, param.content.clone())?; swc_transform_react( &self.core_lib,