diff options
Diffstat (limited to 'packages/integrations/preact')
-rw-r--r-- | packages/integrations/preact/README.md | 126 | ||||
-rw-r--r-- | packages/integrations/preact/package.json | 1 | ||||
-rw-r--r-- | packages/integrations/preact/src/index.ts | 58 |
3 files changed, 152 insertions, 33 deletions
diff --git a/packages/integrations/preact/README.md b/packages/integrations/preact/README.md index c31b9e35c..d414b8af2 100644 --- a/packages/integrations/preact/README.md +++ b/packages/integrations/preact/README.md @@ -2,57 +2,80 @@ This **[Astro integration][astro-integration]** enables server-side rendering and client-side hydration for your [Preact](https://preactjs.com/) components. -## Installation +- <strong>[Why Preact?](#why-preact)</strong> +- <strong>[Installation](#installation)</strong> +- <strong>[Usage](#usage)</strong> +- <strong>[Configuration](#configuration)</strong> +- <strong>[Examples](#examples)</strong> +- <strong>[Troubleshooting](#troubleshooting)</strong> +- <strong>[Contributing](#contributing)</strong> +- <strong>[Changelog](#changelog)</strong> -There are two ways to add integrations to your project. Let's try the most convenient option first! +## Why Preact? -### `astro add` command +Preact is a library that lets you build interactive UI components for the web. If you want to build interactive features on your site using JavaScript, you may prefer using its component format instead of using browser APIs directly. -Astro includes a CLI tool for adding first party integrations: `astro add`. This command will: -1. (Optionally) Install all necessary dependencies and peer dependencies -2. (Also optionally) Update your `astro.config.*` file to apply this integration +Preact is also a great choice if you have previously used React. Preact provides the same API as React, but in a much smaller 3kB package. It even supports rendering many React components using the `compat` configuration option (see below). -To install `@astrojs/preact`, run the following from your project directory and follow the prompts: +**Want to learn more about Preact before using this integration?** +Check out [“Learn Preact in 10 minutes”](https://preactjs.com/tutorial), an interactive tutorial on their website. -```sh -# Using NPM -npx astro add preact -# Using Yarn -yarn astro add preact -# Using PNPM -pnpx astro add preact -``` +## Installation -If you run into any hiccups, [feel free to log an issue on our GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below. +<details> + <summary>Quick Install</summary> + <br/> -### Install dependencies manually +The `astro add` command-line tool automates the installation for you. Run one of the following commands in a new terminal window. (If you aren't sure which package manager you're using, run the first command.) Then, follow the prompts, and type "y" in the terminal (meaning "yes") for each one. -First, install the `@astrojs/preact` integration like so: + ```sh + # Using NPM + npx astro add preact + # Using Yarn + yarn astro add preact + # Using PNPM + pnpx astro add preact + ``` -``` -npm install @astrojs/preact -``` +Then, restart the dev server by typing `CTRL-C` and then `npm run astro dev` in the terminal window that was running Astro. + +Because this command is new, it might not properly set things up. If that happens, [feel free to log an issue on our GitHub](https://github.com/withastro/astro/issues) and try the manual installation steps below. +</details> + +<details> + <summary>Manual Install</summary> + <br/> + +First, install the `@astrojs/preact` package using your package manager. If you're using npm or aren't sure, run this in the terminal: + + ``` + npm install @astrojs/preact + ``` Most package managers will install associated peer dependencies as well. Still, if you see a "Cannot find package 'preact'" (or similar) warning when you start up Astro, you'll need to install Preact: -```sh -npm install preact -``` + ```sh + npm install preact + ``` -Now, apply this integration to your `astro.config.*` file using the `integrations` property: +Then, apply this integration to your `astro.config.*` file using the `integrations` property: __astro.config.mjs__ ```js +import { defineConfig } from 'astro/config'; import preact from '@astrojs/preact'; -export default { +export default defineConfig({ // ... integrations: [preact()], -} +}); ``` -## Getting started +Finally, restart the dev server. +</details> + +## Usage To use your first Preact component in Astro, head to our [UI framework documentation][astro-ui-frameworks]. You'll explore: - 📦 how framework components are loaded, @@ -61,5 +84,52 @@ To use your first Preact component in Astro, head to our [UI framework documenta Also check our [Astro Integration Documentation][astro-integration] for more on integrations. +## Configuration + +The Astro Preact integration handles how Preact components are rendered and it has its own options. Change these in the `astro.config.mjs` file which is where your project's integration settings live. + +For basic usage, you do not need to configure the Preact integration. + +<details> + <summary><strong>compat</strong></summary> + +You can enable `preact/compat`, Preact’s compatibility layer for rendering React components without needing to install or ship React’s larger libraries to your users’ web browsers. + +To do so, pass an object to the Preact integration and set `compat: true`. + +```js +// astro.config.mjs +import { defineConfig } from 'astro/config'; +import preact from '@astrojs/preact'; + +export default defineConfig({ + integrations: [ + preact({ compat: true }) + ], +}); +``` + +With the `compat` option enabled, the Preact integration will render React components as well as Preact components in your project and also allow you to import React components inside Preact components. Read more in [“Switching to Preact (from React)”](https://preactjs.com/guide/v10/switching-to-preact) on the Preact website. +</details> + +## Examples + +- The [Astro Preact example](https://github.com/withastro/astro/tree/latest/examples/framework-preact) shows how to use an interactive Preact component in an Astro project. +- The [Astro Nanostores example](https://github.com/withastro/astro/tree/latest/examples/with-nanostores) shows how to share state between different components — and even different frameworks! — in an Astro project. + +## Troubleshooting + +For help, check out the `#support-threads` channel on [Discord](https://astro.build/chat). Our friendly Support Squad members are here to help! + +You can also check our [Astro Integration Documentation][astro-integration] for more on integrations. + +## Contributing + +This package is maintained by Astro's Core team. You're welcome to submit an issue or PR! + +## Changelog + +See [CHANGELOG.md](CHANGELOG.md) for a history of changes to this integration. + [astro-integration]: https://docs.astro.build/en/guides/integrations-guide/ [astro-ui-frameworks]: https://docs.astro.build/en/core-concepts/framework-components/#using-framework-components diff --git a/packages/integrations/preact/package.json b/packages/integrations/preact/package.json index 7229e6b2e..256da25d6 100644 --- a/packages/integrations/preact/package.json +++ b/packages/integrations/preact/package.json @@ -31,6 +31,7 @@ }, "dependencies": { "@babel/plugin-transform-react-jsx": "^7.17.12", + "babel-plugin-module-resolver": "^4.1.0", "preact-render-to-string": "^5.2.0" }, "devDependencies": { diff --git a/packages/integrations/preact/src/index.ts b/packages/integrations/preact/src/index.ts index 4c01e66f7..7c7ad618d 100644 --- a/packages/integrations/preact/src/index.ts +++ b/packages/integrations/preact/src/index.ts @@ -1,6 +1,6 @@ -import { AstroIntegration } from 'astro'; +import { AstroIntegration, AstroRenderer, ViteUserConfig } from 'astro'; -function getRenderer() { +function getRenderer(): AstroRenderer { return { name: '@astrojs/preact', clientEntrypoint: '@astrojs/preact/client.js', @@ -18,8 +18,36 @@ function getRenderer() { }; } -function getViteConfiguration() { +function getCompatRenderer(): AstroRenderer { return { + name: '@astrojs/preact', + clientEntrypoint: '@astrojs/preact/client.js', + serverEntrypoint: '@astrojs/preact/server.js', + jsxImportSource: 'react', + jsxTransformOptions: async () => { + const { + default: { default: jsx }, + // @ts-expect-error types not found + } = await import('@babel/plugin-transform-react-jsx'); + return { + plugins: [ + jsx({}, { runtime: 'automatic', importSource: 'preact/compat' }), + ['babel-plugin-module-resolver', { + alias: { + 'react': 'preact/compat', + 'react-dom/test-utils': 'preact/test-utils', + 'react-dom': 'preact/compat', + 'react/jsx-runtime': 'preact/jsx-runtime' + } + }], + ], + }; + }, + }; +} + +function getViteConfiguration(compat?: boolean): ViteUserConfig { + const viteConfig: ViteUserConfig = { optimizeDeps: { include: [ '@astrojs/preact/client.js', @@ -33,16 +61,36 @@ function getViteConfiguration() { external: ['preact-render-to-string'], }, }; + + if (compat) { + viteConfig.optimizeDeps!.include!.push( + 'preact/compat', + 'preact/test-utils', + 'preact/compat/jsx-runtime', + ); + viteConfig.resolve = { + alias: [ + { find: 'react', replacement: 'preact/compat' }, + { find: 'react-dom/test-utils', replacement: 'preact/test-utils' }, + { find: 'react-dom', replacement: 'preact/compat' }, + { find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' } + ], + dedupe: ['preact/compat'], + }; + } + + return viteConfig } -export default function (): AstroIntegration { +export default function ({ compat }: { compat?: boolean } = {}): AstroIntegration { return { name: '@astrojs/preact', hooks: { 'astro:config:setup': ({ addRenderer, updateConfig }) => { + if (compat) addRenderer(getCompatRenderer()); addRenderer(getRenderer()); updateConfig({ - vite: getViteConfiguration(), + vite: getViteConfiguration(compat), }); }, }, |