webpack/examples/require.context
Tobias Koppers d0b358d108 update examples 2020-05-20 23:26:51 +02:00
..
templates added contexts, docs and examples 2012-03-11 21:37:18 +01:00
README.md update examples 2020-05-20 23:26:51 +02:00
build.js better free var filling, node.js test 2012-04-05 14:59:01 +02:00
example.js added contexts, docs and examples 2012-03-11 21:37:18 +01:00
template.md chore(examples): fix exmaples template syntax 2019-04-08 19:26:44 +01:00

README.md

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 ^\.\/.*$ ***!
  \*********************************/
/*! unknown exports (runtime-defined) */
/*! exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module, __webpack_require__.o, __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(!__webpack_require__.o(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 ***!
  \************************/
/*! unknown exports (runtime-defined) */
/*! exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! runtime requirements: module */
/***/ ((module) => {

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

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

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

/***/ }),
/* 4 */
/*!************************!*\
  !*** ./templates/c.js ***!
  \************************/
/*! unknown exports (runtime-defined) */
/*! exports [maybe provided (runtime-defined)] [maybe used (runtime-defined)] */
/*! 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] = {
/******/ 			// no module.id needed
/******/ 			// no module.loaded needed
/******/ 			exports: {}
/******/ 		};
/******/ 	
/******/ 		// Execute the module function
/******/ 		__webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/ 	
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/************************************************************************/
/******/ 	/* webpack/runtime/hasOwnProperty shorthand */
/******/ 	(() => {
/******/ 		__webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
/******/ 	})();
/******/ 	
/************************************************************************/
(() => {
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/*! unknown exports (runtime-defined) */
/*! exports [maybe provided (runtime-defined)] [unused] */
/*! 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.16
    Asset      Size
output.js  3.77 KiB  [emitted]  [name: main]
Entrypoint main = output.js
chunk output.js (main) 603 bytes (javascript) 86 bytes (runtime) [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 [built]
     cjs self exports reference ./templates/a.js 1:0-14
     context element ./a ./templates sync ^\.\/.*$ ./a
     context element ./a.js ./templates sync ^\.\/.*$ ./a.js
 ./templates/b.js 80 bytes [built]
     cjs self exports reference ./templates/b.js 1:0-14
     context element ./b ./templates sync ^\.\/.*$ ./b
     context element ./b.js ./templates sync ^\.\/.*$ ./b.js
 ./templates/c.js 80 bytes [built]
     cjs self exports reference ./templates/c.js 1:0-14
     context element ./c ./templates sync ^\.\/.*$ ./c
     context element ./c.js ./templates sync ^\.\/.*$ ./c.js
     + 1 hidden chunk module

Production mode

Hash: 0a1b2c3d4e5f6a7b8c9d
Version: webpack 5.0.0-beta.16
    Asset       Size
output.js  819 bytes  [emitted]  [name: main]
Entrypoint main = output.js
chunk output.js (main) 603 bytes (javascript) 86 bytes (runtime) [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 [built]
     cjs self exports reference ./templates/a.js 1:0-14
     context element ./a ./templates sync ^\.\/.*$ ./a
     context element ./a.js ./templates sync ^\.\/.*$ ./a.js
 ./templates/b.js 80 bytes [built]
     cjs self exports reference ./templates/b.js 1:0-14
     context element ./b ./templates sync ^\.\/.*$ ./b
     context element ./b.js ./templates sync ^\.\/.*$ ./b.js
 ./templates/c.js 80 bytes [built]
     cjs self exports reference ./templates/c.js 1:0-14
     context element ./c ./templates sync ^\.\/.*$ ./c
     context element ./c.js ./templates sync ^\.\/.*$ ./c.js
     + 1 hidden chunk module

Code Splitting

See this example combined with code splitting