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

Enhanced React Native for Web support #814

Closed
fson opened this issue Oct 1, 2016 · 12 comments
Closed

Enhanced React Native for Web support #814

fson opened this issue Oct 1, 2016 · 12 comments

Comments

@fson
Copy link
Contributor

fson commented Oct 1, 2016

Support for React Native for Web apps was added in #316 and it enables building cross platform React Native apps for the web with Create React App.

However, creating this kind of apps with create-react-app is not as straightforward yet as creating web apps: #316 (comment). To create a cross platform app, at the moment you need to initialize a React Native app (react-native init), initialize a React app (create-react-app) in a separate folder, and then merge the folders and package.json files manually.

Being able to bootstrap a cross platform app directly with create-react-app would make this a lot easier.

To create a cross platform app one would simply run create-react-app --react-native. This would do following things:

  • As usual, install react-scripts from npm.
  • Run react-scripts init --react-native which would:
    • Install react, react-dom, react-native and react-native-web as dependencies.
    • Bootstrap a React Native app with react-native/local-cli/cli.js like react-native-cli does (this is equivalent to our react-scripts init, and this way it's always up-to-date with RN.)
    • Copy files from a modified version of our template that uses React Native components and styles.
    • Add the local react-native packager start script, in addition to our usual scripts, to package.json.

An app created by react-native init currently looks like this:

android/
ios/
node_modules/
.buckconfig
.flowconfig
.gitignore
.watchmanconfig
index.android.js
index.ios.js
package.json

An app created with create-react-app --react-native would look like this:

  android/
  ios/
  node_modules/
+ public/
+  favicon.ico
+  index.html
+ src/
+   index.js        # Web entry point
  .buckconfig
  .flowconfig
  .gitignore
  .watchmanconfig
  index.android.js
  index.ios.js
  package.json
+ README.md

Additionally we should also add support for .web.js file extension to our webpack module resolution so that if filename.web.js exists, it will be used instead of filename.js, allowing for web specific code to be written the same way you can with .android.js and .ios.js extensions in React Native.

Note: for consistency with React Native, it might be nice to move the index.js file to the top level. This would also be more agnostic to different folder structures, such as packages/ folder (#741). However, I think that change should be considered independently of React Native support discussed in this issue.

I would be interested in working on this, if we want to support it in Create React App.

@gaearon
Copy link
Contributor

gaearon commented Oct 1, 2016

cc @ericvicenti who experimented with this

@remon-nashid
Copy link
Contributor

Excited to hear @ericvicenti's opinion on this matter :).

@gaearon
Copy link
Contributor

gaearon commented Dec 5, 2016

Also cc @lacker

@lacker
Copy link
Contributor

lacker commented Dec 5, 2016

Ah thanks for ccing me on this issue. Making it reasonable to build a joint React Native + React-web app is definitely something we want to have a recommended way for. I think the root question where we came down differently than this proposal here is whether it's reasonable to expect the same package.json to apply to both web app and native app. My conclusion is "no" because quite a number of packages are going to end up being native-specific or web-specific for any reasonably sized application. So I think the most practical way to support doing joint React + RN development is actually to have separate package.json configurations, and instead to have some recommended way to have a file of code that is shared between the two. I could be wrong here though....

@gaearon
Copy link
Contributor

gaearon commented Dec 5, 2016

There is also a difference between classic code sharing approach and actual attempt to reuse RN primitives on the web (what RNW does).

@brunolemos
Copy link
Contributor

brunolemos commented Dec 26, 2016

Currently most react-native third party packages are published in es6, not es5, so if you try to use them you get a Unexpected token import error on chrome.

Shouldn't create-react-app handle this if it says react-native-web is supported?
Maybe add a config in webpack to compile react-native-* packages with es2015 preset?
Don't wanna eject to do this.

Let me know if this should be a separated issue.

@remon-nashid
Copy link
Contributor

@brunolemos yeah I typically include entire node_modules to be loaded by babel. Despite how terrible that sounds, it seems to work so far. Checkout the discussion in #1125.

@webdevbyjoss
Copy link

webdevbyjoss commented Jun 21, 2017

hey folks, is there any progress planned for this feature? or maybe you could point me to some good examples of combining the Native and Web in a single project, ideally using "react-native-scripts" + "react-scripts" without a need to eject?

@gaearon
Copy link
Contributor

gaearon commented Jun 22, 2017

If you find or build one let us know!
This is not our focus right now.

@gaearon
Copy link
Contributor

gaearon commented Jan 8, 2018

I'm going to close since this is not something we're looking at.
If somebody has specific ideas around improving, we're always happy to consider PRs.

@gaearon gaearon closed this as completed Jan 8, 2018
@brunolemos
Copy link
Contributor

brunolemos commented Jan 8, 2018

Regarding my comment above, what I did was to use a custom react-scripts that allowed me to add some webpack config, so there was no need to eject.

I used my own https://github.com/brunolemos/extensible-react-scripts
but https://github.com/timarney/react-app-rewired should work too.

@gaearon
Copy link
Contributor

gaearon commented Jan 13, 2018

FYI, we're starting the work to compile deps with babel-preset-env in Create React App: #3776

Let us know if you have feedback about how this should work.

@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.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants