diff options
Diffstat (limited to 'packages/integrations/preact')
-rw-r--r-- | packages/integrations/preact/CHANGELOG.md | 14 | ||||
-rw-r--r-- | packages/integrations/preact/package.json | 9 | ||||
-rw-r--r-- | packages/integrations/preact/src/index.ts | 120 | ||||
-rw-r--r-- | packages/integrations/preact/src/server.ts | 4 | ||||
-rw-r--r-- | packages/integrations/preact/tsconfig.json | 2 |
5 files changed, 68 insertions, 81 deletions
diff --git a/packages/integrations/preact/CHANGELOG.md b/packages/integrations/preact/CHANGELOG.md index 0add447c1..bd27174ad 100644 --- a/packages/integrations/preact/CHANGELOG.md +++ b/packages/integrations/preact/CHANGELOG.md @@ -1,5 +1,19 @@ # @astrojs/preact +## 3.0.0-beta.1 + +### Major Changes + +- [#7924](https://github.com/withastro/astro/pull/7924) [`519a1c4e8`](https://github.com/withastro/astro/commit/519a1c4e8407c7abcb8d879b67a9f4b960652cae) Thanks [@matthewp](https://github.com/matthewp)! - New `include` and `exclude` config options + + The Preact integration now has new `include` and `exclude` config options. Use these if you want to use Preact alongside another JSX framework; include specifies files to be compiled for Preact and `exclude` does the opposite. + +## 3.0.0-beta.0 + +### Major Changes + +- [`1eae2e3f7`](https://github.com/withastro/astro/commit/1eae2e3f7d693c9dfe91c8ccfbe606d32bf2fb81) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Remove support for Node 16. The lowest supported version by Astro and all integrations is now v18.14.1. As a reminder, Node 16 will be deprecated on the 11th September 2023. + ## 2.2.2 ### Patch Changes diff --git a/packages/integrations/preact/package.json b/packages/integrations/preact/package.json index b09808a8f..90d54ec25 100644 --- a/packages/integrations/preact/package.json +++ b/packages/integrations/preact/package.json @@ -1,7 +1,7 @@ { "name": "@astrojs/preact", "description": "Use Preact components within Astro", - "version": "2.2.2", + "version": "3.0.0-beta.1", "type": "module", "types": "./dist/index.d.ts", "author": "withastro", @@ -35,10 +35,11 @@ "dev": "astro-scripts dev \"src/**/*.ts\"" }, "dependencies": { - "@babel/core": "^7.22.5", "@babel/plugin-transform-react-jsx": "^7.22.5", + "@babel/plugin-transform-react-jsx-development": "^7.22.5", + "babel-plugin-transform-hook-names": "^1.0.2", + "@preact/preset-vite": "^2.5.0", "@preact/signals": "^1.1.3", - "babel-plugin-module-resolver": "^5.0.0", "preact-render-to-string": "^5.2.6" }, "devDependencies": { @@ -50,6 +51,6 @@ "preact": "^10.6.5" }, "engines": { - "node": ">=16.12.0" + "node": ">=18.14.1" } } diff --git a/packages/integrations/preact/src/index.ts b/packages/integrations/preact/src/index.ts index 98a2dd205..9551f97eb 100644 --- a/packages/integrations/preact/src/index.ts +++ b/packages/integrations/preact/src/index.ts @@ -1,94 +1,66 @@ import type { AstroIntegration, AstroRenderer, ViteUserConfig } from 'astro'; +import preact, { type PreactPluginOptions as VitePreactPluginOptions } from '@preact/preset-vite'; +import { fileURLToPath } from 'node:url'; + +const babelCwd = new URL('../', import.meta.url); function getRenderer(development: boolean): AstroRenderer { return { name: '@astrojs/preact', clientEntrypoint: development ? '@astrojs/preact/client-dev.js' : '@astrojs/preact/client.js', serverEntrypoint: '@astrojs/preact/server.js', - jsxImportSource: 'preact', - jsxTransformOptions: async () => { - // @ts-expect-error types not found - const plugin = await import('@babel/plugin-transform-react-jsx'); - const jsx = plugin.default?.default ?? plugin.default; - return { - plugins: [jsx({}, { runtime: 'automatic', importSource: 'preact' })], - }; - }, }; } -function getCompatRenderer(development: boolean): AstroRenderer { +export type Options = Pick<VitePreactPluginOptions, 'include' | 'exclude'> & { compat?: boolean }; + +export default function ({ include, exclude, compat }: Options = {}): AstroIntegration { return { name: '@astrojs/preact', - clientEntrypoint: development ? '@astrojs/preact/client-dev.js' : '@astrojs/preact/client.js', - serverEntrypoint: '@astrojs/preact/server.js', - jsxImportSource: 'react', - jsxTransformOptions: async () => { - // @ts-expect-error types not found - const plugin = await import('@babel/plugin-transform-react-jsx'); - const jsx = plugin.default?.default ?? plugin.default; - 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', - }, - }, - ], - ], - }; - }, - }; -} + hooks: { + 'astro:config:setup': ({ addRenderer, updateConfig, command }) => { + const preactPlugin = preact({ + include, + exclude, + babel: { + cwd: fileURLToPath(babelCwd), + }, + }); -function getViteConfiguration(compat?: boolean): ViteUserConfig { - const viteConfig: ViteUserConfig = { - optimizeDeps: { - include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime'], - exclude: ['@astrojs/preact/server.js'], - }, - }; + const viteConfig: ViteUserConfig = { + optimizeDeps: { + include: ['@astrojs/preact/client.js', 'preact', 'preact/jsx-runtime'], + exclude: ['@astrojs/preact/server.js'], + }, + }; - 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', 'preact'], - }; - // noExternal React entrypoints to be bundled, resolved, and aliased by Vite - viteConfig.ssr = { - noExternal: ['react', 'react-dom', 'react-dom/test-utils', 'react/jsx-runtime'], - }; - } + // If not compat, delete the plugin that does it + if (!compat) { + const pIndex = preactPlugin.findIndex((p) => p.name == 'preact:config'); + if (pIndex >= 0) { + preactPlugin.splice(pIndex, 1); + } + } else { + viteConfig.optimizeDeps!.include!.push( + 'preact/compat', + 'preact/test-utils', + 'preact/compat/jsx-runtime' + ); + viteConfig.resolve = { + alias: [{ find: 'react/jsx-runtime', replacement: 'preact/jsx-runtime' }], + dedupe: ['preact/compat', 'preact'], + }; + // noExternal React entrypoints to be bundled, resolved, and aliased by Vite + viteConfig.ssr = { + noExternal: ['react', 'react-dom', 'react-dom/test-utils', 'react/jsx-runtime'], + }; + } - return viteConfig; -} + viteConfig.plugins = [preactPlugin]; -export default function ({ compat }: { compat?: boolean } = {}): AstroIntegration { - return { - name: '@astrojs/preact', - hooks: { - 'astro:config:setup': ({ addRenderer, updateConfig, command }) => { - const development = command === 'dev'; - if (compat) addRenderer(getCompatRenderer(development)); - addRenderer(getRenderer(development)); + addRenderer(getRenderer(command === 'dev')); updateConfig({ - vite: getViteConfiguration(compat), + vite: viteConfig, }); }, }, diff --git a/packages/integrations/preact/src/server.ts b/packages/integrations/preact/src/server.ts index 6a2ceb612..b9d063b62 100644 --- a/packages/integrations/preact/src/server.ts +++ b/packages/integrations/preact/src/server.ts @@ -29,8 +29,8 @@ function check(this: RendererContext, Component: any, props: Record<string, any> // There are edge cases (SolidJS) where Preact *might* render a string, // but components would be <undefined></undefined> - - return !/\<undefined\>/.test(html); + // It also might render an empty sting. + return html == '' ? false : !/\<undefined\>/.test(html); } catch (err) { return false; } diff --git a/packages/integrations/preact/tsconfig.json b/packages/integrations/preact/tsconfig.json index 64d4ef454..af1b43564 100644 --- a/packages/integrations/preact/tsconfig.json +++ b/packages/integrations/preact/tsconfig.json @@ -5,6 +5,6 @@ "allowJs": true, "module": "ES2022", "outDir": "./dist", - "target": "ES2021" + "target": "ES2022" } } |