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 = {
2017-12-14 17:58:03 +08:00
// mode: "development || "production",
2014-04-06 00:11:13 +08:00
entry: {
alpha: "./alpha",
beta: "./beta"
},
output: {
2018-01-05 04:39:29 +08:00
path: path.join(__dirname, "dist"),
2014-04-06 00:11:13 +08:00
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
```
2018-01-05 04:39:29 +08:00
# dist/MyLibrary.alpha.js
2014-04-06 00:11:13 +08:00
``` 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();
2018-01-03 18:03:20 +08:00
})(window, 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
2018-12-19 21:05:17 +08:00
return /******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
2014-04-06 00:11:13 +08:00
/******/ // 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
2017-05-23 04:45:18 +08:00
/******/ if(installedModules[moduleId]) {
2014-04-06 00:11:13 +08:00
/******/ return installedModules[moduleId].exports;
2017-05-23 04:45:18 +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
/******/
/******/
/******/
2018-01-20 00:06:59 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Load entry module and return exports
2018-12-19 21:05:17 +08:00
/******/ return __webpack_require__ (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 */
/*!******************!*\
!*** ./alpha.js ** *!
\******************/
2017-06-05 22:12:12 +08:00
/*! no static exports found */
2018-12-19 21:05:17 +08:00
/*! runtime requirements: module */
/***/ (function(module) {
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
```
2018-01-05 04:39:29 +08:00
# dist/MyLibrary.beta.js
2014-04-06 00:11:13 +08:00
``` 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();
2018-01-03 18:03:20 +08:00
})(window, function() {
2018-12-19 21:05:17 +08:00
return /******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
2014-04-06 00:11:13 +08:00
/******/ // 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
2017-05-23 04:45:18 +08:00
/******/ if(installedModules[moduleId]) {
2014-04-06 00:11:13 +08:00
/******/ return installedModules[moduleId].exports;
2017-05-23 04:45:18 +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
/******/
/******/
/******/
2018-01-20 00:06:59 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Load entry module and return exports
2018-12-19 21:05:17 +08:00
/******/ return __webpack_require__ (1);
2014-04-06 00:11:13 +08:00
/******/ })
/************************************************************************/
/******/ ([
2016-02-04 20:02:53 +08:00
/* 0 */,
/* 1 */
2014-04-06 00:11:13 +08:00
/*!*****************!*\
!*** ./beta.js ** *!
\*****************/
2017-06-05 22:12:12 +08:00
/*! no static exports found */
2018-12-19 21:05:17 +08:00
/*! runtime requirements: module */
/***/ (function(module) {
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
2017-12-14 17:58:03 +08:00
## Unoptimized
2014-04-06 00:11:13 +08:00
```
2017-12-14 17:58:03 +08:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2018-09-26 05:13:58 +08:00
Version: webpack 5.0.0-next
2017-11-23 16:47:19 +08:00
Asset Size Chunks Chunk Names
2018-12-19 21:05:17 +08:00
MyLibrary.alpha.js 1.77 KiB {0} [emitted] alpha
MyLibrary.beta.js 1.77 KiB {1} [emitted] beta
2016-09-07 18:28:56 +08:00
Entrypoint alpha = MyLibrary.alpha.js
Entrypoint beta = MyLibrary.beta.js
2018-12-19 21:05:17 +08:00
chunk {0} MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
2018-01-20 00:06:59 +08:00
> ./alpha alpha
2018-05-07 18:36:38 +08:00
[0] ./alpha.js 25 bytes {0} [built]
2018-09-26 05:13:58 +08:00
[used exports unknown]
2018-12-19 21:05:17 +08:00
entry ./alpha alpha
chunk {1} MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
2018-01-20 00:06:59 +08:00
> ./beta beta
2018-05-07 18:36:38 +08:00
[1] ./beta.js 24 bytes {1} [built]
2018-09-26 05:13:58 +08:00
[used exports unknown]
2018-12-19 21:05:17 +08:00
entry ./beta beta
2014-04-06 00:11:13 +08:00
```
2017-12-14 17:58:03 +08:00
## Production mode
2014-04-06 00:11:13 +08:00
```
2017-12-14 17:58:03 +08:00
Hash: 0a1b2c3d4e5f6a7b8c9d
2018-09-26 05:13:58 +08:00
Version: webpack 5.0.0-next
2018-12-19 21:05:17 +08:00
Asset Size Chunks Chunk Names
MyLibrary.alpha.js 480 bytes {963} [emitted] alpha
MyLibrary.beta.js 477 bytes {188} [emitted] beta
2016-09-07 18:28:56 +08:00
Entrypoint alpha = MyLibrary.alpha.js
Entrypoint beta = MyLibrary.beta.js
2018-12-19 21:05:17 +08:00
chunk {188} MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
2018-09-25 23:08:35 +08:00
> ./beta beta
2018-12-19 21:05:17 +08:00
[145] ./beta.js 24 bytes {188} [built]
entry ./beta beta
chunk {963} MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
> ./alpha alpha
[930] ./alpha.js 25 bytes {963} [built]
entry ./alpha alpha
2016-09-29 00:47:55 +08:00
```