forked from kaktus40/cesium-sensors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
executable file
·150 lines (124 loc) · 5.05 KB
/
build.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
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
(function() {
"use strict";
/*jshint node:true*/
var requirejs = require('requirejs');
var fs = require('fs');
if (!fs.existsSync('Build')) {
fs.mkdirSync('Build');
}
if (!fs.existsSync('Build/MinifiedShaders')) {
fs.mkdirSync('Build/MinifiedShaders');
}
process.chdir('Source');
var files = fs.readdirSync('.');
var shims = {};
var minifiedGlslPaths = {};
var shaderLicenseComments = [];
files.forEach(function(path) {
if (/main\.js$/.test(path)) {
return;
}
var contents = fs.readFileSync(path).toString();
if (/\.js$/.test(path)) {
// Search for Cesium modules and add shim
// modules that pull from the Cesium global
var cesiumRequireRegex = /'Cesium\/\w*\/(\w*)'/g;
var match;
while ((match = cesiumRequireRegex.exec(contents)) !== null) {
if (match[0] in shims) {
continue;
}
shims[match[0]] = 'define(' + match[0] + ', function() { return Cesium["' + match[1] + '"]; });';
}
} else if (/\.glsl$/.test(path)) {
var newContents = [];
contents = contents.replace(/\r\n/gm, '\n');
var licenseComments = contents.match(/\/\*\*(?:[^*\/]|\*(?!\/)|\n)*?@license(?:.|\n)*?\*\//gm);
if (licenseComments !== null) {
shaderLicenseComments = shaderLicenseComments.concat(licenseComments);
}
// Remove comments. Code ported from
// https://github.com/apache/ant/blob/master/src/main/org/apache/tools/ant/filters/StripJavaComments.java
for (var i = 0; i < contents.length; ++i) {
var c = contents.charAt(i);
if (c === '/') {
c = contents.charAt(++i);
if (c === '/') {
while (c !== '\r' && c !== '\n' && i < contents.length) {
c = contents.charAt(++i);
}
} else if (c === '*') {
while (i < contents.length) {
c = contents.charAt(++i);
if (c === '*') {
c = contents.charAt(++i);
while (c === '*') {
c = contents.charAt(++i);
}
if (c === '/') {
c = contents.charAt(++i);
break;
}
}
}
} else {
--i;
c = '/';
}
}
newContents.push(c);
}
newContents = newContents.join('');
newContents = newContents.replace(/\s+$/gm, '').replace(/^\s+/gm, '').replace(/\n+/gm, '\n');
var minifiedFile = '../Build/MinifiedShaders/' + path;
fs.writeFileSync(minifiedFile, newContents);
minifiedGlslPaths[path.replace(/\.glsl$/, '')] = minifiedFile.replace(/\.glsl$/, '');
}
});
shims = Object.keys(shims).map(function(key) {
return shims[key];
}).join('\n');
var mainJs = '\
(function() {\n\
"use strict";\n\
/*jshint sub:true*/\n\
/*global define,require,self,Cesium*/\n' + shaderLicenseComments.join('\n') + '\n' + shims + '\n\
require(["CesiumSensors"], function(CesiumSensors) {\n\
var scope = typeof window !== "undefined" ? window : typeof self !== "undefined" ? self : {};\n\
scope.CesiumSensors = CesiumSensors;\n\
}, undefined, true);\n\
})();';
fs.writeFileSync('main.js', mainJs);
var copyrightHeader = fs.readFileSync('copyrightHeader.js').toString();
var rjsConfig = {
useStrict : true,
inlineText : true,
stubModules : ['text'],
baseUrl : '.',
skipModuleInsertion : true,
wrap : {
start : copyrightHeader + '(function() {',
end : '})();'
},
name : '../ThirdParty/almond-0.2.9/almond.js',
include : 'main',
paths : {
'text' : '../ThirdParty/requirejs-2.1.14/text'
}
};
var unminifiedRjsConfig = JSON.parse(JSON.stringify(rjsConfig));
unminifiedRjsConfig.optimize = 'none';
unminifiedRjsConfig.out = '../Build/Unminified/CesiumSensors.js';
requirejs.optimize(unminifiedRjsConfig, function(buildResponse) {
console.log('Built unminified CesiumSensors.js successfully.');
});
var minifiedRjsConfig = JSON.parse(JSON.stringify(rjsConfig));
minifiedRjsConfig.optimize = 'uglify2';
minifiedRjsConfig.out = '../Build/CesiumSensors.js';
Object.keys(minifiedGlslPaths).forEach(function(key) {
minifiedRjsConfig.paths[key] = minifiedGlslPaths[key];
});
requirejs.optimize(minifiedRjsConfig, function(buildResponse) {
console.log('Built minified CesiumSensors.js successfully.');
});
})();