webpack/examples/require.context/README.md

5.8 KiB

example.js

function getTemplate(templateName) {
	return require("./templates/"+templateName);
}
console.log(getTemplate("a"));
console.log(getTemplate("b"));

templates/

  • a.js
  • b.js
  • c.js

All templates are of this pattern:

module.exports = function() {
	return "This text was generated by template X";
}

dist/output.js

/******/ (() => { // webpackBootstrap
/******/ 	var __webpack_modules__ = ([
/* 0 */,
/* 1 */
/*!*********************************!*\
  !*** ./templates sync ^\.\/.*$ ***!
  \*********************************/
/*! exports [maybe provided (runtime-defined)] [no usage info] */
/*! runtime requirements: module, __webpack_require__ */
/***/ ((module, __unused_webpack_exports, __webpack_require__) => {

var map = {
	"./a": 2,
	"./a.js": 2,
	"./b": 3,
	"./b.js": 3,
	"./c": 4,
	"./c.js": 4
};


function webpackContext(req) {
	var id = webpackContextResolve(req);
	return __webpack_require__(id);
}
function webpackContextResolve(req) {
	if(!Object.prototype.hasOwnProperty.call(map, req)) {
		var e = new Error("Cannot find module '" + req + "'");
		e.code = 'MODULE_NOT_FOUND';
		throw e;
	}
	return map[req];
}
webpackContext.keys = function webpackContextKeys() {
	return Object.keys(map);
};
webpackContext.resolve = webpackContextResolve;
module.exports = webpackContext;
webpackContext.id = 1;

/***/ }),
/* 2 */
/*!************************!*\
  !*** ./templates/a.js ***!
  \************************/
/*! exports [maybe provided (runtime-defined)] [no usage info] */
/*! runtime requirements: module */
/***/ ((module) => {

module.exports = function() {
	return "This text was generated by template A";
}

/***/ }),
/* 3 */
/*!************************!*\
  !*** ./templates/b.js ***!
  \************************/
/*! exports [maybe provided (runtime-defined)] [no usage info] */
/*! runtime requirements: module */
/***/ ((module) => {

module.exports = function() {
	return "This text was generated by template B";
}

/***/ }),
/* 4 */
/*!************************!*\
  !*** ./templates/c.js ***!
  \************************/
/*! exports [maybe provided (runtime-defined)] [no usage info] */
/*! runtime requirements: module */
/***/ ((module) => {

module.exports = function() {
	return "This text was generated by template C";
}

/***/ })
/******/ 	]);
/* 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] = {
/******/ 			i: moduleId,
/******/ 			l: false,
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Flag the module as loaded
/******/ 		module.l = true;
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
!function() {
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/*! exports [maybe provided (runtime-defined)] [no usage info] */
/*! runtime requirements: __webpack_require__ */
function getTemplate(templateName) {
	return __webpack_require__(1)("./"+templateName);
}
console.log(getTemplate("a"));
console.log(getTemplate("b"));
}();
/******/ })()
;

Info

Unoptimized

Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.6
    Asset     Size
output.js  3.3 KiB  [emitted]  [name: main]
Entrypoint main = output.js
chunk output.js (main) 603 bytes [entry] [rendered]
    > ./example.js main
 ./example.js 146 bytes [built]
     [used exports unknown]
     entry ./example.js main
 ./templates sync ^\.\/.*$ 217 bytes [built]
     [used exports unknown]
     cjs require context ./templates ./example.js 2:8-44
 ./templates/a.js 80 bytes [optional] [built]
     [used exports unknown]
     context element ./a ./templates sync ^\.\/.*$ ./a
     context element ./a.js ./templates sync ^\.\/.*$ ./a.js
 ./templates/b.js 80 bytes [optional] [built]
     [used exports unknown]
     context element ./b ./templates sync ^\.\/.*$ ./b
     context element ./b.js ./templates sync ^\.\/.*$ ./b.js
 ./templates/c.js 80 bytes [optional] [built]
     [used exports unknown]
     context element ./c ./templates sync ^\.\/.*$ ./c
     context element ./c.js ./templates sync ^\.\/.*$ ./c.js

Production mode

Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.6
    Asset       Size
output.js  820 bytes  [emitted]  [name: main]
Entrypoint main = output.js
chunk output.js (main) 603 bytes [entry] [rendered]
    > ./example.js main
 ./example.js 146 bytes [built]
     [no exports used]
     entry ./example.js main
 ./templates sync ^\.\/.*$ 217 bytes [built]
     cjs require context ./templates ./example.js 2:8-44
 ./templates/a.js 80 bytes [optional] [built]
     context element ./a ./templates sync ^\.\/.*$ ./a
     context element ./a.js ./templates sync ^\.\/.*$ ./a.js
 ./templates/b.js 80 bytes [optional] [built]
     context element ./b ./templates sync ^\.\/.*$ ./b
     context element ./b.js ./templates sync ^\.\/.*$ ./b.js
 ./templates/c.js 80 bytes [optional] [built]
     context element ./c ./templates sync ^\.\/.*$ ./c
     context element ./c.js ./templates sync ^\.\/.*$ ./c.js

Code Splitting

See this example combined with code splitting