webpack/examples/commonjs
Tobias Koppers d6317b330b updated examples 2016-06-05 20:51:44 +02:00
..
README.md updated examples 2016-06-05 20:51:44 +02:00
build.js added commonjs example 2012-05-06 21:14:36 +02:00
example.js Fix CRLF 2012-05-23 10:46:37 +02:00
increment.js Fix CRLF 2012-05-23 10:46:37 +02:00
math.js Fix CRLF 2012-05-23 10:46:37 +02:00
template.md Fix typos in a couple of examples 2015-05-21 13:50:02 -07:00

README.md

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).

example.js

var inc = require('./increment').increment;
var a = 1;
inc(a); // 2

increment.js

var add = require('./math').add;
exports.increment = function(val) {
    return add(val, 1);
};

math.js

exports.add = function() {
    var sum = 0, i = 0, args = arguments, l = args.length;
    while (i < l) {
        sum += args[i++];
    }
    return sum;
};

js/output.js

/******/ (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; };

/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "js/";

/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = 1);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/*!**********************!*\
  !*** ./increment.js ***!
  \**********************/
/***/ function(module, exports, __webpack_require__) {

	var add = __webpack_require__(/*! ./math */ 2).add;
	exports.increment = function(val) {
	    return add(val, 1);
	};

/***/ },
/* 1 */
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/***/ function(module, exports, __webpack_require__) {

	var inc = __webpack_require__(/*! ./increment */ 0).increment;
	var a = 1;
	inc(a); // 2

/***/ },
/* 2 */
/*!*****************!*\
  !*** ./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;
	};

/***/ }
/******/ ]);

Info

Uncompressed

Hash: 604c5c1fe4f4a9772853
Version: webpack 2.1.0-beta.11
Time: 94ms
    Asset     Size  Chunks             Chunk Names
output.js  2.32 kB       0  [emitted]  main
chunk    {0} output.js (main) 329 bytes [rendered]
    > main [1] ./example.js 
    [0] ./increment.js 98 bytes {0} [built]
        cjs require ./increment [1] ./example.js 1:10-32
    [1] ./example.js 69 bytes {0} [built]
    [2] ./math.js 162 bytes {0} [built]
        cjs require ./math [0] ./increment.js 1:10-27

Minimized (uglify-js, no zip)

Hash: 604c5c1fe4f4a9772853
Version: webpack 2.1.0-beta.11
Time: 115ms
    Asset       Size  Chunks             Chunk Names
output.js  438 bytes       0  [emitted]  main
chunk    {0} output.js (main) 329 bytes [rendered]
    > main [1] ./example.js 
    [0] ./increment.js 98 bytes {0} [built]
        cjs require ./increment [1] ./example.js 1:10-32
    [1] ./example.js 69 bytes {0} [built]
    [2] ./math.js 162 bytes {0} [built]
        cjs require ./math [0] ./increment.js 1:10-27