class
AssetPipeline::ScriptRenderer
- AssetPipeline::ScriptRenderer
- Reference
- Object
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:
- Caches script content generation based on import map and custom JS hashes
- Memoizes import statement generation
- Caches dependency analysis results
Direct Known Subclasses
Defined in:
asset_pipeline/script_renderer.crConstructors
-
.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
Instance Method Summary
-
#analyze_code_complexity
Analyzes code complexity
-
#analyze_dependencies_with_suggestions : Hash(Symbol, Array(String))
Advanced dependency analysis with caching
-
#cache_efficiency : Hash(String, Float64)
Returns cache efficiency statistics
-
#cache_stats : Hash(String, Int32)
Performance monitoring methods Returns cache statistics for monitoring performance
-
#clear_caches
Clears all performance caches (useful for testing or memory management)
-
#dependency_analyzer : DependencyAnalyzer | Nil
Gets the dependency analyzer instance
-
#generate_development_report : String
Generates a development report with analysis results
-
#generate_import_statements : String
Generates import statements from the import map and dependency analysis
-
#generate_script_content : String
Generates the script content without wrapping it in script tags
-
#get_import_suggestions : Array(String)
Gets import suggestions based on dependency analysis
-
#render_initialization_script : String
Renders a complete script tag with imports and initialization code
-
#uses_module_syntax? : Bool
Checks if the custom JavaScript block uses module syntax
-
#validate_configuration : Hash(String, Array(String))
Validates the renderer configuration and suggests optimizations
Constructor Detail
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.
Instance Method Detail
Advanced dependency analysis with caching
Returns comprehensive information about dependencies and suggestions
Performance monitoring methods Returns cache statistics for monitoring performance
Gets the dependency analyzer instance
Returns the DependencyAnalyzer if enabled, nil otherwise
Generates a development report with analysis results
Useful for debugging and development to understand what the analyzer detected
Generates import statements from the import map and dependency analysis
Returns a string containing all necessary import statements
Generates the script content without wrapping it in script tags
Useful for testing or when you need the raw JavaScript content
Gets import suggestions based on dependency analysis
Returns an array of human-readable suggestions for missing imports
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>"
Validates the renderer configuration and suggests optimizations