Coding / Programming Videos

Post your favorite coding videos and share them with others!

Setting A React Project From Scratch Using Babel And Webpack

Source link

Let us configure the webpacks

The webpacks are an extremely configurable piece of software we can pass arguments to cli command or create a configuration file named webpack.config.js . We should export an object with configuration property.

{
entry: "./src/index.js",
mode: "development",
output: {
filename: "./main.js"
},
}

The entry specify the root of the source code of the project. The mode specify the type of environment for the compilation tells where the compiled file should be placed.

Creating Our Sample Project

Our project has an index.html in dist folder and the structure of the HTML is

<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>React From Scratch</title>
</head>
<body>
<div id="root">
</div>
</body>
<script type="text/javascript" src="http://blog.bitsrc.io/main.js">
</script>
</html>

The HTML contains the scprit tag pointing to our main compiled file and when the file is compiled it is included in the HTML. Then we should install react and react-dom packages.

npm install react react-dom

Now index.js which is standard react rendering the file.

import React, { Component } from "react";
import ReactDOM from "react-dom";
import App from "./component/app.component";
ReactDOM.render(<App />, document.querySelector("#root"));

app.component.js

import React, { Component } from "react";
import s from "./app.component.css";
class MyComponent extends Component {
render() {
return <div className={s.intro}>Hello World</div>;
}
}
export default MyComponent;

app.component.css

.intro {
background-color: yellow;
}

Configuring the babel

Babel is configured using the presets which can be used to transpile almost any web technology including less to css, jsx to js, typescript to js many more. We would only use two preset react and env . We can configure babel using cli, file or package.json file, we would use package.json for configuration. Preset are like modules which help to transpile one language to other.

"babel": {
"presets": [
"@babel/env",
"@babel/react"
]
}

We would add these lines to the package.json file, it would let the babel know that these preset should be used for transpile. Now we should configure webpack to use babel.

Configuring WebPack for Babel And Other Loaders

Now we would use babel-loader for using babel with webpack and webpack provide us hooks to process the files before it is processed and these are called module and are easy to configure.

Using JS files

module: {
rules: [
{
test: /.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader"
}
},
]
}

The rules are array used to specifying what should be done when this type of file is encountered. The key testspecifies the regex for the file, we use it to select all the files with extension .m and .js and exclude the folder node_module and bower_components because we don’t what to transpile the node_module files then we specify the babel-loader to be used.Now our javascript files will be compiled by babel then processed by the webpack.

Using CSS files

module: {
rules: [
{
test: /.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader"
}
},
{
test: /.css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: true
}
}
]
},
]
}

We would use style-loader and css-loader for this task. The use key can also take an array of the objects or string. The loaders are run from last to first, therefore our file will first go to css-loader, we have specified the option of using modules, which make our css exclusive to the only component in which it is imported. The css-loader would resolve all the import in the css files and then the style-loader would add these in the form of style tag in the head.

<style>
<-- your css -->
</style>

Using Static assets

module: {
rules: [
{
test: /.m?js$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: "babel-loader"
}
},
{
test: /.css$/,
use: [
"style-loader",
{
loader: "css-loader",
options: {
modules: true
}
}
]
},
{
test: /.(png|svg|jpg|gif)$/,
use: ["file-loader"]
}
]
}

If we encounter png, svg, jpg or gif file we would use file-loader for handling these files. It is very important to handle these file carefully and optimize files loading mechanism.

Running dev server

devServer: {
contentBase: path.join(__dirname, "dist"),
compress: true,
port: 9000,
watchContentBase: true,
progress: true
},

The module provides us with a very easy and configurable method of running our development server. contentBase the entry point for our compiled resources which is index.html port tells on which port the server is listening. watchContentBase is very interesting, it tells the server to watch for the changes in the files directory in the contentBase folder.

Complete Example

Let start are server using package.json file.

"scripts": {
"dev": "webpack-dev-server",
"start": "webpack"
},

We can now run npm run dev for starting our web server.

Output running at the port 9000



Source link

Bookmark(0)
 

Leave a Reply

Please Login to comment
  Subscribe  
Notify of
Translate »