diff --git a/.prettierignore b/.prettierignore index 524ec8d7e91..9f5a1e6cb82 100644 --- a/.prettierignore +++ b/.prettierignore @@ -7,4 +7,4 @@ temp rollup **/.git **/node_modules - +scripts diff --git a/crates/rolldown/tests/esbuild/import_star/import_namespace_undefined_property_side_effect_free_file/artifacts.snap b/crates/rolldown/tests/esbuild/import_star/import_namespace_undefined_property_side_effect_free_file/artifacts.snap index 57f11410e0f..20103a1b318 100644 --- a/crates/rolldown/tests/esbuild/import_star/import_namespace_undefined_property_side_effect_free_file/artifacts.snap +++ b/crates/rolldown/tests/esbuild/import_star/import_namespace_undefined_property_side_effect_free_file/artifacts.snap @@ -7,50 +7,50 @@ input_file: crates/rolldown/tests/esbuild/import_star/import_namespace_undefined ```js // foo/no-side-effects.cjs -var require_no-side-effects = __commonJS({ +var require_no_side_effects = __commonJS({ 'foo/no-side-effects.cjs'(exports, module) { console.log('cjs') } }); // foo/no-side-effects.mjs -var no-side-effects_ns$1 = { +var no_side_effects_ns$1 = { }; console.log('mjs') // foo/no-side-effects.js -var no-side-effects_ns = { +var no_side_effects_ns = { }; console.log('js') -export { no-side-effects_ns as no-side-effects_ns$2, no-side-effects_ns$1 as no-side-effects_ns, no-side-effects_ns$2 as no-side-effects_ns$1 }; +export { no_side_effects_ns as no_side_effects_ns$2, no_side_effects_ns$1 as no_side_effects_ns, no_side_effects_ns$2 as no_side_effects_ns$1 }; ``` # entry-default_js.js ```js -import { no-side-effects_ns as no-side-effects_ns$2, no-side-effects_ns$1 as no-side-effects_ns, no-side-effects_ns$2 as no-side-effects_ns$1 } from "./2.js"; +import { no_side_effects_ns as no_side_effects_ns$2, no_side_effects_ns$1 as no_side_effects_ns, no_side_effects_ns$2 as no_side_effects_ns$1 } from "./2.js"; // entry-default.js -var no-side-effects_ns$2 = __toESM(require_no-side-effects()); +var no_side_effects_ns$2 = __toESM(require_no_side_effects()); console.log( - no-side-effects_ns.default, - no-side-effects_ns$1.default, - no-side-effects_ns$2.default, + no_side_effects_ns.default, + no_side_effects_ns$1.default, + no_side_effects_ns$2.default, ) ``` # entry-nope_js.js ```js -import { no-side-effects_ns as no-side-effects_ns$2, no-side-effects_ns$1 as no-side-effects_ns, no-side-effects_ns$2 as no-side-effects_ns$1 } from "./2.js"; +import { no_side_effects_ns as no_side_effects_ns$2, no_side_effects_ns$1 as no_side_effects_ns, no_side_effects_ns$2 as no_side_effects_ns$1 } from "./2.js"; // entry-nope.js -var no-side-effects_ns$2 = __toESM(require_no-side-effects()); +var no_side_effects_ns$2 = __toESM(require_no_side_effects()); console.log( - no-side-effects_ns.nope, - no-side-effects_ns$1.nope, - no-side-effects_ns$2.nope, + no_side_effects_ns.nope, + no_side_effects_ns$1.nope, + no_side_effects_ns$2.nope, ) ``` diff --git a/crates/rolldown_common/src/module_path.rs b/crates/rolldown_common/src/module_path.rs index 331ab458c3d..d33714c854e 100644 --- a/crates/rolldown_common/src/module_path.rs +++ b/crates/rolldown_common/src/module_path.rs @@ -62,8 +62,7 @@ impl ResourceId { return [unique_name_of_parent_dir, "_index"].concat(); } } - // TODO: ensure valid identifier - return unique_name.to_string(); + return ensure_valid_identifier(unique_name); } } @@ -72,3 +71,23 @@ impl AsRef for ResourceId { self.0.path.as_str() } } + +fn ensure_valid_identifier(s: &str) -> String { + let mut ident = String::new(); + let mut need_gap = false; + for i in s.chars() { + if i.is_ascii_alphabetic() || (i.is_ascii_digit() && !ident.is_empty()) { + if need_gap { + ident.push('_'); + need_gap = false; + } + ident.push(i); + } else if !ident.is_empty() { + need_gap = true; + } + } + if ident.is_empty() { + ident.push('_'); + } + ident +}