Giter VIP home page Giter VIP logo

react-app's Introduction

React App SDK

Everything you love about Create React App plus server-side code support (SSR, GraphQL API, etc) and config overrides (Babel, Webpack, etc.). See the demo project.

React App SDK is intended to be used as a drop-in replacement for react-scripts NPM module. If you want to add server-side code into your application built with Create React App, all you have to do is to replace react-scripts dev dependency with react-app-tools plus provide one more entry point for Node.js as demonstrated below:

Directory Layout

.
├── build/                      # Compiled output
├── src/                        # Universal application source code
│   ├── components/             # Shared React.js components
│   │   ├── /App/               #   - The top-level React component
│   │   ├── /Button/            #   - Some other UI element
│   │   └── ...                 #   - etc.
│   ├── server/                 # Node.js app
│   │   ├── ssr.js              #   - Server-side rendering, e.g. ReactDOMServer.renderToString(<App />)
│   │   ├── api.js              #   - GraphQL API endpoint
│   │   └── index.js            #   - Node.js app entry point
│   └── index.js                # Client-side app entry point, e.g. ReactDOM.hydrate(<App />, container)
└── package.json                # List of project dependencies and NPM scripts

package.json

{
  "main": "build/server.js",
  "engines": {
    "node": ">=8.10"
  },
  "dependencies": {
+   "express": "^4.6.14",
    "react": "^16.8.4",
    "react-dom": "^16.8.4"
  },
  "devDependencies": {
-   "react-scripts": "^1.1.1"
+   "react-app-tools": "^3.1.0-preview.7"
  },
  "scripts": {
-   "start": "react-scripts start",
+   "start": "react-app start",
-   "build": "react-scripts build",
+   "build": "react-app build",
-   "test": "react-scripts test"
+   "test": "react-app test"
  }
}

src/index.js - Client-side rendering

import React from 'react';
import ReactDOM from 'react-dom';
import App from './components/App';

ReactDOM.hydrate(<App />, document.getElementById('root'));

src/server/index.js - Server-side rendering and/or API endpoint

const path = require('path');
const express = require('express');
const React = require('react');
const ReactDOMServer = require('react-dom/server');
const App = require('../components/App');
const stats = require('./stats.json');

const app = express();

app.get('*', (req, res) => {
  res.send(`
    <html>
      <body>
        <div id="root">${ReactDOMServer.renderToString(<App />)}</div>
        ${stats.entrypoints.main.assets.map(
          src => `<script src="${src}"></script>`
        )}
      </body>
    </html>
  `);
});

if (process.env.NODE_ENV === 'production') {
  app.listen(process.env.PORT || 8080);
} else {
  module.exports.default = app;
}

You can launch the app in development mode by running:

$ npm install
$ npm start

Then open http://localhost:3000/ to see your app.
When you’re ready to deploy to production, create a minified bundle with npm run build.

npm start

For more information refer to Create React App documentation:

Join our Telegram chat for support and feature requests - https://t.me/reactapp

Server-side rendering with React.js
How fast is React SSR?

How to Customize

Create webpack.config.js file in the root of your project that extends the default Webpack configuration. For example:

module.exports = () => {
  const [
    browserConfig,
    serverConfig,
  ] = require('react-app-tools/config/webpack');
  return [
    browserConfig,
    {
      ...serverConfig,
      plugins: {
        ...serverConfig.plugins.concat(
          new LimitChunkCountPlugin({ maxChunks: 1 })
        ),
      },
    },
  ];
};

Backers

Love React App SDK? Help us keep it alive by donating funds to cover project expenses!

Contribute

Help shape the future of React App SDK by joining our community today, check out the open issues, submit new feature ideas and bug reports, send us pull requests!

License

MIT © 2016-present Facebook, Kriasoft.

react-app's People

Contributors

koistya avatar mglace avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

react-app's Issues

Possible babel issues when migrating existing CRA

I have a working CRA app that I want to add SSR to. This SDK looks promising!

I tried following the steps in the readme. Although the server does start, during loading I get a compilation error:

