Skip to content

Commit

Permalink
feat: treated facade default export ref as never get reassigned (roll…
Browse files Browse the repository at this point in the history
…down#2966)

<!-- Thank you for contributing! -->

### Description

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
  • Loading branch information
hyf0 authored Nov 27, 2024
1 parent 09579b0 commit 5e571e4
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 96 deletions.
3 changes: 3 additions & 0 deletions crates/rolldown/src/ast_scanner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ impl<'me, 'ast: 'me> AstScanner<'me, 'ast> {
let legitimized_repr_name = legitimize_identifier_name(repr_name);
let default_export_ref = symbol_ref_db
.create_facade_root_symbol_ref(concat_string!(legitimized_repr_name, "_default").into());
// This is used for converting "export default foo;" => "var [default_export_ref] = foo;"
// And we consider [default_export_ref] never get reassigned.
default_export_ref.flags_mut(&mut symbol_ref_db).insert(SymbolRefFlags::IS_NOT_REASSIGNED);

let name = concat_string!(legitimized_repr_name, "_exports");
let namespace_object_ref = symbol_ref_db.create_facade_root_symbol_ref(name.into());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# warnings

Expand Down Expand Up @@ -50,12 +51,7 @@ Object.defineProperty(exports, 'b', {
}
});
exports.c = c
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return entry_default;
}
});
exports.default = entry_default
exports.l = l
exports.v = v
return exports;
Expand Down
16 changes: 3 additions & 13 deletions crates/rolldown/tests/esbuild/default/export_forms_iife/bypass.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,7 @@ Object.defineProperty(exports, 'b', {
}
});
exports.c = c
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return entry_default;
}
});
exports.default = entry_default
exports.l = l
exports.v = v
return exports;
Expand All @@ -96,7 +91,7 @@ return exports;
===================================================================
--- esbuild /out.js
+++ rolldown entry.js
@@ -1,27 +1,37 @@
@@ -1,27 +1,32 @@
-var globalName = (() => {
- var entry_exports = {};
- __export(entry_exports, {
Expand Down Expand Up @@ -142,12 +137,7 @@ return exports;
+ }
+ });
+ exports.c = c;
+ Object.defineProperty(exports, 'default', {
+ enumerable: true,
+ get: function () {
+ return entry_default;
+ }
+ });
+ exports.default = entry_default;
+ exports.l = l;
+ exports.v = v;
+ return exports;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

Expand All @@ -14,11 +15,6 @@ var main_default = "if_default_prop_true";
const value = true;
//#endregion
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
exports.value = value
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

Expand All @@ -16,12 +17,7 @@ var main_default = "if_default_prop_true";
const value = true;
//#endregion
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
exports.value = value
return exports;
})({});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

Expand All @@ -23,10 +24,5 @@ var main_default = example();
//#endregion
exports.a = a
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

Expand All @@ -18,11 +19,6 @@ var main_default = "default";
//#endregion
exports.a = a
exports.b = b
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
exports["😈"] = devil
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

