Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add runtime condition for harmony reexport checked #7353

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use std::sync::Arc;
use indexmap::{IndexMap, IndexSet};
use rspack_collections::IdentifierSet;
use rspack_core::{
create_exports_object_referenced, create_no_exports_referenced, get_exports_type,
create_exports_object_referenced, create_no_exports_referenced, filter_runtime, get_exports_type,
process_export_info, property_access, property_name, string_of_used_name, AsContextDependency,
ConnectionState, Dependency, DependencyCategory, DependencyCondition, DependencyId,
DependencyTemplate, DependencyType, ErrorSpan, ExportInfoId, ExportInfoProvided,
ConditionalInitFragment, ConnectionState, Dependency, DependencyCategory, DependencyCondition,
DependencyId, DependencyTemplate, DependencyType, ErrorSpan, ExportInfoId, ExportInfoProvided,
ExportNameOrSpec, ExportPresenceMode, ExportSpec, ExportsInfoId, ExportsOfExportsSpec,
ExportsSpec, ExportsType, ExtendedReferencedExport, HarmonyExportInitFragment, ImportAttributes,
InitFragmentExt, InitFragmentKey, InitFragmentStage, JavascriptParserOptions, ModuleDependency,
ModuleGraph, ModuleIdentifier, NormalInitFragment, RuntimeGlobals, RuntimeSpec, Template,
TemplateContext, TemplateReplaceSource, UsageState, UsedName,
ModuleGraph, ModuleIdentifier, NormalInitFragment, RuntimeCondition, RuntimeGlobals, RuntimeSpec,
Template, TemplateContext, TemplateReplaceSource, UsageState, UsedName,
};
use rspack_error::{
miette::{MietteDiagnostic, Severity},
Expand Down Expand Up @@ -626,6 +626,16 @@ impl HarmonyExportImportedSpecifierDependency {
let key = string_of_used_name(used_name.as_ref());

if checked {
let key = InitFragmentKey::HarmonyImport(format!(
"harmony reexport (checked) {import_var} {name}"
));
let runtime_condition = if self.weak() {
RuntimeCondition::Boolean(false)
} else if let Some(connection) = mg.connection_by_dependency(self.id()) {
filter_runtime(ctxt.runtime, |r| connection.is_target_active(mg, r))
} else {
RuntimeCondition::Boolean(true)
};
let is_async = mg.is_async(&module_identifier).unwrap_or_default();
let stmt = self.get_conditional_reexport_statement(
ctxt,
Expand All @@ -634,16 +644,17 @@ impl HarmonyExportImportedSpecifierDependency {
ids[0].clone(),
ValueKey::Vec(ids),
);
fragments.push(Box::new(NormalInitFragment::new(
fragments.push(Box::new(ConditionalInitFragment::new(
stmt,
if is_async {
InitFragmentStage::StageAsyncHarmonyImports
} else {
InitFragmentStage::StageHarmonyImports
},
self.source_order,
InitFragmentKey::unique(),
key,
None,
runtime_condition,
)));
} else {
let used_name =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { utilA } from "./lib"

it("should not emit error when running a.js (runtime a)", () => {
expect(utilA()).toBe("a");
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { utilB } from "./lib"

it("should not emit error when running b.js (runtime b)", () => {
expect(utilB()).toBe("[object Object] common");
})
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const common = 'common'
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./common"
export * from "./empty"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./util-a"
export * from "./common"
export * from "./util-b"
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function utilA() {
return 'a';
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { common } from "./common"
var b = ({}).toString(); // side effect, this will keep lib/index.js exist in the output, bailout the optimization from SideEffectsFlagPlugin
export function utilB() {
return b + ' ' + common;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
findBundle() {
return ["./lib.js", "./a.js", "./b.js"];
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/** @type {import("webpack").Configuration} */
module.exports = {
entry: {
a: "./a.js",
b: "./b.js"
},
output: {
filename: "[name].js"
},
target: "web",
mode: "production",
optimization: {
concatenateModules: false,
splitChunks: {
cacheGroups: {
lib: {
name: "lib",
test: /lib/,
chunks: "all",
minSize: 0
}
}
}
}
};
Loading