Skip to content
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!: Remove remaining use of goog.module.declareLegacyNamespace. #6254

Merged
merged 9 commits into from
Jun 30, 2022

Conversation

cpcallen
Copy link
Contributor

The basics

  • I branched from develop
  • My pull request is against develop
  • My code follows the style guide
  • I ran npm run format and npm run lint

The details

Resolves

Prerequisite to #5904 and #5857.

Proposed Changes

  • Convert generators to named exports.
  • Remove remaining goog.module.declareLegacyNamespace calls. (There was one for each chunk entry point.)
  • Changes to build system, bootstrap loader and code in tests/ to keep everything working.

Behaviour Before Change

Loading the library blocks or a generator module/chunk creates a new property on Blockly (e.g.., Blockly.JavaScript).

Behaviour After Change

Such a load only creates a new property on Blockly if it is loaded from the <chunk>_compressd.js file using a <script> tag.

Reason for Changes

Removing goog.module.declareLegacyNamespace is a prerequisite to converting a goog.module into an ES or TS module.

Test Coverage

  • Passes npm test.
  • tests/playground.html and tests/multi_playground.js load correctly and appear to be fully functional.
    • The advanced playground fails to load but for this was already broken in develop and will be fixed separately.

Documentation

We need to make sure that our documentation and release notes indicate that if you need to use a generator you now need to use a named import:

import * as Blockly from 'blockly';
import {javascriptGenerator} from 'blockly/javascript';

Additional Information

As of this PR, the backward-compatibility Blockly.JavaScript properties are only created when loading the corresponding chunk using a <script> tag. If desired, we can (perhaps in a separate PR) modify the UMD wrapper so that these properties are always created, even when loading using (e.g.) a side-effects-only import 'blockly/javascript'.

cpcallen added 9 commits June 29, 2022 19:22
- Use TSC_OUTPUT_DIR to find goog/goog.js when suppressing warnings.
- Remove unnecessary trailing semicolons.
Remove the call to goog.module.declareLegacyNamespace from
Blockly.libraryBlocks.  This entails:

- Changes to the UMD wrapper to be able to find the exports object.
- Changes to tests/bootstrap_helper.js to save the exports object
  in the libraryBlocks global variable.
- As a precaution, renaming the tests/compile/test_blocks.js module
  so that goog.provide does not touch Blockly or
  Blockly.libraryBlocks, which may not exist / be writable.
We need to convert the generators to named exports.  For backwards
compatibility we still want e.g. Blockly.JavaScript to point at
the generator object when the chunk is loaded using a script tag.

Modify chunkWrapper to honour a .reexportOnly property in the
chunks table and generate suitable additional code in the UMD
wrapper.
- Export the JavaScript generator object as javascriptGenerator
  from the Blockly.JavaScript module(generators/javascript.js).

- Modify the Blockly.JavaScript.all module
  (generators/javascript/all.js) to reexport the exports from
  Blockly.JavaScript.

- Update chunk configuration so the generator object remains
  available as Blockly.JavaScript when loading
  javascript_compressed.js via a <script> tag.

  (N.B. it is otherwise necessary to destructure the require
  / import.)

- Modify bootstrap_helper.js to store that export as
  window.javascriptGenerator for use in test code.

- Modify test code to use javascriptGenerator instead of
  Blockly.JavaScript.

- Modify .eslintrc.json so that javascriptGenerator is allowed
  as a global in test/.  (Also restrict use of Blockly global
  to test/.)

N.B. that demo code in demos/code/code.js uses <script> tag
loading and so will continue to access Blockly.JavaScript.
Remove the goog.module.declareLegacyNamespace calls from the
generators.

This turns out to have the unexpected side-effect of causing the
compiler to rename the core/blockly.js exports object from
$.Blockly to just Blockly in blockly_compressed.js - presumably
because it no longer needs to be accessed in any subsequent chunk
because they no longer add properties to it.  This requires
some changes (mainly simplification) to the chunkWrapper function
in build_tasks.js.
So easy to do _now_: just need to:

- Make sure the UMD wrapper for the first chunk knows where the
  exports object is.
- Use that same value to set the Blockly.VERSION @define.
- Have bootstrap_helper.js set window.Blockly to the exports
  object.
- Fix tests/compile/test_blocks.js to not assume a Blockly
  global variable, by converting it to a goog.module so we
  can use a named require.
@cpcallen cpcallen added component: build process breaking change Used to mark a PR or issue that changes our public APIs. PR: chore General chores (dependencies, typos, etc) labels Jun 30, 2022
@cpcallen cpcallen requested a review from a team as a code owner June 30, 2022 16:47
@cpcallen cpcallen requested a review from BeksOmega June 30, 2022 16:47
Copy link
Collaborator

@BeksOmega BeksOmega left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One Q. Otherwise LGTM! So exciting :DDD

tests/bootstrap_helper.js Show resolved Hide resolved
@cpcallen cpcallen merged commit f947b3f into google:develop Jun 30, 2022
@cpcallen cpcallen deleted the wrappers/no-declareLegacyNamespace branch June 30, 2022 18:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Used to mark a PR or issue that changes our public APIs. component: build process PR: chore General chores (dependencies, typos, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants