From a8f8114853ebce1d2ccd62cb7428366df0424127 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Thu, 17 Jan 2019 14:25:50 +0700 Subject: [PATCH 1/4] Add support for DocumentFragment in TypeScript Use as: import {React} from 'dom-chef'; --- index.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/index.js b/index.js index 59c6568..1ef1671 100644 --- a/index.js +++ b/index.js @@ -98,3 +98,9 @@ function h(tagName, attrs) { } exports.h = h; + +exports.React = { + createElement: h, + Fragment: DocumentFragment, +}; + From a2681ff85342af5e9620e021ab774c47cf9edc22 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Thu, 17 Jan 2019 14:28:19 +0700 Subject: [PATCH 2/4] =?UTF-8?q?Lint=20=F0=9F=A4=A6=E2=80=8D=E2=99=82?= =?UTF-8?q?=EF=B8=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 1ef1671..a9812d7 100644 --- a/index.js +++ b/index.js @@ -100,7 +100,7 @@ function h(tagName, attrs) { exports.h = h; exports.React = { - createElement: h, - Fragment: DocumentFragment, + createElement: h, + Fragment: DocumentFragment }; From db866911944a5ffc3c9a0091e3bb3dd6c76e3394 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Thu, 17 Jan 2019 14:30:32 +0700 Subject: [PATCH 3/4] =?UTF-8?q?Lint=20=F0=9F=A4=A6=E2=80=8D=E2=99=82?= =?UTF-8?q?=EF=B8=8F=F0=9F=A4=A6=E2=80=8D=E2=99=82=EF=B8=8F=20+=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index a9812d7..d068bfd 100644 --- a/index.js +++ b/index.js @@ -99,8 +99,9 @@ function h(tagName, attrs) { exports.h = h; +// Improve TypeScript support for DocumentFragment +// https://github.com/Microsoft/TypeScript/issues/20469 exports.React = { createElement: h, Fragment: DocumentFragment }; - From 3868596c906e49c95a667bf1eaf218a3fa308b80 Mon Sep 17 00:00:00 2001 From: Federico Brigante Date: Thu, 17 Jan 2019 14:51:20 +0700 Subject: [PATCH 4/4] Document and fix ava --- package.json | 3 ++- readme.md | 19 ++++++++++++++++--- test.js | 4 +++- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 18cc6c9..5764845 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,8 @@ "rules": { "react/prop-types": 0, "react/no-unknown-property": 0, - "react/no-danger": 0 + "react/no-danger": 0, + "import/first": 0 }, "settings": { "react": { diff --git a/readme.md b/readme.md index d0e536f..aa223dd 100644 --- a/readme.md +++ b/readme.md @@ -30,7 +30,7 @@ $ npm install --save dom-chef ## Usage Make sure to use a JSX transpiler, set JSX [`pragma`](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html#pragma) -to `h` and optinally the [`pragmaFrag`](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html#pragmafrag) +to `h` and optionally the [`pragmaFrag`](https://babeljs.io/docs/en/next/babel-plugin-transform-react-jsx.html#pragmafrag) to `DocumentFragment` [if you need fragment support](https://reactjs.org/blog/2017/11/28/react-v16.2.0-fragment-support.html). ```js @@ -40,8 +40,8 @@ const plugins = [ [ '@babel/plugin-transform-react-jsx', { - pragma: 'h', - pragmaFrag: 'DocumentFragment', + pragma: 'h', + pragmaFrag: 'DocumentFragment', } ] ]; @@ -65,6 +65,19 @@ const el = ( document.body.appendChild(el); ``` +### Alternative usage + +You can avoid configuring your JSX compiler by just letting it default to `React` and exporting the `React` object: + +```js +const {React} = require('dom-chef'); +``` + +This has the advantage of enabling `Fragment` support with the TypeScript compiler, if you're using it compile JSX without Babel. Related issue: https://github.com/Microsoft/TypeScript/issues/20469 + +``` +TS17016: JSX fragment is not supported when using --jsxFactory +``` ## Recipes diff --git a/test.js b/test.js index 7581a9c..057b5b1 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,11 @@ import browserEnv from 'browser-env'; import {spy} from 'sinon'; import test from 'ava'; -import {h} from '.'; +// This order and `require` are necessary because the +// `DocumentFragment` global is used/exported right away browserEnv(); +const {h} = require('.'); test('render childless element', t => { const el =
;