-
Notifications
You must be signed in to change notification settings - Fork 2
/
parsers.js
45 lines (40 loc) · 1.23 KB
/
parsers.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
var path = require('path')
, Module = require('module')
;
// by default use just `js` and `json` parsers
module.exports = {
js : jsCompile,
json: JSON.parse
};
/**
* Compiles js content in the manner it's done
* in the node itself
*
* @param {string} content - file's content
* @param {string} file - full path of the file
* @returns {mixed} - result javascript object
*/
function jsCompile(content, file)
{
// make it as a child of this module
// Would be nice to actually make it transparent
// and pretend it to be child of the caller module
// but there is no obvious way, yet
var jsMod = new Module(file, module);
// override parents to exclude configly from the chain
// it's like this js file is included directly
// from the file that included `configly`
while (jsMod.parent
&& typeof jsMod.parent.id === 'string'
&& jsMod.parent.id.match(/\/node_modules\/configly\//)
&& jsMod.parent.parent) {
/* istanbul ignore next */
jsMod.parent = jsMod.parent.parent;
}
// generate node_modules paths for the file
jsMod.paths = Module._nodeModulePaths(path.dirname(file));
// execute the module
jsMod._compile(content, file);
// return just exported object
return jsMod.exports;
}