RFC-010: Runtime ( Module Loader & Generation) #467
Closed
liuliudada-w
started this conversation in
RFC
Replies: 2 comments
-
Good job, but I have a few questions about the technical design:
It will be nice to shed more light on the existing implementation of wepack like what the splitting of runtime plugins try to do and how they are going to solve the problem for wepack other than the different of the Rspack after all as I don't quite get much of the context of wepack's implementation. |
Beta Was this translation helpful? Give feedback.
0 replies
-
should runtime depends on promise, otherwise it's must loaded after promise polyfill loaded |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Summary
This RFC contains two parts
Motivation
Runtime is necessary for many web build tools, because js module system is complex, build tools need an independent module loader to unify different module behaviors. Since the Runtime module of webpack has been tested for a long time, the Runtime module of rspack will be based on webpack, and on this basis, it will be optimized for the special situation of rspack and some actual business conditions.
Detailed design
Runtime Code Content
Core Structure and Data Structure
After the Runtime code is executed, a Runtime instance will be injected into the top-level scope (web is usually window), and different Runtime instances will be isolated, so as to ensure that the compilation products of multiple projects can run at the same time (micro front-end), all module information , chunk information, functional functions and more are all mounted on the runtime instance. Runtime will support module loading and execution, asynchronous Chunk loading and execution, hmr client, Lazy Compile client and other functions.
Process of executed
When the page is loaded for the first time, the runtime module code will be loaded first to ensure that the initialization of the runtime instance is completed. Then load each JS file, each JS file will execute the rspack_register function in chunks, and executing the rspack_register function will register the module in the Chunk into the installedModule. The entry module will provide a callback function to be executed after the registration is completed. The callback function will execute rspack_require on the entry Module, and then execute the corresponding rspack_require according to the code execution order.
Dynamic Chunk will execute the rspack_dynamic_require function when the code is executed to the corresponding part, and pass in the requested ChunkID. The function will find the corresponding hash in chunkHashData according to the incoming ChunkID and determine the URL of the Chunk, and then execute the corresponding loading function according to the type of Chunk, and generate the corresponding HTML tag to complete the loading. After the Chunk is loaded, the Promise.then operation will be executed at the original code execution place, and rspack_require will be executed to request the required Module.
HMR is supported by the dev server. After the developer modifies the code, the dev server will monitor file changes and trigger compilation, and return the updated result to the client through websocket. The client will re-execute the code and replace installedModules and moduleCache module information, and then refresh according to different hmr strategies(react refresh or full reload)
Lazy Compile will take effect on the dynamic import module in dev mode, will generate a virtual module for the module that only contains the loading function, and add it to the Chunk as a sync module. When the code is executed to the corresponding module, the loading function in the virtual Module will be executed first. The loading function will request the dev server and return the Module information. The dev server will trigger the compilation process of the actual code of the Module according to the information. After the compilation is completed, the result of this compilation will be returned, and the corresponding Module information will be updated by hmr, and the virtual Module will be replaced by the real Module.
Runtime Generate
Runtime code is mainly generated on the Rust side. The main process is similar to webpack. Use rspack-sources to assemble the corresponding Runtime code segment according to the user configuration and compilation mode, and finally generate the Runtime module and insert it before the code entry for execution.
The general runtime code will converge into rspack_runtime_plugin as a whole. For other special types of runtime code, each plugin needs to implement its own runtime hook, and rspack will accept the return value of the runtime hook as the final generated content of the runtime.
Prior Art
Basic runtime
First, load all modules to webpack_modules object, then the process will execute the entry to load and execute the actual modules
Async load, the following runtime will be added
Difference from webpack
Although generally similar to Webpack, rspack has been adjusted according to the actual situation, so the following points are different from the default behavior of webpack
Existing questions and discussions
Beta Was this translation helpful? Give feedback.
All reactions