1 | var parent = module.parent;
|
2 |
|
3 |
|
4 |
|
5 |
|
6 | function reloadCtx(require, name) {
|
7 | var id = require.resolve(name),
|
8 | oldCache = require.cache[id];
|
9 | delete require.cache[id];
|
10 | try {
|
11 | return require(id);
|
12 | } catch (e) {
|
13 | if (oldCache !== undefined) {
|
14 | require.cache[id] = oldCache;
|
15 | }
|
16 | throw e;
|
17 | }
|
18 | return null;
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 | function reloadParent(name) {
|
24 | var ctx = parent.constructor,
|
25 | id = ctx._resolveFilename(name, parent),
|
26 | oldCache = ctx._cache[id];
|
27 | delete ctx._cache[id];
|
28 | try {
|
29 | return parent.require(id);
|
30 | } catch (e) {
|
31 | if (oldCache !== undefined) {
|
32 | ctx._cache[id] = oldCache;
|
33 | }
|
34 | throw e;
|
35 | }
|
36 | return null;
|
37 | }
|
38 |
|
39 | function reload(name, ctx) {
|
40 |
|
41 | if (typeof name === 'function' && typeof name.cache === 'object') {
|
42 | var func = reloadCtx.bind(null, name);
|
43 | func.resolve = name.resolve;
|
44 | func.emptyCache = function() {
|
45 | for (var id in name.cache) {
|
46 | delete name.cache[id];
|
47 | }
|
48 | };
|
49 | return func;
|
50 | }
|
51 | if (ctx !== undefined) {
|
52 | return reloadCtx(ctx, name);
|
53 | }
|
54 | if (module.filename === undefined) {
|
55 | throw new Error("Cannot override module.filename since it isn't used anymore. Please upgrade require-reload!");
|
56 | }
|
57 |
|
58 |
|
59 | return reloadParent(name);
|
60 | }
|
61 |
|
62 | reload.resolve = function(req, context) {
|
63 | if (context !== undefined) {
|
64 | return context.resolve(req);
|
65 | }
|
66 | return parent.constructor._resolveFilename(req, parent);
|
67 | };
|
68 | reload.emptyCache = function(context) {
|
69 | var cache = context ? context.cache : parent.constructor._cache;
|
70 | for (var id in cache) {
|
71 | delete cache[id];
|
72 | }
|
73 | };
|
74 | module.exports = reload;
|
75 |
|
76 |
|
77 | delete require.cache[module.id];
|