$ show JuliaPackaging/Requires.jl
Requires.jl is a Julia library that provides a mechanism for conditional code loading based on the presence of other packages. It exports the `@require` macro, which allows package authors to define glue code that only executes when a specific dependency is loaded, avoiding explicit hard dependencies and reducing load times. The library hooks into Julia's internal package loading system via `Base.package_callbacks` and uses a callback pattern to defer execution. It also includes an `@init` macro for deferred initialization and an `@include` macro that caches file content at compile time for use in system images. The package is widely used in the Julia ecosystem to manage optional dependencies and is considered a precursor to Julia's native package extensions (introduced in Julia 1.9). The code is clean, well-structured, and demonstrates non-obvious skill in manipulating Julia's module loading internals.
The LLM identified 2 files worth highlighting for reuse:
Core implementation of the `@require` macro and supporting functions. Defines `listenpkg` to register callbacks for package load events, `loadpkg` to invoke them when a package is loaded, and `withpath`/`err` for safe execution with proper error handling and source path tracking. The `replace_include` function transforms `include` calls inside `@require` blocks to use the cached `@include` macro. This module is self-contained, imports only from Base and UUIDs, and can be extracted as a standalone pattern for any Julia package that needs conditional loading.
Defines the `@include` macro, which behaves like `include` but caches the file content at macro expansion time. At runtime, if the file still exists, it includes normally; otherwise, it falls back to the cached string. This is useful for building system images where files may not be present at runtime. The macro is used automatically by `@require` when `include` is called inside a require block. The pattern is reusable for any Julia project that needs to embed file content at compile time.