Webpack using Notes

Source: Internet
Author: User

Webpack Introduction

Commonjs and AMD are two major specifications for JavaScript module management, which defines the synchronous loading of modules, mainly for Nodejs, while the latter is asynchronous loading, and is suitable for the front end through Requirejs. NPM is among the mainstream JavaScript component publishing platforms, and more and more front-end projects are also dependent on NPM projects, or they will be published on the NPM platform. Therefore, making it easier for front-end projects to use the resources on NPM becomes a big requirement. In addition, if your code contains non-modular parts, then, Webpack can also be compatible, and is the only choice!

Not only that, Webpack can also compile less/sass/coffeescript, such as inserting a piece of HTML into a page, such as modifying a replacement text file, and so on, as shown, so the function is very powerful.

Unlike Seajs\requirejs, Webpack is a modular tool for packaging in the development phase, meaning that it cannot be used directly online without packaging.

Advantages of Webpack

1. Compatible with COMMONJS, AMD, ES6 syntax.

2. To js,css, picture resources can be packaged, can replace the work of gulp, such as packaging, compression confusion, picture to Base64.

3. Inline module loader and plug-in mechanism, with good flexibility and extensibility, such as support for COFFEE,ES6.

4. There is a separate configuration file webpack.config.js.

5. The code can be cut into different chunk, enabling on-demand loading, reducing initialization time.

6. Support Sourceurls and sourcemaps for easy commissioning

7. Powerful plugin interface, many internal plug-ins, use more flexible. Supports hot-swapping of react.

The 8.webpack uses asynchronous IO and has a multilevel cache, so that it is fast and faster on incremental compilation.

Webpack use

Installation

$ NPM Install Webpack-g

If it is a regular project, the dependency is generally written to the Package.json package:

--save-dev

Configuration

It says he has a profile: Webpack.config.js, it's like this:

varWebpack = require (' Webpack '));varCommonsplugin =NewWebpack.optimize.CommonsChunkPlugin (' Common.js '); Module.exports= {    //Pluginsplugins: [Commonsplugin],//entryentry: {index:'./src/js/page/index.js '    },    //Outputoutput: {path:' Dist/js/page ', FileName:' [Name].js '}, module: {//LoadersLoaders: [{test:/\.css$/, loader: ' Style-loader!css-loader '}, {test:/\.js$/, loader: ' Jsx-loader?harmony '}, {test:/\.scss$/, loader: ' Style!css!sass?sourcemap '}, {test:/\. (png|jpg) $/, loader: ' url-loader?limit=8192 '}        ]    },    //ResolveResolve: {root:' E:/github/flux-example/src ', extensions: [', '. js ', '. json ', '. Scss '], alias: {appstore:' Js/stores/appstores.js ', ActionType:' Js/actions/actiontype.js ', Appaction:' Js/actions/appaction.js '        }    }};

Run

$ webpack--display-error-details

Reference: Http://webpack.github.io/docs

Webpack using 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.