webpack/README.md

178 lines
6.2 KiB
Markdown
Raw Normal View History

2014-01-14 16:57:26 +08:00
[![webpack](http://webpack.github.io/assets/logo.png)](http://webpack.github.io)
2012-03-10 20:11:23 +08:00
2014-01-30 07:58:35 +08:00
[![NPM version](https://badge.fury.io/js/webpack.png)](http://badge.fury.io/js/webpack) [![Gitter chat](http://img.shields.io/gitter/webpack/webpack.png)](https://gitter.im/webpack/webpack) [![Gittip donate button](http://img.shields.io/gittip/sokra.png)](https://www.gittip.com/sokra/)
2014-01-28 22:58:44 +08:00
2014-01-14 22:40:37 +08:00
[documentation](http://webpack.github.io/docs/?utm_source=github&utm_medium=readme&utm_campaign=top)
2012-04-03 22:26:08 +08:00
2013-01-31 01:49:25 +08:00
# Introduction
2012-03-10 20:11:23 +08:00
webpack is a bundler for modules. The main purpose is to bundle javascript files for usage in a browser.
2012-03-10 20:11:23 +08:00
**TL;DR**
* bundles [CommonJs](http://www.commonjs.org/specs/modules/1.0/) and [AMD](https://github.com/amdjs/amdjs-api/wiki/AMD) modules. (even combined)
* can create a single bundle or multiple chunks loaded on demand, to reduce initial loading time.
* dependencies are resolved during compilation reducing the runtime size
* loaders can preprocess files while compiling, i. e. coffee-script to javascript
2014-01-14 22:40:37 +08:00
Check the [documentation](http://webpack.github.io/docs/?utm_source=github&utm_medium=readme&utm_campaign=trdr) if you want to know more...
2012-03-10 20:11:23 +08:00
# Installation
2013-12-05 23:28:50 +08:00
2014-04-09 06:20:56 +08:00
project:
2014-04-09 06:21:23 +08:00
`npm install webpack --save-dev`
2014-04-09 06:20:56 +08:00
global:
2014-04-09 06:21:23 +08:00
`npm install webpack -g`
2013-12-05 23:28:50 +08:00
2012-05-22 01:31:13 +08:00
# Examples
2013-03-26 23:54:41 +08:00
Take a look at the [`examples`](https://github.com/webpack/webpack/tree/master/examples) folder.
2013-01-31 01:49:25 +08:00
2013-12-05 23:28:50 +08:00
2013-01-31 01:49:25 +08:00
# Features
2014-01-15 06:22:35 +08:00
## Plugins
webpack has a [rich plugin interface](http://webpack.github.io/docs/plugins.html). Most of the features within webpack itself use this plugin interface. This makes webpack very **flexible**.
2014-01-15 06:22:35 +08:00
## Performance
webpack uses async I/O and has multiple caching levels. This makes webpack fast and incredibly **fast** on incremental compilations.
2014-01-15 06:22:35 +08:00
## Loaders
webpack enables use of loaders to preprocess files. This allows you to bundle **any static resource** way beyond javascript. You can easily [write your own loaders](http://webpack.github.io/docs/loaders.html) using node.js.
2014-01-15 06:22:35 +08:00
## Support
webpack supports **AMD and CommonJS** module styles. It performs clever static analysis on the AST of your code. It even has an evaluation engine to evaluate simple expressions. This allows you to **support most existing libraries**.
2014-01-15 06:22:35 +08:00
## Code Splitting
webpack allows you to split your codebase into multiple chunks. Chunks are loaded **on demand**. This reduces the initial loading time.
2014-01-15 06:22:35 +08:00
## Optimizations
webpack can do many optimizations to **reduce the output size**. It also can make your chunks **cache friendly** by using hashes.
2014-01-15 06:22:35 +08:00
2014-10-03 05:18:55 +08:00
# A small example of what's possible
2012-03-27 10:23:11 +08:00
``` javascript
2014-02-20 15:52:12 +08:00
// webpack is a module bundler
// This means webpack takes modules with dependencies
2014-12-31 00:14:33 +08:00
// and emits static assets representing those modules.
2014-02-20 15:52:12 +08:00
// dependencies can be written in CommonJs
2013-01-31 01:49:25 +08:00
var commonjs = require("./commonjs");
2014-02-20 15:52:12 +08:00
// or in AMD
define(["amd-module", "../file"], function(amdModule, file) {
// while previous constructs are sync
// this is async
2013-01-31 01:49:25 +08:00
require(["big-module/big/file"], function(big) {
2014-02-20 15:52:12 +08:00
// for async dependencies webpack splits
// your application into multiple "chunks".
// This part of your application is
// loaded on demand (Code Splitting)
2013-01-31 01:49:25 +08:00
var stuff = require("../my/stuff");
2014-02-20 15:52:12 +08:00
// "../my/stuff" is also loaded on demand
// because it's in the callback function
// of the AMD require
2013-01-31 01:49:25 +08:00
});
2012-03-27 10:23:11 +08:00
});
2014-02-20 15:52:12 +08:00
2013-01-31 01:49:25 +08:00
require("coffee!./cup.coffee");
2014-02-20 15:52:12 +08:00
// "Loaders" can be used to preprocess files.
// They can be prefixed in the require call
// or configured in the configuration.
2013-01-31 01:49:25 +08:00
require("./cup");
2014-02-20 15:52:12 +08:00
// This does the same when you add ".coffee" to the extensions
// and configure the "coffee" loader for /\.coffee$/
2013-01-31 01:49:25 +08:00
function loadTemplate(name) {
2014-02-20 15:52:12 +08:00
return require("./templates/" + name + ".jade");
2014-12-31 00:18:51 +08:00
// many expressions are supported in require calls
// a clever parser extracts information and concludes
2014-02-20 15:52:12 +08:00
// that everything in "./templates" that matches
// /\.jade$/ should be included in the bundle, as it
// can be required.
2012-05-12 22:43:37 +08:00
}
2014-02-20 15:52:12 +08:00
// ... and you can combine everything
2013-01-31 01:49:25 +08:00
function loadTemplateAsync(name, callback) {
2014-02-20 15:52:12 +08:00
require(["bundle?lazy!./templates/" + name + ".jade"],
function(templateBundle) {
2013-01-31 01:49:25 +08:00
templateBundle(callback);
});
2012-05-12 22:43:37 +08:00
}
```
2012-03-10 20:11:23 +08:00
2012-03-27 06:34:38 +08:00
2013-05-31 19:46:21 +08:00
## Documentation
2014-01-14 22:40:37 +08:00
[documentation](http://webpack.github.io/docs/?utm_source=github&utm_medium=readme&utm_campaign=documentation)
2013-05-31 19:46:21 +08:00
2012-03-10 20:11:23 +08:00
## Tests
2014-07-31 19:44:57 +08:00
You can run the node tests with `npm test`. [![build status (linux)](https://secure.travis-ci.org/webpack/webpack.png)](http://travis-ci.org/webpack/webpack) [![Build status (windows)](https://ci.appveyor.com/api/projects/status/vatlasj366jiyuh6/branch/master)](https://ci.appveyor.com/project/sokra/webpack/branch/master)
2012-03-10 20:11:23 +08:00
You can run the browser tests:
```
cd test/browsertests
node build
```
2012-10-29 05:56:55 +08:00
and open `tests.html` in browser.
2012-03-10 20:11:23 +08:00
2012-10-26 06:01:57 +08:00
2012-03-10 20:11:23 +08:00
## Contribution
You are welcome to contribute by opening an issue or a pull request.
It would be nice if you open sourced your own loaders or webmodules. :)
2012-03-10 20:11:23 +08:00
2012-03-21 06:28:43 +08:00
You are also welcome to correct any spelling mistakes or any language issues, because my english is not perfect...
2014-09-05 08:59:01 +08:00
If you want to discuss something or just need help, [here is a gitter.im room](https://gitter.im/webpack/webpack).
2012-10-26 06:01:57 +08:00
2012-03-10 20:11:23 +08:00
## License
2014-01-15 06:22:35 +08:00
Copyright (c) 2012-2014 Tobias Koppers
2012-05-25 06:17:02 +08:00
2012-05-12 23:02:20 +08:00
MIT (http://www.opensource.org/licenses/mit-license.php)
2012-10-26 06:01:57 +08:00
2014-01-30 16:03:38 +08:00
## Sponsor
2014-12-31 00:18:51 +08:00
This is a freetime project. My time investment fluctuates randomly. If you use webpack for a serious task you may want me to invest more time. Or if you make some good revenue you can give some money back. Keep in mind that this project may increase your income. It makes development and applications faster and reduces the required bandwidth.
2014-01-30 16:03:38 +08:00
I'm very thankful for every dollar. If you leave your username or email I may show my thanks by giving you extra support.
2014-10-22 08:09:06 +08:00
[![Donate to sokra](http://img.shields.io/donate/sokra.png)](http://sokra.github.io/)
2012-10-26 06:01:57 +08:00
2013-01-31 01:49:25 +08:00
2012-05-12 23:02:20 +08:00
## Dependencies
* [esprima](http://esprima.org/)
2013-01-31 01:49:25 +08:00
* [enhanced-resolve](https://github.com/webpack/enhanced-resolve)
2012-05-12 23:02:20 +08:00
* [uglify-js](https://github.com/mishoo/UglifyJS)
2013-01-31 01:49:25 +08:00
* [mocha](https://github.com/visionmedia/mocha)
* [should](https://github.com/visionmedia/should.js)
* [optimist](https://github.com/substack/node-optimist)
* [async](https://github.com/caolan/async)
* [mkdirp](https://github.com/substack/node-mkdirp)
2013-02-11 07:43:35 +08:00
* [clone](https://github.com/pvorb/node-clone)
2013-03-11 17:06:33 +08:00
[![Dependency Status](https://david-dm.org/webpack/webpack.png)](https://david-dm.org/webpack/webpack)