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
2019-04-09 02:29:40 +08:00
```javascript
2014-04-06 00:11:13 +08:00
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
2019-04-09 02:29:40 +08:00
```javascript
2014-04-06 00:11:13 +08:00
(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() {
2019-10-11 05:11:05 +08:00
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ([
/* 0 */
/*!******************!*\
!*** ./alpha.js ** *!
\******************/
/*! exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = "alpha";
/***/ })
/******/ ]);
2019-11-19 21:10:28 +08:00
```
< details > < summary > < code > /* webpack runtime code */< / code > < / summary >
``` js
2019-10-11 05:11:05 +08:00
/************************************************************************/
2014-04-06 00:11:13 +08:00
/******/ // The module cache
2019-10-11 05:11:05 +08:00
/******/ var __webpack_module_cache__ = {};
/******/
2014-04-06 00:11:13 +08:00
/******/ // The require function
/******/ function __webpack_require__ (moduleId) {
/******/ // Check if module is in cache
2019-10-11 05:11:05 +08:00
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__ [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)
2019-10-11 05:11:05 +08:00
/******/ var module = __webpack_module_cache__ [moduleId] = {
2016-06-06 02:51:44 +08:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2014-04-06 00:11:13 +08:00
/******/ };
2019-10-11 05:11:05 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Execute the module function
2019-10-11 05:11:05 +08:00
/******/ __webpack_modules__ [moduleId ](module, module.exports, __webpack_require__ );
/******/
2014-04-06 00:11:13 +08:00
/******/ // Flag the module as loaded
2016-06-06 02:51:44 +08:00
/******/ module.l = true;
2019-10-11 05:11:05 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
2019-10-11 05:11:05 +08:00
/******/
2014-04-06 00:11:13 +08:00
/************************************************************************/
2019-11-19 21:10:28 +08:00
```
< / details >
``` js
2019-10-11 05:11:05 +08:00
/******/ // module exports must be returned from runtime so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ return __webpack_require__ (0);
/******/ })()
;
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
2019-04-09 02:29:40 +08:00
```javascript
2014-04-06 00:11:13 +08:00
(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() {
2019-10-11 05:11:05 +08:00
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/*!*****************!*\
!*** ./beta.js ** *!
\*****************/
/*! exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {
module.exports = "beta";
/***/ })
/******/ ]);
2019-11-19 21:10:28 +08:00
```
< details > < summary > < code > /* webpack runtime code */< / code > < / summary >
``` js
2019-10-11 05:11:05 +08:00
/************************************************************************/
2014-04-06 00:11:13 +08:00
/******/ // The module cache
2019-10-11 05:11:05 +08:00
/******/ var __webpack_module_cache__ = {};
/******/
2014-04-06 00:11:13 +08:00
/******/ // The require function
/******/ function __webpack_require__ (moduleId) {
/******/ // Check if module is in cache
2019-10-11 05:11:05 +08:00
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__ [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)
2019-10-11 05:11:05 +08:00
/******/ var module = __webpack_module_cache__ [moduleId] = {
2016-06-06 02:51:44 +08:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2014-04-06 00:11:13 +08:00
/******/ };
2019-10-11 05:11:05 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Execute the module function
2019-10-11 05:11:05 +08:00
/******/ __webpack_modules__ [moduleId ](module, module.exports, __webpack_require__ );
/******/
2014-04-06 00:11:13 +08:00
/******/ // Flag the module as loaded
2016-06-06 02:51:44 +08:00
/******/ module.l = true;
2019-10-11 05:11:05 +08:00
/******/
2014-04-06 00:11:13 +08:00
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
2019-10-11 05:11:05 +08:00
/******/
2014-04-06 00:11:13 +08:00
/************************************************************************/
2019-11-19 21:10:28 +08:00
```
< / details >
``` js
2019-10-11 05:11:05 +08:00
/******/ // module exports must be returned from runtime so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ return __webpack_require__ (1);
/******/ })()
;
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
2019-11-19 21:10:28 +08:00
Version: webpack 5.0.0-beta.6
2019-10-11 05:11:05 +08:00
Asset Size
MyLibrary.alpha.js 1.97 KiB [emitted] [name: alpha]
MyLibrary.beta.js 1.98 KiB [emitted] [name: beta]
2016-09-07 18:28:56 +08:00
Entrypoint alpha = MyLibrary.alpha.js
Entrypoint beta = MyLibrary.beta.js
2019-10-11 05:11:05 +08:00
chunk MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
2018-01-20 00:06:59 +08:00
> ./alpha alpha
2019-10-11 05:11:05 +08:00
./alpha.js 25 bytes [built]
2018-12-19 21:05:17 +08:00
entry ./alpha alpha
2019-10-11 05:11:05 +08:00
used a library export
chunk MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
2018-01-20 00:06:59 +08:00
> ./beta beta
2019-10-11 05:11:05 +08:00
./beta.js 24 bytes [built]
2018-12-19 21:05:17 +08:00
entry ./beta beta
2019-10-11 05:11:05 +08:00
used a library export
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
2019-11-19 21:10:28 +08:00
Version: webpack 5.0.0-beta.6
2019-10-11 05:11:05 +08:00
Asset Size
2019-10-28 21:09:53 +08:00
MyLibrary.alpha.js 429 bytes [emitted] [name: alpha]
MyLibrary.beta.js 424 bytes [emitted] [name: beta]
2016-09-07 18:28:56 +08:00
Entrypoint alpha = MyLibrary.alpha.js
Entrypoint beta = MyLibrary.beta.js
2019-10-11 05:11:05 +08:00
chunk MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
2018-12-19 21:05:17 +08:00
> ./alpha alpha
2019-10-11 05:11:05 +08:00
./alpha.js 25 bytes [built]
entry ./alpha alpha
used a library export
chunk MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
2018-09-25 23:08:35 +08:00
> ./beta beta
2019-10-11 05:11:05 +08:00
./beta.js 24 bytes [built]
entry ./beta beta
used a library export
2016-09-29 00:47:55 +08:00
```