forked from ForbesLindesay/umd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (74 loc) · 2.43 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var through = require('through');
var rfile = require('rfile');
var templateSTR = rfile('./template.js');
var uglify = require('uglify-js');
function template(moduleName, cjs) {
var str = uglify.minify(
templateSTR.replace(/\{\{defineNamespace\}\}/g, compileNamespace(moduleName)),
{fromString: true}).code
.split('source()')
str[0] = str[0].trim();
//make sure these are undefined so as to not get confused if modules have inner UMD systems
str[0] += 'var define,module,exports;';
if (cjs) str[0] += 'module={exports:(exports={})};';
str[0] += '\n';
if (cjs) str[1] = 'return module.exports;' + str[1];
str[1] = '\n' + str[1];
return str;
}
exports = module.exports = function (name, cjs, src) {
if (typeof cjs === 'string') {
var tmp = cjs;
cjs = src;
src = tmp;
}
if (src) {
return exports.prelude(name, cjs) + src + exports.postlude(name, cjs);
} else {
var strm = through(write, end);
var first = true;
function write(chunk) {
if (first) strm.queue(exports.prelude(name, cjs));
first = false;
strm.queue(chunk);
}
function end() {
if (first) strm.queue(exports.prelude(name, cjs));
strm.queue(exports.postlude(name, cjs));
strm.queue(null);
}
return strm;
}
};
exports.prelude = function (moduleName, cjs) {
return template(moduleName, cjs)[0];
};
exports.postlude = function (moduleName, cjs) {
return template(moduleName, cjs)[1];
};
function camelCase(name) {
name = name.replace(/\-([a-z])/g, function (_, char) { return char.toUpperCase(); });
return name.replace(/[^a-zA-Z0-9]+/g, '')
}
function compileNamespace(name) {
var names = name.split('.')
// No namespaces, yield the best case 'global.NAME = VALUE'
if (names.length === 1) {
return 'g.' + camelCase(name) + ' = f()';
// Acceptable case, with reasonable compilation
} else if (names.length === 2) {
names = names.map(camelCase);
return '(g.' + names[0] + ' || (g.' + names[0] + ' = {})).' + names[1] + ' = f()';
// Worst case, too many namespaces to care about
} else {
var valueContainer = names.pop()
return names.reduce(compileNamespaceStep, ['var ref$ = g'])
.concat(['ref$.' + camelCase(valueContainer) + ' = f()'])
.join(';\n ');
}
}
function compileNamespaceStep(code, name, i, names) {
name = camelCase(name);
code.push('ref$ = (ref$.' + name + ' || (ref$.' + name + ' = {}))')
return code
}