Skip to content

Commit d0b4c88

Browse files
authored
Add support for JSX dev runtime
Closes Gh-15. Reviewed-by: Christian Murphy <[email protected]> Reviewed-by: Titus Wormer <[email protected]>
1 parent e2d664e commit d0b4c88

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed

Diff for: jsx-dev-runtime.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* @typedef {import('xast').Element} Element
3+
* @typedef {import('xast').Root} Root
4+
* @typedef {import('./lib/index.js').XChild} XChild
5+
* @typedef {import('./lib/runtime.js').JSXProps} JSXProps
6+
*/
7+
8+
import {jsx} from './jsx-runtime.js'
9+
10+
export {Fragment} from './jsx-runtime.js'
11+
12+
// eslint-disable-next-line unicorn/prefer-export-from
13+
export const jsxDEV =
14+
/**
15+
* @type {{
16+
* (name: null|undefined, props: {children?: XChild}, ...unused: unknown[]): Root
17+
* (name: string, props: JSXProps, ...unused: unknown[]): Element
18+
* }}
19+
*/
20+
(jsx)

Diff for: package.json

+3
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,15 @@
3333
"lib/",
3434
"index.d.ts",
3535
"index.js",
36+
"jsx-dev-runtime.d.ts",
37+
"jsx-dev-runtime.js",
3638
"jsx-runtime.d.ts",
3739
"jsx-runtime.js"
3840
],
3941
"exports": {
4042
".": "./index.js",
4143
"./index.js": "./index.js",
44+
"./jsx-dev-runtime": "./jsx-dev-runtime.js",
4245
"./jsx-runtime": "./jsx-runtime.js"
4346
},
4447
"dependencies": {

Diff for: script/generate-jsx.js

+38-11
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,26 @@ fs.writeFileSync(
3838
// @ts-ignore Hush, `2021` is fine.
3939
{sourceType: 'module', ecmaVersion: 2021}
4040
),
41-
{runtime: 'automatic', importSource: '.'}
41+
{runtime: 'automatic', importSource: 'xastscript'}
4242
)
4343
// @ts-expect-error Some bug in `to-js`
44-
).value.replace(/\/jsx-runtime(?=["'])/g, './lib/runtime.js')
44+
).value
45+
)
46+
47+
fs.writeFileSync(
48+
path.join('test', 'jsx-build-jsx-automatic-development.js'),
49+
toJs(
50+
// @ts-expect-error it’s a program.
51+
buildJsx(
52+
// @ts-expect-error Acorn nodes are assignable to ESTree nodes.
53+
Parser.extend(acornJsx()).parse(
54+
doc.replace(/'name'/, "'jsx (estree-util-build-jsx, automatic)'"),
55+
{sourceType: 'module', ecmaVersion: 2021}
56+
),
57+
{runtime: 'automatic', importSource: 'xastscript', development: true}
58+
)
59+
// @ts-expect-error Some bug in `to-js`
60+
).value
4561
)
4662

4763
fs.writeFileSync(
@@ -57,14 +73,25 @@ fs.writeFileSync(
5773
fs.writeFileSync(
5874
path.join('test', 'jsx-babel-automatic.js'),
5975
// @ts-expect-error Result always given.
60-
babel
61-
.transformSync(doc.replace(/'name'/, "'jsx (babel, automatic)'"), {
62-
plugins: [
63-
[
64-
'@babel/plugin-transform-react-jsx',
65-
{runtime: 'automatic', importSource: '.'}
66-
]
76+
babel.transformSync(doc.replace(/'name'/, "'jsx (babel, automatic)'"), {
77+
plugins: [
78+
[
79+
'@babel/plugin-transform-react-jsx',
80+
{runtime: 'automatic', importSource: 'xastscript'}
6781
]
68-
})
69-
.code.replace(/\/jsx-runtime(?=["'])/g, './lib/runtime.js')
82+
]
83+
}).code
84+
)
85+
86+
fs.writeFileSync(
87+
path.join('test', 'jsx-babel-automatic-development.js'),
88+
// @ts-expect-error Result always given.
89+
babel.transformSync(doc.replace(/'name'/, "'jsx (babel, automatic)'"), {
90+
plugins: [
91+
[
92+
'@babel/plugin-transform-react-jsx',
93+
{runtime: 'automatic', importSource: 'xastscript', development: true}
94+
]
95+
]
96+
}).code
7097
)

Diff for: test-d/automatic.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* @jsxRuntime automatic */
2-
/* @jsxImportSource .. */
2+
/* @jsxImportSource xastscript */
33

44
import {expectType, expectError} from 'tsd'
55
import type {Root, Element} from 'xast'

Diff for: tsconfig.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
"compilerOptions": {
1212
"target": "ES2020",
1313
"lib": ["ES2020"],
14-
"module": "ES2020",
15-
"moduleResolution": "node",
14+
"module": "Node16",
1615
"allowJs": true,
1716
"checkJs": true,
1817
"declaration": true,

0 commit comments

Comments
 (0)