Skip to content

Commit

Permalink
fix: #1755 (#1814)
Browse files Browse the repository at this point in the history
* fix: #1755

* fix: override export * items

* chore: update test snapshot

* fix: #1755

* fix: incorrect sourcemap when minify enabled
  • Loading branch information
wre232114 authored Oct 11, 2024
1 parent ed676f0 commit 3b6eb91
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
7 changes: 7 additions & 0 deletions .changeset/twenty-fireants-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@farmfe/runtime-plugin-hmr": patch
"@farmfe/plugin-react": patch
"@farmfe/core": patch
---

fix #1755 and support useAbsolutePath for plugin-react
8 changes: 8 additions & 0 deletions crates/node/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/plugin_runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions packages/runtime-plugin-hmr/src/hmr-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 3 additions & 1 deletion packages/runtime-plugin-hmr/src/hot-module-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions rust-plugins/react/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
*
Expand All @@ -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;
15 changes: 10 additions & 5 deletions rust-plugins/react/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ const GLOBAL_INJECT_MODULE_ID: &str = "farmfe_plugin_react_global_inject";
#[serde(rename_all = "camelCase", default)]
struct SwcTransformReactOptions {
pub refresh: Option<bool>,
pub use_absolute_path: Option<bool>,
}

#[farm_plugin]
pub struct FarmPluginReact {
core_lib: Library,
options: String,
enable_react_refresh: bool,
use_absolute_path: bool,
}

impl FarmPluginReact {
Expand All @@ -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),
}
}
}
Expand Down Expand Up @@ -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,
&param.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,
Expand Down

0 comments on commit 3b6eb91

Please sign in to comment.