forked from systemjs/builder
/
json.js
61 lines (50 loc) · 1.7 KB
/
json.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
function hasProperties(obj) {
for (var p in obj)
return true;
return false;
}
exports.compile = function(load, opts, loader) {
try {
var json = JSON.parse(load.source)
}
catch(e) {
throw new Error('Unable to parse JSON module ' + load.name + ' contents as JSON.');
}
if (load.isPackageConfig)
json = optimizePackageConfig(json);
return Promise.resolve({
source: opts.systemGlobal + '.registerDynamic(' + (opts.anonymous ? '' : '"' + load.name + '", ') + '[], false, function() {\n' +
' return ' + JSON.stringify(json, null, 2).replace(/\n/g, '\n ') + ';\n' +
'});\n'
});
};
// because bundles are for the browser only
// if this is a package config file json we are compiling
// then we can optimize out the node-only configurations to make it smaller
function optimizePackageConfig(json) {
if (json.systemjs)
json = json.systemjs;
// remove non SystemJS package config properties
var loaderConfigProperties = ['baseDir', 'defaultExtension', 'format', 'meta', 'map', 'main'];
for (var p in json)
if (loaderConfigProperties.indexOf(p) == -1)
delete json[p];
if (json.map && !json.map['@env']) {
Object.keys(json.map).forEach(function(target) {
var mapped = json.map[target];
if (typeof mapped == 'string' && mapped.substr(0, 6) == '@node/')
delete json.map[target];
if (typeof mapped == 'object') {
Object.keys(mapped).forEach(function(condition) {
if (condition == 'node')
delete mapped[condition];
});
if (!hasProperties(mapped))
delete json.map[target];
}
});
if (!hasProperties(json.map))
delete json.map;
}
return json;
}