webpack/examples/require.context
Tobias Koppers 92d2a7ffbf updated examples 2013-02-13 11:58:13 +01:00
..
templates added contexts, docs and examples 2012-03-11 21:37:18 +01:00
README.md updated examples 2013-02-13 11:58:13 +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
graph.svg updated examples 2012-11-07 12:49:01 +01:00
template.md github do not server static files -> gh-pages 2012-10-09 17:24:06 +02: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 webpackBootstrap(modules) {
/******/ 	var installedModules = {};
/******/ 	function require(moduleId) {
/******/ 		if(typeof moduleId !== "number") throw new Error("Cannot find module '"+moduleId+"'");
/******/ 		if(installedModules[moduleId])
/******/ 			return installedModules[moduleId].exports;
/******/ 		var module = installedModules[moduleId] = {
/******/ 			exports: {},
/******/ 			id: moduleId,
/******/ 			loaded: false
/******/ 		};
/******/ 		modules[moduleId].call(null, module, module.exports, require);
/******/ 		module.loaded = true;
/******/ 		return module.exports;
/******/ 	}
/******/ 	require.e = function requireEnsure(chunkId, callback) {
/******/ 		callback.call(null, require);
/******/ 	};
/******/ 	require.modules = modules;
/******/ 	require.cache = installedModules;
/******/ 	return require(0);
/******/ })({
/******/ c: "",

/***/ 0:
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/***/ function(module, exports, require) {

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

/***/ },

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

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

/***/ },

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

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

/***/ },

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

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

/***/ },

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

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

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

Info

Uncompressed

Hash: 1fe0d041cafa3e29d3084946180e73a8
Time: 33ms
    Asset  Size  Chunks  Chunk Names
output.js  2427       0  main       
chunk    {0} output.js (main) 603
    [0] ./example.js 146 [built] {0}
    [1] ./templates/a.js 80 [built] {0}
        context element ./a [4] ./templates ^\.\/.*$
        context element ./a.js [4] ./templates ^\.\/.*$
    [2] ./templates/b.js 80 [built] {0}
        context element ./b [4] ./templates ^\.\/.*$
        context element ./b.js [4] ./templates ^\.\/.*$
    [3] ./templates/c.js 80 [built] {0}
        context element ./c [4] ./templates ^\.\/.*$
        context element ./c.js [4] ./templates ^\.\/.*$
    [4] ./templates ^\.\/.*$ 217 [built] {0}
        cjs require context ./templates [0] ./example.js 2:8-44

Minimized (uglify-js, no zip)

Hash: 1fe0d041cafa3e29d3084946180e73a8
Time: 84ms
    Asset  Size  Chunks  Chunk Names
output.js   870       0  main       
chunk    {0} output.js (main) 603
    [0] ./example.js 146 [built] {0}
    [1] ./templates/a.js 80 [built] {0}
        context element ./a [4] ./templates ^\.\/.*$
        context element ./a.js [4] ./templates ^\.\/.*$
    [2] ./templates/b.js 80 [built] {0}
        context element ./b [4] ./templates ^\.\/.*$
        context element ./b.js [4] ./templates ^\.\/.*$
    [3] ./templates/c.js 80 [built] {0}
        context element ./c [4] ./templates ^\.\/.*$
        context element ./c.js [4] ./templates ^\.\/.*$
    [4] ./templates ^\.\/.*$ 217 [built] {0}
        cjs require context ./templates [0] ./example.js 2:8-44

Graph

webpack-graph

Code Splitting

See this example combined with code splitting