-
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
refactor(tests): Use goog.bootstrap
to allow loading ES modules in playground
#5931
Conversation
* Update base.js from the latest version (20220104.0.0). * Also copy over goog.js, which provides access to asuitable subset of goog.* via an importable module).
N.B.: * We still need a preparation step, in order to load base.js and deps.js via <script> tags in uncompiled mode; in compiled mode it will instead load all the *_compressed.js files via <script> tags. Acess to the Blockly object is via: import Blockly from './playgrounds/blockly.mjs'; (N.B: no "* as", since blockly.mjs has only a default export.) * There remain two serious defects when running in uncompiled mode: * It does not attempt to load msg/messages.js, causing startup to fail. * Module loading only works if there are no ES Modules; if there are, something goes wrong with base.js's attempt to sequence module loads causing goog.modules that import ES modules to get a null exports object for that import. X-(
This fixes the issue caused by missing messages when loading the generators.
Move the calls to goog.bootstrap from blockly.mjs to prepare.mjs. This is needed to work around a bug in the Cosure Library debug loader (google/closure-library#1152). This gets a bit ugly because most of the code has to go in a <script> (because it needs goog.bootstrap, which was loaded by an earlier <script> tag).
// limitations under the License. | ||
|
||
/** | ||
* @fileoverview ES6 module that exports symbols from base.js so that ES6 |
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.
Oh sweet, I think this will make the typescript compiler happier. Did you copy it from closure, or write it yourself?
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.
Yes: this is copied straight over. I didn't end up using it in this PR, but we will need it to do goog.declareModuleId
in ES modules, if we are to comply with the recommended approach.
The only other file that uses
@alschmiedt are we getting any value from the test themes at this point? Are they part of our testing steps? If not, let's remove them. |
The basics
The details
Resolves
Part of #5904.
Proposed Changes
Change how
tests/playground.html
loads Blockly.Behaviour Before Change
playground.html
would use a<script>
tag to loadtests/playgrounds/load_all.js
<script>
tag to loadblockly_uncompressed.js
from the project root<script>
tags to loadbase.js
anddeps.js
and then finally do agoog.require('Blockly')
call to get the debug module loader to loadcore/*
.load_all.js
would use further<script>
tags to loadmsg/messages.js
,tests/themes/test_themes.js
and@blockly/block-test
.<body onload="...">
would call thestart
function inplayground.html
.Behaviour After Change
playground.html
will use a<script>
tag to loadtests/playgrounds/prepare.js
<script>
tags to loadbase.js
,deps.js
, andmsg/messages.js
.window.BlocklyLoader
, that will:goog.bootstrap()
, passing in a list of the top-level entrypoints (Blockly
,Blockly.blocks.all
,Blockly.Javascript.all
, etc.)msg/messages.js
from where they were temporarily stored intoBlockly.Msg
.Blockly
module exports object.playground.html
will then use an inline<script type=module>
toimport
tests/playgrounds/blockly.mjs
,await
the resolution of the loader Promise, andexport
the returned value.start()
directly, which is safe because the page will have been loaded by this point.(In compiled mode,
prepare.js
will loadblockly_compressed.js
et al. using the same implementation asload_all.js
did previously.)Reason for Changes
Because the debug loader loads ES modules using
<script type=module>
tags, their loading is deferred until after all the top level (non-module
)<script>
tags have been run. This means that any ES modules (and anygoog.module
s that depend on them, including the rootBlockly
module) will not be loaded in time to callBlockly.inject
from the start script.Test Coverage
Tested on:
Also passes
npm test
.Documentation
Once the dust has settled, we probably ought to update any documentation that we have that discusses loading Blockly in uncompiled mode since any previous sample code snippets will not work.
Additional Information & questions
tests/playground.html
, not any of the other playgrounds, tests, samples, etc.Blockly.Msg
had been populated, but moving thegoog.bootstrap
call fromblockly.mjs
(executed after page has loaded) toprepare.js
seems to fixed this problem as well.tests/themes/test_themes.js
and@blockly/block-test
as these were not being loaded in compiled mode.