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

Add preact/compat support to @astrojs/preact #3712

Merged
merged 17 commits into from
Jun 29, 2022
Merged

Conversation

delucis
Copy link
Member

@delucis delucis commented Jun 25, 2022

Changes

This branch tries to add a compat configuration option to the @astrojs/preact integration based on the the old preact/compat renderer.

I don’t think this works yet — trying with a local copy of this integration on Stackblitz still produces a Cannot find module 'react' error: https://stackblitz.com/edit/github-6sydzv?file=integrations/preact.mjs

I couldn't find any documentation or tests for the old renderer, so I'm not totally clear how it was used. I imagine you always need @astrojs/preact as well as compat, so thought this API would be nice:

defineConfig({
  integrations: [
    preact({ compat: true }),
  ],
});

If that could work, I think it would be the simplest Preact compat support around?

Testing

Nate added an E2E test for the compat option

Docs

Chris updated the integration README to follow our new template and add details about the new compat config option.

@changeset-bot
Copy link

changeset-bot bot commented Jun 25, 2022

🦋 Changeset detected

Latest commit: 85cf581

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added pkg: preact Related to Preact (scope) pkg: integration Related to any renderer integration (scope) labels Jun 25, 2022
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good!

I bet the issue with any errors like "react" not found happens when any dependencies are treated as vite.ssr.external—they'll use standard Node resolution rather than Vite's resolution. So in that case, the error is correct... react cannot be found since it's not installed.

The fix is possibly to set vite.ssr.noExternal to true, ensuring that all dependencies are built.

packages/integrations/preact/src/index.ts Show resolved Hide resolved
packages/integrations/preact/src/index.ts Show resolved Hide resolved
@delucis
Copy link
Member Author

delucis commented Jun 27, 2022

No dice with ssr.noExternal — surfaces a __vite_ssr_import_1__ is not defined error. I’ve tried the following values:

  • true
    Errors quickly as it’s trying to bundle github-slugger.

  • ['react']
    Routes that don’t use React now load OK, but those that do error with Error when evaluating SSR module for

    • compat: /node_modules/preact/compat/dist/compat.module.js
    • the component: /src/components/ReactCounter.tsx
    • and the route: /src/pages/react.astro
  • ['react', 'react-dom'] and ['preact/compat', 'react', 'react-dom'] both behave the same as ['react']

  • Further adding 'preact' to the array completely crashes the dev server on routes rendering a Preact component.

Next hint, please! 😅

@natemoo-re
Copy link
Member

@delucis I was able to get things mostly working by using manual aliases via babel-plugin-module-resolver. Not sure why it works when the alternative (plain Vite) doesn't, but it seems to be effective. Let me know if you run into anything else!

@delucis
Copy link
Member Author

delucis commented Jun 28, 2022

Amazing work @natemoo-re!!! 🥳

This is very close to perfect. Here’s what works and doesn’t work now:

Preact component React component Preact importing React
build render
build hydration
dev render
dev hydration

In dev, when the page tries to hydrate a React component or Preact component that uses a React component, I get the following errors in the browser:

image

Weirder is that the same behaviour happens on a page with just a Preact component if you visit it after having visited a page with a React component. It’s almost like that 504 Error crashes some internal or causes Vite to cache some bad code? Not sure.

I’ll also stress — in case it’s important — that I’m testing this by copy-pasting the changes as a local integration in my test project. It’s on Stackblitz here: https://stackblitz.com/edit/github-6sydzv?file=integrations/preact.mjs

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Jun 28, 2022
Copy link
Member

@natemoo-re natemoo-re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM whenever this is ready for review! It'd be nice to add some docs about this to the README.

@delucis delucis marked this pull request as ready for review June 28, 2022 21:26
@delucis delucis changed the title Add preact/compat renderer Add preact/compat support to @astrojs/preact Jun 28, 2022
@sarah11918
Copy link
Member

Giving a README LGTM!

Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>
@kevinzunigacuellar
Copy link
Member

LGTM 🚀

@natemoo-re
Copy link
Member

Plenty of LGTMs, merge away @delucis!

@delucis delucis merged commit e3fdc9b into main Jun 29, 2022
@delucis delucis deleted the delucis/preact-compat branch June 29, 2022 15:42
@astrobot-houston astrobot-houston mentioned this pull request Jun 29, 2022
SiriousHunter pushed a commit to SiriousHunter/astro that referenced this pull request Feb 3, 2023
* Add preact/compat renderer (likely broken)

Based on the current Preact renderer and the old preact/compat implementation: https://github.com/withastro/astro/blob/f892aeb52f5a93d81a68d9833eb26bedd06aa2f0/packages/renderers/renderer-preact/compat/index.js

* Make sure name is consistent

* Switch to single integration with compat option

* fix: add module-resolver to alias react => preact/compat

* fix: preact/compat mode

* chore: remove client-compat entrypoint

* chore: add e2e test for preact/compat

* Try to fix frozen lock file error

* Add changeset

* Update README to new structure & document `compat`

* Fix changeset wording

* Fix README typo

* Tweak wording

Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>

Co-authored-by: Nate Moore <nate@astro.build>
Co-authored-by: Nate Moore <natemoo-re@users.noreply.github.com>
Co-authored-by: Kevin Zuniga Cuellar <46791833+kevinzunigacuellar@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope) pkg: integration Related to any renderer integration (scope) pkg: preact Related to Preact (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants