-
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 import
instead of goog.bootstrap
to load Blockly in mocha tests
#7406
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We need to create a build/package.json file to allow node.js to load build/src/core/blockly.js and the other chunk entry points as ES modules (it forcibly assumes .js means CJS even if one is trying to import, unless package.json says {"type": "module"}), but this interferes with scripts/migration/js2ts doing a require('build/deps.js'), which is _not_ an ES module. Specific error message was: /Users/cpcallen/src/blockly/scripts/migration/js2ts:56 require(path.resolve(__dirname, '../../build/deps.js')); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/cpcallen/src/blockly/build/deps.js from /Users/cpcallen/src/blockly/scripts/migration/js2ts not supported. deps.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules. Instead rename deps.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/cpcallen/src/blockly/build/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead). at Object.<anonymous> (/Users/cpcallen/src/blockly/scripts/migration/js2ts:56:1) { code: 'ERR_REQUIRE_ESM' }
These modules were depending on being loaded via the debug module loader, which cannot be used without first loading base.js as a script, and thereby defining goog.declareModuleId as a side effect—but if they are to be loaded via direct import statements then they need to actually import their own dependencies. This is a temporary measure as soon the goog.declareMouleId calls can themselves be deleted.
This file was only needed by tests/mocha/index.html's use of the debug module loader (via bootstrap.js), which has now been removed.
These were only needed because these modules were previously being loaded by goog.require and/or goog.bootstrap.
We are fully committed to proper modules now.
This is a lot, but commit-by-commit it all makes sense. If you have not already done so, please make sure that everything still works/tests pass after running |
rachel-fenichel
approved these changes
Aug 17, 2023
cpcallen
added a commit
to cpcallen/blockly
that referenced
this pull request
Aug 20, 2023
This was deleted in PR google#7406 as it was mainly being used to filter core/ vs. test/mocha/ deps into separate deps files - but it turns out also to be used for filtering error messages too. Oops.
1 task
cpcallen
added a commit
that referenced
this pull request
Aug 30, 2023
* fix(build): Restore erroneously-deleted filter function This was deleted in PR #7406 as it was mainly being used to filter core/ vs. test/mocha/ deps into separate deps files - but it turns out also to be used for filtering error messages too. Oops. * refactor(tests): Migrate advanced compilation test to ES Modules * refactor(build): Migrate main.js to TypeScript This turns out to be pretty straight forward, even if it would cause crashing if one actually tried to import this module instead of just feeding it to Closure Compiler. * chore(build): Remove goog.declareModuleId calls Replace goog.declareModuleId calls with a comment recording the former module ID for posterity (or at least until we decide how to reformat the renamings file. * chore(tests): Delete closure/goog/* For the moment we still need something to serve as base.js for the benefit of closure-make-deps, so we keep a vestigial base.js around, containing only the @provideGoog declaration. * refactor(build): Remove vestigial base.js By changing slightly the command line arguments to closure-make-deps and closure-calculate-chunks the need to have any base.js is eliminated. * chore: Typo fix for PR #7415
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The basics
npm run format
andnpm run lint
The details
Resolves
Part of #6858.
Proposed Changes
Have the
buildShims
task clean up after itself.We need to create a
build/package.json
file to allow node.js to loadbuild/src/core/blockly.js
and the other chunk entry points as ES modules (it otherwise forcibly assumes.js
means CJS even if one is trying toimport
the file rather thanrequire
it, unlesspackage.json
says{"type": "module"}
), but this interferes withscripts/migration/js2ts
doing arequire('build/deps.js')
, sincedeps.js
is not an ES module.Temporarily add missing
import
s ofclosure/goog/goog.js
to modules intests/mocha/
(see de9499b for rationale).Migrate
tests/mocha/index.html
toimport
the loader shims instead of usingbootstrap.js
.import
to load Blockly, the library blocks and the JavaScript generator via the shims create in PR refactor(tests): Introduce loading shims, use in playgrounds #7380. Because the mocha tests have always been run in uncompressed mode it could instead directly import the chunk entry point s (e.g.build/src/core/blockly.js
), but using the shims allows us to optionally run the tests in compressed mode as well. At the moment they do not all pass, because several tests directly import individual modules fromcore/
, which is not compatible withblockly_compressed.js
, but Use Blockly consistently in mocha tests #7224 is tracking work that would resolve this problem.import
to load the individual test files that were originallygoog.require()
ed and more recently loaded viabootstrap.js
.loadScript
(fromtests/scripts/load.mjs
) to load various additional scripts includingen.js
.Remove code from
build_tasks.js
that generatestests/deps.mocha.js
, since it was only needed by the bootstrap code now deleted fromtests/mocha/index.html
.Remove
goog.declareModuleId
calls (and addedimport
s) from alltests/mocha/**/*.js
.Remove some dead code from
tests/mocha/test_helpers/serialization.js
.Behaviour Before Change
Mocha tests run and 2994/2994 (100%) tests pass in uncompressed mode.
Behaviour After Change
Mocha tests run and 2994/2994 (100%) tests pass in uncompressed mode.
Moreover, tests can optionally be run in compressed mode, with 2174/2994 (97%) passing.
Reason for Changes
Remove dependency on
bootstrap.js
so it and the Closure debug module loader can be deleted.Test Coverage
Unchanged.
Additional Info
Due to the large number of files touched it may be easier to review commit-by-commit.