Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80668 views
1
/**
2
* Copyright 2013 Facebook, Inc.
3
*
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
* you may not use this file except in compliance with the License.
6
* You may obtain a copy of the License at
7
*
8
* http://www.apache.org/licenses/LICENSE-2.0
9
*
10
* Unless required by applicable law or agreed to in writing, software
11
* distributed under the License is distributed on an "AS IS" BASIS,
12
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
* See the License for the specific language governing permissions and
14
* limitations under the License.
15
*/
16
17
var blockCommentRe = /\/\*(.|\n)*?\*\//g;
18
var lineCommentRe = /\/\/.+(\n|$)/g;
19
20
function extractStrings(code, regex, index) {
21
// clean up most comments
22
code = code
23
.replace(blockCommentRe, '')
24
.replace(lineCommentRe, '');
25
26
var match;
27
var visited = {};
28
while (match = regex.exec(code)) {
29
// manually check for preceding dot since we don't have backreferences
30
if (match.index === 0 || code.charAt(match.index - 1) !== '.' &&
31
match[index]) {
32
visited[match[index]] = 1;
33
}
34
}
35
return Object.keys(visited);
36
}
37
38
function extractStringArrays(code, regex, index, index2) {
39
// clean up most comments
40
code = code
41
.replace(blockCommentRe, '')
42
.replace(lineCommentRe, '');
43
44
var match;
45
var visited = {};
46
var m;
47
while (match = regex.exec(code)) {
48
if (match.index === 0 || code.charAt(match.index - 1) !== '.') {
49
m = match[index] || (index2 && match[index2]);
50
if (m) {
51
try {
52
JSON.parse('[' + m.replace(/'/g, '"') + ']')
53
.forEach(function(key) {
54
visited[key] = 1;
55
});
56
} catch(e) {}
57
}
58
}
59
}
60
return Object.keys(visited);
61
}
62
63
var requireRe = /\brequire\s*\(\s*[\'"]([^"\']+)["\']\s*\)/g;
64
function requireCalls(code) {
65
if (code.indexOf('require(') === -1) {
66
return [];
67
}
68
return extractStrings(code, requireRe, 1);
69
}
70
71
var requireLazyRe = /\brequireLazy\s*\(\s*\[([^\]]+)\]/g;
72
function requireLazyCalls(code) {
73
if (code.indexOf('requireLazy(') === -1) {
74
return [];
75
}
76
return extractStringArrays(code, requireLazyRe, 1);
77
}
78
79
var loadModulesRe = /\bBootloader\.loadModules\s*\(\s*(?:\[([^\]]+)\])?/g;
80
function loadModules(code) {
81
if (code.indexOf('Bootloader.loadModules(') === -1) {
82
return [];
83
}
84
return extractStringArrays(code, loadModulesRe, 1);
85
}
86
87
var loadComponentsRe =
88
/\bBootloader\.loadComponents\s*\(\s*(?:\[([^\]]+)\]|([\'"][^\'"]+[\'"]))/g;
89
function loadComponents(code) {
90
if (code.indexOf('Bootloader.loadComponents(') === -1) {
91
return [];
92
}
93
return extractStringArrays(code, loadComponentsRe, 1, 2);
94
}
95
96
97
var cxModulesRe = /\bcx\s*\(\s*([^)]+)\s*\)/g;
98
function cxModules(code) {
99
if (code.indexOf('cx(') === -1) {
100
return [];
101
}
102
var map = {};
103
extractStringArrays(code, cxModulesRe, 1).forEach(function(m) {
104
var parts = m.split('/');
105
if (parts[0] === 'public') {
106
parts = parts.slice(1);
107
}
108
if (parts.length > 1 && parts[0]) {
109
map[parts[0]] = 1;
110
}
111
});
112
return Object.keys(map);
113
}
114
115
116
exports.strings = extractStrings;
117
exports.requireCalls = requireCalls;
118
exports.requireLazyCalls = requireLazyCalls;
119
exports.loadModules = loadModules;
120
exports.loadComponents = loadComponents;
121
exports.cxModules = cxModules;
122
123