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.
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.
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.
We are using the libraryTarget
option to generate a UMD (Universal Module Definition) 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.
You can see that webpack automatically wraps your module so that it is consumable in every environment. All you need is this simple config.
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.
var path = require("path");
module.exports = {
// mode: "development || "production",
entry: {
alpha: "./alpha",
beta: "./beta"
},
output: {
path: path.join(__dirname, "dist"),
filename: "MyLibrary.[name].js",
library: ["MyLibrary", "[name]"],
libraryTarget: "umd"
}
};
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["MyLibrary"] = factory();
else
root["MyLibrary"] = root["MyLibrary"] || {}, root["MyLibrary"]["alpha"] = factory();
})(self, function() {
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ([
/* 0 */
/*!******************!*\
!*** ./alpha.js ***!
\******************/
/*! unknown exports (runtime-defined) */
/*! runtime requirements: module */
/*! CommonJS bailout: module.exports is used directly at 1:0-14 */
/***/ ((module) => {
module.exports = "alpha";
/***/ })
/******/ ]);
/* webpack runtime code */
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ // module exports must be returned from runtime so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ return __webpack_require__(0);
/******/ })()
;
});
(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else if(typeof exports === 'object')
exports["MyLibrary"] = factory();
else
root["MyLibrary"] = root["MyLibrary"] || {}, root["MyLibrary"]["beta"] = factory();
})(self, function() {
return /******/ (() => { // webpackBootstrap
/******/ var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/*!*****************!*\
!*** ./beta.js ***!
\*****************/
/*! unknown exports (runtime-defined) */
/*! runtime requirements: module */
/*! CommonJS bailout: module.exports is used directly at 1:0-14 */
/***/ ((module) => {
module.exports = "beta";
/***/ })
/******/ ]);
/* webpack runtime code */
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ // module exports must be returned from runtime so entry inlining is disabled
/******/ // startup
/******/ // Load entry module and return exports
/******/ return __webpack_require__(1);
/******/ })()
;
});
asset MyLibrary.beta.js 1.96 KiB [emitted] (name: beta)
asset MyLibrary.alpha.js 1.95 KiB [emitted] (name: alpha)
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
webpack 5.11.1 compiled successfully
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
webpack 5.11.1 compiled successfully