Webpack Study Notes

Source: Internet
Author: User

What is Webpack?

Webpack is a module packer, it is mainly based on the structure of your project, to find the Project JS module and other browsers do not directly identify the operation of other extended language, and convert it and packaged in the appropriate format for the browser to identify the use.

Compared with Grunt/gulp's working mode:

Webpack is exactly the opposite of their mode of operation, Grunt/gulp is to specify which files are compiled, packaged, and then their tools are automatically completed in a file. It is equivalent to the water coming out of a stream to follow the specified pipe down.

And Webpack is the project as a whole, there is a master of the portal file, Webpack will find from this file the project depends on the package, and the corresponding processing with loader, and finally packaged into one or more browser-recognized files, It is equivalent to a certain amount of water flowing through each pipe and finally converging to a certain creek port.

Pros: processing faster and processing more different types of files.

Installation:

npm install -g webpack //全局安装  

现在webpack的最新版本是4.8.1,如果不指定版本,会自动安装最新版,最新本中cli包单独移出了,需要另外单独安装。

入门:

1. First create a new empty project folder locally, and then create a new app folder and public folder, In the app folder to create a new Main.js,greeter.js file, this folder mainly store the original data and we will write JS, and then in the public folder mainly store the package generated JS file and a basic HTML template file index.html, its main purpose is to introduce packaged files.

2. Next, run NPM Init in the terminal to add the Package.json file, which is the NPM documentation that contains rich script definition tasks, dependent modules, etc.

3. configuration file: Create a new webpack.config.js in the root directory of the project. This file mainly describes the location of the entry and exit files, as well as the various loader and plugins used.

 (这里出口文件我放在build下了,和public是一样的)

4. Because NPM is able to boot task execution, set scripts in Package.json.

So in the terminal only need to input NPM run start can be compiled packaging, server is used to open a local service, using Webpack to start the local service, first need to install Webpack-dev-server, Then configure the service information in the Webpack.config.js file,

Description of each parameter

Loaders: Webpack Most powerful function is a variety of loader, through the use of loader different rules, Webpack can be very powerful to invoke a variety of external scripts, and can implement a variety of different formats of the file processing, such as convert less sass for CSS, ES6, 7 Convert to es5 that the browser can recognize, and convert the JSX in the react to JS. loader needs to be installed on demand and configured under the module keyword of the webpack.config.js file, the main options for configuration are :

Test: A regular expression (Y) that matches the extension name of the file that the loaders handles;

Loader:loader's name (Y);

Exclude: Add files that must be processed or block certain files (N);

Query: Provides additional setup options (N) for loader.

Several of the most commonly used loader in the project have Css-loader,style-loader,json-loader,babel-loader,less-loader,sass-loader installed on demand according to the actual situation of the project , although the Css-loader,style-loader are all processing styles, but their division of labor is different, Cssloader is to put those URIs (), @import able to implement the function of require, style is to put the calculated values on the page to use, The Jsonloader is built-in after the 2 release and does not require installation.

Babel:Babel is a package of several modules, his main core function is in the Babel-core NPM package,Webpack can integrate their different packages to use, for each function or extension that you need, You need to install separate packages (most of which are parsing ES6 babel-env-preset packages and parsing JSX babel-preset-react packages)

We can install these packages at oncenpm install --save-dev babel-core babel-loader babel-preset-env babel-preset-react

Then configure Babel, consider Babel configuration items too many, alone in a new BABELRC file configuration, Webpack will automatically invoke the configuration of this file.

CSS Module is called CSS modules the technology is intended to put JS Modular thinking into the CSS, through the CSS module, all the class name, the animation name by default only action on the current module. Webpack provides very good support for CSS modularity by simply configuring it in CSS loader.

There is also a CSS processing platform -PostCSS , it can help your CSS to achieve more features, using POSTCSS to automatically add CSS code to adapt to different browsers CSS prefix, first install postcss-loader and autoprefixer (automatically add prefix plugin)

npm install --save-dev postcss-loader autoprefixer  安装好之后配置loader,然后require即可,对于同一个文件需要使用多个loader时,用
ExtractTextPlugin中. Extract this method, the incoming loader array is OK.

Plug - ins:

Webpack Study Notes

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.