How to build a pipeline for a customized Hello world with images.
suggest changeStep 1: Install Node.js
The build pipeline you will be building is based on Node.js, so install that first. For instructions, check out the SO docs here.
Step 2: Initialise your project as a Node module
Open your project folder on the command line and use the following command:
npm init
For the purposes of this example you can feel free to take the defaults or if you’d like more info on what all this means you can check out this SO doc on setting up package configuration.
Step 3: Install necessary npm packages
Run the following command on the command line to install the packages necessary for this example:
npm install --save react react-dom
Then for the dev dependencies run this command:
npm install --save-dev babel-core babel-preset-react babel-preset-es2015 webpack babel-loader css-loader style-loader file-loader image-webpack-loader
Webpack and webpack-dev-server can be installed globally rather than as a project dependency. A local install works too. Here is the global command:
npm install --global webpack webpack-dev-server
Step 4: Add a .babelrc file to the root of your project
This will setup babel to use the presets you’ve just installed. Your .babelrc file should look like this:
{
"presets": ["react", "es2015"]
}
Step 5: Set up the project directory structure
Set up a directory structure like the one below, from the root of your project:
|- node_modules
|- src/
|- components/
|- images/
|- styles/
|- index.html
|- index.jsx
|- .babelrc
|- package.json
NOTE: The node_modules, .babelrc and package.json should all have already been there from previous steps I just included them so you can see where they fit.
Step 6: Populate the project with the Hello World project files
This isn’t really important to the process of building a pipeline so I’ll just give you the code for these and you can copy paste them in:
import React, { Component } from 'react';
import myImage from '../images/myImage.gif';
class HelloWorldComponent extends Component {
constructor(props) {
super(props);
this.state = {name: 'Student'};
this.handleChange = this.handleChange.bind(this);
}
handleChange(e) {
this.setState({name: e.target.value});
}
render() {
return (
<div>
<div className="image-container">
<img src={myImage} />
</div>
<div className="form">
<input type="text" onChange={this.handleChange} />
<div>
My name is {this.state.name} and I'm a clever clogs because I built a React build pipeline
</div>
</div>
</div>
);
}
}
export default HelloWorldComponent;
src/images/myImage.gif
Feel free to substitute any image; it is there to show that images can be bundled. If you name it differently, update the import in HelloWorldComponent.jsx. If the file extension changes, update the test regex of the image loader in webpack.config.js. The component imports the file so file-loader can replace src with the emitted URL. A string path such as ./images/myImage.gif is not rewritten.
.form {
margin: 25px;
padding: 25px;
border: 1px solid #ddd;
background-color: #eaeaea;
border-radius: 10px;
}
.form div {
padding-top: 25px;
}
.image-container {
display: flex;
justify-content: center;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Learning to build a React pipeline</title>
</head>
<body>
<div id="content"></div>
<script src="app.js"></script>
</body>
</html>
import React from 'react';
import { render } from 'react-dom';
import HelloWorldComponent from './components/HelloWorldComponent.jsx';
require('./images/myImage.gif');
require('./styles/styles.css');
require('./index.html');
render(<HelloWorldComponent />, document.getElementById('content'));
Step 7: Create the webpack configuration
Create a file called webpack.config.js in the root of your project and copy this code into it:
var path = require('path');
var config = {
context: path.resolve(__dirname + '/src'),
entry: './index.jsx',
output: {
filename: 'app.js',
path: path.resolve(__dirname + '/dist'),
},
devServer: {
contentBase: path.join(__dirname + '/dist'),
port: 3000,
open: true,
},
module: {
loaders: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: 'babel-loader'
},
{
test: /\.css$/,
loader: "style!css"
},
{
test: /\.gif$/,
loaders: [
'file?name=[path][name].[ext]',
'image-webpack',
]
},
{ test: /\.(html)$/,
loader: "file?name=[path][name].[ext]"
}
],
},
};
module.exports = config;
Step 8: Create npm tasks for your pipeline
To do this you will need to add two properties to the scripts key of the JSON defined in the package.json file in the root of your project. Make your scripts key look like this:
"scripts": {
"start": "webpack-dev-server",
"build": "webpack",
"test": "echo \"Error: no test specified\" && exit 1"
},
The test script will have already been there and you can choose whether to keep it or not, it’s not important to this example.
Step 9: Use the pipeline
From the command line, if you are in the project root directory you should now be able to run the command:
npm run build
This will bundle up the little application you’ve built and place it in the dist/ directory that it will create in the root of your project folder.
If you run the command:
npm start
Then the application you’ve built will be served up in your default web browser inside of a webpack dev server instance.