Integrating ReactJS With Flask: Difference between revisions
(→Notes) |
m (→= See also) |
||
| Line 102: | Line 102: | ||
== Notes == | == Notes == | ||
=== See also == | === 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 | * [https://scotch.io/tutorials/setup-a-react-environment-using-webpack-and-babel Set Up A React Environment Using Webpack and Babel] - Scotch.io | ||
Revision as of 13:58, 7 March 2018
Prerequisites
NPM
- NPM
- CSS and JavaScript should live in separate directories within the
staticdirectory, e.g.my_proj/static/css/andmy_proj/static/js/ - Create a
package.jsonfile in the project'sstaticdirectory:[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
- ↑ Creating a full-stack web application with Python, NPM, Webpack and React, codeburst.io
- ↑ Browserify vs Webpack - Medium (2015)