webpack/examples/require.context
Tobias Koppers 484dfde58f regenerated examples 2016-12-23 09:24:32 +01:00
..
templates added contexts, docs and examples 2012-03-11 21:37:18 +01:00
README.md regenerated examples 2016-12-23 09:24:32 +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 */ })` ``` javascript /******/ (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); // }) /******************************************************************/

</details>
``` javascript
/******/ ([
/* 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
		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.2.0-rc.2
    Asset     Size  Chunks             Chunk Names
output.js  4.43 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 ^\.\/.*$
        context element ./a.js [3] ./templates ^\.\/.*$
    [1] ./templates/b.js 82 bytes {0} [optional] [built]
        context element ./b [3] ./templates ^\.\/.*$
        context element ./b.js [3] ./templates ^\.\/.*$
    [2] ./templates/c.js 82 bytes {0} [optional] [built]
        context element ./c [3] ./templates ^\.\/.*$
        context element ./c.js [3] ./templates ^\.\/.*$
    [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.2.0-rc.2
    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 ^\.\/.*$
        context element ./a.js [3] ./templates ^\.\/.*$
    [1] ./templates/b.js 82 bytes {0} [optional] [built]
        context element ./b [3] ./templates ^\.\/.*$
        context element ./b.js [3] ./templates ^\.\/.*$
    [2] ./templates/c.js 82 bytes {0} [optional] [built]
        context element ./c [3] ./templates ^\.\/.*$
        context element ./c.js [3] ./templates ^\.\/.*$
    [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