MishkaCmsCore.Runtime.Compilers.ComponentImportMacroCompiler (Mishka CMS Core v0.0.1)

Copy Markdown View Source

Runtime compiler for generating component import macro modules.

This module provides functionality to dynamically create modules with __using__ macros that import specific functions from various component modules. This enables runtime composition of component libraries with selective function imports.

Purpose

The primary use case is to create dynamic import modules at runtime that can be used with use statements in other modules to import specific functions from component libraries. This is particularly useful in CMS systems where component availability needs to be configured dynamically.

Generated Module Structure

The generated module contains:

  • A __using__/1 macro that handles selective imports
  • Import statements for each specified module and function list
  • Proper macro expansion for compile-time import resolution

Usage Pattern

# Create the import module
params = %{
  module_name: MyApp.RuntimeComponents,
  imports: [
    {MyApp.Components.Alert, [flash: 1, alert: 1]},
    {MyApp.Components.Button, [button: 1]}
  ]
}

ast = ComponentImportMacroCompiler.build_module(params)
MishkaCmsCore.Runtime.Compilers.ModuleCompiler.compile_module(ast)

# Use the generated module
defmodule MyView do
  use MyApp.RuntimeComponents

  # Now has access to flash/1, alert/1, button/1
end

Benefits

  • Selective imports: Only import specific functions, not entire modules
  • Runtime configuration: Component availability can be configured at runtime
  • Namespace management: Avoid function name conflicts
  • Performance: Only imported functions are available, reducing compile overhead

Parameters

  • params - Map containing module configuration with keys:
    • :module_name - Target module name (required)
    • :imports - List of {module, functions} tuples (required)

Import Format

Each import tuple consists of:

  • module - The module to import from (e.g., MyApp.Components.Button)
  • functions - List of {function_name, arity} tuples or keyword list format

Summary

Functions

Builds a module with imported functions based on the provided parameters.

Functions

build_module(params)

Builds a module with imported functions based on the provided parameters.