webpack/examples/commonjs/README.md

218 lines
6.5 KiB
Markdown
Raw Normal View History

2015-05-21 22:49:03 +02:00
This very simple example shows usage of CommonJS.
2014-07-24 11:29:49 +02:00
The three files `example.js`, `increment.js` and `math.js` form a dependency chain. They use `require(dependency)` to declare dependencies.
2015-05-21 22:49:03 +02:00
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.
2014-07-24 11:29:49 +02:00
You can also see the info messages webpack prints to console (for both normal and minimized build).
2012-05-06 21:14:36 +02:00
# example.js
2019-04-08 20:29:40 +02:00
```javascript
2018-07-29 15:11:11 +02:00
const inc = require('./increment').increment;
const a = 1;
2012-05-06 21:14:36 +02:00
inc(a); // 2
```
# increment.js
2019-04-08 20:29:40 +02:00
```javascript
2018-07-29 15:11:11 +02:00
const add = require('./math').add;
2012-05-06 21:14:36 +02:00
exports.increment = function(val) {
return add(val, 1);
};
```
2012-07-19 12:15:52 +02:00
# math.js
2012-05-06 21:14:36 +02:00
2019-04-08 20:29:40 +02:00
```javascript
2012-05-06 21:14:36 +02:00
exports.add = function() {
2018-09-25 17:08:35 +02:00
var sum = 0, i = 0, args = arguments, l = args.length;
2012-05-06 21:14:36 +02:00
while (i < l) {
sum += args[i++];
}
return sum;
};
```
# dist/output.js
2012-05-06 21:14:36 +02:00
2017-03-30 20:42:42 +02:00
<details><summary><code>/******/ (function(modules) { /* webpackBootstrap */ })</code></summary>
2019-04-08 20:29:40 +02:00
```javascript
2013-03-28 10:31:52 +01:00
/******/ (function(modules) { // webpackBootstrap
/******/ // The module cache
/******/ var installedModules = {};
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // The require function
2014-03-25 10:44:10 +01:00
/******/ function __webpack_require__(moduleId) {
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Check if module is in cache
2017-05-22 22:45:18 +02:00
/******/ if(installedModules[moduleId]) {
2014-03-25 10:44:10 +01:00
/******/ return installedModules[moduleId].exports;
2017-05-22 22:45:18 +02:00
/******/ }
2013-03-28 10:31:52 +01:00
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
2016-06-05 20:51:44 +02:00
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
2013-03-28 10:31:52 +01:00
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Execute the module function
2014-03-25 10:44:10 +01:00
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Flag the module as loaded
2016-06-05 20:51:44 +02:00
/******/ module.l = true;
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // Return the exports of the module
2014-03-25 10:44:10 +01:00
/******/ return module.exports;
2013-03-28 10:31:52 +01:00
/******/ }
2017-03-30 20:25:01 +02:00
/******/
/******/
2013-03-28 10:31:52 +01:00
/******/ // expose the modules object (__webpack_modules__)
2014-03-25 10:44:10 +01:00
/******/ __webpack_require__.m = modules;
2017-03-30 20:25:01 +02:00
/******/
2013-03-28 10:31:52 +01:00
/******/ // expose the module cache
2014-03-25 10:44:10 +01:00
/******/ __webpack_require__.c = installedModules;
2017-03-30 20:25:01 +02:00
/******/
2016-12-07 19:14:47 +01:00
/******/ // define getter function for harmony exports
2016-09-07 12:28:56 +02:00
/******/ __webpack_require__.d = function(exports, name, getter) {
2016-12-14 12:03:24 +01:00
/******/ if(!__webpack_require__.o(exports, name)) {
2018-09-25 17:08:35 +02:00
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
2016-12-14 12:03:24 +01:00
/******/ }
2016-09-07 12:28:56 +02:00
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2017-11-24 08:40:39 +01:00
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
2018-09-25 17:08:35 +02:00
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
2017-11-24 08:40:39 +01:00
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
2018-09-25 17:08:35 +02:00
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
2016-09-07 12:28:56 +02:00
/******/ // 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;
/******/ };
2017-03-30 20:25:01 +02:00
/******/
2016-09-07 12:28:56 +02:00
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
2017-03-30 20:25:01 +02:00
/******/
2013-12-15 23:30:50 +01:00
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "dist/";
2017-03-30 20:25:01 +02:00
/******/
/******/
2013-03-28 10:31:52 +01:00
/******/ // Load entry module and return exports
2017-06-05 16:12:12 +02:00
/******/ return __webpack_require__(__webpack_require__.s = 0);
2013-03-28 10:31:52 +01:00
/******/ })
2013-03-26 18:22:30 +01:00
/************************************************************************/
2016-09-07 12:28:56 +02:00
```
2017-03-30 20:42:42 +02:00
2016-09-07 12:28:56 +02:00
</details>
2017-03-30 20:42:42 +02:00
2019-04-08 20:29:40 +02:00
```javascript
2013-12-15 23:30:50 +01:00
/******/ ([
/* 0 */
2017-06-05 16:12:12 +02:00
/*!********************!*\
!*** ./example.js ***!
\********************/
/*! no static exports found */
/***/ (function(module, exports, __webpack_require__) {
2018-09-25 17:08:35 +02:00
const inc = __webpack_require__(/*! ./increment */ 1).increment;
const a = 1;
2017-06-05 16:12:12 +02:00
inc(a); // 2
2018-09-25 17:08:35 +02:00
2017-06-05 16:12:12 +02:00
/***/ }),
/* 1 */
2013-01-30 18:49:25 +01:00
/*!**********************!*\
!*** ./increment.js ***!
\**********************/
2017-06-05 16:12:12 +02:00
/*! no static exports found */
2017-03-30 20:25:01 +02:00
/***/ (function(module, exports, __webpack_require__) {
2013-01-30 18:49:25 +01:00
2018-09-25 17:08:35 +02:00
const add = __webpack_require__(/*! ./math */ 2).add;
2016-09-07 12:28:56 +02:00
exports.increment = function(val) {
return add(val, 1);
};
2013-01-30 18:49:25 +01:00
2018-09-25 17:08:35 +02:00
2017-03-30 20:25:01 +02:00
/***/ }),
/* 2 */
2013-01-30 18:49:25 +01:00
/*!*****************!*\
!*** ./math.js ***!
\*****************/
2017-06-05 16:12:12 +02:00
/*! no static exports found */
2017-03-30 20:25:01 +02:00
/***/ (function(module, exports) {
2013-01-30 18:49:25 +01:00
2016-09-07 12:28:56 +02:00
exports.add = function() {
var sum = 0, i = 0, args = arguments, l = args.length;
while (i < l) {
sum += args[i++];
}
return sum;
};
2017-03-30 20:25:01 +02:00
/***/ })
2015-06-13 17:41:14 +02:00
/******/ ]);
2012-05-06 21:14:36 +02:00
```
# Info
2017-12-14 10:58:03 +01:00
## Unoptimized
2012-05-06 21:14:36 +02:00
```
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 4.29.6
2018-09-25 17:08:35 +02:00
Asset Size Chunks Chunk Names
output.js 4.4 KiB 0 [emitted] main
2016-09-07 12:28:56 +02:00
Entrypoint main = output.js
2018-09-25 17:08:35 +02:00
chunk {0} output.js (main) 326 bytes [entry] [rendered]
2019-01-25 13:15:22 +01:00
> ./example.js main
2018-09-25 17:08:35 +02:00
[0] ./example.js 72 bytes {0} [built]
2019-01-25 13:15:22 +01:00
single entry ./example.js main
2018-05-07 12:36:38 +02:00
[1] ./increment.js 98 bytes {0} [built]
2018-09-25 17:08:35 +02:00
cjs require ./increment [0] ./example.js 1:12-34
[2] ./math.js 156 bytes {0} [built]
cjs require ./math [1] ./increment.js 1:12-29
2012-05-06 21:14:36 +02:00
```
2017-12-14 10:58:03 +01:00
## Production mode
2012-05-06 21:14:36 +02:00
```
2017-12-14 10:58:03 +01:00
Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 4.29.6
2018-09-25 17:08:35 +02:00
Asset Size Chunks Chunk Names
output.js 1.1 KiB 0 [emitted] main
2016-09-07 12:28:56 +02:00
Entrypoint main = output.js
2018-09-25 17:08:35 +02:00
chunk {0} output.js (main) 326 bytes [entry] [rendered]
2019-01-25 13:15:22 +01:00
> ./example.js main
2018-09-25 17:08:35 +02:00
[0] ./example.js 72 bytes {0} [built]
2019-01-25 13:15:22 +01:00
single entry ./example.js main
2018-09-25 17:08:35 +02:00
[1] ./increment.js 98 bytes {0} [built]
cjs require ./increment [0] ./example.js 1:12-34
[2] ./math.js 156 bytes {0} [built]
cjs require ./math [1] ./increment.js 1:12-29
2019-04-08 20:29:40 +02:00
```