/
requirejs.js
111 lines (96 loc) · 2.19 KB
/
requirejs.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
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
module.exports = {
full: {
options: {
out: 'tmp/ractive.js',
paths: {
'legacy': 'empty/legacy'
},
onBuildWrite: getOnBuildWrite()
}
},
legacy: {
options: {
out: 'tmp/ractive-legacy.js',
onBuildWrite: getOnBuildWrite()
}
},
runtime: {
options: {
out: 'tmp/ractive.runtime.js',
paths: {
'parse/_parse': 'empty/parse',
'legacy': 'empty/legacy'
},
onBuildWrite: getOnBuildWrite()
}
},
runtime_legacy: {
options: {
out: 'tmp/ractive-legacy.runtime.js',
paths: {
'parse/_parse': 'empty/parse'
},
onBuildWrite: getOnBuildWrite()
}
},
options: {
baseUrl: 'tmp/amd/',
name: 'Ractive',
optimize: 'none',
logLevel: 2
}
};
function getOnBuildWrite () {
var prefixes, used, banned;
console.log( 'creating closure' );
prefixes = {
'typeof': '_typeof'
}; // this is why we need a closure for each build target
used = {};
banned = /^(typeof)$/; // some module names cannot be function names
return function ( name, path, contents ) {
var relativePath, prefix, moduleNames = {};
relativePath = path.substring( path.indexOf( 'tmp/amd/' ) + 8 );
prefix = '/* ' + relativePath + ' */\n';
return prefix + require( 'amdclean' ).clean({
prefixTransform: function ( prefix ) {
var match, result, lastPart;
// special case
if ( prefix === 'utils_hasOwnProperty' ) {
return 'hasOwn';
}
prefix = prefix.replace( /(\w+)__(\w+)/, function ( match, $1, $2 ) {
if ( $1 === $2 ) {
return $1;
}
return match;
});
if ( match = /(\w+)_prototype_(\w+)$/.exec( prefix ) ) {
result = match[1] + '$' + match[2];
} else {
if ( prefixes[ prefix ] ) {
result = prefixes[ prefix ];
}
else {
lastPart = prefix.substring( prefix.lastIndexOf( '_' ) + 1 );
if ( banned.test( lastPart ) ) {
lastPart += '_';
}
if ( !used[ lastPart ] ) {
process.stdout.write('-');
result = lastPart;
prefixes[ prefix ] = lastPart;
used[ lastPart ] = true;
}
else {
console.error( 'Existing prefix', prefix );
result = prefix;
}
}
}
return result;
},
code: contents
}) + '\n';
};
};