Expand All @@ -20,12 +21,7 @@ function inc() {
var main_default = count;
//#endregion
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
exports.inc = inc
exports.reset = reset
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

Expand All @@ -14,11 +15,6 @@ const foo = "foo";
var main_default = "main";
//#endregion
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
exports.foo = foo
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,15 @@ snapshot_kind: text
```js
const require_main = require('./main.js');
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return require_main.main_default;
}
});
exports.default = require_main.main_default
exports.foo = require_main.foo
```
## entry2.js

```js
const require_main = require('./main.js');
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return require_main.main_default;
}
});
exports.default = require_main.main_default
exports.foo = require_main.foo
```
## main.js
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# Assets

Expand All @@ -25,11 +26,6 @@ var init_main = __esm({ "main.js"() {
//#endregion
init_main();
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
exports.foo = foo
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
---
source: crates/rolldown_testing/src/integration_test.rs
snapshot_kind: text
---
# warnings

Expand All @@ -25,12 +26,7 @@ const a = 2;
//#endregion
exports.a = a
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
return exports;
})({});
```
Original file line number Diff line number Diff line change
Expand Up @@ -798,7 +798,7 @@ snapshot_kind: text
# tests/esbuild/default/export_forms_iife
- entry-!~{000}~.js => entry-B6vlME3I.js
- entry-!~{000}~.js => entry-BA-K-SNR.js
# tests/esbuild/default/export_forms_with_minify_identifiers_and_no_bundle
Expand Down Expand Up @@ -3935,15 +3935,15 @@ snapshot_kind: text
# tests/rolldown/function/es_module/if_default_prop_cjs_true
- main-!~{000}~.js => main-CsgEFrlH.js
- main-!~{000}~.js => main-cgJDF9TZ.js
# tests/rolldown/function/es_module/if_default_prop_iife_false
- main-!~{000}~.js => main-B_AS4TD4.js
# tests/rolldown/function/es_module/if_default_prop_iife_true
- main-!~{000}~.js => main-IwFiE4Ff.js
- main-!~{000}~.js => main-D9LdNfYj.js
# tests/rolldown/function/es_module/never
Expand Down Expand Up @@ -3995,7 +3995,7 @@ snapshot_kind: text
# tests/rolldown/function/export_mode/cjs/named
- main-!~{000}~.js => main-B0d7SG5w.js
- main-!~{000}~.js => main-BwzQ9T-O.js
# tests/rolldown/function/export_mode/iife/auto/none
Expand Down Expand Up @@ -4939,7 +4939,7 @@ snapshot_kind: text
# tests/rolldown/topics/cjs_module_lexer_compat/exports
- main-!~{000}~.js => main-C_LhPujj.js
- main-!~{000}~.js => main-ncpt5xmU.js
# tests/rolldown/topics/css/align_vite
Expand Down Expand Up @@ -5043,7 +5043,7 @@ snapshot_kind: text
# tests/rolldown/topics/live_bindings/default_export_expr_cjs
- main-!~{000}~.js => main-DURgDUYH.js
- main-!~{000}~.js => main-B_iLbtpg.js
# tests/rolldown/topics/live_bindings/default_export_expr_in_common_chunks
Expand Down Expand Up @@ -5117,7 +5117,7 @@ snapshot_kind: text
# tests/rolldown/topics/preserve_semantic_of_entries_exports/named_export_cjs
- main-!~{000}~.js => main-Cmb9Qu4e.js
- main-!~{000}~.js => main-SgusirFn.js
# tests/rolldown/topics/preserve_semantic_of_entries_exports/named_export_in_shared_entries
Expand All @@ -5127,8 +5127,8 @@ snapshot_kind: text
# tests/rolldown/topics/preserve_semantic_of_entries_exports/named_export_in_shared_entries_cjs
- entry-!~{000}~.js => entry-D8lyh8F8.js
- entry2-!~{001}~.js => entry2-CoQWESLQ.js
- entry-!~{000}~.js => entry-DSxxgWLd.js
- entry2-!~{001}~.js => entry2-CXxnJInr.js
- main-!~{002}~.js => main-DallkI37.js
# tests/rolldown/topics/preserve_semantic_of_entries_exports/named_export_in_wrapped
Expand All @@ -5143,7 +5143,7 @@ snapshot_kind: text
# tests/rolldown/topics/preserve_semantic_of_entries_exports/named_export_in_wrapped_cjs
- main-!~{000}~.js => main-D4BrWgkl.js
- main-!~{000}~.js => main-BfW_etaZ.js
# tests/rolldown/tree_shaking/advanced_barrel_exports
Expand Down Expand Up @@ -5268,7 +5268,7 @@ snapshot_kind: text
# tests/rolldown/warnings/mixed_export
- main-!~{000}~.js => main-BO4-4DlB.js
- main-!~{000}~.js => main-BMoXN5sO.js
# tests/rolldown/warnings/none-ascii-content
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,7 @@ export default defineTest({
var main_default = node_path.join;
//#endregion
Object.defineProperty(exports, 'default', {
enumerable: true,
get: function () {
return main_default;
}
});
exports.default = main_default
return exports;
})({}, node_path);"
`)
Expand Down

0 comments on commit 5e571e4

Please sign in to comment.