2014-07-26 22:40:33 +08:00
This example demonstrates how to build a complex library with webpack. The library consist of multiple parts that are usable on its own and together.
When using this library with script tags it exports itself to the namespace `MyLibrary` and each part to a property in this namespace (`MyLibrary.alpha` and `MyLibrary.beta` ). When consuming the library with CommonsJs or AMD it just export each part.
2016-09-29 00:47:55 +08:00
We are using multiple entry points (`entry` option) to build every part of the library as separate output file. The `output.filename` option contains `[name]` to give each output file a different name.
2014-07-26 22:40:33 +08:00
We are using the `libraryTarget` option to generate a UMD ([Universal Module Definition](https://github.com/umdjs/umd)) module that is consumable in CommonsJs, AMD and with script tags. The `library` option defines the namespace. We are using `[name]` in the `library` option to give every entry a different namespace.
2017-03-04 18:23:25 +08:00
You can see that webpack automatically wraps your module so that it is consumable in every environment. All you need is this simple config.
2014-07-26 22:40:33 +08:00
Note: You can also use the `library` and `libraryTarget` options without multiple entry points. Then you don't need `[name]` .
Note: When your library has dependencies that should not be included in the compiled version, you can use the `externals` option. See [externals example ](https://github.com/webpack/webpack/tree/master/examples/externals ).
2014-04-06 00:11:13 +08:00
# webpack.config.js
``` javascript
var path = require("path");
module.exports = {
entry: {
alpha: "./alpha",
beta: "./beta"
},
output: {
path: path.join(__dirname, "js"),
filename: "MyLibrary.[name].js",
library: ["MyLibrary", "[name]"],
libraryTarget: "umd"
}
2017-03-31 02:25:01 +08:00
};
2014-04-06 00:11:13 +08:00
```
# js/MyLibrary.alpha.js
``` javascript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' & & typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' & & define.amd)
2016-02-04 20:02:53 +08:00
define([], factory);
2014-04-06 00:11:13 +08:00
else if(typeof exports === 'object')
exports["alpha"] = factory();
else
root["MyLibrary"] = root["MyLibrary"] || {}, root["MyLibrary"]["alpha"] = factory();
})(this, function() {
2016-12-14 19:03:24 +08:00
```
2017-03-31 02:42:42 +08:00
< details > < summary > < code > return /******/ (function(modules) { /* webpackBootstrap */ })< / code > < / summary >
2016-12-14 19:03:24 +08:00
``` js
2014-04-06 00:11:13 +08:00
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // The require function
/******/ function __webpack_require__ (moduleId) {
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-06 02:51:44 +08:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2014-04-06 00:11:13 +08:00
/******/ };
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__ );
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Flag the module as loaded
2016-06-06 02:51:44 +08:00
/******/ module.l = true;
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
2017-03-31 02:25:01 +08:00
/******/
/******/
2014-04-06 00:11:13 +08:00
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__ .m = modules;
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // expose the module cache
/******/ __webpack_require__ .c = installedModules;
2017-03-31 02:25:01 +08:00
/******/
2016-12-08 02:14:47 +08:00
/******/ // identity function for calling harmony imports with the correct context
2016-06-06 02:51:44 +08:00
/******/ __webpack_require__ .i = function(value) { return value; };
2017-03-31 02:25:01 +08:00
/******/
2016-12-08 02:14:47 +08:00
/******/ // define getter function for harmony exports
2016-09-07 18:28:56 +08:00
/******/ __webpack_require__ .d = function(exports, name, getter) {
2016-12-14 19:03:24 +08:00
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
2016-09-07 18:28:56 +08:00
/******/ };
2017-03-31 02:25:01 +08:00
/******/
2016-09-07 18:28:56 +08:00
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__ .n = function(module) {
/******/ var getter = module & & module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__ .d(getter, 'a', getter);
/******/ return getter;
/******/ };
2017-03-31 02:25:01 +08:00
/******/
2016-09-07 18:28:56 +08:00
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__ .o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // __webpack_public_path__
/******/ __webpack_require__ .p = "js/";
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Load entry module and return exports
2016-02-04 20:02:53 +08:00
/******/ return __webpack_require__ (__webpack_require__.s = 0);
2014-04-06 00:11:13 +08:00
/******/ })
/************************************************************************/
2016-12-14 19:03:24 +08:00
```
2017-03-31 02:42:42 +08:00
2016-12-14 19:03:24 +08:00
< / details >
2017-03-31 02:42:42 +08:00
2016-12-14 19:03:24 +08:00
``` js
2014-04-06 00:11:13 +08:00
/******/ ([
/* 0 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
2014-04-06 00:11:13 +08:00
/*!******************!*\
!*** ./alpha.js ** *!
\******************/
2017-03-31 02:25:01 +08:00
/***/ (function(module, exports) {
2014-04-06 00:11:13 +08:00
2016-09-07 18:28:56 +08:00
module.exports = "alpha";
2014-04-06 00:11:13 +08:00
2017-03-31 02:25:01 +08:00
/***/ })
2016-12-14 19:03:24 +08:00
/******/ ]);
2015-01-18 07:49:58 +08:00
});
2014-04-06 00:11:13 +08:00
```
# js/MyLibrary.beta.js
``` javascript
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' & & typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' & & define.amd)
2016-02-04 20:02:53 +08:00
define([], factory);
2014-04-06 00:11:13 +08:00
else if(typeof exports === 'object')
exports["beta"] = factory();
else
root["MyLibrary"] = root["MyLibrary"] || {}, root["MyLibrary"]["beta"] = factory();
})(this, function() {
return /******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // The require function
/******/ function __webpack_require__ (moduleId) {
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-06 02:51:44 +08:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2014-04-06 00:11:13 +08:00
/******/ };
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__ );
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Flag the module as loaded
2016-06-06 02:51:44 +08:00
/******/ module.l = true;
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
2017-03-31 02:25:01 +08:00
/******/
/******/
2014-04-06 00:11:13 +08:00
/******/ // expose the modules object (__webpack_modules__)
/******/ __webpack_require__ .m = modules;
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // expose the module cache
/******/ __webpack_require__ .c = installedModules;
2017-03-31 02:25:01 +08:00
/******/
2016-12-08 02:14:47 +08:00
/******/ // identity function for calling harmony imports with the correct context
2016-06-06 02:51:44 +08:00
/******/ __webpack_require__ .i = function(value) { return value; };
2017-03-31 02:25:01 +08:00
/******/
2016-12-08 02:14:47 +08:00
/******/ // define getter function for harmony exports
2016-09-07 18:28:56 +08:00
/******/ __webpack_require__ .d = function(exports, name, getter) {
2016-12-14 19:03:24 +08:00
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, {
/******/ configurable: false,
/******/ enumerable: true,
/******/ get: getter
/******/ });
/******/ }
2016-09-07 18:28:56 +08:00
/******/ };
2017-03-31 02:25:01 +08:00
/******/
2016-09-07 18:28:56 +08:00
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__ .n = function(module) {
/******/ var getter = module & & module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__ .d(getter, 'a', getter);
/******/ return getter;
/******/ };
2017-03-31 02:25:01 +08:00
/******/
2016-09-07 18:28:56 +08:00
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__ .o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // __webpack_public_path__
/******/ __webpack_require__ .p = "js/";
2017-03-31 02:25:01 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Load entry module and return exports
2016-02-04 20:02:53 +08:00
/******/ return __webpack_require__ (__webpack_require__.s = 1);
2014-04-06 00:11:13 +08:00
/******/ })
/************************************************************************/
/******/ ([
2016-02-04 20:02:53 +08:00
/* 0 */,
/* 1 */
2016-09-07 18:28:56 +08:00
/* unknown exports provided */
/* all exports used */
2014-04-06 00:11:13 +08:00
/*!*****************!*\
!*** ./beta.js ** *!
\*****************/
2017-03-31 02:25:01 +08:00
/***/ (function(module, exports) {
2014-04-06 00:11:13 +08:00
2016-09-07 18:28:56 +08:00
module.exports = "beta";
2014-04-06 00:11:13 +08:00
2017-03-31 02:25:01 +08:00
/***/ })
2016-12-14 19:03:24 +08:00
/******/ ]);
2015-01-18 07:49:58 +08:00
});
2014-04-06 00:11:13 +08:00
```
# Info
## Uncompressed
```
2016-09-07 18:28:56 +08:00
Hash: 082bbeea226fa367215b
2017-03-31 02:25:01 +08:00
Version: webpack 2.3.2
2015-06-13 23:41:14 +08:00
Asset Size Chunks Chunk Names
2017-03-31 02:25:01 +08:00
MyLibrary.beta.js 3.21 kB 0 [emitted] beta
MyLibrary.alpha.js 3.21 kB 1 [emitted] alpha
2016-09-07 18:28:56 +08:00
Entrypoint alpha = MyLibrary.alpha.js
Entrypoint beta = MyLibrary.beta.js
chunk {0} MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
2016-02-04 20:02:53 +08:00
> beta [1] ./beta.js
[1] ./beta.js 24 bytes {0} [built]
2016-09-07 18:28:56 +08:00
chunk {1} MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
2014-04-06 00:11:13 +08:00
> alpha [0] ./alpha.js
2016-02-04 20:02:53 +08:00
[0] ./alpha.js 25 bytes {1} [built]
2014-04-06 00:11:13 +08:00
```
## Minimized (uglify-js, no zip)
```
2016-09-07 18:28:56 +08:00
Hash: 082bbeea226fa367215b
2017-03-31 02:25:01 +08:00
Version: webpack 2.3.2
2015-06-13 23:41:14 +08:00
Asset Size Chunks Chunk Names
2016-12-14 19:03:24 +08:00
MyLibrary.beta.js 785 bytes 0 [emitted] beta
MyLibrary.alpha.js 787 bytes 1 [emitted] alpha
2016-09-07 18:28:56 +08:00
Entrypoint alpha = MyLibrary.alpha.js
Entrypoint beta = MyLibrary.beta.js
chunk {0} MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
2016-02-04 20:02:53 +08:00
> beta [1] ./beta.js
[1] ./beta.js 24 bytes {0} [built]
2016-09-07 18:28:56 +08:00
chunk {1} MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
2014-04-06 00:11:13 +08:00
> alpha [0] ./alpha.js
2015-06-13 23:41:14 +08:00
[0] ./alpha.js 25 bytes {1} [built]
2016-09-29 00:47:55 +08:00
```