function getTemplate(templateName) {
return require("./templates/"+templateName);
}
console.log(getTemplate("a"));
console.log(getTemplate("b"));- a.js
- b.js
- c.js
All templates are of this pattern:
module.exports = function() {
return "This text was generated by template X";
}/******/ (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__(__webpack_require__.s = 4);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/*!************************!*\
!*** ./templates/a.js ***!
\************************/
/***/ function(module, exports) {
module.exports = function() {
return "This text was generated by template A";
}
/***/ },
/* 1 */
/*!************************!*\
!*** ./templates/b.js ***!
\************************/
/***/ function(module, exports) {
module.exports = function() {
return "This text was generated by template B";
}
/***/ },
/* 2 */
/*!************************!*\
!*** ./templates/c.js ***!
\************************/
/***/ function(module, exports) {
module.exports = function() {
return "This text was generated by template C";
}
/***/ },
/* 3 */
/*!****************************!*\
!*** ./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 */
/*!********************!*\
!*** ./example.js ***!
\********************/
/***/ function(module, exports, __webpack_require__) {
function getTemplate(templateName) {
return __webpack_require__(/*! ./templates */ 3)("./"+templateName);
}
console.log(getTemplate("a"));
console.log(getTemplate("b"));
/***/ }
/******/ ]);Hash: 450c039bc9254b2b669f
Version: webpack 2.0.6-beta
Time: 86ms
Asset Size Chunks Chunk Names
output.js 3.1 kB 0 [emitted] main
chunk {0} output.js (main) 613 bytes [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]
Hash: 450c039bc9254b2b669f
Version: webpack 2.0.6-beta
Time: 191ms
Asset Size Chunks Chunk Names
output.js 823 bytes 0 [emitted] main
chunk {0} output.js (main) 613 bytes [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]