webpack/examples/require.context
Tobias Koppers 859b8cde6b updated examples 2017-05-22 22:45:18 +02:00
..
templates added contexts, docs and examples 2012-03-11 21:37:18 +01:00
README.md updated examples 2017-05-22 22:45:18 +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 removed graphs 2013-02-25 12:30:25 +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";
}

js/output.js

/******/ (function(modules) { /* webpackBootstrap */ })
/******/ (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 harmony imports with the correct context
/******/ 	__webpack_require__.i = function(value) { return value; };
/******/
/******/ 	// define getter function for harmony exports
/******/ 	__webpack_require__.d = function(exports, name, getter) {
/******/ 		if(!__webpack_require__.o(exports, name)) {
/******/ 			Object.defineProperty(exports, name, {
/******/ 				configurable: false,
/******/ 				enumerable: true,
/******/ 				get: getter
/******/ 			});
/******/ 		}
/******/ 	};
/******/
/******/ 	// 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;
/******/ 	};
/******/
/******/ 	// Object.prototype.hasOwnProperty.call
/******/ 	__webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "js/";
/******/
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/* unknown exports provided */
/* all exports used */
/*!************************!*\
  !*** ./templates/a.js ***!
  \************************/
/***/ (function(module, exports) {

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

/***/ }),
/* 1 */
/* unknown exports provided */
/* all exports used */
/*!************************!*\
  !*** ./templates/b.js ***!
  \************************/
/***/ (function(module, exports) {

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

/***/ }),
/* 2 */
/* unknown exports provided */
/* all exports used */
/*!************************!*\
  !*** ./templates/c.js ***!
  \************************/
/***/ (function(module, exports) {

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

/***/ }),
/* 3 */
/* unknown exports provided */
/* all exports used */
/*!****************************!*\
  !*** ./templates ^\.\/.*$ ***!
  \****************************/
/***/ (function(module, exports, __webpack_require__) {

var map = {
	"./a": 0,
	"./a.js": 0,
	"./b": 1,
	"./b.js": 1,
	"./c": 2,
	"./c.js": 2
};
function webpackContext(req) {
	return __webpack_require__(webpackContextResolve(req));
};
function webpackContextResolve(req) {
	var id = map[req];
	if(!(id + 1)) // check for number or string
		throw new Error("Cannot find module '" + req + "'.");
	return id;
};
webpackContext.keys = function webpackContextKeys() {
	return Object.keys(map);
};
webpackContext.resolve = webpackContextResolve;
module.exports = webpackContext;
webpackContext.id = 3;

/***/ }),
/* 4 */
/* unknown exports provided */
/* all exports used */
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/***/ (function(module, exports, __webpack_require__) {

function getTemplate(templateName) {
	return __webpack_require__(/*! ./templates */ 3)("./"+templateName);
}
console.log(getTemplate("a"));
console.log(getTemplate("b"));

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

Info

Uncompressed

Hash: 219dcd379f9f54c00e1f
Version: webpack 2.6.0
    Asset     Size  Chunks             Chunk Names
output.js  4.58 kB       0  [emitted]  main
Entrypoint main = output.js
chunk    {0} output.js (main) 613 bytes [entry] [rendered]
    > main [4] ./example.js 
    [0] ./templates/a.js 82 bytes {0} [optional] [built]
        context element ./a [3] ./templates ^\.\/.*$ ./a
        context element ./a.js [3] ./templates ^\.\/.*$ ./a.js
    [1] ./templates/b.js 82 bytes {0} [optional] [built]
        context element ./b [3] ./templates ^\.\/.*$ ./b
        context element ./b.js [3] ./templates ^\.\/.*$ ./b.js
    [2] ./templates/c.js 82 bytes {0} [optional] [built]
        context element ./c [3] ./templates ^\.\/.*$ ./c
        context element ./c.js [3] ./templates ^\.\/.*$ ./c.js
    [3] ./templates ^\.\/.*$ 217 bytes {0} [built]
        cjs require context ./templates [4] ./example.js 2:8-44
    [4] ./example.js 150 bytes {0} [built]

Minimized (uglify-js, no zip)

Hash: 219dcd379f9f54c00e1f
Version: webpack 2.6.0
    Asset     Size  Chunks             Chunk Names
output.js  1.11 kB       0  [emitted]  main
Entrypoint main = output.js
chunk    {0} output.js (main) 613 bytes [entry] [rendered]
    > main [4] ./example.js 
    [0] ./templates/a.js 82 bytes {0} [optional] [built]
        context element ./a [3] ./templates ^\.\/.*$ ./a
        context element ./a.js [3] ./templates ^\.\/.*$ ./a.js
    [1] ./templates/b.js 82 bytes {0} [optional] [built]
        context element ./b [3] ./templates ^\.\/.*$ ./b
        context element ./b.js [3] ./templates ^\.\/.*$ ./b.js
    [2] ./templates/c.js 82 bytes {0} [optional] [built]
        context element ./c [3] ./templates ^\.\/.*$ ./c
        context element ./c.js [3] ./templates ^\.\/.*$ ./c.js
    [3] ./templates ^\.\/.*$ 217 bytes {0} [built]
        cjs require context ./templates [4] ./example.js 2:8-44
    [4] ./example.js 150 bytes {0} [built]

Code Splitting

See this example combined with code splitting