Integrating ReactJS With Flask: Difference between revisions

From Littledamien Wiki
Jump to navigation Jump to search
(Created page with "Category:ReactJSCategory:FlaskCategory:PythonCategory:Web Development == Prerequisites == * [https://npmjs.com NPM] * CSS and JavaScript should live in separa...")
 
Line 1: Line 1:
[[Category:ReactJS]][[Category:Flask]][[Category:Python]][[Category:Web Development]]
[[Category:ReactJS]][[Category:Flask]][[Category:Python]][[Category:Web Development]]
== Prerequisites ==
== Prerequisites ==
=== NPM ===


* [https://npmjs.com NPM]
* [https://npmjs.com NPM]
Line 8: Line 10:
$ cd app_package/static
$ cd app_package/static
$ npm init
$ npm init
</pre>
=== Webpack ===
[https://webpack.js.org/guides/getting-started/ 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. <ref>[https://medium.freecodecamp.org/browserify-vs-webpack-b3d7ca08a0a9 Browserify vs Webpack] - Medium (2015)</ref>
==== Installation ====
Run installation from the `static` directory.
<pre>
$ npm install webpack --save-dev
</pre>
==== Configuration ====
Create a configuration file named `webpack.config.js` in the `static` directory:
<syntaxhighlight lang="javascript">
const webpack = require('webpack');
const config = {
    entry:  __dirname + '/js/index.jsx',
    output: {
        path: __dirname + '/dist',
        filename: 'bundle.js',
    },
    resolve: {
        extensions: ['.js', '.jsx', '.css']
    },
};
module.exports = config;
</syntaxhighlight>
Add run commands to `package.json`:
<syntaxhighlight lang="javascript" highlight="2,3,5">
"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"
},
</syntaxhighlight>
=== Babel ===
[https://babeljs.io 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.
<pre>
$ npm install babel-preset-env --save-dev
</pre>
==== Configuration ====
Add babel presets to `package.json`:
<syntaxhighlight lang="javascript">
  "babel": {
    "presets": [
      "dev",
      "react"
    ]
  }
</syntaxhighlight>
=== ReactJS ===
[https://reactjs.org React] installation (again, in the `static` directory):
<pre>
$ npm install react react-dom --save-dev
</pre>
</pre>


== Notes ==
== Notes ==
<references />
<references />

Revision as of 17:50, 6 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.jsx',
    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"
    ]
  }

ReactJS

React installation (again, in the static directory):

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

Notes