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

Compability with Preact #1230

Closed
just-boris opened this issue Dec 10, 2016 · 18 comments
Closed

Compability with Preact #1230

just-boris opened this issue Dec 10, 2016 · 18 comments

Comments

@just-boris
Copy link
Contributor

I couldn't find an issue about this topic, so I'd like to create this one to discussion.

Surprisingly, Preact is pretty compatible with React, so this project can also support project based on Preact without big changes.

Any chances for any move toward this?

@just-boris
Copy link
Contributor Author

Actually, there is only one difference: Preact requires different @jsx pragma definition. I tried to build a Preact-based project with react-scripts and realized that we need to tell ESlint and Babel about new rules.

So I tried to add the following babelrc:

{
  "plugins": [
    ["transform-react-jsx", { "pragma": "h" }]
  ]
}

and eslintrc

{
  "settings": {
    "react": {
      "pragma": "h"
    }
  }
}

The only problem is that react-scripts ignores eslintrc and babelrc, so my changes have no effect.

@gaearon
Copy link
Contributor

gaearon commented Dec 10, 2016

I think it’s great many projects can run on Preact without significant changes but I don’t think we’ll be spending effort supporting this as a built-in configuration (just like we don’t support, for example, Relay out of the box).

The best course of action for those interested in it would be to fork react-scripts (as described in #779), make the necessary changes, and try to keep it up to date with upstream version.

@just-boris
Copy link
Contributor Author

Well, forking may be a solution. I will try to create a fork and use it.

Actually, there already was an attempt to do this: https://github.com/alexkuz/create-preact-app
It seems like there were no pulls from upstream since forking, so I have to do this again.

@alexkuz, is there any chance that you will update your create-preact-app fork?

@shrynx
Copy link
Contributor

shrynx commented Dec 11, 2016

@just-boris I am working on putting this in my fork. Will get back to you when I am done. I need someone to test complete compatibility, as i don't have a lot experience with preact.

@alexkuz
Copy link

alexkuz commented Dec 11, 2016

@just-boris I could, but tbh I'm not really using preact (at least for now), so I have no big interest to keep it up to date. Just fork it and update. Or I could transfer this fork to you if you want to take care of it from here.

@gaearon
Copy link
Contributor

gaearon commented Dec 11, 2016

Oh hi @alexkuz

@just-boris
Copy link
Contributor Author

Hello again,

So far, I have been working on this solution and have finally published my fork. So, if you want to create a Preact app without any setup, you may run

create-react-app <app-name> --scripts-version @just-boris/preact-scripts 

and it will give you a Preact-based application.

@alexkuz that was not possible to pick up your fork, because the structure of main repo was changed a lot since that time (now this is a monorepo with Lerna).

But it would be helpful if you will put a note about my newer fork in your Readme.

Also, I have opened "Issues" section in my repo, any further suggestions are welcome

@just-boris
Copy link
Contributor Author

For those, who wants to check out, what was changed, you may look at this diff

@shrynx
Copy link
Contributor

shrynx commented Dec 18, 2016

@just-boris For a complete react compatibility wouldn't one need preact-compat too ?
In my fork i alias react and react-dom to preact-compat
https://github.com/shrynx/react-super-scripts/blob/0c4c4ddbabc77847a9694647c28dd94e48251ec3/packages/react-scripts/utils/customWebpackConfig.js#L19-L28

@just-boris
Copy link
Contributor Author

Yes, just added this feature per request in just-boris#1.

@just-boris
Copy link
Contributor Author

Closing this as no any further action from the upstream side.

@gaearon
Copy link
Contributor

gaearon commented Dec 19, 2016

Nice, thank you for sharing this!

@ghost
Copy link

ghost commented Oct 4, 2017

Well, if anyone still need this, I created a build.js file with the following contents:

process.env.NODE_ENV = "production"

const config = require("react-scripts/config/webpack.config.prod")

config.resolve.alias["react"] = "preact-compat"
config.resolve.alias["react-dom"] = "preact-compat"

require("react-scripts/scripts/build")

It's not ideal, but it's very simple and easy to maintain

@prasadmsvs
Copy link

This doesn't work in tests. It gives an error "Cannot find module 'react' from 'App.test.js'". Can't jest read aliases? Is there any way to configure jest to use preact for react?

@gaearon
Copy link
Contributor

gaearon commented Jan 23, 2018

Not without ejecting. (Just like you can’t configure any other alias.)

@prasadmsvs
Copy link

It worked when I added to preact-compat to ModuleNameMapper in package.json. However, enzyme didn't work. It would be great if we have a full implementation with preact-compat from someone.

@ghost
Copy link

ghost commented Jan 25, 2018

@just-boris
Copy link
Contributor Author

just-boris commented Jan 28, 2018

@prasadmsvs I am still maintaining my fork supporting Preact. In version 1.1.0 I have also added a mapping into Jest config.

Also, look at the official Preact documentation: https://preactjs.com/guide/unit-testing-with-enzyme
UPD: there is also Enzyme v3 adapter: https://github.com/aweary/preact-enzyme-adapter, but it is not published to NPM yet.

@lock lock bot locked and limited conversation to collaborators Jan 20, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants