-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (144 loc) · 4.7 KB
/
index.js
File metadata and controls
152 lines (144 loc) · 4.7 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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env node
'use strict';
var path = require('path');
var mdeps = require('module-deps');
var concat = require('concat-stream');
var esprima = require('esprima');
var estraverse = require('estraverse');
var estemplate = require('estemplate');
var escodegen = require('escodegen');
var xtend = require('xtend');
var dynamicRequires;
// core modules for node.js v0.12.7
// https://github.com/nodejs/node/blob/db1087c9757c31a82c50a1eba368d8cba95b57d0/lib/internal/module.js
var builtins = ['assert', 'buffer', 'child_process', 'cluster',
'crypto', 'dgram', 'dns', 'domain', 'events', 'fs', 'http', 'https', 'net',
'os', 'path', 'punycode', 'querystring', 'readline', 'repl', 'stream',
'string_decoder', 'tls', 'tty', 'url', 'util', 'v8', 'vm', 'zlib'];
function initMap(moduleDeps) {
var modIDtoName = Object.create(null);
moduleDeps.forEach(function (dep) {
var name;
var ext = path.extname(dep.id);
if (ext === '.js') {
name = path.basename(dep.id, '.js').replace(/-/g, '_');
name = '_mod_' + name;
}
else if (ext === '.json') {
name = path.basename(dep.id, '.json').replace(/-/g, '_');
name = '_mod_' + name;
}
else {
throw ext + ' filename extension not supported';
}
var next = -1;
var re = new RegExp(name + '(\\d*)$');
for (var id in modIDtoName) {
var result = re.exec(modIDtoName[id]);
if (result) {
if (result[1] === '') {
next = 0;
continue;
}
var count = parseInt(result[1], 10);
if (next <= count) next = count + 1;
}
}
if (next >= 0) name = name + next;
modIDtoName[dep.id] = name;
});
return modIDtoName;
}
function wrapModule(module, modIDtoName) {
var modAST = esprima.parse(module.source, { loc: true });
// wrap the module code in a function declaration, set the exports alias (node.js) and return exported API
var ast = estemplate('function <%= modName %>(module) {var exports = module.exports; %= body %; return module.exports}', {
modName: {type: 'Identifier', name: modIDtoName[module.id]},
body: modAST.body
});
// replace the require calls in the module code by function calls
estraverse.replace(ast, {
enter: function (node) {
if (node.type === 'CallExpression' && node.callee.name === 'require') {
if (node.arguments[0].type !== 'Literal') {
var start = node.loc.start, end = node.loc.end;
console.log(module.id + ':' + start.line + ':' + start.column + ':' + end.line + ':' + end.column + ': dynamic require call');
dynamicRequires = true;
this.skip();
}
// get the module name in the string argument of the require call
var reqModID = module.deps[node.arguments[0].value];
// if required module is a node.js module, skip
if (!reqModID) return this.skip();
// and replace it by a function call
var replaced;
if (path.extname(reqModID) === '.json') {
replaced = estemplate('<%= modName %>();', {
modName: {type: 'Identifier', name: modIDtoName[reqModID]}
});
}
else {
replaced = estemplate('<%= modName %>({exports: {}});', {
modName: {type: 'Identifier', name: modIDtoName[reqModID]}
});
}
return replaced.body[0].expression;
}
}
});
// return the AST of the module function declaration
return ast.body[0];
}
function wrapJSONModule(module, modIDtoName) {
// a hack to parse json in esprima
var expr = esprima.parse('(' + module.source + ');');
var jsonAST = expr.body[0].expression;
var ast = estemplate('function <%= modName %>() { return <%= object %>; }', {
modName: {type: 'Identifier', name: modIDtoName[module.id]},
object: jsonAST
});
return ast.body[0];
}
module.exports = Spack;
function Spack (file, opts) {
if (!opts) opts = {};
var mopts = xtend(opts);
if (opts.node) {
mopts.filter = function (id) {
if (builtins.indexOf(id) < 0) return true;
return false;
};
}
var md = mdeps(opts);
md.end(file);
return new Promise (function (fulfill, reject) {
md.pipe(concat(function (moduleDeps) {
dynamicRequires = false;
var entry;
var program = { "type": "Program", "body": [], "sourceType": "script" };
var modIDtoName = initMap(moduleDeps);
moduleDeps.forEach(function (dep) {
var ast;
if (path.extname(dep.id) === '.json') {
ast = wrapJSONModule(dep, modIDtoName);
}
else {
ast = wrapModule(dep, modIDtoName);
}
if (dep.entry) {
entry = dep.id;
}
program.body.push(ast);
});
var entryPoint = estemplate('<%= modName %>({exports: {}});', {
modName: {type: 'Identifier', name: modIDtoName[entry]}
});
program.body.push(entryPoint.body[0]);
if (dynamicRequires) {
reject(new Error("Can't handle dynamic arguments to require calls. Consider refactoring the code."));
} else {
fulfill(escodegen.generate(program));
}
}));
});
}