[v4.0] Switch parser to SWC and introduce native/WASM code#5073
[v4.0] Switch parser to SWC and introduce native/WASM code#5073lukastaegert merged 208 commits intorelease-4.0from
Conversation
…rtDefaultExpression
…PatternProperty, ArrayLiteral, ImportExpression
Start sorting AST nodes
…WhileStatement, EmptyStatement
|
This PR has been released as part of rollup@4.0.0-19. Note that this is a pre-release, so to test it, you need to install Rollup via |
|
This PR has been released as part of rollup@4.0.0-20. Note that this is a pre-release, so to test it, you need to install Rollup via |
|
This PR has been released as part of rollup@4.0.0-21. Note that this is a pre-release, so to test it, you need to install Rollup via |
|
This PR has been released as part of rollup@4.0.0-22. Note that this is a pre-release, so to test it, you need to install Rollup via |
|
This PR has been released as part of rollup@4.0.0-23. Note that this is a pre-release, so to test it, you need to install Rollup via |
|
This PR has been released as part of rollup@4.0.0-24. Note that this is a pre-release, so to test it, you need to install Rollup via |
|
This PR has been released as part of rollup@4.0.0-25. Note that this is a pre-release, so to test it, you need to install Rollup via |
|
This PR has been released as part of rollup@4.0.0. You can test it via |
This PR contains:
Are tests included?
Breaking Changes?
List any relevant issue numbers:
Description
This is a massive undertaking and there is still a lot to do. The goal is to switch the parser from
acorntoswcwhile actually improving performance. Unfortunately, this does not mean using the JavaScript bindings of swc. At least in my experience, the cost of serializing an deserializing the complex AST nearly eats up the performance benefits of switching the parser. Moreover, swc has a very different AST (and even if you use the ESTree compat module, it is still a Babel AST and not an ESTree AST). Also, and this is critical, it uses utf-8 file positions while Rollup relies on the standard JavaScript utf-16 positions.Instead, the approach is to directly use the Rust bindings of swc and convert the AST to a binary format before passing it as an (Array)Buffer to JavaScript. While doing this, we account for the AST differences and correct the file positions. Passing an ArrayBuffer is basically a free operation, so we just need to teach the JavaScript side to work on the ArrayBuffer. Also, the buffer is only about a third the size of stringified JSON. And last, this will enable us to easily pass ASTs to different threads or rather, do the parsing in a WebWorker and pass the buffer to the main thread for free.
To interact with the Rust code, I use napi-rs in NodeJS and will probably use wasm-pack for the browser build.
For now, the actual conversion is done. As a by-product of the process, the custom swc AST is converted to an ESTree-compliant AST, which has been verified by deep comparison with the acorn AST for all Rollup test cases.
The goal is to make this the core change in Rollup 4.
There is a lot that needs to be done, though, before this can even be tested.
Most critical changes to enable external testing
Critical for release
@rollup/browserwith included WASM filenpm install github/branchworks again across environments. If necessary, add instructions how to install rustup nightly to PR comment. Remove browser build frompreparescript to make this process faster.Not critical but should be part of Rollup 4.0 to avoid a breaking change
Nice to have but not critical
this.parseAPI with an asynchronous alternative that does not create the actual AST and supports very fast tree-walking via special APIs.@rollup/browser.BREAKING CHANGES
acornInjectPluginsoption has been removedacornoption has been removedthis.parseoutput.externalImportAssertionshas been deprecated in favor ofoutput.externalImportAttributesINVALID_IMPORT_ASSERTIONerror code has been replaced withINVALID_IMPORT_ATTRIBUTE