This very simple example shows usage of CommonJS.
The three files example.js, increment.js and math.js form a dependency chain. They use require(dependency) to declare dependencies.
You can see the output file that webpack creates by bundling them together in one file. Keep in mind that webpack adds comments to make reading this file easier. These comments are removed when minimizing the file.
You can also see the info messages webpack prints to console (for both normal and minimized build).
var inc = require('./increment').increment;
var a = 1;
inc(a); // 2var add = require('./math').add;
exports.increment = function(val) {
return add(val, 1);
};exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};`/******/ (function(modules) { /* webpackBootstrap */ })`
``` javascript /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {};// // The require function // function webpack_require(moduleId) {
// // Check if module is in cache // if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports;
// // Create a new module (and put it into the cache) // var module = installedModules[moduleId] = { // i: moduleId, // l: false, // exports: {} // };
// // Execute the module function // modules[moduleId].call(module.exports, module, module.exports, webpack_require);
// // Flag the module as loaded // module.l = true;
// // Return the exports of the module // return module.exports; /******/ }
// // expose the modules object (webpack_modules) // webpack_require.m = modules;
// // expose the module cache // webpack_require.c = installedModules;
// // identity function for calling harmory imports with the correct context // webpack_require.i = function(value) { return value; };
// // define getter function for harmory exports // webpack_require.d = function(exports, name, getter) { // Object.defineProperty(exports, name, { // configurable: false, // enumerable: true, // get: getter // }); // };
// // 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; // };
// // Object.prototype.hasOwnProperty.call // webpack_require.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
// // webpack_public_path // webpack_require.p = "js/";
// // Load entry module and return exports // return webpack_require(webpack_require.s = 2); // }) /******************************************************************/
</details>
``` javascript
/******/ ([
/* 0 */
/* unknown exports provided */
/* all exports used */
/*!**********************!*\
!*** ./increment.js ***!
\**********************/
/***/ function(module, exports, __webpack_require__) {
var add = __webpack_require__(/*! ./math */ 1).add;
exports.increment = function(val) {
return add(val, 1);
};
/***/ },
/* 1 */
/* unknown exports provided */
/* all exports used */
/*!*****************!*\
!*** ./math.js ***!
\*****************/
/***/ function(module, exports) {
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
/***/ },
/* 2 */
/* unknown exports provided */
/* all exports used */
/*!********************!*\
!*** ./example.js ***!
\********************/
/***/ function(module, exports, __webpack_require__) {
var inc = __webpack_require__(/*! ./increment */ 0).increment;
var a = 1;
inc(a); // 2
/***/ }
/******/ ]);
Hash: 347d551e233e017e5be5
Version: webpack 2.1.0-beta.25
Time: 114ms
Asset Size Chunks Chunk Names
output.js 3.33 kB 0 [emitted] main
Entrypoint main = output.js
chunk {0} output.js (main) 318 bytes [entry] [rendered]
> main [2] ./example.js
[0] ./increment.js 95 bytes {0} [built]
cjs require ./increment [2] ./example.js 1:10-32
[1] ./math.js 156 bytes {0} [built]
cjs require ./math [0] ./increment.js 1:10-27
[2] ./example.js 67 bytes {0} [built]
Hash: 347d551e233e017e5be5
Version: webpack 2.1.0-beta.25
Time: 219ms
Asset Size Chunks Chunk Names
output.js 703 bytes 0 [emitted] main
Entrypoint main = output.js
chunk {0} output.js (main) 318 bytes [entry] [rendered]
> main [2] ./example.js
[0] ./increment.js 95 bytes {0} [built]
cjs require ./increment [2] ./example.js 1:10-32
[1] ./math.js 156 bytes {0} [built]
cjs require ./math [0] ./increment.js 1:10-27
[2] ./example.js 67 bytes {0} [built]