Skip to content

Commit

Permalink
fix: defonflict export default function (#385)
Browse files Browse the repository at this point in the history
  • Loading branch information
underfin committed Dec 4, 2023
1 parent 9f3332b commit 518aa5f
Show file tree
Hide file tree
Showing 12 changed files with 148 additions and 3 deletions.
4 changes: 2 additions & 2 deletions crates/rolldown/src/bundler/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl<'r> AstRenderer<'r> {
oxc::ast::ast::ExportDefaultDeclarationKind::Expression(exp) => match exp {
Expression::Identifier(_) => {
self.ctx.remove_node(default_decl.span);
return RenderControl::Skip;
}
_ => {
let default_ref_name = self.ctx.default_ref_name.expect("Should generated a name");
Expand All @@ -207,7 +208,6 @@ impl<'r> AstRenderer<'r> {
exp.span().start,
format!("var {default_ref_name} = "),
);
self.visit_expression(exp);
}
},
oxc::ast::ast::ExportDefaultDeclarationKind::FunctionDeclaration(decl) => {
Expand All @@ -218,7 +218,7 @@ impl<'r> AstRenderer<'r> {
}
_ => unreachable!("TypeScript code should be preprocessed"),
}
RenderControl::Skip
RenderControl::Continue
}

fn render_require_expr(&mut self, expr: &oxc::ast::ast::CallExpression) {
Expand Down
2 changes: 1 addition & 1 deletion crates/rolldown/src/bundler/renderer/render_wrapped_esm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl<'r> AstRenderer<'r> {
}
_ => {}
}
RenderControl::Skip
RenderControl::Continue
}

pub fn render_export_named_declaration_for_wrapped_esm(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/deconflict/default_function
---
# Assets

## main.mjs

```js
import { default as assert } from "assert";
// foo.js
const a$1 = 1;
function foo$1(a$1$1) {
console.log(a$1$1, a$1)
}
// bar.js
var bar_default = { foo: foo$1 }
// main.js
const a = 2; // make foo `a` conflict
const { foo } = bar_default
assert.strictEqual(typeof foo, 'function')
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from './foo'

export default { foo }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const a = 1;

export default function foo(a$1) {
console.log(a$1, a)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import assert from 'assert'
import bar from './bar'

const a = 2; // make foo `a` conflict

const { foo } = bar

assert.strictEqual(typeof foo, 'function')
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"input": {
"external": [
"assert"
],
"treeshake": false
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
source: crates/rolldown/tests/common/case.rs
expression: content
input_file: crates/rolldown/tests/fixtures/deconflict/wrapped_esm_default_function
---
# Assets

## main.mjs

```js
import { default as assert } from "assert";
// <runtime>
var __defProp = Object.defineProperty
var __getOwnPropDesc = Object.getOwnPropertyDescriptor
var __getOwnPropNames = Object.getOwnPropertyNames
var __hasOwnProp = Object.prototype.hasOwnProperty
var __esm = (fn, res) => function () {
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res
}
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === 'object' || typeof from === 'function')
for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
key = keys[i]
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: (k => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable })
}
return to
}
var __toCommonJS = mod => __copyProps(__defProp({}, '__esModule', { value: true }), mod)
// foo.js
function foo$1(a$1$1) {
console.log(a$1$1, a$1)
}
var foo_ns = {
get default() { return foo$1 }
};
var init_foo = __esm({
'foo.js'() {
const a$1 = 1;
}
});
// bar.js
init_foo();
var bar_default = { foo: foo$1 }
// main.js
const a = 2; // make foo `a` conflict
const { foo } = bar_default
assert.strictEqual(typeof foo, 'function')
init_foo()
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import foo from './foo'

export default { foo }
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const a = 1;

export default function foo(a$1) {
console.log(a$1, a)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import assert from 'assert'
import bar from './bar'

const a = 2; // make foo `a` conflict

const { foo } = bar

assert.strictEqual(typeof foo, 'function')

require('./foo')
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"input": {
"external": [
"assert"
],
"treeshake": false
}
}

0 comments on commit 518aa5f

Please sign in to comment.