From 188ba985ccd413c8b8f969c2fcb43f5af181d507 Mon Sep 17 00:00:00 2001 From: Beka Westberg Date: Mon, 13 Jun 2022 14:13:54 -0700 Subject: [PATCH] fix: loading messages from script tags. (#6184) * fix: loading messages in the browser * chore: fix comment * fix: change unwrapped message files to write to a new object, rather than Blockly.Msg * fix: fixup exports * fix: PR comments * fix: change to use for-in loop * fix: ES6 compatibility and formatting --- scripts/gulpfiles/package_tasks.js | 15 ++++++--------- scripts/i18n/create_messages.py | 8 +++++--- scripts/package/templates/umd-msg.template | 16 ++++++++++++++++ 3 files changed, 27 insertions(+), 12 deletions(-) create mode 100644 scripts/package/templates/umd-msg.template diff --git a/scripts/gulpfiles/package_tasks.js b/scripts/gulpfiles/package_tasks.js index 83bf5700b..d37fcba0e 100644 --- a/scripts/gulpfiles/package_tasks.js +++ b/scripts/gulpfiles/package_tasks.js @@ -29,12 +29,12 @@ const TEMPLATE_DIR = 'scripts/package/templates'; * @param {string} namespace The export namespace. * @param {Array} dependencies An array of dependencies to inject. */ -function packageUMD(namespace, dependencies) { +function packageUMD(namespace, dependencies, template = 'umd.template') { return gulp.umd({ dependencies: function () { return dependencies; }, namespace: function () { return namespace; }, exports: function () { return namespace; }, - template: path.join(TEMPLATE_DIR, 'umd.template') + template: path.join(TEMPLATE_DIR, template) }); }; @@ -321,13 +321,10 @@ function packageLocales() { // Remove references to goog.provide and goog.require. return gulp.src(`${BUILD_DIR}/msg/js/*.js`) .pipe(gulp.replace(/goog\.[^\n]+/g, '')) - .pipe(gulp.insert.prepend(` - var Blockly = {};Blockly.Msg={};`)) - .pipe(packageUMD('Blockly.Msg', [{ - name: 'Blockly', - amd: '../core', - cjs: '../core', - }])) + .pipe(packageUMD( + 'Blockly.Msg', + [{name: 'Blockly'}], + 'umd-msg.template')) .pipe(gulp.dest(`${RELEASE_DIR}/msg`)); }; diff --git a/scripts/i18n/create_messages.py b/scripts/i18n/create_messages.py index 70ce61de3..ab418a8d6 100755 --- a/scripts/i18n/create_messages.py +++ b/scripts/i18n/create_messages.py @@ -41,7 +41,7 @@ def load_constants(filename): for key in constant_defs: value = constant_defs[key] value = value.replace('"', '\\"') - constants_text += u'\nBlockly.Msg["{0}"] = \"{1}\";'.format( + constants_text += u'\nmessages["{0}"] = \"{1}\";'.format( key, value) return constants_text @@ -88,7 +88,7 @@ def main(): os.curdir, args.source_synonym_file)) # synonym_defs is also being sorted to ensure the same order is kept - synonym_text = '\n'.join([u'Blockly.Msg["{0}"] = Blockly.Msg["{1}"];' + synonym_text = '\n'.join([u'messages["{0}"] = messages["{1}"];' .format(key, synonym_defs[key]) for key in sorted(synonym_defs)]) # Read in constants file, which must be output in every language. @@ -123,6 +123,8 @@ def main(): 'use strict'; +const messages = Object.create(null); + """.format(target_lang.replace('-', '.'))) # For each key in the source language file, output the target value # if present; otherwise, output the source language value with a @@ -136,7 +138,7 @@ def main(): value = source_defs[key] comment = ' // untranslated' value = value.replace('"', '\\"') - outfile.write(u'Blockly.Msg["{0}"] = "{1}";{2}\n' + outfile.write(u'messages["{0}"] = "{1}";{2}\n' .format(key, value, comment)) # Announce any keys defined only for target language. diff --git a/scripts/package/templates/umd-msg.template b/scripts/package/templates/umd-msg.template new file mode 100644 index 000000000..74c8de0bf --- /dev/null +++ b/scripts/package/templates/umd-msg.template @@ -0,0 +1,16 @@ +/* eslint-disable */ +;(function(root, factory) { + if (typeof define === 'function' && define.amd) { // AMD + define(<%= amd %>, factory); + } else if (typeof exports === 'object') { // Node.js + module.exports = factory(); + } else { // Browser + var messages = factory(); + for (var key in messages) { + root.<%= namespace %>[key] = messages[key]; + } + } +}(this, function() { +<%= contents %> +return messages; +}));