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

Fix preact compat support for libraries #4213

Merged
merged 7 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add test
  • Loading branch information
bluwy committed Aug 9, 2022
commit 66cc9241c696c624395b5864ab265ab572d4d37c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { defineConfig } from 'astro/config';
import preact from '@astrojs/preact';

// https://astro.build/config
export default defineConfig({
integrations: [preact({ compat: true })],
});
11 changes: 11 additions & 0 deletions packages/astro/test/fixtures/preact-compat-component/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@test/preact-compat-component",
"version": "0.0.0",
"private": true,
"dependencies": {
"@astrojs/preact": "workspace:*",
"@test/react-lib": "workspace:*",
"astro": "workspace:*",
"preact": "^10.10.1"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { useState } from "react";

export function useSpecialState(initialState) {
return useState(initialState);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@test/react-lib",
"version": "0.0.0",
"private": true,
"type": "module",
"dependencies": {
"react": "^18.2.0"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/** @jsxImportSource preact */
import { useSpecialState } from '@test/react-lib'

export default function Counter({ children }) {
const [count, setCount] = useSpecialState(0);
const add = () => setCount((i) => i + 1);
const subtract = () => setCount((i) => i - 1);

return (
<>
<div class="counter">
<button onClick={subtract}>-</button>
<pre id="counter-text">{count}</pre>
<button onClick={add}>+</button>
</div>
<div class="counter-message">{children}</div>
</>
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
import Counter from '../components/Counter.jsx';
---

<html>
<head>
<title>Preact compat component</title>
</head>
<body>
<Counter client:load />
</body>
</html>
41 changes: 41 additions & 0 deletions packages/astro/test/preact-compat-component.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { expect } from 'chai';
import * as cheerio from 'cheerio';
import { loadFixture } from './test-utils.js';

describe('Preact compat component', () => {
describe('Development', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/preact-compat-component/',
});
await fixture.startDevServer();
});

it('Can load Counter', async () => {
const html = await fixture.fetch('/').then((res) => res.text());
const $ = cheerio.load(html);

expect($('#counter-text').text()).to.be.eq('0');
});
});

describe('Build', () => {
let fixture;

before(async () => {
fixture = await loadFixture({
root: './fixtures/preact-compat-component/',
});
await fixture.build();
});

it('Can load Counter', async () => {
const html = await fixture.readFile('/index.html');
const $ = cheerio.load(html);

expect($('#counter-text').text()).to.be.eq('0');
});
});
});
43 changes: 18 additions & 25 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.