Integrating ReactJS With Flask: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
Line 102: Line 102:


== Notes ==
== Notes ==
=== See also ==
* [https://scotch.io/tutorials/setup-a-react-environment-using-webpack-and-babel Set Up A React Environment Using Webpack and Babel] - Scotch.io
=== References ===
<references />
<references />

Revision as of 13:58, 7 March 2018

Prerequisites

NPM

  • NPM
  • CSS and JavaScript should live in separate directories within the static directory, e.g. my_proj/static/css/ and my_proj/static/js/
  • Create a package.json file in the project's static directory:[1]
$ cd app_package/static
$ npm init

Webpack

Webpack is a module bundler, similar to Browserify, gulp, and grunt. Supposedly it is easier to configure in that it can intelligently make more assumptions about what needs to be accomplished. However that ease may come at the expense of defining more specific configurations with Browserify and gulp. [2]

Installation

Run installation from the static directory.

$ npm install webpack --save-dev

Configuration

Create a configuration file named webpack.config.js in the static directory:

const webpack = require('webpack');
const config = {
    entry:  __dirname + '/js/index.js',
    output: {
        path: __dirname + '/dist',
        filename: 'bundle.js',
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css']
    },
};
module.exports = config;

Add run commands to package.json:

"scripts": {
    "build": "webpack -p --progress --config webpack.config.js",
    "dev-build": "webpack --progress -d --config webpack.config.js",
    "test": "echo \"Error: no test specified\" && exit 1",
    "watch": "webpack --progress -d --config webpack.config.js --watch"
},

Babel

Babel converts JavaScript that is ahead of browser standards (e.g. React JSX) into JavaScript syntax that is compatible with all current browsers.

Installation

Run installation from the static directory.

$ npm install babel-preset-env --save-dev

Configuration

Add babel presets to package.json:

"babel": {
    "presets": [
      "dev",
      "react"
    ]
  }

Add a babel-loader rule to the Webpack config:

module: {
  rules: [
    {
      test: /\.jsx?/,
      exclude: /node_modules/,
      use: 'babel-loader'
    }
  ]
}

ReactJS

React installation (again, in the static directory):

$ npm install react react-dom --save-dev

Notes

= See also

References