-
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
feat(scripts): Create script to help with CJS -> ESM migration #8197
Conversation
This is based on js2ts, but considerably simplified since we no longer need to deal with goog.module IDs.
Marking this draft as I want to make a few changes to deal with a few common syntax styles that we use in our CJS modules that we didn't seem to use (or at least: not frequently) in our goog.modules—notably |
Sweet sweet, please send me a ping when this reopens b/c GitHub doesn't notify! |
Support (optionally renaming) assignments to module.exports, in addition to the existing support for simple exports property assignmenets.
OK, this is now ready for review. |
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.
No functional comments! Just some Qs, some docs, and some spellings.
scripts/migration/cjs2esm
Outdated
const requireRE = | ||
/(?:const\s+(?:([$\w]+)|(\{[^}]*\}))\s+=\s+)?require\('([^']+)'\);/g; | ||
|
||
/** RegExp matching key: value pairs in destructuring assignments. */ | ||
const keyValueRE = /([$\w]+)\s*:\s*([$\w]+)\s*(?=,|})/g; | ||
|
||
/** Prefix for RegExp matching a top-level declaration. */ | ||
const declPrefix = | ||
'(?:const|let|var|(?:async\\s+)?function(?:\\s+\\*)?|class)'; |
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'm just gonna trust this is correct.
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 guess I could write …|function|async function|function *|async function *|…
Though actually I just noticed an error: the space between function
and *
in generator decls is optional.
if (moduleName[0] === '.') { | ||
// Relative path. Could check and add '.mjs' suffix if desired. | ||
} |
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.
This is mean to be empty?
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. There is a bit of optional code that I might write to save having to fix up import paths manually—the only difficulty is that sometimes the imported filename will be an .mjs
and other times it will just be .js
, and not even testing file existence can necessarily distinguish because the file in question might be mid-conversion by the same invocation of this tool. So I left this as a placeholder for the moment.
The basics
The details
Proposed Changes
Create script to help with CJS -> ESM migration.
Reason for Changes
We need to finish migrating tests to ESM as the latest version of chai no longer supports loading via require()—see PR #8092.
Test Coverage
Some very cursory manual testing of this script has been done. No changes to testing of Blockly anticipated.
Additional Information
This is based on js2ts, but considerably simplified since we no longer need to deal with goog.module IDs.