Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for JSX dev runtime #15

Merged
merged 3 commits into from
Sep 8, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions jsx-dev-runtime.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @typedef {import('xast').Element} Element
* @typedef {import('xast').Root} Root
* @typedef {import('./lib/index.js').XChild} XChild
* @typedef {import('./lib/runtime.js').JSXProps} JSXProps
*/

import {jsx} from './jsx-runtime.js'

export {Fragment} from './jsx-runtime.js'

export const jsxDEV =
/**
* @type {{
* (name: null|undefined, props: {children?: XChild}, ...unused: unknown[]): Root
* (name: string, props: JSXProps, ...unused: unknown[]): Element
* }}
*/
(
function (name, props) {
return jsx(/** @type {string} */ (name), props)
}
)
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,15 @@
"lib/",
"index.d.ts",
"index.js",
"jsx-dev-runtime.d.ts",
"jsx-dev-runtime.js",
"jsx-runtime.d.ts",
"jsx-runtime.js"
],
"exports": {
".": "./index.js",
"./index.js": "./index.js",
"./jsx-dev-runtime": "./jsx-dev-runtime.js",
"./jsx-runtime": "./jsx-runtime.js"
},
"dependencies": {
Expand Down
49 changes: 38 additions & 11 deletions script/generate-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,26 @@ fs.writeFileSync(
// @ts-ignore Hush, `2021` is fine.
{sourceType: 'module', ecmaVersion: 2021}
),
{runtime: 'automatic', importSource: '.'}
{runtime: 'automatic', importSource: 'xastscript'}
)
// @ts-expect-error Some bug in `to-js`
).value.replace(/\/jsx-runtime(?=["'])/g, './lib/runtime.js')
).value
)

fs.writeFileSync(
path.join('test', 'jsx-build-jsx-automatic-development.js'),
toJs(
// @ts-expect-error it’s a program.
buildJsx(
// @ts-expect-error Acorn nodes are assignable to ESTree nodes.
Parser.extend(acornJsx()).parse(
doc.replace(/'name'/, "'jsx (estree-util-build-jsx, automatic)'"),
{sourceType: 'module', ecmaVersion: 2021}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might error (it just did for me on the above ones, see the ts-ignore for more info

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh it passes, weird 🤷‍♂️ well nvm then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I had to resolve some merge conflicts here. Not sure what happened upstream.

),
{runtime: 'automatic', importSource: 'xastscript', development: true}
)
// @ts-expect-error Some bug in `to-js`
).value
)

fs.writeFileSync(
Expand All @@ -57,14 +73,25 @@ fs.writeFileSync(
fs.writeFileSync(
path.join('test', 'jsx-babel-automatic.js'),
// @ts-expect-error Result always given.
babel
.transformSync(doc.replace(/'name'/, "'jsx (babel, automatic)'"), {
plugins: [
[
'@babel/plugin-transform-react-jsx',
{runtime: 'automatic', importSource: '.'}
]
babel.transformSync(doc.replace(/'name'/, "'jsx (babel, automatic)'"), {
plugins: [
[
'@babel/plugin-transform-react-jsx',
{runtime: 'automatic', importSource: 'xastscript'}
]
})
.code.replace(/\/jsx-runtime(?=["'])/g, './lib/runtime.js')
]
}).code
)

fs.writeFileSync(
path.join('test', 'jsx-babel-automatic-development.js'),
// @ts-expect-error Result always given.
babel.transformSync(doc.replace(/'name'/, "'jsx (babel, automatic)'"), {
plugins: [
[
'@babel/plugin-transform-react-jsx',
{runtime: 'automatic', importSource: 'xastscript', development: true}
]
]
}).code
)
2 changes: 1 addition & 1 deletion test-d/automatic.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* @jsxRuntime automatic */
/* @jsxImportSource .. */
/* @jsxImportSource xastscript */

import {expectType, expectError} from 'tsd'
import type {Root, Element} from 'xast'
Expand Down
3 changes: 1 addition & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
"compilerOptions": {
"target": "ES2020",
"lib": ["ES2020"],
"module": "ES2020",
"moduleResolution": "node",
"module": "Node16",
"allowJs": true,
"checkJs": true,
"declaration": true,
Expand Down