This example demonstrates how to use Rspack for generating code at build time and executing it at runtime.
In this example, we use Rspack to generate a JavaScript file at build time that contains a dynamic function. This function is then executed at runtime.
-
CodeGenPlugin.js:
- This is a custom Rspack plugin responsible for generating JavaScript code at build time.
- The plugin creates a
generated.js
file inside thedist
folder. - The generated code exports a simple function
dynamicGreeting
which logs a greeting message.
-
Rspack Configuration:
- The plugin is added to the
rspack.config.js
file under theplugins
section. - The configuration ensures that the plugin runs at the appropriate phase during the Rspack build process.
- The plugin is added to the
-
Generated Code (
generated.js
):- This file contains the generated
dynamicGreeting
function, which will be executed at runtime. - The code is simple: it logs a greeting message with a dynamic input.
- This file contains the generated
-
Runtime Execution (
index.js
):- The main code in
index.js
imports thedynamicGreeting
function fromgenerated.js
and executes it. - The
generated.js
file is dynamically injected by the plugin at build time. - The
dynamicGreeting
function is called during runtime to display a greeting message.
- The main code in
- Rspack compiles the project using the custom plugin.
- At build time, the plugin generates
generated.js
in thedist
folder. - When running the application,
index.js
imports the generated file and executes the function, which prints a dynamic greeting message to the console.
- CodeGenPlugin: Generates the
generated.js
file. - Rspack Configuration: Ensures the plugin is executed during the build process.
- Generated Code: Contains the
dynamicGreeting
function, which is imported and executed at runtime.
This setup allows code generation at build time, with the generated code being injected into the project and executed during runtime.
- Clone the repository.
- Run
yarn build
to generate the code. - Run
yarn start
to execute the application and see the greeting message in the console.