Chapter 02 (npm, babel, and webpack) - Intuit-London/imperial-react-workshop GitHub Wiki
npm, webpack, babel
Create an empty folder say chapter02 and run npm init
Install react and react-dom using npm command npm install --save react react-dom
Install dev dependencies of babel by using the npm npm install --save-dev babel-preset-react babel-loader babel-core
Install dev dependencies of babel to use es2015 by using command npm install --save-dev babel-preset-es2015
Webpack is a module bundler, lets install that using npm npm install --save-dev webpack
Webpack-dev-server gives a node express server. Lets install that using npm install --save-dev webpack-dev-server
Create src folder under chapter02.
Create React class with the filename App.js under src folder.
importReact,{Component}from'react';classAppextendsComponent{render(){varelapsed=Math.round(this.props.elapsed/100);varseconds=elapsed/10+(elapsed%10 ? '' : '.0');varmessage='React has been successfully running for '+seconds+' seconds.';return(<div>{message}</div>);}}exportdefaultApp;
Use the above created React component and render it to a root node by creating a index.js file under src folder.
Bundle the code using webpack (with React as a loader) to create a app.bundle.js file under build folder using the command ./node_modules/webpack/bin/webpack.js
Start the webpack dev server using the command ./node_modules/webpack-dev-server/bin/webpack-dev-server.js --content-base build/
Goto http://localhost:8080/
Add the app.bundle.js in the index.html and refresh the browser.
<scriptsrc="./app.bundle.js"></script>
html-loader and html-webpack-plugin
Install a html-webpack-plugin using npm install --save-dev html-webpack-plugin and update the webpack.config.js as follows after the modules section