webpack/examples/require.resolve
Tobias Koppers d0b358d108 update examples 2020-05-20 23:26:51 +02:00
..
README.md update examples 2020-05-20 23:26:51 +02:00
a.js added require.resolve example 2012-07-19 11:48:07 +02:00
build.js added require.resolve example 2012-07-19 11:48:07 +02:00
example.js spelling: verify 2018-02-26 02:50:43 +00:00
template.md chore(examples): fix exmaples template syntax 2019-04-08 19:26:44 +01:00

README.md

example.js

var a = require("./a");

// get module id
var aId = require.resolve("./a.js");

// clear module in require.cache
delete require.cache[aId];

// require module again, it should be reexecuted
var a2 = require("./a");

// verify it
if(a == a2) throw new Error("Cache clear failed :(");

a.js

module.exports = Math.random();

dist/output.js

/******/ (() => { // webpackBootstrap
/******/ 	var __webpack_modules__ = ([
/* 0 */
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/*! unknown exports (runtime-defined) */
/*! exports [maybe provided (runtime-defined)] [unused] */
/*! runtime requirements: __webpack_require__, __webpack_require__.c, module.id, module.loaded, __webpack_require__.*, module */
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

var a = __webpack_require__(/*! ./a */ 1);

// get module id
var aId = /*require.resolve*/(/*! ./a.js */ 1);

// clear module in require.cache
delete __webpack_require__.c[aId];

// require module again, it should be reexecuted
var a2 = __webpack_require__(/*! ./a */ 1);

// verify it
if(a == a2) throw new Error("Cache clear failed :(");

/***/ }),
/* 1 */
/*!**************!*\
  !*** ./a.js ***!
  \**************/
/*! unknown exports (runtime-defined) */
/*! exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {

module.exports = Math.random();

/***/ })
/******/ 	]);
/* 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] = {
/******/ 			id: moduleId,
/******/ 			loaded: false,
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Flag the module as loaded
/******/ 		module.loaded = true;
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = __webpack_module_cache__;
/******/ 	
/************************************************************************/
/******/ 	// module cache are used so entry inlining is disabled
/******/ 	// startup
/******/ 	// Load entry module
/******/ 	__webpack_require__(0);
/******/ })()
;

Info

Unoptimized

Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.16
    Asset      Size
output.js  2.38 KiB  [emitted]  [name: main]
Entrypoint main = output.js
chunk output.js (main) 313 bytes [entry] [rendered]
    > ./example.js main
 ./a.js 31 bytes [built]
     cjs self exports reference ./a.js 1:0-14
     cjs require ./a ./example.js 1:8-22
     require.resolve ./a.js ./example.js 4:10-35
     cjs require ./a ./example.js 10:9-23
 ./example.js 282 bytes [built]
     [no exports used]
     entry ./example.js main

Production mode

Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.16
    Asset       Size
output.js  297 bytes  [emitted]  [name: main]
Entrypoint main = output.js
chunk output.js (main) 313 bytes [entry] [rendered]
    > ./example.js main
 ./a.js 31 bytes [built]
     cjs self exports reference ./a.js 1:0-14
     cjs require ./a ./example.js 1:8-22
     require.resolve ./a.js ./example.js 4:10-35
     cjs require ./a ./example.js 10:9-23
 ./example.js 282 bytes [built]
     [no exports used]
     entry ./example.js main