Skip to content

Commit 2533420

Browse files
authored
Fix ExportDefaultDeclaration components (#46)
* Fix `ExportDefaultDeclaration` * Update ci.yml
1 parent 1d53eab commit 2533420

7 files changed

Lines changed: 66 additions & 37 deletions

File tree

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ jobs:
1515
- uses: actions/checkout@v3
1616
- uses: pnpm/action-setup@v2.2.2
1717
with:
18-
version: 7
18+
version: 8
1919
- uses: actions/setup-node@v3
2020
with:
21-
node-version: 16
21+
node-version: 20
2222
cache: 'pnpm'
2323

2424
- name: Installing deps

src/babel/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -658,11 +658,15 @@ export default function solidRefreshPlugin(): babel.PluginObj<State> {
658658
t.functionExpression(decl.id, decl.params, decl.body),
659659
decl
660660
);
661-
path.replaceWith(
662-
path.parentPath.isExportDefaultDeclaration()
663-
? replacement
664-
: t.variableDeclaration('var', [t.variableDeclarator(decl.id, replacement)])
665-
);
661+
const newDecl = t.variableDeclaration('var', [t.variableDeclarator(decl.id, replacement)]);
662+
if (path.parentPath.isExportDefaultDeclaration()) {
663+
path.parentPath.insertBefore(
664+
newDecl,
665+
);
666+
path.replaceWith(decl.id);
667+
} else {
668+
path.replaceWith(newDecl);
669+
}
666670
}
667671
}
668672
}

tests/__snapshots__/esm.test.ts.snap

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ exports[`esm > ExportDefaultDeclaration w/ FunctionExpression > should transform
3535
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
3636
import { $$registry as _$$registry } from \\"solid-refresh\\";
3737
const _REGISTRY = _$$registry();
38-
// @refresh granular
39-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
38+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
4039
return <h1>Foo</h1>;
4140
}, {
4241
location: \\"example.jsx:3:21\\",
4342
signature: \\"83fbb41\\"
4443
});
44+
// @refresh granular
45+
export default Foo;
4546
if (import.meta.hot) {
4647
_$$refresh(\\"esm\\", import.meta.hot, _REGISTRY);
4748
}"
@@ -54,7 +55,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
5455
const _REGISTRY = _$$registry();
5556
// @refresh granular
5657
const example = 'Foo';
57-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
58+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
5859
return <h1>{example}</h1>;
5960
}, {
6061
location: \\"example.jsx:4:21\\",
@@ -63,6 +64,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
6364
example
6465
}
6566
});
67+
export default Foo;
6668
if (import.meta.hot) {
6769
_$$refresh(\\"esm\\", import.meta.hot, _REGISTRY);
6870
}"
@@ -75,7 +77,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
7577
const _REGISTRY = _$$registry();
7678
// @refresh granular
7779
const Example = createContext();
78-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
80+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
7981
return <Example.Provider>Foo</Example.Provider>;
8082
}, {
8183
location: \\"example.jsx:4:21\\",
@@ -84,6 +86,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
8486
Example
8587
}
8688
});
89+
export default Foo;
8790
if (import.meta.hot) {
8891
_$$refresh(\\"esm\\", import.meta.hot, _REGISTRY);
8992
}"
@@ -94,11 +97,12 @@ exports[`esm > ExportDefaultDeclaration w/ FunctionExpression > should transform
9497
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
9598
import { $$registry as _$$registry } from \\"solid-refresh\\";
9699
const _REGISTRY = _$$registry();
97-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
100+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
98101
return <h1>Foo</h1>;
99102
}, {
100103
location: \\"example.jsx:2:21\\"
101104
});
105+
export default Foo;
102106
if (import.meta.hot) {
103107
_$$refresh(\\"esm\\", import.meta.hot, _REGISTRY);
104108
}"
@@ -109,11 +113,12 @@ exports[`esm > ExportDefaultDeclaration w/ FunctionExpression > should transform
109113
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
110114
import { $$registry as _$$registry } from \\"solid-refresh\\";
111115
const _REGISTRY = _$$registry();
112-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
116+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
113117
return <h1>Foo</h1>;
114118
}, {
115119
location: \\"example.jsx:2:21\\"
116120
});
121+
export default Foo;
117122
if (import.meta.hot) {
118123
_$$refresh(\\"esm\\", import.meta.hot, _REGISTRY);
119124
}"

tests/__snapshots__/rspack-esm.test.ts.snap

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ exports[`rspack-esm > ExportDefaultDeclaration w/ FunctionExpression > should tr
3535
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
3636
import { $$registry as _$$registry } from \\"solid-refresh\\";
3737
const _REGISTRY = _$$registry();
38-
// @refresh granular
39-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
38+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
4039
return <h1>Foo</h1>;
4140
}, {
4241
location: \\"example.jsx:3:21\\",
4342
signature: \\"83fbb41\\"
4443
});
44+
// @refresh granular
45+
export default Foo;
4546
if (import.meta.webpackHot) {
4647
_$$refresh(\\"rspack-esm\\", import.meta.webpackHot, _REGISTRY);
4748
}"
@@ -54,7 +55,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
5455
const _REGISTRY = _$$registry();
5556
// @refresh granular
5657
const example = 'Foo';
57-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
58+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
5859
return <h1>{example}</h1>;
5960
}, {
6061
location: \\"example.jsx:4:21\\",
@@ -63,6 +64,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
6364
example
6465
}
6566
});
67+
export default Foo;
6668
if (import.meta.webpackHot) {
6769
_$$refresh(\\"rspack-esm\\", import.meta.webpackHot, _REGISTRY);
6870
}"
@@ -75,7 +77,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
7577
const _REGISTRY = _$$registry();
7678
// @refresh granular
7779
const Example = createContext();
78-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
80+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
7981
return <Example.Provider>Foo</Example.Provider>;
8082
}, {
8183
location: \\"example.jsx:4:21\\",
@@ -84,6 +86,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
8486
Example
8587
}
8688
});
89+
export default Foo;
8790
if (import.meta.webpackHot) {
8891
_$$refresh(\\"rspack-esm\\", import.meta.webpackHot, _REGISTRY);
8992
}"
@@ -94,11 +97,12 @@ exports[`rspack-esm > ExportDefaultDeclaration w/ FunctionExpression > should tr
9497
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
9598
import { $$registry as _$$registry } from \\"solid-refresh\\";
9699
const _REGISTRY = _$$registry();
97-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
100+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
98101
return <h1>Foo</h1>;
99102
}, {
100103
location: \\"example.jsx:2:21\\"
101104
});
105+
export default Foo;
102106
if (import.meta.webpackHot) {
103107
_$$refresh(\\"rspack-esm\\", import.meta.webpackHot, _REGISTRY);
104108
}"
@@ -109,11 +113,12 @@ exports[`rspack-esm > ExportDefaultDeclaration w/ FunctionExpression > should tr
109113
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
110114
import { $$registry as _$$registry } from \\"solid-refresh\\";
111115
const _REGISTRY = _$$registry();
112-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
116+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
113117
return <h1>Foo</h1>;
114118
}, {
115119
location: \\"example.jsx:2:21\\"
116120
});
121+
export default Foo;
117122
if (import.meta.webpackHot) {
118123
_$$refresh(\\"rspack-esm\\", import.meta.webpackHot, _REGISTRY);
119124
}"

