forked from TurboWarp/scratch-blocks
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbuild.js
More file actions
242 lines (215 loc) · 7.49 KB
/
Copy pathbuild.js
File metadata and controls
242 lines (215 loc) · 7.49 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#!/usr/bin/env node
// Compresses the core Blockly files into a single JavaScript file.
const onlyUncompressed = process.argv.includes("--uncompressed");
const fs = require("fs");
const path = require("path");
const Compiler = require("google-closure-compiler").compiler;
const HEADER =
"// Do not edit this file; automatically generated by build.js.\n'use strict';\n";
const CLOSURE_ROOT = "node_modules";
const CLOSURE_LIBRARY = "google-closure-library";
const LICENSE_RE =
/\/\*\n\n [\w ]+\n\n Copyright \d+ Google Inc\.\n https:\/\/developers\.google\.com\/blockly\/\n\n Licensed under the Apache License, Version 2\.0 \(the "License"\);\n you may not use this file except in compliance with the License\.\n You may obtain a copy of the License at\n\n http:\/\/www\.apache\.org\/licenses\/LICENSE-2\.0\n\n Unless required by applicable law or agreed to in writing, software\n distributed under the License is distributed on an "AS IS" BASIS,\n WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied\.\n See the License for the specific language governing permissions and\n limitations under the License\.\n\*\//g;
const toForward = (p) => p.replace(/\\/g, "/");
const findClosureBase = () => {
const p = path.join(
CLOSURE_ROOT,
CLOSURE_LIBRARY,
"closure",
"goog",
"base.js",
);
if (fs.existsSync(p)) return path.resolve(p);
throw new Error("Closure base.js not found at " + p);
};
const runCompiler = (args) => {
return new Promise((resolve, reject) => {
const compiler = new Compiler(args);
compiler.run((exitCode, stdout, stderr) => {
if (exitCode === 0) {
resolve(stdout);
} else {
reject(new Error(stderr || "Exit code " + exitCode));
}
});
});
};
const writeFile = (target, code, remove) => {
if (remove) code = code.replace(remove, "");
code = code.replace(LICENSE_RE, "");
code = HEADER + "\n" + code.trim();
fs.writeFileSync(target, code, "utf8");
console.log("SUCCESS: " + target);
};
const buildCompressedCore = () => {
const target = "blockly_compressed_vertical.js";
const closureDir = path.join(CLOSURE_ROOT, CLOSURE_LIBRARY);
const args = [
"--compilation_level=SIMPLE",
"--language_in=ECMASCRIPT_2017",
"--language_out=ECMASCRIPT5",
"--rewrite_polyfills=false",
"--define=goog.DEBUG=false",
"--js=" + toForward(path.join(closureDir, "closure", "goog", "**.js")),
"--js=" +
toForward(
path.join(closureDir, "third_party", "closure", "goog", "**.js"),
),
"--js=core/**.js",
"--entry_point=Blockly",
"--dependency_mode=STRICT",
];
return runCompiler(args).then((stdout) => {
writeFile(target, stdout);
});
};
const buildCompressedBlocks = (common) => {
const target =
(common ? "blocks_compressed" : "blocks_compressed_vertical") + ".js";
const dir = common ? "blocks_common" : "blocks_vertical";
const args = [
"--compilation_level=SIMPLE",
"--js=build/gen_blocks.js",
"--js=core/colours.js",
"--js=core/constants.js",
];
fs.readdirSync(dir)
.filter((f) => f.endsWith(".js"))
.sort()
.forEach((f) => {
args.push("--js=" + path.join(dir, f).replace(/\\/g, "/"));
});
return runCompiler(args).then((stdout) => {
writeFile(target, stdout, "var Blockly={Blocks:{}};");
});
};
const buildUncompressed = () => {
const target = "blockly_uncompressed_vertical.js";
const closureBase = findClosureBase();
const closureBaseDir = path.dirname(closureBase);
const closureDir = "/node_modules";
const closureLibrary = "google-closure-library";
let content = HEADER + "\n";
content += [
"var isNodeJS = !!(typeof module !== 'undefined' && module.exports && typeof window === 'undefined');",
"if (isNodeJS) {",
" var window = {};",
' require("' + closureDir + "/" + closureLibrary + '");',
"}",
"window.BLOCKLY_DIR = (function() {",
" if (!isNodeJS) {",
" var scripts = document.getElementsByTagName('script');",
" var re = /(.+)[\\\\/]blockly_uncompressed(_vertical|_horizontal|)\\.js$/;",
" for (var i = 0, script; script = scripts[i]; i++) {",
" var match = re.exec(script.src);",
" if (match) {",
" return match[1];",
" }",
" }",
" }",
" return '';",
"})();",
"window.BLOCKLY_BOOT = function() {",
" if (isNodeJS) {",
' require("' + closureDir + "/" + closureLibrary + '");',
" } else {",
" if (!window.goog) {",
" alert('Error: Closure not found. Read this:\\n' +",
" 'developers.google.com/blockly/guides/modify/web/closure');",
" }",
" }",
].join("\n");
const coreFiles = fs
.readdirSync("core")
.filter((f) => f.endsWith(".js"))
.sort();
coreFiles.forEach((f) => {
const fullPath = path.join("core", f);
const relPath = path
.relative(closureBaseDir, path.resolve(fullPath))
.replace(/\\/g, "/");
const src = fs.readFileSync(fullPath, "utf8");
const provides = [];
const requires = [];
src.split("\n").forEach((line) => {
let m;
m = line.match(/^\s*goog\.provide\s*\(\s*['"](.+)['"]\s*\)/);
if (m) provides.push(m[1]);
m = line.match(/^\s*goog\.require\s*\(\s*['"](.+)['"]\s*\)/);
if (m) requires.push(m[1]);
});
content +=
'\ngoog.addDependency("' +
relPath +
'", ' +
JSON.stringify(provides) +
", " +
JSON.stringify(requires) +
");";
});
content += "\n\n// Load Blockly.\n";
coreFiles.forEach((f) => {
const fullPath = path.join("core", f);
const src = fs.readFileSync(fullPath, "utf8");
src.split("\n").forEach((line) => {
const m = line.match(/^\s*goog\.provide\s*\(\s*['"](.+)['"]\s*\)/);
if (m) content += "goog.require('" + m[1] + "');\n";
});
});
content += "\n";
content +=
[
"delete this.BLOCKLY_DIR;",
"delete this.BLOCKLY_BOOT;",
"};",
"if (isNodeJS) {",
" window.BLOCKLY_BOOT();",
" module.exports = Blockly;",
"} else {",
" document.write('<script>var goog = undefined;</script>');",
" document.write('<script src=\"' + window.BLOCKLY_DIR + '" +
closureDir +
"/" +
closureLibrary +
"/closure/goog/base.js\"></script>');",
" document.write('<script>window.BLOCKLY_BOOT();</script>');",
"}",
].join("\n") + "\n";
fs.writeFileSync(target, content, "utf8");
console.log("SUCCESS: " + target);
};
const main = () => {
console.log("Using local compiler: google-closure-compiler ...\n");
const startTime = Date.now();
runCompiler(["--compilation_level=SIMPLE", "--js=build/test_input.js"])
.then((stdout) => {
const expected = fs.readFileSync("build/test_expect.js", "utf8").trim();
if (stdout.trim() !== expected) {
throw new Error(
'Compiler self-test failed.\nExpected: "' +
expected +
'"\nGot: "' +
stdout.trim() +
'"',
);
}
console.log("Compiler self-test passed.\n");
buildUncompressed();
if (onlyUncompressed) return;
return Promise.all([
buildCompressedCore(),
buildCompressedBlocks(false),
buildCompressedBlocks(true),
]);
})
.then(() => {
const endTime = Date.now();
const duration = (endTime - startTime) / 1000;
console.log("\nBuild complete in " + duration.toFixed(2) + " seconds.");
})
.catch((err) => {
console.error("Build failed:", err.message);
process.exit(1);
});
};
main();