mirror of https://github.com/webpack/webpack.git
228 lines
6.0 KiB
Markdown
228 lines
6.0 KiB
Markdown
# example.js
|
|
|
|
``` javascript
|
|
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:
|
|
|
|
``` javascript
|
|
module.exports = function() {
|
|
return "This text was generated by template X";
|
|
}
|
|
```
|
|
|
|
# dist/output.js
|
|
|
|
<details><summary><code>/******/ (function(modules) { /* webpackBootstrap */ })</code></summary>
|
|
|
|
``` javascript
|
|
/******/ (function(modules, runtime) { // webpackBootstrap
|
|
/******/ "use strict";
|
|
/******/ // 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;
|
|
/******/ }
|
|
/******/
|
|
/******/
|
|
/******/
|
|
/******/
|
|
/******/ // Load entry module and return exports
|
|
/******/ return __webpack_require__(0);
|
|
/******/ })
|
|
/************************************************************************/
|
|
```
|
|
|
|
</details>
|
|
|
|
``` javascript
|
|
/******/ ([
|
|
/* 0 */
|
|
/*!********************!*\
|
|
!*** ./example.js ***!
|
|
\********************/
|
|
/*! no static exports found */
|
|
/*! runtime requirements: __webpack_require__ */
|
|
/***/ (function(__unusedmodule, __unusedexports, __webpack_require__) {
|
|
|
|
function getTemplate(templateName) {
|
|
return __webpack_require__(1)("./"+templateName);
|
|
}
|
|
console.log(getTemplate("a"));
|
|
console.log(getTemplate("b"));
|
|
|
|
/***/ }),
|
|
/* 1 */
|
|
/*!*********************************!*\
|
|
!*** ./templates sync ^\.\/.*$ ***!
|
|
\*********************************/
|
|
/*! no static exports found */
|
|
/*! runtime requirements: module, __webpack_require__ */
|
|
/***/ (function(module, __unusedexports, __webpack_require__) {
|
|
|
|
var map = {
|
|
"./a": 2,
|
|
"./a.js": 2,
|
|
"./b": 3,
|
|
"./b.js": 3,
|
|
"./c": 4,
|
|
"./c.js": 4
|
|
};
|
|
|
|
|
|
function webpackContext(req) {
|
|
var id = webpackContextResolve(req);
|
|
return __webpack_require__(id);
|
|
}
|
|
function webpackContextResolve(req) {
|
|
var id = map[req];
|
|
if(!(id + 1)) { // check for number or string
|
|
var e = new Error("Cannot find module '" + req + "'");
|
|
e.code = 'MODULE_NOT_FOUND';
|
|
throw e;
|
|
}
|
|
return id;
|
|
}
|
|
webpackContext.keys = function webpackContextKeys() {
|
|
return Object.keys(map);
|
|
};
|
|
webpackContext.resolve = webpackContextResolve;
|
|
module.exports = webpackContext;
|
|
webpackContext.id = 1;
|
|
|
|
/***/ }),
|
|
/* 2 */
|
|
/*!************************!*\
|
|
!*** ./templates/a.js ***!
|
|
\************************/
|
|
/*! no static exports found */
|
|
/*! runtime requirements: module */
|
|
/***/ (function(module) {
|
|
|
|
module.exports = function() {
|
|
return "This text was generated by template A";
|
|
}
|
|
|
|
/***/ }),
|
|
/* 3 */
|
|
/*!************************!*\
|
|
!*** ./templates/b.js ***!
|
|
\************************/
|
|
/*! no static exports found */
|
|
/*! runtime requirements: module */
|
|
/***/ (function(module) {
|
|
|
|
module.exports = function() {
|
|
return "This text was generated by template B";
|
|
}
|
|
|
|
/***/ }),
|
|
/* 4 */
|
|
/*!************************!*\
|
|
!*** ./templates/c.js ***!
|
|
\************************/
|
|
/*! no static exports found */
|
|
/*! runtime requirements: module */
|
|
/***/ (function(module) {
|
|
|
|
module.exports = function() {
|
|
return "This text was generated by template C";
|
|
}
|
|
|
|
/***/ })
|
|
/******/ ]);
|
|
```
|
|
|
|
# Info
|
|
|
|
## Unoptimized
|
|
|
|
```
|
|
Hash: 0a1b2c3d4e5f6a7b8c9d
|
|
Version: webpack 5.0.0-next
|
|
Asset Size Chunks Chunk Names
|
|
output.js 3.24 KiB {0} [emitted] main
|
|
Entrypoint main = output.js
|
|
chunk {0} output.js (main) 603 bytes [entry] [rendered]
|
|
> .\example.js main
|
|
[0] ./example.js 146 bytes {0} [built]
|
|
[used exports unknown]
|
|
entry .\example.js main
|
|
[1] ./templates sync ^\.\/.*$ 217 bytes {0} [built]
|
|
[used exports unknown]
|
|
cjs require context ./templates [0] ./example.js 2:8-44
|
|
[2] ./templates/a.js 80 bytes {0} [optional] [built]
|
|
[used exports unknown]
|
|
context element ./a [1] ./templates sync ^\.\/.*$ ./a
|
|
context element ./a.js [1] ./templates sync ^\.\/.*$ ./a.js
|
|
[3] ./templates/b.js 80 bytes {0} [optional] [built]
|
|
[used exports unknown]
|
|
context element ./b [1] ./templates sync ^\.\/.*$ ./b
|
|
context element ./b.js [1] ./templates sync ^\.\/.*$ ./b.js
|
|
[4] ./templates/c.js 80 bytes {0} [optional] [built]
|
|
[used exports unknown]
|
|
context element ./c [1] ./templates sync ^\.\/.*$ ./c
|
|
context element ./c.js [1] ./templates sync ^\.\/.*$ ./c.js
|
|
```
|
|
|
|
## Production mode
|
|
|
|
```
|
|
Hash: 0a1b2c3d4e5f6a7b8c9d
|
|
Version: webpack 5.0.0-next
|
|
Asset Size Chunks Chunk Names
|
|
output.js 869 bytes {404} [emitted] main
|
|
Entrypoint main = output.js
|
|
chunk {404} output.js (main) 603 bytes [entry] [rendered]
|
|
> .\example.js main
|
|
[117] ./templates/a.js 80 bytes {404} [optional] [built]
|
|
context element ./a [545] ./templates sync ^\.\/.*$ ./a
|
|
context element ./a.js [545] ./templates sync ^\.\/.*$ ./a.js
|
|
[132] ./templates/c.js 80 bytes {404} [optional] [built]
|
|
context element ./c [545] ./templates sync ^\.\/.*$ ./c
|
|
context element ./c.js [545] ./templates sync ^\.\/.*$ ./c.js
|
|
[139] ./templates/b.js 80 bytes {404} [optional] [built]
|
|
context element ./b [545] ./templates sync ^\.\/.*$ ./b
|
|
context element ./b.js [545] ./templates sync ^\.\/.*$ ./b.js
|
|
[275] ./example.js 146 bytes {404} [built]
|
|
entry .\example.js main
|
|
[545] ./templates sync ^\.\/.*$ 217 bytes {404} [built]
|
|
cjs require context ./templates [275] ./example.js 2:8-44
|
|
```
|
|
|
|
# Code Splitting
|
|
|
|
See [this example combined with code splitting](../code-splitted-require.context)
|