tests/__snapshots__/standard.test.ts.snap

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ exports[`vite > ExportDefaultDeclaration w/ FunctionExpression > should transfor
3535
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
3636
import { $$registry as _$$registry } from \\"solid-refresh\\";
3737
const _REGISTRY = _$$registry();
38-
// @refresh granular
39-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
38+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
4039
return <h1>Foo</h1>;
4140
}, {
4241
location: \\"example.jsx:3:21\\",
4342
signature: \\"83fbb41\\"
4443
});
44+
// @refresh granular
45+
export default Foo;
4546
if (module.hot) {
4647
_$$refresh(\\"standard\\", module.hot, _REGISTRY);
4748
}"
@@ -54,7 +55,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
5455
const _REGISTRY = _$$registry();
5556
// @refresh granular
5657
const example = 'Foo';
57-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
58+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
5859
return <h1>{example}</h1>;
5960
}, {
6061
location: \\"example.jsx:4:21\\",
@@ -63,6 +64,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
6364
example
6465
}
6566
});
67+
export default Foo;
6668
if (module.hot) {
6769
_$$refresh(\\"standard\\", module.hot, _REGISTRY);
6870
}"
@@ -75,7 +77,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
7577
const _REGISTRY = _$$registry();
7678
// @refresh granular
7779
const Example = createContext();
78-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
80+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
7981
return <Example.Provider>Foo</Example.Provider>;
8082
}, {
8183
location: \\"example.jsx:4:21\\",
@@ -84,6 +86,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
8486
Example
8587
}
8688
});
89+
export default Foo;
8790
if (module.hot) {
8891
_$$refresh(\\"standard\\", module.hot, _REGISTRY);
8992
}"
@@ -94,11 +97,12 @@ exports[`vite > ExportDefaultDeclaration w/ FunctionExpression > should transfor
9497
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
9598
import { $$registry as _$$registry } from \\"solid-refresh\\";
9699
const _REGISTRY = _$$registry();
97-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
100+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
98101
return <h1>Foo</h1>;
99102
}, {
100103
location: \\"example.jsx:2:21\\"
101104
});
105+
export default Foo;
102106
if (module.hot) {
103107
_$$refresh(\\"standard\\", module.hot, _REGISTRY);
104108
}"
@@ -109,11 +113,12 @@ exports[`vite > ExportDefaultDeclaration w/ FunctionExpression > should transfor
109113
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
110114
import { $$registry as _$$registry } from \\"solid-refresh\\";
111115
const _REGISTRY = _$$registry();
112-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
116+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
113117
return <h1>Foo</h1>;
114118
}, {
115119
location: \\"example.jsx:2:21\\"
116120
});
121+
export default Foo;
117122
if (module.hot) {
118123
_$$refresh(\\"standard\\", module.hot, _REGISTRY);
119124
}"

tests/__snapshots__/vite.test.ts.snap

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ exports[`vite > ExportDefaultDeclaration w/ FunctionExpression > should transfor
3636
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
3737
import { $$registry as _$$registry } from \\"solid-refresh\\";
3838
const _REGISTRY = _$$registry();
39-
// @refresh granular
40-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
39+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
4140
return <h1>Foo</h1>;
4241
}, {
4342
location: \\"example.jsx:3:21\\",
4443
signature: \\"83fbb41\\"
4544
});
45+
// @refresh granular
46+
export default Foo;
4647
if (import.meta.hot) {
4748
_$$refresh(\\"vite\\", import.meta.hot, _REGISTRY);
4849
import.meta.hot.accept();
@@ -56,7 +57,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
5657
const _REGISTRY = _$$registry();
5758
// @refresh granular
5859
const example = 'Foo';
59-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
60+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
6061
return <h1>{example}</h1>;
6162
}, {
6263
location: \\"example.jsx:4:21\\",
@@ -65,6 +66,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
6566
example
6667
}
6768
});
69+
export default Foo;
6870
if (import.meta.hot) {
6971
_$$refresh(\\"vite\\", import.meta.hot, _REGISTRY);
7072
import.meta.hot.accept();
@@ -78,7 +80,7 @@ import { $$registry as _$$registry } from \\"solid-refresh\\";
7880
const _REGISTRY = _$$registry();
7981
// @refresh granular
8082
const Example = createContext();
81-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
83+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
8284
return <Example.Provider>Foo</Example.Provider>;
8385
}, {
8486
location: \\"example.jsx:4:21\\",
@@ -87,6 +89,7 @@ export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
8789
Example
8890
}
8991
});
92+
export default Foo;
9093
if (import.meta.hot) {
9194
_$$refresh(\\"vite\\", import.meta.hot, _REGISTRY);
9295
import.meta.hot.accept();
@@ -98,11 +101,12 @@ exports[`vite > ExportDefaultDeclaration w/ FunctionExpression > should transfor
98101
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
99102
import { $$registry as _$$registry } from \\"solid-refresh\\";
100103
const _REGISTRY = _$$registry();
101-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo() {
104+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo() {
102105
return <h1>Foo</h1>;
103106
}, {
104107
location: \\"example.jsx:2:21\\"
105108
});
109+
export default Foo;
106110
if (import.meta.hot) {
107111
_$$refresh(\\"vite\\", import.meta.hot, _REGISTRY);
108112
import.meta.hot.accept();
@@ -114,11 +118,12 @@ exports[`vite > ExportDefaultDeclaration w/ FunctionExpression > should transfor
114118
import { $$refresh as _$$refresh } from \\"solid-refresh\\";
115119
import { $$registry as _$$registry } from \\"solid-refresh\\";
116120
const _REGISTRY = _$$registry();
117-
export default _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
121+
var Foo = _$$component(_REGISTRY, \\"Foo\\", function Foo(props) {
118122
return <h1>Foo</h1>;
119123
}, {
120124
location: \\"example.jsx:2:21\\"
121125
});
126+
export default Foo;
122127
if (import.meta.hot) {
123128
_$$refresh(\\"vite\\", import.meta.hot, _REGISTRY);
124129
import.meta.hot.accept();

0 commit comments

Comments
 (0)