-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jsx): base for supporting more runtimes (#236)
Co-authored-by: Kent C. Dodds <[email protected]>
- Loading branch information
1 parent
9e5c958
commit 70cb04f
Showing
15 changed files
with
496 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
module.exports = require('../dist/client') | ||
module.exports = require('../dist/client/index.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../dist/client/jsx') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
{ | ||
"type": "commonjs", | ||
"main": "./index.js", | ||
"types": "./index.d.ts" | ||
"types": "./index.d.ts", | ||
"exports": { | ||
"react": "./react.js", | ||
"jsx": "./jsx.js" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../dist/client/index.js') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import './setup-tests.js' | ||
import { Hono } from "hono"; | ||
/* eslint-disable import/no-unresolved -- | ||
* imports paths are there in node_modules/hono/package.json | ||
* but it doesn't get resolved | ||
*/ | ||
import * as HonoJSX from "hono/jsx"; | ||
import * as HonoDOM from "hono/jsx/dom"; | ||
import * as _jsx_runtime from "hono/jsx/jsx-runtime"; | ||
/* eslint-enable import/no-unresolved */ | ||
import {suite} from 'uvu' | ||
import * as assert from 'uvu/assert' | ||
import {bundleMDX} from '../index.js' | ||
import {getMDXComponent} from '../client/jsx.js' | ||
|
||
const test = suite("hono"); | ||
|
||
const jsxBundlerConfig = { | ||
jsxLib: { | ||
varName: 'HonoJSX', | ||
package: 'hono/jsx', | ||
}, | ||
jsxDom: { | ||
varName: 'HonoDOM', | ||
package: 'hono/jsx/dom', | ||
}, | ||
jsxRuntime: { | ||
varName: '_jsx_runtime', | ||
package: 'hono/jsx/jsx-runtime', | ||
}, | ||
} | ||
const jsxComponentConfig = { HonoJSX, HonoDOM, _jsx_runtime } | ||
|
||
const mdxSource = ` | ||
--- | ||
title: Example Post | ||
published: 2021-02-13 | ||
description: This is some meta-data | ||
--- | ||
import Demo from './demo' | ||
# This is the title | ||
Here's a **neat** demo: | ||
<Demo /> | ||
`.trim(); | ||
|
||
const demoTsx = ` | ||
export default function Demo() { | ||
return <div>mdx-bundler with hono's runtime!</div> | ||
} | ||
`.trim(); | ||
|
||
|
||
test('smoke test for hono', async () => { | ||
|
||
const result = await bundleMDX({ | ||
source: mdxSource, | ||
jsxConfig: jsxBundlerConfig, | ||
files: { | ||
'./demo.tsx': demoTsx | ||
} | ||
}); | ||
|
||
/** @param {HonoJSX.DOMAttributes} props */ | ||
const SpanBold = ({ children }) => { | ||
return HonoJSX.createElement('span', { className: "strong" }, children) | ||
} | ||
|
||
const app = new Hono() | ||
.get("/", (c) => { | ||
const Component = getMDXComponent(result.code, jsxComponentConfig); | ||
return c.html(HonoJSX.jsx(Component, { components: { strong: SpanBold } }).toString()); | ||
}); | ||
|
||
const req = new Request("http://localhost/"); | ||
const res = await app.fetch(req); | ||
assert.equal(await res.text(), `<h1>This is the title</h1> | ||
<p>Here's a <span class="strong">neat</span> demo:</p> | ||
<div>mdx-bundler with hono's runtime!</div>`); | ||
}) | ||
|
||
test.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import './setup-tests.js' | ||
import * as Preact from "preact"; | ||
import * as PreactDOM from "preact/compat"; | ||
import * as _jsx_runtime from 'preact/jsx-runtime'; | ||
import {suite} from 'uvu' | ||
import * as assert from 'uvu/assert' | ||
import { render } from '@testing-library/preact' | ||
import {bundleMDX} from '../index.js' | ||
import {getMDXComponent} from '../client/jsx.js' | ||
|
||
const test = suite("preact"); | ||
|
||
const jsxBundlerConfig = { | ||
jsxLib: { | ||
varName: 'Preact', | ||
package: 'preact', | ||
}, | ||
jsxDom: { | ||
varName: 'PreactDom', | ||
package: 'preact/compat', | ||
}, | ||
jsxRuntime: { | ||
varName: '_jsx_runtime', | ||
package: 'preact/jsx-runtime', | ||
}, | ||
} | ||
const jsxComponentConfig = { Preact, PreactDOM, _jsx_runtime } | ||
|
||
const mdxSource = ` | ||
--- | ||
title: Example Post | ||
published: 2021-02-13 | ||
description: This is some meta-data | ||
--- | ||
import Demo from './demo' | ||
# This is the title | ||
Here's a **neat** demo: | ||
<Demo /> | ||
`.trim(); | ||
|
||
const demoTsx = ` | ||
export default function Demo() { | ||
return <div>mdx-bundler with Preact's runtime!</div> | ||
} | ||
`.trim(); | ||
|
||
|
||
test('smoke test for preact', async () => { | ||
|
||
const result = await bundleMDX({ | ||
source: mdxSource, | ||
jsxConfig: jsxBundlerConfig, | ||
files: { | ||
'./demo.tsx': demoTsx | ||
} | ||
}); | ||
|
||
/** | ||
* @type {import('preact').FunctionComponent<{ components?: Record<string, any> }>} | ||
*/ | ||
const Component = getMDXComponent(result.code, jsxComponentConfig) | ||
|
||
/** @type {Preact.FunctionComponent<{ children:Preact.ComponentChildren }>} props */ | ||
const SpanBold = ({children}) => { | ||
return Preact.createElement('span', { className: "strong" }, children) | ||
} | ||
|
||
assert.equal(result.frontmatter, { | ||
title: 'Example Post', | ||
published: new Date('2021-02-13'), | ||
description: 'This is some meta-data', | ||
}) | ||
|
||
const {container} = render( | ||
Preact.h(Component, {components: {strong: SpanBold}}) | ||
) | ||
|
||
assert.equal( | ||
container.innerHTML, | ||
`<h1>This is the title</h1> | ||
<p>Here's a <span class="strong">neat</span> demo:</p> | ||
<div>mdx-bundler with Preact's runtime!</div>`, | ||
) | ||
}) | ||
|
||
test.run() |
Oops, something went wrong.