Skip to content

Customizing a loader

Aritra Karak edited this page Oct 14, 2023 · 1 revision

Let's customize the behavior of the built-in Zig loader and make it log the the path of the source file every time it is run and log whenever the source file is built.

  1. Create a new file called myZigLoader.ts.

  2. Inside we'll extend the built-in zig loader and swap out implementations with our custom ones by taking advantage of inheritance.

    import { ZigLoader } from "hyperimport";
    export default class extends ZigLoader {
        name = "My Zig Loader";
        async build() {
            console.log("Building the source file...");
            super.build();
            console.log("Successfully built the source file!");
        }
        async preload() {
            super.preload();
            console.log("Importing", this.config.importPath, "...");
        }
    }
  3. Now we'll add this to custom in the hyperimport config.

    [hyperimport]
    custom = ["./myZigLoader.ts"]
  4. Import your zig file and run bun . to run index.ts.

  5. You'll find everytime the file is imported, it will log the message.

  6. On changing the source file, the source file is rebuilt and that is too logged in the console.

Clone this wiki locally