mirror of
https://github.com/google/blockly.git
synced 2025-12-16 06:10:12 +01:00
Compile messages with externs and demo with advanced optimizations (#1259)
* Revert "Fix synonyms when compiled. (#1248)" This reverts commitf08afbb351. * Revert "Compatibility for Closure Compiler. (#1240)" [fc8d4c9] * Adding exports to all messages. * Fixed missing dependency to Blockly.defineBlocksWithJsonArray() * Adding a fully compiled demo draft (still simple optimizations). * Demo optimizations switched to advanced and enabled exports (for Blockly.Msg). * Message interpolation updated to use the exported (global) Blockly.Msg array. * Adding some debug compilation options to the build script. * Adding SVG externs. * Fixed Blockly.inject's config array to work with compilation. * Reverting all compiled code. This fixes commitb307ba1151. This fixes commitdec6910b67. * Reverting all compiled code. This fixes commit824c806ec3. * Removing old todo * Merge commit 'fe96bec765f0eb58c5321101965100c2716760ed' into compile-messages-with-externs * commit 'fe96bec765f0eb58c5321101965100c2716760ed': Fixes positional index for Czech translation (#1264) Missed one use of string instead of .property in extensions.js (#1262) Update extensions.js to be compatible with ADVANCED_OPTIMIZATIONS (#1253) Fix type tags and todo placement. Procedure block renames variable in mutator if there is a case change. * Fixes based on review by @NeilFraser - 80 cols - using goog.global instead of window - @export on the same line as messages * BF: Moving the msg dependency earlier, since Blockly.Msg.en is filling the Blockly.Msg object, which is empty without Blockly.Msg.en (and the rest of the code is using it as Blockly.Msg). * Updating some texts in the demo's html file to be more descriptive. * Commenting the debug options in the build, to maximize the optimizations. They are not removed, to allow anybody to turn them on if needed (since they are not documented on the Closure Compiler's REST API pages). * BF: fixed blocks_compressed.js compilation, as it now requires Blockly namespace to exist. * SVG externs file updated based on the one in https://github.com/google/closure-compiler/blob/master/contrib/externs/svg.js (eliminating 2 warnings)
This commit is contained in:
@@ -33,6 +33,7 @@ goog.provide('Blockly.Blocks.colour'); // Deprecated
|
||||
goog.provide('Blockly.Constants.Colour');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,7 @@ goog.provide('Blockly.Blocks.lists'); // Deprecated
|
||||
goog.provide('Blockly.Constants.Lists');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,7 @@ goog.provide('Blockly.Blocks.logic'); // Deprecated
|
||||
goog.provide('Blockly.Constants.Logic');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,7 @@ goog.provide('Blockly.Blocks.loops'); // Deprecated
|
||||
goog.provide('Blockly.Constants.Loops');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,7 @@ goog.provide('Blockly.Blocks.math'); // Deprecated
|
||||
goog.provide('Blockly.Constants.Math');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
goog.provide('Blockly.Blocks.procedures');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -28,6 +28,7 @@ goog.provide('Blockly.Blocks.texts'); // Deprecated
|
||||
goog.provide('Blockly.Constants.Text');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@@ -33,6 +33,7 @@ goog.provide('Blockly.Blocks.variables'); // Deprecated.
|
||||
goog.provide('Blockly.Constants.Variables');
|
||||
|
||||
goog.require('Blockly.Blocks');
|
||||
goog.require('Blockly');
|
||||
|
||||
|
||||
/**
|
||||
|
||||
59
build.py
59
build.py
@@ -60,9 +60,10 @@ for arg in sys.argv[1:len(sys.argv)]:
|
||||
if (arg != 'core' and
|
||||
arg != 'accessible' and
|
||||
arg != 'generators' and
|
||||
arg != 'langfiles'):
|
||||
arg != 'langfiles' and
|
||||
arg != 'demo'):
|
||||
raise Exception("Invalid argument: \"" + arg + "\". Usage: build.py <0 or more of accessible," +
|
||||
" core, generators, langfiles>")
|
||||
" core, generators, langfiles, demo>")
|
||||
|
||||
import errno, glob, httplib, json, os, re, subprocess, threading, urllib
|
||||
|
||||
@@ -214,6 +215,47 @@ class Gen_compressed(threading.Thread):
|
||||
self.gen_generator("dart")
|
||||
self.gen_generator("lua")
|
||||
|
||||
if ('demo' in self.bundles):
|
||||
self.gen_together()
|
||||
|
||||
def gen_together(self):
|
||||
target_filename = os.path.join("demos", "fixed-advanced", "main_compressed.js")
|
||||
# Define the parameters for the POST request.
|
||||
params = [
|
||||
("compilation_level", "ADVANCED_OPTIMIZATIONS"),
|
||||
("use_closure_library", "true"),
|
||||
("generate_exports", "true"),
|
||||
("output_format", "json"),
|
||||
("output_info", "compiled_code"),
|
||||
("output_info", "warnings"),
|
||||
("output_info", "errors"),
|
||||
("output_info", "statistics"),
|
||||
# debug options (to make the uglified code readable)
|
||||
# ("formatting", "pretty_print"),
|
||||
# ("formatting", "print_input_delimiter"),
|
||||
# ("debug", "true"),
|
||||
]
|
||||
|
||||
# Read in all the source files.
|
||||
filenames = calcdeps.CalculateDependencies(self.search_paths,
|
||||
[os.path.join("demos", "fixed-advanced", "main.js")])
|
||||
filenames.sort() # Deterministic build.
|
||||
for filename in filenames:
|
||||
# Filter out the Closure files (the compiler will add them).
|
||||
if filename.startswith(os.pardir + os.sep): # '../'
|
||||
continue
|
||||
f = open(filename)
|
||||
params.append(("js_code", "".join(f.readlines())))
|
||||
f.close()
|
||||
|
||||
externs = [os.path.join("externs", "svg-externs.js")]
|
||||
for filename in externs:
|
||||
f = open(filename)
|
||||
params.append(("js_externs", "".join(f.readlines())))
|
||||
f.close()
|
||||
|
||||
self.do_compile(params, target_filename, filenames, "")
|
||||
|
||||
def gen_core(self):
|
||||
target_filename = "blockly_compressed.js"
|
||||
# Define the parameters for the POST request.
|
||||
@@ -310,7 +352,7 @@ class Gen_compressed(threading.Thread):
|
||||
|
||||
# Read in all the source files.
|
||||
# Add Blockly.Blocks to be compatible with the compiler.
|
||||
params.append(("js_code", "goog.provide('Blockly.Blocks');"))
|
||||
params.append(("js_code", "goog.provide('Blockly');goog.provide('Blockly.Blocks');"))
|
||||
filenames = glob.glob(os.path.join("blocks", "*.js"))
|
||||
filenames.sort() # Deterministic build.
|
||||
for filename in filenames:
|
||||
@@ -544,8 +586,15 @@ developers.google.com/blockly/guides/modify/web/closure""")
|
||||
if ('accessible' in args):
|
||||
Gen_uncompressed(full_search_paths, 'blockly_accessible_uncompressed.js').start()
|
||||
|
||||
# Compressed is limited by network and server speed.
|
||||
Gen_compressed(full_search_paths, args).start()
|
||||
if ('demo' in args):
|
||||
all_search_paths = calcdeps.ExpandDirectories(
|
||||
["accessible", "core", "blocks", os.path.join("demos", "fixed-advanced"), os.path.join("msg", "js"), os.path.join(os.path.pardir, "closure-library")])
|
||||
all_search_paths.sort() # Deterministic build.
|
||||
Gen_compressed(all_search_paths, args).start()
|
||||
|
||||
else:
|
||||
# Compressed is limited by network and server speed.
|
||||
Gen_compressed(full_search_paths, args).start()
|
||||
|
||||
# This is run locally in a separate thread
|
||||
# defaultlangfiles checks for changes in the msg files, while manually asking
|
||||
|
||||
@@ -38,6 +38,20 @@ goog.require('goog.events.BrowserFeature');
|
||||
goog.require('goog.math.Coordinate');
|
||||
goog.require('goog.userAgent');
|
||||
|
||||
|
||||
/**
|
||||
* To allow ADVANCED_OPTIMIZATIONS, combining variable.name and variable['name']
|
||||
* is not possible. To access the exported Blockly.Msg.Something it needs to be
|
||||
* accessed through the exact name that was exported. Note, that all the exports
|
||||
* are happening as the last thing in the generated js files, so they won't be
|
||||
* accessible before javascript loads!
|
||||
* @return {!Object<string, string>}
|
||||
* @private
|
||||
*/
|
||||
Blockly.utils.getMessageArray_ = function() {
|
||||
return goog.global['Blockly']['Msg'];
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove an attribute from a element even if it's in IE 10.
|
||||
* Similar to Element.removeAttribute() but it works on SVG elements in IE 10.
|
||||
@@ -457,7 +471,7 @@ Blockly.utils.checkMessageReferences = function(message) {
|
||||
var match = regex.exec(message);
|
||||
while (match != null) {
|
||||
var msgKey = match[1];
|
||||
if (Blockly.Msg[msgKey] == null) {
|
||||
if (Blockly.utils.getMessageArray_()[msgKey] == null) {
|
||||
console.log('WARNING: No message string for %{BKY_' + msgKey + '}.');
|
||||
isValid = false;
|
||||
}
|
||||
@@ -550,8 +564,8 @@ Blockly.utils.tokenizeInterpolation_ = function(message, parseInterpolationToken
|
||||
// are defined in ../msgs/ files.
|
||||
var bklyKey = goog.string.startsWith(keyUpper, 'BKY_') ?
|
||||
keyUpper.substring(4) : null;
|
||||
if (bklyKey && bklyKey in Blockly.Msg) {
|
||||
var rawValue = Blockly.Msg[bklyKey];
|
||||
if (bklyKey && bklyKey in Blockly.utils.getMessageArray_()) {
|
||||
var rawValue = Blockly.utils.getMessageArray_()[bklyKey];
|
||||
if (goog.isString(rawValue)) {
|
||||
// Attempt to dereference substrings, too, appending to the end.
|
||||
Array.prototype.push.apply(tokens,
|
||||
|
||||
45
demos/fixed-advanced/index.html
Normal file
45
demos/fixed-advanced/index.html
Normal file
@@ -0,0 +1,45 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Blockly Demo: Fixed Blockly built with Closure Compiler's advanced optimizations</title>
|
||||
<script src="main_compressed.js"></script>
|
||||
<style>
|
||||
body {
|
||||
background-color: #fff;
|
||||
font-family: sans-serif;
|
||||
}
|
||||
h1 {
|
||||
font-weight: normal;
|
||||
font-size: 140%;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>
|
||||
<a href="https://developers.google.com/blockly/">Blockly</a> >
|
||||
<a href="../index.html">Demos</a> >
|
||||
Fixed Blockly built with Closure Compiler's advanced optimizations
|
||||
</h1>
|
||||
|
||||
<p>
|
||||
This is a simple demo of injecting Blockly into a fixed-sized 'div' element
|
||||
from a Closure-compiled source code with advanced optimizations.
|
||||
</p>
|
||||
|
||||
<p>→ More info on <a href="https://developers.google.com/blockly/guides/configure-blockly/web/fixed-size">injecting fixed-sized Blockly</a>…</p>
|
||||
|
||||
<div id="blocklyDiv" style="height: 480px; width: 600px;"></div>
|
||||
|
||||
<xml id="toolbox" style="display: none">
|
||||
<block type="controls_if"></block>
|
||||
<block type="logic_compare"></block>
|
||||
<block type="controls_repeat_ext"></block>
|
||||
<block type="math_number"></block>
|
||||
<block type="math_arithmetic"></block>
|
||||
<block type="text"></block>
|
||||
<block type="text_print"></block>
|
||||
</xml>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
31
demos/fixed-advanced/main.js
Normal file
31
demos/fixed-advanced/main.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* @fileoverview Main file (entry point) for the advanced compilation demo.
|
||||
*/
|
||||
'use strict';
|
||||
|
||||
goog.provide('Demo');
|
||||
|
||||
// messages (in some language)
|
||||
goog.require('Blockly.Msg.en');
|
||||
// core
|
||||
goog.require('Blockly');
|
||||
// blocks
|
||||
goog.require('Blockly.Constants.Colour');
|
||||
goog.require('Blockly.Constants.Lists');
|
||||
goog.require('Blockly.Constants.Logic');
|
||||
goog.require('Blockly.Constants.Loops');
|
||||
goog.require('Blockly.Constants.Math');
|
||||
goog.require('Blockly.Blocks.procedures');
|
||||
goog.require('Blockly.Constants.Text');
|
||||
goog.require('Blockly.Constants.Variables');
|
||||
|
||||
|
||||
Demo.init = function() {
|
||||
Blockly.inject('blocklyDiv', {
|
||||
'media': '../../media/',
|
||||
'toolbox': document.getElementById('toolbox')
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener('load', Demo.init);
|
||||
|
||||
10243
externs/svg-externs.js
Normal file
10243
externs/svg-externs.js
Normal file
File diff suppressed because it is too large
Load Diff
@@ -42,7 +42,8 @@ def load_constants(filename):
|
||||
for key in constant_defs:
|
||||
value = constant_defs[key]
|
||||
value = value.replace('"', '\\"')
|
||||
constants_text += '\nBlockly.Msg["{0}"] = "{1}";'.format(key, value)
|
||||
constants_text += u'\n/** @export */ Blockly.Msg.{0} = \"{1}\";'.format(
|
||||
key, value)
|
||||
return constants_text
|
||||
|
||||
def main():
|
||||
@@ -87,8 +88,8 @@ def main():
|
||||
# Read in synonyms file, which must be output in every language.
|
||||
synonym_defs = read_json_file(os.path.join(
|
||||
os.curdir, args.source_synonym_file))
|
||||
synonym_text = '\n'.join(['Blockly.Msg["{0}"] = Blockly.Msg["{1}"];'.format(
|
||||
key, synonym_defs[key]) for key in synonym_defs])
|
||||
synonym_text = '\n'.join([u'/** @export */ Blockly.Msg.{0} = Blockly.Msg.{1};'
|
||||
.format(key, synonym_defs[key]) for key in synonym_defs])
|
||||
|
||||
# Read in constants file, which must be output in every language.
|
||||
constants_text = load_constants(os.path.join(os.curdir, args.source_constants_file))
|
||||
@@ -139,8 +140,8 @@ goog.require('Blockly.Msg');
|
||||
value = source_defs[key]
|
||||
comment = ' // untranslated'
|
||||
value = value.replace('"', '\\"')
|
||||
outfile.write(u'Blockly.Msg["{0}"] = "{1}";{2}\n'.format(
|
||||
key, value, comment))
|
||||
outfile.write(u'/** @export */ Blockly.Msg.{0} = "{1}";{2}\n'
|
||||
.format(key, value, comment))
|
||||
|
||||
# Announce any keys defined only for target language.
|
||||
if target_defs:
|
||||
|
||||
Reference in New Issue
Block a user