class AssetPipeline::ScriptRenderer

Overview

The ScriptRenderer handles framework-agnostic JavaScript script generation and rendering. It provides advanced functionality for generating import statements, analyzing dependencies, and wrapping custom JavaScript initialization blocks in proper module script tags.

This class serves as the foundation for framework-specific renderers like StimulusRenderer.

Performance optimizations:

Direct Known Subclasses

Defined in:

asset_pipeline/script_renderer.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new(import_map : ImportMap, custom_javascript_block : String = "", enable_dependency_analysis : Bool = true) #

Initialize the ScriptRenderer with an import map and optional custom JavaScript block

renderer = ScriptRenderer.new(import_map, "console.log('initialized');")

Set enable_dependency_analysis to true to automatically detect missing dependencies from the custom JavaScript block.


[View source]

Instance Method Detail

def analyze_code_complexity #

Analyzes code complexity


[View source]
def analyze_dependencies_with_suggestions : Hash(Symbol, Array(String)) #

Advanced dependency analysis with caching

Returns comprehensive information about dependencies and suggestions


[View source]
def cache_efficiency : Hash(String, Float64) #

Returns cache efficiency statistics


[View source]
def cache_stats : Hash(String, Int32) #

Performance monitoring methods Returns cache statistics for monitoring performance


[View source]
def clear_caches #

Clears all performance caches (useful for testing or memory management)


[View source]
def dependency_analyzer : DependencyAnalyzer | Nil #

Gets the dependency analyzer instance

Returns the DependencyAnalyzer if enabled, nil otherwise


[View source]
def generate_development_report : String #

Generates a development report with analysis results

Useful for debugging and development to understand what the analyzer detected


[View source]
def generate_import_statements : String #

Generates import statements from the import map and dependency analysis

Returns a string containing all necessary import statements


[View source]
def generate_script_content : String #

Generates the script content without wrapping it in script tags

Useful for testing or when you need the raw JavaScript content


[View source]
def get_import_suggestions : Array(String) #

Gets import suggestions based on dependency analysis

Returns an array of human-readable suggestions for missing imports


[View source]
def render_initialization_script : String #

Renders a complete script tag with imports and initialization code

Returns a string containing the complete <script type="module"> tag with:

  • Generated import statements for detected dependencies
  • Custom JavaScript initialization block
renderer.render_initialization_script
# => "<script type=\"module\">\nimport ...\nconsole.log('initialized');\n</script>"

[View source]
def uses_module_syntax? : Bool #

Checks if the custom JavaScript block uses module syntax


[View source]
def validate_configuration : Hash(String, Array(String)) #

Validates the renderer configuration and suggests optimizations


[View source]