From 65f94772d6b9d4101aa4e7d2e1556f51aa65f872 Mon Sep 17 00:00:00 2001 From: ahabhgk Date: Fri, 25 Oct 2024 18:56:41 +0800 Subject: [PATCH] fix: incremental provided exports should rebuild clean exports info --- crates/rspack_core/src/exports_info.rs | 34 +++++++++++++++++++ .../plugin/flag_dependency_exports_plugin.rs | 4 ++- .../reexport-chain-strict/0/a.js | 1 + .../reexport-chain-strict/0/b.js | 1 + .../reexport-chain-strict/0/c.js | 1 + .../reexport-chain-strict/0/index.js | 12 +++++++ .../reexport-chain-strict/1/c.js | 1 + .../reexport-chain-strict/2/a.js | 1 + .../reexport-chain-strict/2/c.js | 1 + 9 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/a.js create mode 100644 packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/b.js create mode 100644 packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/c.js create mode 100644 packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/index.js create mode 100644 packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/1/c.js create mode 100644 packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/a.js create mode 100644 packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/c.js diff --git a/crates/rspack_core/src/exports_info.rs b/crates/rspack_core/src/exports_info.rs index 12b72324bef..304315b8d36 100644 --- a/crates/rspack_core/src/exports_info.rs +++ b/crates/rspack_core/src/exports_info.rs @@ -61,6 +61,14 @@ impl ExportsInfo { info.other_exports_info } + pub fn redirect_to(&self, mg: &ModuleGraph) -> Option { + self.as_exports_info(mg).redirect_to + } + + pub fn side_effects_only_info(&self, mg: &ModuleGraph) -> ExportInfo { + self.as_exports_info(mg).side_effects_only_info + } + pub fn as_exports_info<'a>(&self, mg: &'a ModuleGraph) -> &'a ExportsInfoData { mg.get_exports_info_by_id(self) } @@ -105,6 +113,21 @@ impl ExportsInfo { false } + // TODO: remove this, we should refactor ExportInfo into ExportName and ExportProvideInfo and ExportUsedInfo + // ExportProvideInfo is created by FlagDependencyExportsPlugin, and should not mutate after create + // ExportUsedInfo is created by FlagDependencyUsagePlugin or Plugin::finish_modules, and should not mutate after create + pub fn reset_provide_info(&self, mg: &mut ModuleGraph) { + let exports: Vec<_> = self.exports(mg).collect(); + for export_info in exports { + export_info.reset_provide_info(mg); + } + self.side_effects_only_info(mg).reset_provide_info(mg); + if let Some(redirect_to) = self.redirect_to(mg) { + redirect_to.reset_provide_info(mg); + } + self.other_exports_info(mg).reset_provide_info(mg); + } + /// # Panic /// it will panic if you provide a export info that does not exists in the module graph pub fn set_has_provide_info(&self, mg: &mut ModuleGraph) { @@ -814,6 +837,17 @@ impl ExportInfo { Self(NEXT_EXPORT_INFO_UKEY.fetch_add(1, Relaxed).into()) } + pub fn reset_provide_info(&self, mg: &mut ModuleGraph) { + let data = self.as_export_info_mut(mg); + data.provided = None; + data.can_mangle_provide = None; + data.exports_info_owned = false; + data.exports_info = None; + data.target_is_set = false; + data.target.clear(); + data.terminal_binding = false; + } + pub fn name<'a>(&self, mg: &'a ModuleGraph) -> Option<&'a Atom> { self.as_export_info(mg).name.as_ref() } diff --git a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs index a8c1e80d27a..fefa76003ed 100644 --- a/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs +++ b/crates/rspack_plugin_javascript/src/plugin/flag_dependency_exports_plugin.rs @@ -39,10 +39,12 @@ impl<'a> FlagDependencyExportsState<'a> { .module_graph_module_by_identifier(&module_id) .expect("mgm should exist"); let exports_info = mgm.exports; + // Reset exports provide info back to initial + exports_info.reset_provide_info(self.mg); let module = self .mg - .module_by_identifier(&mgm.module_identifier) + .module_by_identifier(&module_id) .expect("should have module"); let is_module_without_exports = if let Some(build_meta) = module.build_meta() { build_meta.exports_type == BuildMetaExportsType::Unset diff --git a/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/a.js b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/a.js new file mode 100644 index 00000000000..14065b7bb3f --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/a.js @@ -0,0 +1 @@ +export * from "./b"; diff --git a/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/b.js b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/b.js new file mode 100644 index 00000000000..6531c232656 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/b.js @@ -0,0 +1 @@ +export * from "./c"; diff --git a/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/c.js b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/c.js new file mode 100644 index 00000000000..3328d2923cf --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/c.js @@ -0,0 +1 @@ +export var x0 = "0"; diff --git a/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/index.js b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/index.js new file mode 100644 index 00000000000..8eaac569d17 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/0/index.js @@ -0,0 +1,12 @@ +import * as a from "./a"; + +const nsObj = m => { + Object.defineProperty(m, Symbol.toStringTag, { value: "Module" }); + return m; +}; + +it("should have to correct exports", () => { + expect(a).toStrictEqual(nsObj({ + [`x${WATCH_STEP}`]: WATCH_STEP + })); +}) diff --git a/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/1/c.js b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/1/c.js new file mode 100644 index 00000000000..e6f97c0bbdf --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/1/c.js @@ -0,0 +1 @@ +export var x1 = "1"; diff --git a/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/a.js b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/a.js new file mode 100644 index 00000000000..6531c232656 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/a.js @@ -0,0 +1 @@ +export * from "./c"; diff --git a/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/c.js b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/c.js new file mode 100644 index 00000000000..7b519b5f4a3 --- /dev/null +++ b/packages/rspack-test-tools/tests/watchCases/provided-exports/reexport-chain-strict/2/c.js @@ -0,0 +1 @@ +export var x2 = "2";