This repository has been archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
remapper.js
71 lines (52 loc) · 1.74 KB
/
remapper.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
61
62
63
64
65
66
67
68
69
70
71
//remapper
var modules = require('remap/modules')
, resolve = require('remap/resolve')
, log = require('logger') //console.log
, assert = require('assert')
module.exports = Remapper
function Remapper (_module,remaps){
var self = this
modules = modules.useCache({})
//to get NpmRemapper, probably best to just rewrite this.
function Maker (depends,loaded,remaps){
var self = this
var _depends = {}
loaded = loaded || {}
self.resolve = function (request,module){
if(remaps.hasOwnProperty(request) && remaps[request]){
request = remaps[request]
}
var resolved = resolve.resolveModuleFilename(request,module)
if(remaps.hasOwnProperty(resolved[0]) && remaps[resolved[0]])
return resolve.resolveModuleFilename(remaps[resolved[0]],module)
return resolved
}
self.load = function (id, filename, parent, makeR ){
var newModule = modules.defaultLoad(id, filename, parent, makeR)
if(!loaded[id])
loaded[id] = {} //should only happen when it's a native module
assert.notEqual(loaded[parent.id],null, "SHOULD have record for parent " + parent.id + "")
loaded[parent.id][id] = loaded[id]
return newModule
}
self.make = function (thisModule){
var id = thisModule.id
loaded[id] = {}
return modules.makeMake(new Maker(loaded[id],loaded,remaps))(thisModule)
}
}
/**
* the dependencies loaded through require.
*
* depends is easier to spell than dependencies
*/
self.depends = {}
self.depends[_module.id] = {}
self.loaded = {}
self.remaps = remaps || {}
self.loaded[_module.id] = self.depends[_module.id]
self.require = make()
function make(){
return modules.makeMake(new Maker(self.depends[_module.id],self.loaded,self.remaps))(_module)
}
}