2020-03-27 10:03:41 +08:00
This example demonstrates how to build a complex library with webpack. The library consists of multiple parts that are usable on its own and together.
2014-07-26 22:40:33 +08:00
2020-03-27 10:03:41 +08:00
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 exports each part.
2014-07-26 22:40:33 +08:00
2020-03-27 10:03:41 +08:00
We are using multiple entry points (`entry` option) to build every part of the library as a 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
2020-05-21 05:26:51 +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.
2014-07-26 22:40:33 +08:00
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')
2020-05-21 05:26:51 +08:00
exports["MyLibrary"] = factory();
2014-04-06 00:11:13 +08:00
else
root["MyLibrary"] = root["MyLibrary"] || {}, root["MyLibrary"]["alpha"] = factory();
2020-09-21 04:39:12 +08:00
})(self, function() {
2019-10-11 05:11:05 +08:00
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ([
/* 0 */
/*!******************!*\
!*** ./alpha.js ** *!
\******************/
2020-05-21 05:26:51 +08:00
/*! unknown exports (runtime-defined) */
2019-10-11 05:11:05 +08:00
/*! runtime requirements: module */
2020-09-21 04:39:12 +08:00
/*! CommonJS bailout: module.exports is used directly at 1:0-14 */
2019-10-11 05:11:05 +08:00
/***/ ((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] = {
2020-05-21 05:26:51 +08:00
/******/ // no module.id needed
/******/ // no module.loaded needed
2016-06-06 02:51:44 +08:00
/******/ 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
/******/ // 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')
2020-05-21 05:26:51 +08:00
exports["MyLibrary"] = factory();
2014-04-06 00:11:13 +08:00
else
root["MyLibrary"] = root["MyLibrary"] || {}, root["MyLibrary"]["beta"] = factory();
2020-09-21 04:39:12 +08:00
})(self, function() {
2019-10-11 05:11:05 +08:00
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/*!*****************!*\
!*** ./beta.js ** *!
\*****************/
2020-05-21 05:26:51 +08:00
/*! unknown exports (runtime-defined) */
2019-10-11 05:11:05 +08:00
/*! runtime requirements: module */
2020-09-21 04:39:12 +08:00
/*! CommonJS bailout: module.exports is used directly at 1:0-14 */
2019-10-11 05:11:05 +08:00
/***/ ((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] = {
2020-05-21 05:26:51 +08:00
/******/ // no module.id needed
/******/ // no module.loaded needed
2016-06-06 02:51:44 +08:00
/******/ 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
/******/ // 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
```
2020-09-21 04:39:12 +08:00
asset MyLibrary.beta.js 1.96 KiB [emitted] (name: beta)
asset MyLibrary.alpha.js 1.95 KiB [emitted] (name: alpha)
2020-12-11 17:29:32 +08:00
chunk (runtime: alpha) MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
2020-09-21 04:39:12 +08:00
> ./alpha alpha
./alpha.js 25 bytes [built] [code generated]
[used exports unknown]
cjs self exports reference ./alpha.js 1:0-14
entry ./alpha alpha
used as library export
2020-12-11 17:29:32 +08:00
chunk (runtime: beta) MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
2020-09-21 04:39:12 +08:00
> ./beta beta
./beta.js 24 bytes [built] [code generated]
[used exports unknown]
cjs self exports reference ./beta.js 1:0-14
entry ./beta beta
used as library export
2020-12-11 17:29:32 +08:00
webpack 5.11.1 compiled successfully
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
```
2020-09-21 04:39:12 +08:00
asset MyLibrary.alpha.js 415 bytes [emitted] [minimized] (name: alpha)
asset MyLibrary.beta.js 411 bytes [emitted] [minimized] (name: beta)
chunk (runtime: alpha) MyLibrary.alpha.js (alpha) 25 bytes [entry] [rendered]
> ./alpha alpha
./alpha.js 25 bytes [built] [code generated]
[used exports unknown]
cjs self exports reference ./alpha.js 1:0-14
entry ./alpha alpha
used as library export
chunk (runtime: beta) MyLibrary.beta.js (beta) 24 bytes [entry] [rendered]
> ./beta beta
./beta.js 24 bytes [built] [code generated]
[used exports unknown]
cjs self exports reference ./beta.js 1:0-14
entry ./beta beta
used as library export
2020-12-11 17:29:32 +08:00
webpack 5.11.1 compiled successfully
2016-09-29 00:47:55 +08:00
```