added require.resolve example
This commit is contained in:
parent
dc65a165ac
commit
6d424fcab1
|
@ -8,6 +8,10 @@ example demonstrating a very simple programm
|
|||
|
||||
example demonstrating a very simple case of Code Splitting.
|
||||
|
||||
## require.resolve
|
||||
|
||||
example demonstrating to cache clearing of modules with `require.resolve` and `require.cache`.
|
||||
|
||||
## require.context
|
||||
|
||||
example demonstrating to automatic creation of contexts when using variables in `require`.
|
||||
|
|
|
@ -0,0 +1,124 @@
|
|||
# example.js
|
||||
|
||||
``` javascript
|
||||
var a = require("./a");
|
||||
|
||||
// get module id
|
||||
var aId = require.resolve("./a.js");
|
||||
|
||||
// clear module in require.cache
|
||||
delete require.cache[aId];
|
||||
|
||||
// require module again, it should be reexecuted
|
||||
var a2 = require("./a");
|
||||
|
||||
// vertify it
|
||||
if(a == a2) throw new Error("Cache clear failed :(");
|
||||
```
|
||||
|
||||
# a.js
|
||||
|
||||
|
||||
``` javascript
|
||||
module.exports = Math.random();
|
||||
```
|
||||
|
||||
# js/output.js
|
||||
|
||||
``` javascript
|
||||
/******/(function(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: {}
|
||||
/******/ };
|
||||
/******/ modules[moduleId](module, module.exports, require);
|
||||
/******/ return module.exports;
|
||||
/******/ }
|
||||
/******/ require.e = function(chunkId, callback) {
|
||||
/******/ callback(require);
|
||||
/******/ };
|
||||
/******/ require.modules = modules;
|
||||
/******/ require.cache = installedModules;
|
||||
/******/ return require(0);
|
||||
/******/})
|
||||
/******/({
|
||||
/******/0: function(module, exports, require) {
|
||||
|
||||
/*** .\example.js ***/
|
||||
|
||||
var a = require(/* ./a */1);
|
||||
|
||||
// get module id
|
||||
var aId = (/* ./a.js */1);
|
||||
|
||||
// clear module in require.cache
|
||||
delete require.cache[aId];
|
||||
|
||||
// require module again, it should be reexecuted
|
||||
var a2 = require(/* ./a */1);
|
||||
|
||||
// vertify it
|
||||
if(a == a2) throw new Error("Cache clear failed :(");
|
||||
|
||||
/******/},
|
||||
/******/
|
||||
/******/1: function(module, exports, require) {
|
||||
|
||||
/*** .\a.js ***/
|
||||
|
||||
module.exports = Math.random();
|
||||
|
||||
/******/},
|
||||
/******/
|
||||
/******/})
|
||||
```
|
||||
|
||||
# Info
|
||||
|
||||
## Uncompressed
|
||||
|
||||
```
|
||||
Hash: 8c012745533a08a15f1e6f422f4b96f8
|
||||
Compile Time: 20ms
|
||||
Chunks: 1
|
||||
Modules: 2
|
||||
Modules including duplicates: 2
|
||||
Modules per chunk: 2
|
||||
Modules first chunk: 2
|
||||
output.js: 1220 characters
|
||||
|
||||
<id> <size> <filename>
|
||||
<reason> from <filename>
|
||||
output.js
|
||||
0 283 .\example.js
|
||||
main
|
||||
1 31 .\a.js
|
||||
require (2x) from .\example.js
|
||||
require (1x) from .\example.js
|
||||
```
|
||||
|
||||
## Minimized (uglify-js, no zip)
|
||||
|
||||
```
|
||||
Hash: bb8c15c0f6816c382b20abadbda51c0d
|
||||
Compile Time: 67ms
|
||||
Chunks: 1
|
||||
Modules: 2
|
||||
Modules including duplicates: 2
|
||||
Modules per chunk: 2
|
||||
Modules first chunk: 2
|
||||
output.js: 410 characters
|
||||
|
||||
<id> <size> <filename>
|
||||
<reason> from <filename>
|
||||
output.js
|
||||
0 116 .\example.js
|
||||
main
|
||||
1 28 .\a.js
|
||||
require (2x) from .\example.js
|
||||
require (1x) from .\example.js
|
||||
```
|
|
@ -0,0 +1 @@
|
|||
module.exports = Math.random();
|
|
@ -0,0 +1 @@
|
|||
require("../build-common");
|
|
@ -0,0 +1,13 @@
|
|||
var a = require("./a");
|
||||
|
||||
// get module id
|
||||
var aId = require.resolve("./a.js");
|
||||
|
||||
// clear module in require.cache
|
||||
delete require.cache[aId];
|
||||
|
||||
// require module again, it should be reexecuted
|
||||
var a2 = require("./a");
|
||||
|
||||
// vertify it
|
||||
if(a == a2) throw new Error("Cache clear failed :(");
|
|
@ -0,0 +1,32 @@
|
|||
# example.js
|
||||
|
||||
``` javascript
|
||||
{{example.js}}
|
||||
```
|
||||
|
||||
# a.js
|
||||
|
||||
|
||||
``` javascript
|
||||
{{a.js}}
|
||||
```
|
||||
|
||||
# js/output.js
|
||||
|
||||
``` javascript
|
||||
{{js/output.js}}
|
||||
```
|
||||
|
||||
# Info
|
||||
|
||||
## Uncompressed
|
||||
|
||||
```
|
||||
{{stdout}}
|
||||
```
|
||||
|
||||
## Minimized (uglify-js, no zip)
|
||||
|
||||
```
|
||||
{{min:stdout}}
|
||||
```
|
Loading…
Reference in New Issue