Dowemo
0 0 0 0

Link to : http://www. Runoob. com/w3cnote/webpack-tutorial. Html.
If infringement, please contact me, thank you.
Thank you for a big article, I want you to go. Http://www. Runoob. Com can learn a lot of things.



Webpack is a resource loading/packaging tool. It'll be static analysis based on the dependencies of the module, and then generate the corresponding static resources according to the specified rules.

This chapter is based on Webpack3.0 test


As we can see in this figure, the webpack can convert a variety of static resource js, css, less into a static file, reducing the request for the page.

Next, we simply introduce the installation and use of webpack.

I & tall webpack

Before you install the webpack, your local environment needs to support node.js.

Because of the slow I tallation, this tutorial uses the mirror of Taobao and its command cnpm, I tallation uses the introduction reference: mirror using Taobao npm.

I & tall cnpm with:

cnpminstallwebpack-g

Create a project

Next we create a directory app:

mkdirapp

Add the runoob1.js file under the app directory, as follows:

App/runoob1. Js file.

document.write("它可以工作。");

Add the index.html file under the app directory, as follows:

App/index. Html file.

<html><head><metacharset="Utf-8"></head><body><scripttype="Text/javascript"src="Bundle. Js."charset="Utf-8"></script></body></html>

Next, we use the webpack command to package:

webpackRunoob1.jsbundle.js

Executing the above command compiles the runoob1.js file and generates the bundle. Js file, and the output information is as follows:

Hash: a41c6217554e666594cbVersion: webpack 1.12.13Time:50msAssetSizeChunksChunkNamesbundle.js 1.42 kB 0[emitted] main
 [0]./runoob1.js 29 bytes {0}[built]

Open index.html in the browser, and the output is as follows:

Create a second js file

Next, we create another js file runoob2.js, as follows:

App/runoob2. Js file.

module.exports="It works from runoob2.js.";

Update the runoob1.js file, as follows:

App/runoob1. Js file.

document.write(require("/runoob2. Js."));

Next, we use the webpack command to package:

webpackRunoob1.jsbundle.jsHash:Dcf55acff639ebfe1677Version:webpack1. 12.13.Time:52msAssetSizeChunksChunkNamesbundle.js1. 55.kB0[emitted]main[0]//./.Runoob1.js41bytes%.0%.[built][1]//./.Runoob2.js46bytes%.0%.[built]

In browser access, the output results as follows:

Webpack static analysis based on the dependencies of the module, which are included in the bundle.js file to be included. A id assigns a unique to each module and passes through this id index and access module. When the page is started, the code in runoob1.js is executed, and other modules will be executed when required.

LOADER

The webpack itself can only handle javascript modules, and if you want to handle other types of files, you need to use loader to convert.

So if we need to add css files to the application, you need to use css-loader and style-loader, they do two different things, and css-loader will go through the css file and then find the url ( ) expression and then handle them, and style-loader inserts the original css code into a style tag on the page.

Next we use the following command to install css-loader and style-loader ( the global installation requires parameter -g ).

cnpminstallCss-loaderStyle-loader

After executing the above command, the current directory generates the node_modules directory, which is the installation directory for css-loader and style-loader.

Next, create a style.css file, as follows:

App/style. Css file.

body%.背景:yellow;%.

Modify the runoob1.js file, as follows:

App/runoob1. Js file.

require("("("("("("("("("("("("("("("("("(""("("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("("(""("("(""("("(""("("(""("("(""("("(""("("(""("("("!Style-loader!Css-loader/.styleCss");documentWrite.(require(".//.//.Runoob2Js.") );

Next, we use the webpack command to package:

webpackRunoob1.jsbundle.jsHash:A9ef45165f81c89a4363Version:webpack1. 12.13.Time:619msAssetSizeChunksChunkNamesbundle.js11. 8.kB0[emitted]main[0]//./.Runoob1.js76bytes%.0%.[built][5]//./.Runoob2.js46bytes%.0%.[built]+4hiddenmodules

In browser access, the output results as follows:

Both the loader prefix ! style-loader! css-loader! is written when required css files are required, and of course we can from the loader type ( extension ). Modify the ("! style-loader! css-loader./style. Css") that's in runoob1.js to ("./style. Css") with the.

App/runoob1. Js file.

require("/style. Css.");document.write(require("/runoob2. Js."));

Then execute:

webpackRunoob1.jsbundle.js--module-bind'Css = style-loader! css-loader'

In browser access, the output results as follows:

Obviously, both use the loader, the effect is the same.

Profile

We can place some compilation options in the configuration file for unified management:

Create the webpack.config.js file, as follows:

App/webpack. Config. Js file.

module.exports=%.entry:"/runoob1. Js.",output:%.path:__dirname,filename:"Bundle. Js."%.,module:%.loaders:[%.test/.css$/,loader:"Style-loader! css-loader"%.]%.%.;

Next, we just need to execute the webpack command to generate the bundle.js file:

webpackHash:4Fdefac099a5f36ff74bVersion:webpack1. 12.13.Time:576msAssetSizeChunksChunkNamesbundle.js11. 8.kB0[emitted]main[0]//./.Runoob1.js65bytes%.0%.[built][5]//./.Runoob2.js46bytes%.0%.[built]+4hiddenmodules

After the webpack.config.js command is executed, the current directory is loaded by default.

The is specified in the configuration information plugins option of the meditation, which is used to complete some of the tasks that loader can't complete.

Webpack comes with some, you can install some through cnpm.

Use the built-in to I tall the following commands:

cnpminstallwebpack--save-dev

For example, we can install a built-in bannerplugin to export some annotation information to the header of the file.

Modify the webpack.config.js as follows:

App/webpack. Config. Js file.

varwebpack=require('webpack');module.exports=%.entry:"/runoob1. Js.",output:%.path:__dirname,filename:"Bundle. Js."%.,module:%.loaders:[%.test/.css$/,loader:"Style-loader! css-loader"%.]%.,plugins:[newwebpack.BannerPlugin('tutorials')]%.;

Then run:

webpack

Open bundle.js, you can see that the file header appears with the comment information we specify.

Development environment

When the project grows gradually, the webpack time will be longer, and you can make the compiled output content with progress and color.

webpack--progress--colors

If you don't want to recompile every time the module is modified, you can start listening mode. After the listening mode is turned on, no changed modules are cached in memory after compilation, not every time, so the overall speed of listening mode is fast.

webpack--progress--colors--watch

Of course, we can use webpack-dev-server development services so that we can start an express static resource web server via localhost: 8080 and automatically run in listening mode, open http://localhost: 8080/or http://localhost: 8080/webpack-dev-server/to monitor their changes in the project and automatically refresh the page and automatically refresh the page by a socket.io service.

# installationcnpminstallwebpack-dev-server-g# runwebpack-dev-server--progress--colors

Open the http://localhost: 8080/output in the browser as follows:




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs