-
Notifications
You must be signed in to change notification settings - Fork 27
/
shortcuts.js
60 lines (52 loc) · 1.76 KB
/
shortcuts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* This plugin enables shortcuts
*
* Flag "shortcuts"
*
* This plugin provides private "is_shortcut" function
*/
/**
* @name sandbox
*/
(function (sb) {
function is_shortcut(moduleName, moduleContent) {
return !sb.initialized[moduleName] &&
typeof moduleContent === "string" &&
moduleContent.charAt(0) == '@';
}
function rewrite_shortcut(moduleName, module) {
if (is_shortcut(moduleName, module)) {
sb.trigger('shortcuts:before-resolve', moduleName, module);
moduleName = module.replace('@', '');
// #66 Shortcut self reference should be resolved as undefined->global name
var newModule = sb.modules[moduleName];
module = newModule === module ? sb.undefined : newModule;
}
return [moduleName, module];
}
/**
* @event *:rewrite-shortcut request for shortcut rewrite
*
* @param {String} moduleName race for module name
* @param {String} module this callback will be called when module inited
*
* @retuns yes returns modified moduleName and module itself
*/
sb.on('*:rewrite-shortcut', rewrite_shortcut);
/**
* @event *:rewrite-shortcut fires before stats plugin counts require same as *:rewrite-shortcut
* but without triggering shortcuts:before-resolve event
*
* @param {String} moduleName race for module name
* @param {String} module this callback will be called when module inited
*
* @retuns yes returns modified moduleName and module itself
*/
sb.on('stats:before-require-count', function (moduleName, module) {
if (is_shortcut(moduleName, module)) {
moduleName = module.replace('@', '');
module = sb.modules[moduleName];
return [moduleName, module];
}
});
}(sandbox));