-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodule.js
81 lines (74 loc) · 2.93 KB
/
module.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
72
73
74
75
76
77
78
79
80
81
'use strict'
var fs = require('promised-fs')
, when = require('q').when
, Trait = require('light-traits').Trait
, M = require('./common/utils/module'), wrapInTransport = M.wrapInTransport
, getPackageName = M.getPackageName
, getPackageRelativeId = M.getPackageRelativeId
, isMainModule = M.isMainModule
, isModuleIdRelative = M.isModuleIdRelative
, CONST = require('./strings')
, MODULE_NOT_FOUND_ERROR = CONST.MODULE_NOT_FOUND_ERROR
, PACKAGE_NOT_FOUND_ERROR = CONST.PACKAGE_NOT_FOUND_ERROR
exports.PackageModules = Trait(
{ path: Trait.required
, dependencies: Trait.required
, name: Trait.required
, descriptor: Trait.required
, isAMDFormat: Trait.required
, getModuleTransport: function getModuleTransport(id) {
var errorSource = MODULE_NOT_FOUND_ERROR
.replace('{{id}}', id)
.replace('{{path}}', fs.join(this.path, this.getModulePath(id)))
return when
( this.getModuleSource(id)
, this.isAMDFormat ? null : wrapInTransport.bind(null, id)
, wrapInTransport.bind(null, id, errorSource)
)
}
, getModulePath: function getModuleSource(id) {
var packageName = getPackageName(id)
, relativeId
, path
, descriptor
, modules
// If this module is not from this package we can't get path to it
// so setting it to `null`.
if (packageName !== this.name)
path = null
else {
// If module id is for a main module (does not contains separators)
// checking taking path to the main module from the package descriptor.
descriptor = this.descriptor.overlay.teleport
if (isMainModule(id)) path = descriptor.main
// If module is not a main then checking if path for this module
// is defined in package descriptor, if it's not there then we generate
// path by adding relative module ID to path of a package `lib`. In
// addition we add `.js` extension if id does not already has one.
else {
modules = descriptor.modules
relativeId = getPackageRelativeId(id)
if (modules && (path = modules[relativeId])) path
else path = fs.join(descriptor.directories.lib, relativeId)
}
if ('.js' !== path.substr(-3)) path += '.js'
}
return path
}
, getModuleSource: function getModuleSource(id) {
var packageName = getPackageName(id)
, source
if (isModuleIdRelative(id))
source = this.getContent(id)
else if (packageName === this.name)
source = this.getContent(this.getModulePath(id))
else
source = when(this.dependencies, function(dependencies) {
var dependency = dependencies[packageName]
if (dependency) source = dependency.invoke('getModuleSource', [id])
else source = PACKAGE_NOT_FOUND_ERROR.replace('{{name}}', packageName)
return source
})
return source
}
})