Skip to content

feat: should rename default import based on source #10598

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

Merged
merged 1 commit into from
Jun 9, 2025
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
14 changes: 10 additions & 4 deletions crates/rspack_core/src/concatenated_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use rspack_javascript_compiler::ast::Ast;
use rspack_sources::{
BoxSource, CachedSource, ConcatSource, RawStringSource, ReplaceSource, Source, SourceExt,
};
use rspack_util::{ext::DynHash, itoa, source_map::SourceMapKind, swc::join_atom};
use rspack_util::{ext::DynHash, itoa, json_stringify, source_map::SourceMapKind, swc::join_atom};
use rustc_hash::{FxHashMap as HashMap, FxHashSet as HashSet, FxHasher};
use swc_core::{
common::{FileName, Spanned, SyntaxContext},
Expand Down Expand Up @@ -795,7 +795,11 @@ impl Module for ConcatenatedModule {
}

let new_name = if all_used_names.contains(atom) {
let new_name = find_new_name(atom, &all_used_names, None, &readable_identifier);
let new_name = if atom == "default" {
find_new_name("", &all_used_names, None, source)
} else {
find_new_name(atom, &all_used_names, None, &readable_identifier)
};
all_used_names.insert(new_name.clone());
// if the imported symbol is exported, we rename the export as well
if let Some(raw_export_map) = info.raw_export_map.as_mut()
Expand Down Expand Up @@ -1049,15 +1053,16 @@ impl Module for ConcatenatedModule {
let default_import = import_spec.default_import;
let import_stmt = if atoms.is_empty() {
format!(
"import {}{source}{};\n",
"import {}{}{};\n",
default_import
.map(|default_atom| { format!("{default_atom} from ") })
.unwrap_or_default(),
json_stringify(&source),
attr.unwrap_or_default()
)
} else {
format!(
"import {}{{ {} }} from {source}{};\n",
"import {}{{ {} }} from {}{};\n",
default_import
.map(|default_atom| { format!("{default_atom}, ") })
.unwrap_or_default(),
Expand All @@ -1072,6 +1077,7 @@ impl Module for ConcatenatedModule {
})
.collect::<Vec<String>>()
.join(", "),
json_stringify(&source),
attr.unwrap_or_default()
)
};
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,13 @@ impl ExternalModule {
}
UsedExports::UsedNames(atoms) => {
concatenation_scope.register_import(
json_stringify(request.primary()),
request.primary().to_string(),
attributes.clone(),
None,
);
for atom in &atoms {
concatenation_scope.register_import(
json_stringify(request.primary()),
request.primary().to_string(),
attributes.clone(),
Some(atom.clone()),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ module.exports = {
const bundle = Object.values(assets)[0]._value;
expect(bundle)
.toContain(`var __webpack_exports__cjsInterop = (foo_default());
export { external_external_module_default as defaultImport, namedImport, __webpack_exports__cjsInterop as cjsInterop };`);
export { external_module as defaultImport, namedImport, __webpack_exports__cjsInterop as cjsInterop };`);
expect(bundle).toContain(
'import external_external_module_default, { namedImport } from "external-module";'
'import external_module, { namedImport } from "external-module";'
);
});
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
expect(source).toMatchInlineSnapshot(`
import { HomeLayout as external_externals0_HomeLayout, a } from "externals0";
import { a as external_externals1_a } from "externals1";
import external_externals2_default from "externals2";
import externals2 from "externals2";
import "externals4";
import * as __WEBPACK_EXTERNAL_MODULE_externals3__ from "externals3";
Expand Down Expand Up @@ -76,7 +76,7 @@ module.exports = {
external_externals1_a;
external_externals2_default;
externals2;
__WEBPACK_EXTERNAL_MODULE_externals3__;
export { a };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const path = require("path");

it("module-import should correctly get fallback type", function() {
const content = fs.readFileSync(path.resolve(__dirname, "a.js"), "utf-8");
expect(content).toContain(`import external_external0_default from \"external0\";`); // module
expect(content).toContain(`import external0 from \"external0\";`); // module
expect(content).toContain(`import * as __WEBPACK_EXTERNAL_MODULE_external1__ from "external1"`); // module
expect(content).toContain(`module.exports = __WEBPACK_EXTERNAL_createRequire(import.meta.url)("external2")`); // node-commonjs
expect(content).toContain(`import * as __WEBPACK_EXTERNAL_MODULE_external3__ from "external3"`); // module
Expand Down
Loading