webpack/examples/require.context
Tobias Koppers a79fecfb6a Updated examples for 1.1.0 2014-03-25 10:44:10 +01:00
..
templates added contexts, docs and examples 2012-03-11 21:37:18 +01:00
README.md Updated examples for 1.1.0 2014-03-25 10:44:10 +01: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
/******/ 	
/******/ 	// 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] = {
/******/ 			exports: {},
/******/ 			id: moduleId,
/******/ 			loaded: false
/******/ 		};
/******/ 		
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ 		
/******/ 		// Flag the module as loaded
/******/ 		module.loaded = 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;
/******/ 	
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "js/";
/******/ 	
/******/ 	
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/***/ function(module, exports, __webpack_require__) {

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

/***/ },
/* 1 */
/*!************************!*\
  !*** ./templates/a.js ***!
  \************************/
/***/ function(module, exports, __webpack_require__) {

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

/***/ },
/* 2 */
/*!************************!*\
  !*** ./templates/b.js ***!
  \************************/
/***/ function(module, exports, __webpack_require__) {

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

/***/ },
/* 3 */
/*!************************!*\
  !*** ./templates/c.js ***!
  \************************/
/***/ function(module, exports, __webpack_require__) {

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

/***/ },
/* 4 */
/*!****************************!*\
  !*** ./templates ^\.\/.*$ ***!
  \****************************/
/***/ function(module, exports, __webpack_require__) {

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


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

Info

Uncompressed

Hash: 1853a9528b05335d6ca9
Version: webpack 1.1.0
Time: 73ms
    Asset  Size  Chunks             Chunk Names
output.js  3211       0  [emitted]  main       
chunk    {0} output.js (main) 613 [rendered]
    > main [0] ./example.js 
    [0] ./example.js 150 {0} [built]
    [1] ./templates/a.js 82 {0} [built]
        context element ./a.js [4] ./templates ^\.\/.*$
        context element ./a [4] ./templates ^\.\/.*$
    [2] ./templates/b.js 82 {0} [built]
        context element ./b.js [4] ./templates ^\.\/.*$
        context element ./b [4] ./templates ^\.\/.*$
    [3] ./templates/c.js 82 {0} [built]
        context element ./c.js [4] ./templates ^\.\/.*$
        context element ./c [4] ./templates ^\.\/.*$
    [4] ./templates ^\.\/.*$ 217 {0} [built]
        cjs require context ./templates [0] ./example.js 2:8-44

Minimized (uglify-js, no zip)

Hash: 368d2da3d6d88e7d3822
Version: webpack 1.1.0
Time: 159ms
    Asset  Size  Chunks             Chunk Names
output.js   803       0  [emitted]  main       
chunk    {0} output.js (main) 613 [rendered]
    > main [0] ./example.js 
    [0] ./example.js 150 {0} [built]
    [1] ./templates/a.js 82 {0} [built]
        context element ./a.js [4] ./templates ^\.\/.*$
        context element ./a [4] ./templates ^\.\/.*$
    [2] ./templates/b.js 82 {0} [built]
        context element ./b.js [4] ./templates ^\.\/.*$
        context element ./b [4] ./templates ^\.\/.*$
    [3] ./templates/c.js 82 {0} [built]
        context element ./c.js [4] ./templates ^\.\/.*$
        context element ./c [4] ./templates ^\.\/.*$
    [4] ./templates ^\.\/.*$ 217 {0} [built]
        cjs require context ./templates [0] ./example.js 2:8-44

Code Splitting

See this example combined with code splitting