Skip to content

Commit 54e1c35

Browse files
authored
Merge pull request #11 from barelyhuman/fix/acorn-jsx-fragments
Fix: astring-jsx fragment
2 parents c805272 + 3e853e4 commit 54e1c35

File tree

11 files changed

+139
-26
lines changed

11 files changed

+139
-26
lines changed

lib/ast.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const acorn = require('acorn')
2-
const generate = require('astring-jsx')
2+
const generate = require('./astring')
33
const jsx = require('acorn-jsx')
44
const classFields = require('acorn-class-fields')
55
const { importAssertions } = require('acorn-import-assertions')

lib/astring.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Modified version of astring-jsx to handle
2+
// certain jsx elements that the upstream astring
3+
// doesn't handle yet
4+
// https://github.com/Qard/astring-jsx/issues/8
5+
6+
const astringJSX = require('astring-jsx')
7+
var extend = require('xtend')
8+
9+
const generator = Object.assign(
10+
{
11+
JSXFragment: function JSXFragment(node, state) {
12+
var output = state.output
13+
output.write('<>')
14+
for (child of node.children) {
15+
this[child.type](child, state)
16+
}
17+
output.write('</>')
18+
},
19+
JSXEmptyExpression: function JSXEmptyExpression(node, state) {
20+
// do nothing
21+
},
22+
JSXText: function JSXText(node, state) {
23+
var output = state.output
24+
output.write(node.value)
25+
},
26+
},
27+
astringJSX.generator
28+
)
29+
30+
function astring(ast, options) {
31+
return astringJSX(
32+
ast,
33+
extend(
34+
{
35+
generator: generator,
36+
},
37+
options
38+
)
39+
)
40+
}
41+
42+
astring.generator = generator
43+
module.exports = astring

lib/plugin.js

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,21 @@ function constructIslandClient(name, importPath) {
104104
import { render, h } from 'preact';
105105
106106
${readFileSync(resolve(__dirname, './client/helpers.js'), 'utf8')}
107+
108+
customElements.define("${islandName}", class Island${name} extends HTMLElement {
109+
constructor(){
110+
super();
111+
}
112+
113+
async connectedCallback() {
114+
const c = await import(${JSON.stringify(importPath)});
115+
const props = JSON.parse(this.dataset.props || '{}');
116+
mergePropsWithDOM(this,props);
117+
render(restoreTree(c.default, props), this, this)
118+
}
119+
})
107120
108-
customElements.define("${islandName}", class Island${name} extends HTMLElement {
109-
async connectedCallback() {
110-
const c = await import(${JSON.stringify(importPath)});
111-
const props = JSON.parse(this.dataset.props || '{}');
112-
mergePropsWithDOM(this,props);
113-
render(restoreTree(c.default, props), this, this)
114-
}
115-
})`
121+
`
116122
}
117123

118124
/**
@@ -145,7 +151,7 @@ function constructIslandServer(ast, name, options = {}) {
145151
code = `
146152
export default function Island${name}(props) {
147153
return h(Fragment,{},
148-
h("${islandName}",{ "data-props": JSON.stringify(props) },h(${name}, props)),
154+
h("${islandName}",{ "data-props": JSON.stringify(props) }, h(${name},props)),
149155
h("script",{async:true, src:"${path.join(
150156
baseURL,
151157
scriptBaseName

package-lock.json

Lines changed: 8 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@
4343
"acorn-static-class-features": "^1.0.0",
4444
"astring": "^1.8.6",
4545
"astring-jsx": "^1.0.1",
46-
"esbuild": "^0.18.11"
46+
"esbuild": "^0.18.11",
47+
"xtend": "^4.0.2"
4748
},
4849
"devDependencies": {
4950
"@babel/core": "^7.22.8",

playground/Container.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// @island
2+
3+
import { signal } from '@preact/signals'
4+
import Counter from './Counter'
5+
import TextIsland from './TextIsland'
6+
7+
const count = signal(0)
8+
9+
export default function Container({ children }) {
10+
return (
11+
<>
12+
<Counter value={count} inc={() => (count.value += 1)} />
13+
<Counter value={count} inc={() => (count.value += 1)} />
14+
<TextIsland />
15+
</>
16+
)
17+
}

playground/Counter.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
11
// @island
2-
import { useState } from 'preact/hooks'
3-
4-
export default function Counter() {
5-
const [count, setCount] = useState(0)
6-
7-
return <button onClick={() => setCount(count + 1)}>{count}</button>
2+
export default function Counter({ value, inc }) {
3+
return <button onClick={inc}>{value}</button>
84
}

playground/TextIsland.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @island
2+
import { useState } from 'preact/hooks'
3+
4+
export default function TextIsland() {
5+
const [message, setMessage] = useState('Some Text')
6+
return <a href="/">{message}</a>
7+
}

playground/package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

playground/package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,20 @@
1313
"author": "",
1414
"license": "ISC",
1515
"dependencies": {
16+
"@preact/signals": "^1.2.0",
1617
"esbuild": "^0.18.11",
1718
"express": "^4.18.2",
1819
"preact": "^10.15.1",
1920
"preact-render-to-string": "^6.1.0",
2021
"serve-static": "^1.15.0"
2122
},
2223
"devDependencies": {
23-
"@rollup/plugin-typescript": "^11.1.2",
24-
"rollup": "^3.26.2",
2524
"@babel/core": "^7.21.0",
25+
"@babel/plugin-transform-react-jsx": "^7.21.0",
2626
"@rollup/plugin-babel": "^6.0.3",
2727
"@rollup/plugin-node-resolve": "^15.0.1",
28-
"@babel/plugin-transform-react-jsx": "^7.21.0",
28+
"@rollup/plugin-typescript": "^11.1.2",
29+
"rollup": "^3.26.2",
2930
"tslib": "^2.6.0",
3031
"typescript": "^5.1.6"
3132
}

0 commit comments

Comments
 (0)