Kotlin/JS codegen compiler extension #4925
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation
Currently, the code generation process (IR to JS conversion) is fixed and cannot be modified by plugins. This on the one hand leaves flexibility for compiler developers, but at the same time does not allows to customize transformations at later stages of compilation.
For example, currently, there is no way to generate multiple .js files for one module. Such abilities a very useful in some scenarios like Web Workers API.
This PR aims to provide some basic implementation for such extension points.
Safe harbor
It's also worth noting that the changes I'm suggesting don't consider many edge cases - for example, incremental compilation, bundle size, etc. Therefore, I propose to join the discussion to design a better solution that considers various use cases.
The main question - why can't this be done at the IR level? This is due to the fact that in some situations, plugin developers need the ability to perform platform optimizations and have access to the infrastructure of the corresponding backend.
As far as I know, the compiler has bytecode post-processing for the JVM platform, but this tool is not available for plugins. There is no such functionality in the JS IR backend (to be more precise, it is not highlighted as an explicit post-processing stage).
Proposed compiler changes
The following must be taken into account here:
Worker
constructor accepts script URL. It is worth mentioning that support for web workers is a separate issue and for now I will not touch on the details of implementing their support in Kotlin.Use cases
The main reason to inject into code generation process - there is currently no way to emit multiple .js files for a single module. The community needs this functionality because its absence makes it difficult to implement Kotlin/JS in the following scenarios:
importScripts(...)
to include dependencies in worker context.Samples
This PR also includes a sample plugin to demonstrate the process of generating multiple .js files for a single module.