Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c0e7b5f

Browse files
authored
fix(iife): MISSING_NAME_OPTION_FOR_IIFE_EXPORT is output even if nothing is exported from the entrypoint (#2256)
### Description closes #2255 The main improvements include adding a check for `hasExports` and altering some of the structural writing, which makes the code clearer. The following are the conditions for warning in `Rollup`. ```ts if (hasExports && !name) { log(LOGLEVEL_WARN, logMissingNameOptionForIifeExport()); } ``` At present, there is no clue about `reexports`. ```ts const renderedExports = exportMode === 'none' ? [] : this.getChunkExportDeclarations(format); let hasExports = renderedExports.length > 0; let hasDefaultExport = false; for (const renderedDependence of renderedDependencies) { const { reexports } = renderedDependence; if (reexports?.length) { hasExports = true; if (!hasDefaultExport && reexports.some(reexport => reexport.reexported === 'default')) { hasDefaultExport = true; } if (format === 'es') { renderedDependence.reexports = reexports.filter( // eslint-disable-next-line unicorn/prefer-array-some ({ reexported }) => !renderedExports.find(({ exported }) => exported === reexported) ); } } } ```
1 parent d2fc33a commit c0e7b5f

3 files changed

Lines changed: 67 additions & 41 deletions

File tree

crates/rolldown/src/ecmascript/format/utils/namespace.rs

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -64,50 +64,60 @@ pub fn generate_identifier(
6464
ctx: &mut GenerateContext<'_>,
6565
export_mode: &OutputExports,
6666
) -> DiagnosableResult<(String, String)> {
67-
if let Some(name) = &ctx.options.name {
68-
// It is same as Rollup.
69-
if name.contains('.') {
70-
let (decl, expr) = generate_namespace_definition(name);
71-
Ok((
72-
decl,
73-
// Extend the object if the `extend` option is enabled.
74-
if ctx.options.extend && matches!(export_mode, OutputExports::Named) {
75-
format!("{expr} = {expr} || {{}}")
76-
} else {
77-
expr
78-
},
79-
))
80-
} else if ctx.options.extend {
81-
let caller = generate_caller(name.as_str());
82-
if matches!(export_mode, OutputExports::Named) {
83-
// In named exports, the `extend` option will make the assignment disappear and
84-
// the modification will be done extending the existed object (the `name` option).
85-
Ok((String::new(), format!("this{caller} = this{caller} || {{}}")))
86-
} else {
87-
Ok((
88-
String::new(),
89-
// If there isn't a name in default export, we shouldn't assign the function to `this[""]`.
90-
// If there is, we should assign the function to `this["name"]`,
91-
// because there isn't an object that we can extend.
92-
if name.is_empty() { String::new() } else { format!("this{caller}") },
93-
))
94-
}
95-
} else if is_validate_assignee_identifier_name(name) {
96-
// If valid, we can use the `var` statement to declare the variable.
97-
Ok((String::new(), format!("var {name}")))
98-
} else {
99-
// This behavior is aligned with Rollup. If using `output.extend: true`, this error won't be triggered.
100-
let name = ArcStr::from(name);
101-
Err(vec![BuildDiagnostic::illegal_identifier_as_name(name)])
102-
}
103-
} else {
104-
// If the `name` is empty, you may be impossible to call the result.
105-
// But it is normal if we do not have exports.
106-
// However, if there is no export, it is recommended to use `app` format.
67+
// Handle the diagnostic warning
68+
if ctx.options.name.as_ref().map_or(true, String::is_empty)
69+
&& !matches!(export_mode, OutputExports::None)
70+
{
10771
ctx
10872
.warnings
10973
.push(BuildDiagnostic::missing_name_option_for_iife_export().with_severity_warning());
110-
Ok((String::new(), String::new()))
74+
}
75+
76+
// Early return if `name` is None
77+
let Some(name) = &ctx.options.name else {
78+
return Ok((String::new(), String::new()));
79+
};
80+
81+
// It is same as Rollup.
82+
if name.contains('.') {
83+
let (decl, expr) = generate_namespace_definition(name);
84+
// Extend the object if the `extend` option is enabled.
85+
let final_expr = if ctx.options.extend && matches!(export_mode, OutputExports::Named) {
86+
format!("{expr} = {expr} || {{}}")
87+
} else {
88+
expr
89+
};
90+
91+
return Ok((decl, final_expr));
92+
}
93+
94+
if ctx.options.extend {
95+
let caller = generate_caller(name.as_str());
96+
let final_expr = if matches!(export_mode, OutputExports::Named) {
97+
// In named exports, the `extend` option will make the assignment disappear and
98+
// the modification will be done extending the existed object (the `name` option).
99+
format!("this{caller} = this{caller} || {{}}")
100+
} else {
101+
// If there isn't a name in default export, we shouldn't assign the function to `this[""]`.
102+
// If there is, we should assign the function to `this["name"]`,
103+
// because there isn't an object that we can extend.
104+
if name.is_empty() {
105+
String::new()
106+
} else {
107+
format!("this{caller}")
108+
}
109+
};
110+
111+
return Ok((String::new(), final_expr));
112+
}
113+
114+
if is_validate_assignee_identifier_name(name) {
115+
// If valid, we can use the `var` statement to declare the variable.
116+
Ok((String::new(), format!("var {name}")))
117+
} else {
118+
// This behavior is aligned with Rollup. If using `output.extend: true`, this error won't be triggered.
119+
let name = ArcStr::from(name);
120+
Err(vec![BuildDiagnostic::illegal_identifier_as_name(name)])
111121
}
112122
}
113123

crates/rolldown/tests/rolldown/function/extend/iife/no_name_default/artifacts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
---
22
source: crates/rolldown_testing/src/integration_test.rs
33
---
4+
# warnings
5+
6+
## MISSING_NAME_OPTION_FOR_IIFE_EXPORT
7+
8+
```text
9+
[MISSING_NAME_OPTION_FOR_IIFE_EXPORT] Warning: If you do not supply "output.name", you may not be able to access the exports of an IIFE bundle.
10+
11+
```
412
# Assets
513

614
## main.mjs

crates/rolldown/tests/rolldown/function/extend/iife/no_name_named/artifacts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
---
22
source: crates/rolldown_testing/src/integration_test.rs
33
---
4+
# warnings
5+
6+
## MISSING_NAME_OPTION_FOR_IIFE_EXPORT
7+
8+
```text
9+
[MISSING_NAME_OPTION_FOR_IIFE_EXPORT] Warning: If you do not supply "output.name", you may not be able to access the exports of an IIFE bundle.
10+
11+
```
412
# Assets
513

614
## main.mjs

0 commit comments

Comments
 (0)