requirepaths.js
1.1 KB
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
/*
Copyright (c) 2014 Google Inc. All rights reserved.
Use of this source code is governed by the MIT License, available in this package's LICENSE file
or at http://opensource.org/licenses/MIT.
*/
'use strict';
var path = require('path');
function resolvePaths(parentModule, paths) {
if (!paths) {
return [];
}
return paths.slice(0).map(function(p) {
return path.resolve(parentModule.filepath, p);
});
}
function requirePaths(parentModule, opts) {
var result = {
before: [],
after: []
};
if (!parentModule) {
return result;
}
if (Array.isArray(opts)) {
result.before = resolvePaths(parentModule, opts);
} else {
result.before = resolvePaths(parentModule, opts.before);
result.after = resolvePaths(parentModule, opts.after);
}
return result;
}
exports.before = function before(targetPath, parentModule, opts) {
var resolvedPaths = requirePaths(parentModule, opts);
return 'module.paths = ' + JSON.stringify(resolvedPaths.before) + '.concat(module.paths)' +
'.concat(' + JSON.stringify(resolvedPaths.after) + '); ';
};
exports.after = function after(targetPath, parentModule, opts) {
return '';
};