From 40c87b0aaf95c5437aa87770bf1213db2e1b6ab6 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Fri, 28 Jan 2022 10:57:25 +0000 Subject: [PATCH 1/3] refactor(blocks): Make loopTypes a Set This is likely to slightly improve performance, especially if there are many entries. --- blocks/loops.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/blocks/loops.js b/blocks/loops.js index cc3c27d371f..21e66ece3ad 100644 --- a/blocks/loops.js +++ b/blocks/loops.js @@ -287,21 +287,21 @@ Extensions.register( * * // If using the Blockly npm package and es6 import syntax: * import {loopTypes} from 'blockly/blocks'; - * loopTypes.push('custom_loop'); + * loopTypes.add('custom_loop'); * * // Else if using Closure Compiler and goog.modules: * const {loopTypes} = goog.require('Blockly.blocks.loops'); - * loopTypes.push('custom_loop'); + * loopTypes.add('custom_loop'); * - * @type {!Array} + * @type {!Set} */ -const loopTypes = [ +const loopTypes = new Set([ 'controls_repeat', 'controls_repeat_ext', 'controls_forEach', 'controls_for', 'controls_whileUntil', -]; +]); exports.loopTypes = loopTypes; /** @@ -321,7 +321,7 @@ const CONTROL_FLOW_IN_LOOP_CHECK_MIXIN = { getSurroundLoop: function() { let block = this; do { - if (loopTypes.includes(block.type)) { + if (loopTypes.has(block.type)) { return block; } block = block.getSurroundParent(); From e5357e541f8df978e4f388660b71144a43f54353 Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Fri, 28 Jan 2022 10:59:14 +0000 Subject: [PATCH 2/3] refactor(blocks): Re-export individual block modules from Blockly.blocks.all --- blocks/all.js | 30 +++++++++++++++++++++--------- tests/deps.js | 2 +- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/blocks/all.js b/blocks/all.js index 17000f8c96d..6e2cef55d73 100644 --- a/blocks/all.js +++ b/blocks/all.js @@ -12,12 +12,24 @@ goog.module('Blockly.blocks.all'); -goog.require('Blockly.blocks.colour'); -goog.require('Blockly.blocks.lists'); -goog.require('Blockly.blocks.logic'); -goog.require('Blockly.blocks.loops'); -goog.require('Blockly.blocks.math'); -goog.require('Blockly.blocks.procedures'); -goog.require('Blockly.blocks.texts'); -goog.require('Blockly.blocks.variables'); -goog.require('Blockly.blocks.variablesDynamic'); +const colour = goog.require('Blockly.blocks.colour'); +const lists = goog.require('Blockly.blocks.lists'); +const logic = goog.require('Blockly.blocks.logic'); +const loops = goog.require('Blockly.blocks.loops'); +const math = goog.require('Blockly.blocks.math'); +const procedures = goog.require('Blockly.blocks.procedures'); +const texts = goog.require('Blockly.blocks.texts'); +const variables = goog.require('Blockly.blocks.variables'); +const variablesDynamic = goog.require('Blockly.blocks.variablesDynamic'); + + +exports.colour = colour; +exports.lists = lists; +exports.logic = logic; +exports.loops = loops; +exports.math = math; +exports.procedures = procedures; +exports.texts = texts; +exports.variables = variables; +exports.variablesDynamic = variablesDynamic; + diff --git a/tests/deps.js b/tests/deps.js index 1946968c43b..601c79e77ef 100644 --- a/tests/deps.js +++ b/tests/deps.js @@ -1,4 +1,4 @@ -goog.addDependency('../../blocks/all.js', ['Blockly.blocks.all'], ['Blockly.blocks.colour', 'Blockly.blocks.lists', 'Blockly.blocks.logic', 'Blockly.blocks.loops', 'Blockly.blocks.math', 'Blockly.blocks.procedures', 'Blockly.blocks.texts', 'Blockly.blocks.variables', 'Blockly.blocks.variablesDynamic'], {'module': 'goog'}); +goog.addDependency('../../blocks/all.js', ['Blockly.blocks.all'], ['Blockly.blocks.colour', 'Blockly.blocks.lists', 'Blockly.blocks.logic', 'Blockly.blocks.loops', 'Blockly.blocks.math', 'Blockly.blocks.procedures', 'Blockly.blocks.texts', 'Blockly.blocks.variables', 'Blockly.blocks.variablesDynamic'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../blocks/colour.js', ['Blockly.blocks.colour'], ['Blockly.FieldColour', 'Blockly.common'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../blocks/lists.js', ['Blockly.blocks.lists'], ['Blockly.ConnectionType', 'Blockly.FieldDropdown', 'Blockly.FieldDropdown', 'Blockly.Input', 'Blockly.Msg', 'Blockly.Mutator', 'Blockly.blocks', 'Blockly.common', 'Blockly.utils.xml'], {'lang': 'es6', 'module': 'goog'}); goog.addDependency('../../blocks/logic.js', ['Blockly.blocks.logic'], ['Blockly.Events', 'Blockly.Extensions', 'Blockly.FieldDropdown', 'Blockly.FieldLabel', 'Blockly.Msg', 'Blockly.Mutator', 'Blockly.common', 'Blockly.utils.xml'], {'lang': 'es6', 'module': 'goog'}); From 3effa4240572a6dace0bf3ee45332a83ec4b708a Mon Sep 17 00:00:00 2001 From: Christopher Allen Date: Fri, 28 Jan 2022 12:30:25 +0000 Subject: [PATCH 3/3] fix!(blocks): Have blocks_compressed.js export Blockly.blocks.all Previously the value obtained by const blocks = require('blockly/blocks'); // Node.js or import blocks from 'blockly/blocks.js'; // ES Modules would be the block definitions dictionary (Blockly.Blocks). Change this so that it is instead the export object from Blockly.blocks.all. This means you can now access loopTypes via: import blocks from 'blockly/blocks.js'; blocks.loops.loopTypes.add('my_loop_blocktype'); This is a breaking change for any code which depended on the value that was exported by blocks_compressed.js. N.B.: at least in node.js, "import blocks from 'blockly/blocks'" fails with the following error: Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import '<...>/blocks' is not supported resolving ES modules imported from <...> Did you mean to import blockly/dist/blocks.js? (The "dist" in the error message seems to be spurious.) --- blocks/all.js | 1 + scripts/gulpfiles/build_tasks.js | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/blocks/all.js b/blocks/all.js index 6e2cef55d73..b145d4107d7 100644 --- a/blocks/all.js +++ b/blocks/all.js @@ -11,6 +11,7 @@ 'use strict'; goog.module('Blockly.blocks.all'); +goog.module.declareLegacyNamespace(); const colour = goog.require('Blockly.blocks.colour'); const lists = goog.require('Blockly.blocks.lists'); diff --git a/scripts/gulpfiles/build_tasks.js b/scripts/gulpfiles/build_tasks.js index 0748ea118c1..aef2bf95b6e 100644 --- a/scripts/gulpfiles/build_tasks.js +++ b/scripts/gulpfiles/build_tasks.js @@ -108,7 +108,7 @@ const chunks = [ { name: 'blocks', entry: 'blocks/all.js', - exports: 'Blockly.Blocks', + exports: 'Blockly.blocks.all', importAs: 'BlocklyBlocks', }, { @@ -321,7 +321,7 @@ function chunkWrapper(chunk) { const cjsDeps = fileNames.map(f => `require(${f})`).join(', '); const browserDeps = chunk.dependencies.map(d => `root.${d.exports}`).join(', '); - const imports = chunk.dependencies.map(d => d.importAs).join(', '); + const factoryParams = chunk.dependencies.map(d => d.importAs).join(', '); return `// Do not edit this file; automatically generated. /* eslint-disable */ @@ -333,7 +333,7 @@ function chunkWrapper(chunk) { } else { // Browser root.${chunk.exports} = factory(${browserDeps}); } -}(this, function(${imports}) { +}(this, function(${factoryParams}) { ${chunk.factoryPreamble || FACTORY_PREAMBLE} %output% ${chunk.factoryPostamble || FACTORY_POSTAMBLE}