-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (52 loc) · 1.61 KB
/
index.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
'use strict';
var tmpl = require('estemplate').compile(require('fs').readFileSync(__dirname + '/template.jst', 'utf-8'));
var parse = require('esprima').parse;
function normalizeDeps(deps) {
return (deps || []).map(function (dep) {
if (typeof dep === 'string') {
dep = {name: dep};
}
return {
name: dep.name,
globalName: dep.globalName || dep.name,
paramName: dep.paramName || dep.globalName || dep.name,
amdName: dep.amdName || dep.name,
cjsName: dep.cjsName || dep.name
};
});
}
function literal(value) {
return {type: 'Literal', value: value};
}
function id(name) {
return {type: 'Identifier', name: name};
}
module.exports = function (options) {
options = options || {};
var deps = normalizeDeps(options.deps);
var amdDeps = deps.map(function (dep) { return literal(dep.amdName) });
var globalDeps = deps.map(function (dep) { return id(dep.globalName); });
var cjsDeps = deps.map(function (dep) {
return {type: 'CallExpression', callee: id('require'), arguments: [literal(dep.cjsName)]};
});
var depNames = deps.map(function (dep) { return id(dep.paramName) });
var exports = options.exports && parse(options.exports).body[0].expression;
return function (files) {
return files.do(function (file) {
if (options.fromGlobal) {
file.program.body.push({
type: 'ReturnStatement',
argument: exports
});
}
file.program = tmpl({
amdDeps: amdDeps,
cjsDeps: cjsDeps,
globalDeps: globalDeps,
depNames: depNames,
exports: exports || parse(file.loc.source.replace(/\//g, '.').replace(/\.js$/, '')).body[0].expression,
file: file
});
});
}
};