Skip to content

refactor(migrations): support use of an ESM @angular/compiler-cli package#43657

Closed
clydin wants to merge 6 commits into
angular:masterfrom
clydin:esm/migrations
Closed

refactor(migrations): support use of an ESM @angular/compiler-cli package#43657
clydin wants to merge 6 commits into
angular:masterfrom
clydin:esm/migrations

Conversation

@clydin

@clydin clydin commented Sep 30, 2021

Copy link
Copy Markdown
Member

Currently, migrations and schematics must be in CommonJS format. However, framework packages will only be ESM from v13 and onward. To support this configuration, dynamic import expressions are now used to load @angular/compiler-cli and its new secondary entry point @angular/compiler-cli/private/migrations. Dynamic imports within Node.js allow the @angular/core migrations’ CommonJS code to load ESM code. Unfortunately, TypeScript will currently, unconditionally down-level dynamic import into a require call. require calls cannot load ESM code and will result in a runtime error. To workaround this, a Function constructor is used to prevent TypeScript from changing the dynamic import. Once TypeScript provides support for keeping the dynamic import this workaround can be dropped and replaced with a standard dynamic import. Due to the use of the dynamic import, a reference to the dynamically loaded modules must now be passed to all locations that use values from the modules.

@clydin clydin added target: major This PR is targeted for the next major release area: migrations Issues related to `ng update`/`ng generate` migrations labels Sep 30, 2021
@ngbot ngbot Bot modified the milestone: Backlog Sep 30, 2021
@google-cla google-cla Bot added the cla: yes label Sep 30, 2021
@pullapprove pullapprove Bot requested a review from devversion September 30, 2021 20:44

@devversion devversion left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM. Hopefully we can use ESM for the migrations in the future as this would simplify things a lot.

@devversion devversion added action: merge The PR is ready for merge by the caretaker action: presubmit The PR is in need of a google3 presubmit labels Sep 30, 2021
@alxhub alxhub modified the milestones: Backlog, v13 Feature Freeze Oct 1, 2021
devversion added a commit to devversion/angular that referenced this pull request Oct 1, 2021
Temporarily disables the ng_update_migrations integration test
until angular#43657 lands.
dylhunn pushed a commit that referenced this pull request Oct 1, 2021
Temporarily disables the ng_update_migrations integration test
until #43657 lands.

PR Close #43431
…ackage

Currently, migrations and schematics must be in CommonJS format. However, framework packages will only be ESM from v13 and onward. To support this configuration, dynamic import expressions are now used to load `@angular/compiler-cli` and its new secondary entry point `@angular/compiler-cli/private/migrations`. Dynamic imports within Node.js allow the `@angular/core` migrations’ CommonJS code to load ESM code. Unfortunately, TypeScript will currently, unconditionally down-level dynamic import into a require call. `require` calls cannot load ESM code and will result in a runtime error. To workaround this, a Function constructor is used to prevent TypeScript from changing the dynamic import. Once TypeScript provides support for keeping the dynamic import this workaround can be dropped and replaced with a standard dynamic import.  Due to the use of the dynamic import, a reference to the dynamically loaded modules must now be passed to all locations that use values from the modules.
@clydin clydin added action: merge The PR is ready for merge by the caretaker action: presubmit The PR is in need of a google3 presubmit and removed action: merge The PR is ready for merge by the caretaker action: presubmit The PR is in need of a google3 presubmit labels Oct 1, 2021
@pullapprove pullapprove Bot requested a review from alxhub October 4, 2021 16:16
clydin added 2 commits October 4, 2021 12:17
The `@angular/core` migrations are currently published as CommonJS and also not bundled. Schematics, of which migrations are a type, are currently required to be CommonJS modules due to the Schematics Runtime not yet supporting ESM-based schematics. To ensure that the migration files are loaded as CommonJS modules, a nested `package.json` file has been introduced within the `schematics` directory of the package to set the `type` field for all containing JavaScript files as CommonJS. Since the migrations are also not currently bundled and the `devmode` output used to build the migrations will replace all relative imports with fully-qualified module specifiers, an additional `exports` entry must be added to allow the fully-qualified module specifiers to correctly resolve. If `devmode` did not change the relative imports the additional entry would not be needed. Bundling would also remedy the situation and is the long-term path, especially once the migrations are converted to ESM. Additional followup changes should investigate bundling each migration. The additional `exports` entry should be removed if either `devmode` behavior is changed or the migrations are bundled.
@clydin clydin removed the action: presubmit The PR is in need of a google3 presubmit label Oct 4, 2021

@devversion devversion left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Still LGTM!

Comment thread packages/core/schematics/utils/load_esm.ts Outdated
@alxhub alxhub added the merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note label Oct 4, 2021
@alxhub

alxhub commented Oct 4, 2021

Copy link
Copy Markdown
Member

Caretaker: cl/400826396 is a necessary update to the g3 patch for this change. Please coordinate with me to land these together. We will also need a -next release after this lands.

@dylhunn

dylhunn commented Oct 4, 2021

Copy link
Copy Markdown
Contributor

This PR was merged into the repository by commit 6d1c2f7.

@dylhunn dylhunn closed this in e0015d3 Oct 4, 2021
dylhunn pushed a commit that referenced this pull request Oct 4, 2021
)

The `@angular/core` migrations are currently published as CommonJS and also not bundled. Schematics, of which migrations are a type, are currently required to be CommonJS modules due to the Schematics Runtime not yet supporting ESM-based schematics. To ensure that the migration files are loaded as CommonJS modules, a nested `package.json` file has been introduced within the `schematics` directory of the package to set the `type` field for all containing JavaScript files as CommonJS. Since the migrations are also not currently bundled and the `devmode` output used to build the migrations will replace all relative imports with fully-qualified module specifiers, an additional `exports` entry must be added to allow the fully-qualified module specifiers to correctly resolve. If `devmode` did not change the relative imports the additional entry would not be needed. Bundling would also remedy the situation and is the long-term path, especially once the migrations are converted to ESM. Additional followup changes should investigate bundling each migration. The additional `exports` entry should be removed if either `devmode` behavior is changed or the migrations are bundled.

PR Close #43657
@clydin clydin deleted the esm/migrations branch October 4, 2021 23:38
@angular-automatic-lock-bot

Copy link
Copy Markdown

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators Nov 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

action: merge The PR is ready for merge by the caretaker area: migrations Issues related to `ng update`/`ng generate` migrations cla: yes merge: caretaker note Alert the caretaker performing the merge to check the PR for an out of normal action needed or note target: major This PR is targeted for the next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants