Description
🚀 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 ofawait 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 therequire()
call - for more see the workaround functionloadEsmModule()
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).