ITZone

[JavaScript] Basic Webpack part 1

Opening

Hello friends. I am currently learning about webpack, this article and you can help each other to learn about it.

First I would like to talk about two concepts: task runner and module bundler

Task runner

Module bundler

Body post

The goal of webpack

Install project

All projects using the package manager (npm) must be initialized. To initialize we use the following statement:

After executing this command line, your project directory will create a file named package.json When you execute npm init there will be questions, you can press enter or answer to continue, but go to the yes section, remember to press the word 'yes' too.

Once completed, the package.json file will have content like this:

I will use Reactjs in this project.

Download webpack and webpack-cli

Of course, the first thing to download is:

And when in the package.json file:

When this line appears, it has been successfully installed webpack and webpack-cli.

webpack-cli

I do not understand the function of this guy very well, read the documentation on the Internet, then tell him to provide tools to configure and customize webpack with the commands.

Download React

Continue to install react and react-dom to be able to deploy 1 reactjs app.

And when in the package.json file:

When this line appears, it has been successfully installed react and react-dom .

Download Babel

For React to work, we need to install Babel along with React. We need Babel to transcode ES6 and JSX to ES5. (Most browsers don't support ES6 + and JSX yet)

Continue to install babel-core , babel-loader , babel-preset-env , babel-preset-react ..

And when in the package.json file:

When this line appears, it has been successfully installed babel-core , babel-loader , babel-preset-env , babel-preset-react .

Initialize project

In the current project directory, I will create another folder named src .

index.js

Create an index.js file inside the src directory. This index.js file will be the entry point of our application.

Go to edit file index.js and add console.log

index.html

Create an index.html file inside the src directory, the index.html file and the index.js file will be level with each other.

Go to edit file index.html and add:

Module in webpack

We have the entry and the output , next we will have the Module , In the module is divided into 3 small sections

But from version v2.1-beta.23 loaders will be renamed rules and preLoaders / postLoaders will be identified with the enforce attribute if the loaders are preLoaders or postLoaders .

Share the news now