From 62f84c30bb940258f3dd44982aeb19f851540a9b Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Tue, 6 Sep 2022 20:50:52 +0200 Subject: [PATCH 1/3] Support JSX dev runtime --- jsx-dev-runtime.js | 22 +++++++++++++++++++ package.json | 3 +++ script/generate-jsx.js | 49 ++++++++++++++++++++++++++++++++---------- test-d/automatic.tsx | 2 +- tsconfig.json | 3 +-- 5 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 jsx-dev-runtime.js diff --git a/jsx-dev-runtime.js b/jsx-dev-runtime.js new file mode 100644 index 0000000..4eed232 --- /dev/null +++ b/jsx-dev-runtime.js @@ -0,0 +1,22 @@ +/** + * @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 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) + } + ) +export {Fragment} from './jsx-runtime.js' diff --git a/package.json b/package.json index 3678641..5d62812 100644 --- a/package.json +++ b/package.json @@ -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": { diff --git a/script/generate-jsx.js b/script/generate-jsx.js index 8484b87..4411102 100644 --- a/script/generate-jsx.js +++ b/script/generate-jsx.js @@ -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} + ), + {runtime: 'automatic', importSource: 'xastscript', development: true} + ) + // @ts-expect-error Some bug in `to-js` + ).value ) fs.writeFileSync( @@ -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 ) diff --git a/test-d/automatic.tsx b/test-d/automatic.tsx index dc3f4e8..61f11e8 100644 --- a/test-d/automatic.tsx +++ b/test-d/automatic.tsx @@ -1,5 +1,5 @@ /* @jsxRuntime automatic */ -/* @jsxImportSource .. */ +/* @jsxImportSource xastscript */ import {expectType, expectError} from 'tsd' import type {Root, Element} from 'xast' diff --git a/tsconfig.json b/tsconfig.json index ec45143..ab14bdd 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -11,8 +11,7 @@ "compilerOptions": { "target": "ES2020", "lib": ["ES2020"], - "module": "ES2020", - "moduleResolution": "node", + "module": "Node16", "allowJs": true, "checkJs": true, "declaration": true, From 6f273e74b970fbab938ffb8c07d3991c9a586549 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Tue, 6 Sep 2022 21:03:34 +0200 Subject: [PATCH 2/3] Fix formatting issue --- jsx-dev-runtime.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/jsx-dev-runtime.js b/jsx-dev-runtime.js index 4eed232..0e872fc 100644 --- a/jsx-dev-runtime.js +++ b/jsx-dev-runtime.js @@ -7,6 +7,8 @@ import {jsx} from './jsx-runtime.js' +export {Fragment} from './jsx-runtime.js' + export const jsxDEV = /** * @type {{ @@ -19,4 +21,3 @@ export const jsxDEV = return jsx(/** @type {string} */ (name), props) } ) -export {Fragment} from './jsx-runtime.js' From 8f225043bea65abb0119d14136a2f934a0ac9392 Mon Sep 17 00:00:00 2001 From: Remco Haszing Date: Thu, 8 Sep 2022 18:12:34 +0200 Subject: [PATCH 3/3] Simplify jsxDEV implementation --- jsx-dev-runtime.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/jsx-dev-runtime.js b/jsx-dev-runtime.js index 0e872fc..dccba61 100644 --- a/jsx-dev-runtime.js +++ b/jsx-dev-runtime.js @@ -9,6 +9,7 @@ import {jsx} from './jsx-runtime.js' export {Fragment} from './jsx-runtime.js' +// eslint-disable-next-line unicorn/prefer-export-from export const jsxDEV = /** * @type {{ @@ -16,8 +17,4 @@ export const jsxDEV = * (name: string, props: JSXProps, ...unused: unknown[]): Element * }} */ - ( - function (name, props) { - return jsx(/** @type {string} */ (name), props) - } - ) + (jsx)