forked from alberto-acevedo/cesium-navigation
-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
217 lines (188 loc) · 7.98 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
(function () {
"use strict";
/*jshint node:true*/
var sourceDir = 'Source';
var buildDir = 'dist',
standaloneSubDir = 'standalone',
amdSubDir = 'amd',
buildName = 'viewerCesiumNavigationMixin';
var examplesDir = 'Examples';
var requirejs = require('requirejs');
var path = require('path');
var fs = require('fs-extra');
var nodeMinify = require('node-minify');
var minify = function (fileIn, callback) {
var fileOut = path.join(path.dirname(fileIn), path.basename(fileIn, path.extname(fileIn)) + '.min' + path.extname(fileIn));
new nodeMinify.minify({
type: 'uglifyjs',
fileIn: fileIn,
fileOut: fileOut,
callback: function (err) {
if (err) {
console.log(err);
return;
}
callback(fileOut);
}
});
};
var shimsGlobal = {},
shimsBuild = {};
var licenseComments = [];
var findAllCesiumReferences = function (absPath) {
if (fs.lstatSync(absPath).isDirectory()) {
var files = fs.readdirSync(absPath);
files.forEach(function (subpath) {
findAllCesiumReferences(path.join(absPath, subpath));
});
return;
} else if (!fs.lstatSync(absPath).isFile()) {
return;
}
var contents = fs.readFileSync(absPath).toString();
if (/\.js$/.test(absPath)) {
// 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[1] in shimsGlobal)) {
shimsGlobal[match[1]] = 'define(\'' + match[1] + '\', function() { return Cesium[\'' + match[2] + '\']; });';
}
if (!(match[1] in shimsBuild)) {
shimsBuild[match[1]] = 'define(\'' + match[1] + '\', [\'Cesium\'], function(Cesium) { return Cesium[\'' + match[2] + '\']; });';
}
}
} else if (/\.glsl$/.test(absPath)) {
var newContents = [];
contents = contents.replace(/\r\n/gm, '\n');
var licenseComments = contents.match(/\/\*\*(?:[^*\/]|\*(?!\/)|\n)*?@license(?:.|\n)*?\*\//gm);
if (licenseComments !== null) {
licenseComments = licenseComments.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');
}
};
findAllCesiumReferences(sourceDir);
shimsGlobal = Object.keys(shimsGlobal).map(function (key) {
return shimsGlobal[key];
}).join('\n');
shimsBuild = Object.keys(shimsBuild).map(function (key) {
return shimsBuild[key];
}).join('\n');
var copyrightHeader = fs.readFileSync(sourceDir + '/copyrightHeader.js').toString();
// <-- build standalone edition
var rjsBasicConfig = {
mainConfigFile: 'mainConfig.js',
wrap: {
start: copyrightHeader + '\n' +
"(function (root, factory) {\n" +
" 'use strict';\n" +
" /*jshint sub:true*/\n\n" +
" if (typeof define === 'function' && define.amd) {\n" +
" if(require.specified('Cesium/Cesium')) {\n" +
" define(['Cesium/Cesium'], factory);\n" +
" } else if(require.specified('Cesium')) {\n" +
" define(['Cesium'], factory);\n" +
" } else {\n" +
" define([], factory);\n" +
" }\n" +
" } else {\n" +
" factory();\n" +
" }\n" +
"}(typeof window !== 'undefined' ? window : typeof self !== 'undefined' ? self : this, function (C) {\n\n" +
" if (typeof C === 'object' && C !== null) {\n" +
" Cesium = C;\n" +
" }\n\n" +
"// <-- actual code\n\n\n",
end: "\n\n" +
"// actual code -->\n\n" +
" /*global define,require,self,Cesium*/\n" +
" " + licenseComments.join('\n ') + "\n" +
shimsGlobal + "\n" +
" \n" +
" var mixin = require('viewerCesiumNavigationMixin');\n" +
" if (typeof Cesium === 'object' && Cesium !== null) {\n" +
" Cesium['" + buildName + "'] = mixin;\n" +
" }\n\n" +
" return mixin;" +
"}));"
},
name: 'almond',
include: ['viewerCesiumNavigationMixin'],
logLevel: 0
};
var rjsConfig = JSON.parse(JSON.stringify(rjsBasicConfig));
rjsConfig.optimize = 'none';
rjsConfig.out = path.join(buildDir, standaloneSubDir, buildName + '.js');
requirejs.optimize(rjsConfig, function (buildResponse) {
console.log('Built standalone edition ' + rjsConfig.out + ' successfully.');
minify(rjsConfig.out, function (minFile) {
console.log('Generated minified ' + minFile);
});
});
// -->
// <-- build amd compatible edition
var rjsAMDBasicConfig = {
mainConfigFile: 'mainConfig.js',
name: 'viewerCesiumNavigationMixin',
wrap: {
start: copyrightHeader + '\n\n',
end: '\n\n\n'+
"/*global define,require*/\n" +
"if(!require.specified('Cesium/Cesium')) {\n" +
" if(typeof Cesium === 'object' && Cesium !== null) {\n" +
shimsGlobal + "\n"+
" } else {\n" +
shimsBuild + "\n"+
" }\n" +
"}\n\n" +
'define([\'viewerCesiumNavigationMixin\'], function(viewerCesiumNavigationMixin) {\n' +
' return viewerCesiumNavigationMixin;\n' +
'});'
},
logLevel: 0
};
var rjsAMDConfig = JSON.parse(JSON.stringify(rjsAMDBasicConfig));
rjsAMDConfig.optimize = 'none';
rjsAMDConfig.out = path.join(buildDir, amdSubDir, buildName + '.js');
requirejs.optimize(rjsAMDConfig, function (buildResponse) {
console.log('Built AMD compatible edition ' + rjsAMDConfig.out + ' successfully.');
minify(rjsAMDConfig.out, function (minFile) {
console.log('Generated minified ' + minFile);
});
});
// -->
})();