Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Support executing ESM schematics

未关闭
#22,786 7 条评论 61 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
35/100
Issue 类型
功能
描述清晰度
基本清楚
活跃度
停滞
技术栈
angular, typescript
领域
cli

调研方向

首先跟踪编译和执行自定义 schematics 的 Angular CLI 路径,然后将其与所引用的 Angular loadEsmModule() workaround 以及链接的 schematics 示例进行比较。完成标准是 ESM schematics 可以在不出现报告中的 CommonJS require() 错误的情况下执行,并且有针对该行为的测试覆盖。

由索引模型根据 Issue 内容生成。

描述

area: @angular-devkit/schematics feature feature: under consideration

🚀 Feature request

Support schematics written in ES Modules. Currently only CommonJS schematics are supported.

Command (mark with an x)
  • new
  • build
  • serve
  • test
  • e2e
  • generate
  • add
  • update
  • lint
  • extract-i18n
  • run
  • config
  • help
  • version
  • doc
  • other: compiling custom schematics
Description

Currently custom (and official) schematics can execute only CommonJS code, but not ES Modules. This is problematic when you want to import an ES Module in your custom schematics, for example HtmlParser from @angular/compiler (which since ng13 is shipped only as ESM).

Problems with not supporting ESM:

The following error started to occur for our custom schematics in runtime, since we upgraded from ng12 to 13:
require() of ES Module <path-to-the-repo>/node_modules/@angular/compiler/fesm2015/compiler.mjs not supported. Instead change the require of <path-to-the-repo>/node_modules/@angular/compiler/fesm2015/compiler.mjs to a dynamic import() which is available in all CommonJS modules.

But the advised change of the static @angular/compiler to a dynamic import is cumbersome due to various reasons:

  • it unnecessarily changes the our synchronous code to asynchronous code
  • it induces passing down the compilerModule (result of await import(@angular/compiler)) as an argument down to all helper functions (example in angular repo)
  • it requires a hacky workaround ticking the Typescript compiler so to avoid compiling the dynamic import() into the require() call - for more see the workaround function loadEsmModule() in angular repo . Btw. this function is not exported in public API, so we need to copy-paste it to our repo.
Describe the solution you'd like

Support executing ESM schematics .

Describe alternatives you've considered

Change the import of ESM to dynamic imports in our schematics code and deal with all its cumbersome consequences (described above).

主要语言
TypeScript
星标
27k
派生
11.8k
平均合并
16 小时 35 分钟
30 天内合并 PR
176

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

angular/angular-cli 的其他 Issue

查看 angular/angular-cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。