(function (exports, require, module, __filename, __dirname) { .react-datepicker-popper[data-placement^="bottom"] .react-datepicker__triangle, .react-datepicker-popper[data-placement^="top"] .react-datepicker__triangle, .react-datepicker__year-read-view--down-arrow, ^ SyntaxError: Unexpected token . at createScript (vm.js:53:10) at Object.runInThisContext (vm.js:95:10) at Module._compile (module.js:543:28) ...

This appears to be because I'm importing a css file from node_modules like so:

import "react-datepicker/dist/react-datepicker.css";

This works in the regular CRA app, but not after the migration. I'm not exactly sure how to fix that, but I assumed I might need to use config-overrides.js to add some custom babel configuration? However, when I add the default config-overrides.js. I get the following error:

/Users/<blahblahblah>/client/config-overrides.js:4 ...config, ^^^ SyntaxError: Unexpected token ... at createScript (vm.js:53:10) at Object.runInThisContext (vm.js:95:10) at Module._compile (module.js:543:28)

I'd rather address the first issue without having a custom configuration, but perhaps the second issue gives you a clue?

Thanks.

Vulnerabilty in webpack-dev-server dependency of react-app-tools

Hello can you fix the dependency of the react-app-tools by making it to its new version min. 3.1.6?

=== npm audit security report ===

┌──────────────────────────────────────────────────────────────────────────────┐
│ Manual Review │
│ Some vulnerabilities require your attention to resolve │
│ │
│ Visit https://go.npm.me/audit-guide for additional guidance │
└──────────────────────────────────────────────────────────────────────────────┘
┌───────────────┬──────────────────────────────────────────────────────────────┐
│ High │ Missing Origin Validation │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Package │ webpack-dev-server │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Patched in │ >=3.1.6 │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Dependency of │ react-app-tools [dev] │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ Path │ react-app-tools > webpack-dev-server │
├───────────────┼──────────────────────────────────────────────────────────────┤
│ More info │ https://nodesecurity.io/advisories/725
└───────────────┴──────────────────────────────────────────────────────────────┘

react-app new fails after install

I just installed this and tried it out and it fails automatically. What version of node can I use?

$ npm install -g react-app
/usr/local/bin/react-app -> /usr/local/lib/node_modules/react-app/bin/react-app.js
/usr/local/lib
`-- [email protected]

$ mkdir test-react-app 
$ cd test-react-app 
$ react-app new    
/usr/local/lib/node_modules/react-app/bin/react-app.js:6
const { execSync, spawn } = require('child_process');
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:141:18)
    at node.js:933:3
~/git/test-react-app

$ node --version
v5.7.1


Deploy app in S3 subfolder

Hi,
How can we deploy an app in a S3 subfolder. Optimal solution is that all paths to scripts and css are relative to the homepage.

No package.json created

I just installed the tool and created my first project.

$ react-app new
Scaffolding a new JavaScript application project.
Installing npm modules. This may take a couple minutes.

npm WARN enoent ENOENT: no such file or directory, open '/Users/steve/Projects/package.json'
npm WARN Projects No description
npm WARN Projects No repository field.
npm WARN Projects No README data
npm WARN Projects No license field.

All done! Now you can launch your app by running: npm start

$ ls
components core       pages      public     test       utils

$

assets.json file structure

the steps in the readme and the demo app always referenced are two different stories entirely. i find the demo app to be something else (an overkill) a demo should at least show how to run a CRA generated app using react-app including just the app.browser.js, app.node.js and the config-overides.js . the app.node.js file example in the readme, makes reference to an asset.json file , no detail is provided about that file. the demo should at least show that

Unable to start App

Im getting below error in cmd

Failed at the [email protected] start script 'webpack-dev-server --hot'.
Make sure you have the latest version of node.js and npm installed.
If you do, this is most likely a problem with the reactapp package,not with npm itself.

v3 - Commas added in body

I've been using v2 and when I first set it up, I remember having one comma appended to the body after mapping the assets. This was fixed by adding .join('') to the ended of the return statement.

Now after upgrading to v3, I am getting the same treatment, but this time it is appending four commas and I can't get rid of them. Basically my markup looks like this at render:

<body>
    ,,
    <div id="app"></div>
    ,,
</body>

Any idea what's going on?

issue with react-app

I installed react-app globally, then in an empty folder ran react-app new.
then got the below

Scaffolding a new JavaScript application in /Users/admin/Documents/Development_Stuff/B/ra
Installing 'react-app-tools' from npm... This may take a couple minutes.

> [email protected] install /Users/admin/Documents/Development_Stuff/B/ra/node_modules/fsevents
> node install

[fsevents] Success: "/Users/admin/Documents/Development_Stuff/B/ra/node_modules/fsevents/lib/binding/Release/node-v57-darwin-x64/fse.node" already installed
Pass --update-binary to reinstall or --build-from-source to recompile

> [email protected] postinstall /Users/admin/Documents/Development_Stuff/B/ra/node_modules/webpack/node_modules/uglifyjs-webpack-plugin
> node lib/post_install.js

npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN @babel/[email protected] requires a peer of @babel/[email protected] but none is installed. You must install peer dependencies yourself.
npm WARN [email protected] requires a peer of ajv@^6.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN ra No description
npm WARN ra No repository field.
npm WARN ra No license field.

+ [email protected]
added 1758 packages from 885 contributors in 76.217s
Error: Cannot find module '/Users/admin/Documents/Development_Stuff/B/ra/node_modules/react-app-tools/scripts/new'
    at Function.Module._resolveFilename (module.js:538:15)
    at Function.Module._load (module.js:468:25)
    at Module.require (module.js:587:17)
    at require (internal/module.js:11:18)
    at Promise.resolve.then.then.then (/Users/admin/.config/yarn/global/node_modules/react-app/cli.js:107:13)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)

Add tests

Need to add e2e tests to this project

Rename 'pages' folder to 'routes'

React components inside the routes folder (previously pages) are intended to be used for rendering web pages (or modal dialogs), where each of these components matches a specific application route (see routes.json.

Need documentation

There should be documentation for explaining the server side code, as well as the folder structure. Even though it is inherited from create-react-app, some kind of documentation should be present.
Steps to Reproduce
NA
I would like to work on this.

Add `react-app view config` command

$ react-app view config                # Prints Webpack, Babel, Browsersync configurations
$ react-app view config.webpack.entry  # Prints a single node from the configuration

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.