-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Make message files work in unpackaged mode, and rebuild msg files #6329
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this makes sense and covers all of our cases (given I thought our previous fixes did as well, but alas).
- In the browser, we want to overwrite both the
Blockly
tree, and the module-junk (as defined here).- This does that in the unwrapped case, by accessing the global
Blockly
and then assigning to it, which also modifies the module junk. - This does this in the wrapped case, by creating a new object
{Msg: Object.create(null)}
and then allowing the uml wrapper to use afor in
loop to assign the messages to theBlockly
tree which also modifies the module junk.
- This does that in the unwrapped case, by accessing the global
- In a module system, we do not want to overwrite either the
Blockly
tree or the module-junk directly. Instead we want to letBlockly.setLocale
do that.- This does this by creating a new object
{Msg: Object.create(null)}
and then allowing the external developer to callsetLocale
with the returned value.
- This does this by creating a new object
I think that technically (if my understanding is correct) you can actually use:
var messages = Blockly || {Msg: Object.create(null)};
and then use messages.Msg
in the rest of the file. I kind of think this is clearer, because in the browser wrapped case, and in the module system case, you're not actually modifying Blockly.Msg
directly (that is done in a separate step). But I guess in the unwrapped browser case you are modifying it directly... so up to you.
You are correct. However:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing all the careful testing, which is really the important part of this commit!
My only thought was to question whether msg/messages.js
should get the same treatment, but:
- it already references
Blockly.Msg
(good), and - doesn't get wrapped so (I think)
- can only be loaded as a
<script>
and so (I'm fairly sure) - doesn't need the
var Blockly = Blockly || ...
preamble.
Oh, and minor note about presentation: I would have found this PR slightly nicer to review if the script changes had been done in one commit and then the generated output updated in a second. (As it is the interesting bit gets buried at the end of a lot of boilerplate changes.)
Thanks for the review! for Sorry about the giant commit, I realized after I had already pushed that I should have done it differently. |
I concur.
No problem. I was only ever going to spot-check the language files anyway, so it was just a matter of making sure that I had got right to the end. |
@@ -123,7 +123,7 @@ def main(): | |||
|
|||
'use strict'; | |||
|
|||
const messages = Object.create(null); | |||
var Blockly = Blockly || {{ Msg: Object.create(null) }}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While reviewing PR #7661 I discovered that I had overlooked a confusing feature of this line:
I read var Blockly = Blockly …
to mean var Blockly = globalThis.Blockly …
, and indeed when the generated files (e.g. build/msg/en.js
) are loaded as scripts it does mean just that—but after being UMD wrapped (as e.g. dist/msg/en.js
) the second Blockly
ends up referring to the var Blockly
being defined on the same line, and so is always undefined
at this point (and that's true whether loaded as a CJS module or as a script).
Interestingly this does not cause any problems, because only playgrounds and tests load the unwrapped build/msg/en.js
(and do so as a <script>
), while the UMD wrapper added to (e.g.) dist/msg/en.js
contains special code to copy the message properties one-by-one from the temporary object onto the actual Blockly.Msg
dictionary—but only when the wrapped file is loaded as a script.
The basics
npm run format
andnpm run lint
The details
Resolves
Fixes #6123
Proposed Changes
Updates the Msg files to use
Blockly.Msg
instead ofmessages
, and associated changes in the preamble and wrapper to make that approach work in all cases.n.b. Thanks to @cpcallen for suggesting the approach taken here.
Behavior Before Change
Behavior After Change
Reason for Changes
In #6184 the message files were changed. Instead of assigning each translation directly on
Blockly.Msg
, the translations were put on a new object calledmessages
. In the UMD wrapper for the language files, all of the properties of that new object are then placed onBlockly.Msg
. This works for both of the first two cases above, but it doesn't work for anyone who is using the unwrapped message files.That is a scenario we need to support for now since it is explicitly in our documentation that you can download and run the code from github.
Also, the file
blockly.min.js
in the packaged output does not use the wrapped message file, so e.g. using a script tag to load<script src="https://unpkg.com/blockly"></script>
does not work because the unwrapped message files don't load prior to this change. That line of code is in the Getting Started codelab so that also needs to work. That will be fixed in a follow up PR.Test Coverage
msg/messages.js
(default behavior. this is the raw message file that isn't run through the python script and does not have the umd wrapper so it is not a good test of any real world behavior and should probably be changed)'dist/msg/es.js'
instead (compiled through python script and umd wrapped)'msg/js/es.js'
instead (compiled through python script but not umd wrapped)test/index.js
:<script src="https://unpkg.com/blockly"></script>
with<script src="../../../../blockly/dist/blockly.min.js"></script>
(linked to this build of blockly. this is the equivalent file that gets loaded when visiting the unpkg link. it contains the unwrapped english message file as noted above.)<script src="../../../../blockly/dist/msg/fr.js"></script>
(compiled and umd wrapped)msg/js/en.js
(default - compiled but not umd wrapped)dist/msg/az.js
(compiled and umd wrapped)