Webpack Getting Started

Source: Internet
Author: User

1.webpack Introduction

A packaging tool, a module loading tool, a variety of resources can be treated as a module.

Official website: http://webpack.github.io/

2. Directly on the Code tutorial

If you need to install node. js, all commands require node, and the installation method can be Baidu. It's all a tutorial.

3. First step: Create an empty folder, Webpack, the name is arbitrary.

Instruction code for installing Webpack: NPM (or CNPM) install Webpack-g

Step Two: Initialize NPM init

This step will have a lot of information, you can not fill out, you can also fill in.

4. Configuration parameter Description:

Entry: Is the page Portal file configuration HTML file to introduce a unique JS file

Output: The configuration of the corresponding outputs

Path: Where will the portal file eventually be exported?

FileName: Name of the output file

Publicpath: Public Resource Path

5. In the project root directory creation: Webpack.config.js configuration file for Webpack configuration.

And also create some paths to save the base files: For example, you can create related folders for your own needs.

Where the out file is code-generated and is described later. SRC below the folder.

6. Create a portal file under the JS folder in Src, Index.js, and create the index.html in the root directory of the project.

Write the Webpack.config.js code:

In your index.html and index.js in the random writing code, there is output information can be.

Index.html

Index.js

Below: Run command line: Webpack or Webpack-w

You can then open the Index.html page locally to see the displayed information.

7.Webpack Loader Loader

Continue to configure loader to process files via the loader: for example sass Less, tell each file what loader processing is required.

Download the required packages below:

Execute the following command: CNPM install Babel-loader Babel babel-core css-loader style-loader url-loader

File-loader Less-loader Less–save-dev

You can see the download package in the Package.json file:

After the download is complete in the Webpack.config.js file, the loader is introduced

8. Next Test CSS:

Created under the SRC folder under the project folder: Index.css, write a little style;

Because all the files in Webpack are modules, you must introduce the CSS and add the following code to the Index.js.

Index.js

Index.html

Index.css

The results are as follows:

9. When there are multiple JS files, how to introduce?

Here is a small demo, click on a DOM to make it change.

Now create a gadget under the JS folder of SRC Tool.js

Tool.js

Create a demo.js file under the JS folder of SRC;

Modify the entry file Index.js

The effect of running:

The height and width of the div have changed.

8. Packaging of pictures;

Place a picture in the IMG folder

Introduce the image as a module, insert the IMG inside the body

Since we are introducing a static resource, we need to modify the path in the Webpack.config.js

You can see the effect by running the local index.html.

Webpack Getting Started

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.