Fix for using React in the static build (#2369)
* Fix react in the static build * Adds a changeset
This commit is contained in:
parent
500295395c
commit
20eaddb2a7
8 changed files with 63 additions and 8 deletions
5
.changeset/fuzzy-peas-nail.md
Normal file
5
.changeset/fuzzy-peas-nail.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/renderer-react': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Fix for using the React renderer with the static build
|
18
packages/astro/test/fixtures/static-build-frameworks/src/components/RCounter.jsx
vendored
Normal file
18
packages/astro/test/fixtures/static-build-frameworks/src/components/RCounter.jsx
vendored
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { useState } from 'react';
|
||||||
|
|
||||||
|
export default function Counter({ children }) {
|
||||||
|
const [count, setCount] = useState(0);
|
||||||
|
const add = () => setCount((i) => i + 1);
|
||||||
|
const subtract = () => setCount((i) => i - 1);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="counter">
|
||||||
|
<button onClick={subtract}>-</button>
|
||||||
|
<pre>{count}</pre>
|
||||||
|
<button onClick={add}>+</button>
|
||||||
|
</div>
|
||||||
|
<div className="counter-message">{children}</div>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
12
packages/astro/test/fixtures/static-build-frameworks/src/pages/react.astro
vendored
Normal file
12
packages/astro/test/fixtures/static-build-frameworks/src/pages/react.astro
vendored
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
---
|
||||||
|
import RCounter from '../components/RCounter.jsx';
|
||||||
|
---
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Testing</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Testing</h1>
|
||||||
|
<RCounter client:load />
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -2,17 +2,16 @@ import { expect } from 'chai';
|
||||||
import cheerio from 'cheerio';
|
import cheerio from 'cheerio';
|
||||||
import { loadFixture } from './test-utils.js';
|
import { loadFixture } from './test-utils.js';
|
||||||
|
|
||||||
function addLeadingSlash(path) {
|
|
||||||
return path.startsWith('/') ? path : '/' + path;
|
|
||||||
}
|
|
||||||
|
|
||||||
describe('Static build - frameworks', () => {
|
describe('Static build - frameworks', () => {
|
||||||
let fixture;
|
let fixture;
|
||||||
|
|
||||||
before(async () => {
|
before(async () => {
|
||||||
fixture = await loadFixture({
|
fixture = await loadFixture({
|
||||||
projectRoot: './fixtures/static-build-frameworks/',
|
projectRoot: './fixtures/static-build-frameworks/',
|
||||||
renderers: ['@astrojs/renderer-preact'],
|
renderers: [
|
||||||
|
'@astrojs/renderer-preact',
|
||||||
|
'@astrojs/renderer-react'
|
||||||
|
],
|
||||||
buildOptions: {
|
buildOptions: {
|
||||||
experimentalStaticBuild: true,
|
experimentalStaticBuild: true,
|
||||||
},
|
},
|
||||||
|
@ -21,7 +20,12 @@ describe('Static build - frameworks', () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
it('can build preact', async () => {
|
it('can build preact', async () => {
|
||||||
const html = await fixture.readFile('/index.html');
|
const html = await fixture.readFile('/preact/index.html');
|
||||||
|
expect(html).to.be.a('string');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('can build react', async () => {
|
||||||
|
const html = await fixture.readFile('/react/index.html');
|
||||||
expect(html).to.be.a('string');
|
expect(html).to.be.a('string');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -8,7 +8,10 @@ export default {
|
||||||
default: { default: jsx },
|
default: { default: jsx },
|
||||||
} = await import('@babel/plugin-transform-react-jsx');
|
} = await import('@babel/plugin-transform-react-jsx');
|
||||||
return {
|
return {
|
||||||
plugins: [jsx({}, { runtime: 'automatic', importSource: 'react' })],
|
plugins: [jsx({}, {
|
||||||
|
runtime: 'automatic',
|
||||||
|
importSource: '@astrojs/renderer-react'
|
||||||
|
})],
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
viteConfig() {
|
viteConfig() {
|
||||||
|
|
12
packages/renderers/renderer-react/jsx-runtime.js
Normal file
12
packages/renderers/renderer-react/jsx-runtime.js
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
// This module is a simple wrapper around react/jsx-runtime so that
|
||||||
|
// it can run in Node ESM. 'react' doesn't declare this module as an export map
|
||||||
|
// So we have to use the .js. The .js is not added via the babel automatic JSX transform
|
||||||
|
// hence this module as a workaround.
|
||||||
|
import jsxr from 'react/jsx-runtime.js';
|
||||||
|
const { jsx, jsxs, Fragment } = jsxr;
|
||||||
|
|
||||||
|
export {
|
||||||
|
jsx,
|
||||||
|
jsxs,
|
||||||
|
Fragment
|
||||||
|
};
|
|
@ -17,7 +17,8 @@
|
||||||
"./*": "./*",
|
"./*": "./*",
|
||||||
"./client.js": "./client.js",
|
"./client.js": "./client.js",
|
||||||
"./server.js": "./server.js",
|
"./server.js": "./server.js",
|
||||||
"./package.json": "./package.json"
|
"./package.json": "./package.json",
|
||||||
|
"./jsx-runtime": "./jsx-runtime.js"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@babel/plugin-transform-react-jsx": "^7.16.0",
|
"@babel/plugin-transform-react-jsx": "^7.16.0",
|
||||||
|
|
Loading…
Reference in a new issue