From a4ec65d85d056e52c7f15812742546d6d6eacdbc Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Thu, 25 Jan 2018 09:29:31 -0800 Subject: [PATCH] Improve support >ES5 and Node. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds ‘yield’ and ‘**’ to JS order list. Build generates files (more) parsable by Node. This PR should have no effect on existing code. --- build.py | 47 +++++++++++++++++++--------------------- generators/javascript.js | 8 ++++--- package.json | 8 +++---- 3 files changed, 30 insertions(+), 33 deletions(-) diff --git a/build.py b/build.py index fab3d0039..67bb05b70 100755 --- a/build.py +++ b/build.py @@ -104,16 +104,10 @@ class Gen_uncompressed(threading.Thread): f = open(self.target_filename, 'w') f.write(HEADER) f.write(""" -var isNodeJS = !!(typeof module !== 'undefined' && module.exports && - typeof window === 'undefined'); +this.IS_NODE_JS = !!(typeof module !== 'undefined' && module.exports); -if (isNodeJS) { - var window = {}; - require('closure-library'); -} - -window.BLOCKLY_DIR = (function() { - if (!isNodeJS) { +this.BLOCKLY_DIR = (function(root) { + if (!root.IS_NODE_JS) { // Find name of current directory. var scripts = document.getElementsByTagName('script'); var re = new RegExp('(.+)[\/]blockly_(.*)uncompressed\.js$'); @@ -126,21 +120,23 @@ window.BLOCKLY_DIR = (function() { alert('Could not detect Blockly\\'s directory name.'); } return ''; -})(); +})(this); + +this.BLOCKLY_BOOT = function(root) { + if (root.IS_NODE_JS) { + require('google-closure-library'); + } else if (typeof goog == 'undefined') { + alert('Error: Closure not found. Read this:\\n' + + 'developers.google.com/blockly/guides/modify/web/closure'); + } -window.BLOCKLY_BOOT = function() { var dir = ''; - if (isNodeJS) { - require('closure-library'); + if (root.IS_NODE_JS) { dir = 'blockly'; } else { - // Execute after Closure has loaded. - if (!window.goog) { - alert('Error: Closure not found. Read this:\\n' + - 'developers.google.com/blockly/guides/modify/web/closure'); - } - dir = window.BLOCKLY_DIR.match(/[^\\/]+$/)[0]; + dir = this.BLOCKLY_DIR.match(/[^\\/]+$/)[0]; } + // Execute after Closure has loaded. """) add_dependency = [] base_path = calcdeps.FindClosureBasePath(self.search_paths) @@ -167,20 +163,21 @@ window.BLOCKLY_BOOT = function() { f.write("goog.require('%s');\n" % provide) f.write(""" -delete this.BLOCKLY_DIR; -delete this.BLOCKLY_BOOT; +delete root.BLOCKLY_DIR; +delete root.BLOCKLY_BOOT; +delete root.IS_NODE_JS; }; -if (isNodeJS) { - window.BLOCKLY_BOOT(); +if (this.IS_NODE_JS) { + this.BLOCKLY_BOOT(this); module.exports = Blockly; } else { // Delete any existing Closure (e.g. Soy's nogoog_shim). document.write(''); // Load fresh Closure Library. - document.write(''); - document.write(''); + document.write(''); } """) f.close() diff --git a/generators/javascript.js b/generators/javascript.js index 4e978ed74..be62a4c19 100644 --- a/generators/javascript.js +++ b/generators/javascript.js @@ -84,8 +84,9 @@ Blockly.JavaScript.ORDER_TYPEOF = 4.5; // typeof Blockly.JavaScript.ORDER_VOID = 4.6; // void Blockly.JavaScript.ORDER_DELETE = 4.7; // delete Blockly.JavaScript.ORDER_AWAIT = 4.8; // await -Blockly.JavaScript.ORDER_DIVISION = 5.1; // / -Blockly.JavaScript.ORDER_MULTIPLICATION = 5.2; // * +Blockly.JavaScript.ORDER_EXPONENTIATION = 5.0; // ** +Blockly.JavaScript.ORDER_MULTIPLICATION = 5.1; // * +Blockly.JavaScript.ORDER_DIVISION = 5.2; // / Blockly.JavaScript.ORDER_MODULUS = 5.3; // % Blockly.JavaScript.ORDER_SUBTRACTION = 6.1; // - Blockly.JavaScript.ORDER_ADDITION = 6.2; // + @@ -100,7 +101,8 @@ Blockly.JavaScript.ORDER_BITWISE_OR = 12; // | Blockly.JavaScript.ORDER_LOGICAL_AND = 13; // && Blockly.JavaScript.ORDER_LOGICAL_OR = 14; // || Blockly.JavaScript.ORDER_CONDITIONAL = 15; // ?: -Blockly.JavaScript.ORDER_ASSIGNMENT = 16; // = += -= *= /= %= <<= >>= ... +Blockly.JavaScript.ORDER_ASSIGNMENT = 16; // = += -= **= *= /= %= <<= >>= ... +Blockly.JavaScript.ORDER_YIELD = 16.5; // yield Blockly.JavaScript.ORDER_COMMA = 17; // , Blockly.JavaScript.ORDER_NONE = 99; // (...) diff --git a/package.json b/package.json index 985c15170..03074fe61 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,8 @@ "license": "Apache-2.0", "private": true, "devDependencies": { - "jshint": "latest" + "jshint": "latest", + "eslint": "2.9.0" }, "jshintConfig": { "globalstrict": true, @@ -41,12 +42,9 @@ "unused": true }, "dependencies": { + "google-closure-library": "^20171203.0.0", "install": "^0.8.8", "npm": "^4.4.4", - "closure-library": "^1.43629075.2", "webdriverio": "^4.6.2" - }, - "devDependencies": { - "eslint": "2.9.0" } }