-
Notifications
You must be signed in to change notification settings - Fork 115
/
resolve-id.js
68 lines (62 loc) · 1.38 KB
/
resolve-id.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
var resolve = require("resolve")
var jspmResolve
// Works around https://github.com/jspm/jspm-cli/pull/1779 is released
try {
jspmResolve = require("pkg-resolve")
}
catch (ex) {
// pass
}
var moduleDirectories = [
"web_modules",
"node_modules",
]
function resolveModule(id, opts) {
return new Promise(function(res, rej) {
resolve(id, opts, function(err, path) {
if (err) {
return rej(err)
}
res(path)
})
})
}
module.exports = function(id, base, options) {
var paths = options.path
var resolveOpts = {
basedir: base,
moduleDirectory: moduleDirectories,
paths: paths,
extensions: [ ".css" ],
packageFilter: function processPackage(pkg) {
if (pkg.style) {
pkg.main = pkg.style
}
else if (!pkg.main || !/\.css$/.test(pkg.main)) {
pkg.main = "index.css"
}
return pkg
},
}
return resolveModule("./" + id, resolveOpts)
.catch(function() {
return resolveModule(id, resolveOpts)
})
.catch(function() {
return jspmResolve.default(id, {
basedir: resolveOpts.basedir,
extensions : resolveOpts.extensions,
})
})
.catch(function() {
if (paths.indexOf(base) === -1) {
paths.unshift(base)
}
throw new Error([
"Failed to find '" + id + "'",
"in [ ",
" " + paths.join(",\n "),
"]",
].join("\n "))
})
}