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

chore(generators): Migrate generators to ES Modules #7103

Merged
merged 5 commits into from
May 19, 2023

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
    • (Though formatting is temporarily disabled in generators/.)

The details

Resolves

Part of #6828 and #6858.

Proposed Changes

  • Add support for migrating renaming imports to js2ts: convert

    const {foo: bar} = require(/*...*/);

    into

    import {foo as bar} from /*...*/;
            ^^^^^^^^^^

    Also fix a bug that caused relative paths to ESM in the same directory to be missing a leading ./.

  • Migrate all the goog.module modules in generators/ (that is: all .js files in that subtree) to ES modules, replacing goog.require with import.

    • This was done mostly with js2ts then renaming the .ts file back to .js, with some small manual fixup.
    • Since tsc does not permit import type statements in .js files, such statements generated by js2ts are commented out for now.

Behaviour Before Change

These modules are loaded as goog.modules.

Behaviour After Change

These modules are loaded as ES modules.

Reason for Changes

This is the first step of migrating these files to TypeScript, and also the only part of the TypeScript migration required in order to complete (their part) of the work to remove dependencies on the Closure module system (except for goog.declareModuleId calls, which are still required due to a couple of the tests still using goog.require).

Test Coverage

Passes npm test and quick manual check that generators are not completely broken in the playground. No changes to manual testing anticipated.

Documentation

Should be (approximately) no visible external changes.

Additional Information

As usual with such migrations there are some changes to the internal names generated by Closure Compiler. This necessitated some small changes to the chunk configuration in build_tasks.js and will break devs who monkey patch
non-public generator methods.

There are some additional details discussed in the individual commit messages.

cpcallen added 5 commits May 19, 2023 12:03
Convert

    const {foo: bar} = require(/*...*/);

into

    import {foo as bar} from /*...*/;
            ^^^^^^^^^^

Also fix a bug that caused relative paths to ESM in the same
directory to be missing a leading "./".
The UMD wrapper was inadvertently exporting the contents of (e.g.)
the Blockly.JavaScript closure module rather than the intended
export of Blockly.JavaScript.all module - which went unnoticed
because the latter just reexported the former - but we are
about to convert the former to ESM.
Migrate the main language generators in generators/*.js to ESM.

This was done by running js2ts on the files, renaming them back
to .js, and commenting out "import type" statements, which are
legal TS but not needed in JS (at least if you are not actually
letting Closure Compiler do type checking, which we are not.)
Migrate generators/*/*.js (except all.js) to ESM.

This was done by running js2ts on the files, renaming them back
to .js, and removing now-spurious @Suppress {extraRequire}
directives.
This was done by running js2ts on the files, renaming them back
to .js, and manually fixing the export statements.

An additional change to the chunk exports configuration in
build_tasks.js was necessary in order for the UMD wrapper to
find the new module object, which is given a different name
than the old exports object.
@cpcallen cpcallen added component: generators PR: chore General chores (dependencies, typos, etc) labels May 19, 2023
@cpcallen cpcallen requested a review from a team as a code owner May 19, 2023 13:38
@cpcallen cpcallen requested a review from BeksOmega May 19, 2023 13:38
@github-actions github-actions bot added PR: chore General chores (dependencies, typos, etc) and removed PR: chore General chores (dependencies, typos, etc) labels May 19, 2023
@rachel-fenichel
Copy link
Collaborator

@maribethb asked if this will change anything about how devs import generators.

@cpcallen
Copy link
Contributor Author

@maribethb asked if this will change anything about how devs import generators.

No, absolutely not as far as I am aware. The proposed changes to <script> imports will be in a later PR.

@cpcallen cpcallen merged commit 4d2201a into google:develop May 19, 2023
@cpcallen cpcallen deleted the fix/6828/generators/esm branch May 19, 2023 22:09
@BeksOmega BeksOmega assigned cpcallen and unassigned BeksOmega May 31, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component: generators PR: chore General chores (dependencies, typos, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants