Module: Jekyll::Minifier::CompressorFactory
- Defined in:
- lib/jekyll-minifier.rb
Overview
CompressorFactory module extracts complex compressor setup logic Reduces complexity and centralizes compressor configuration
Class Method Summary collapse
-
.compress_css(content, config, file_path = 'unknown') ⇒ String
Compresses CSS content using appropriate compressor with validation.
-
.compress_js(content, config, file_path = 'unknown') ⇒ String
Compresses JavaScript content using Terser with validation.
-
.compress_json(content, file_path = 'unknown') ⇒ String
Compresses JSON content with validation.
-
.create_css_compressor(config) ⇒ Object
Creates CSS compressor based on configuration.
-
.create_css_compressor_uncached(config) ⇒ Object
Internal method to create CSS compressor without caching (avoids deadlock).
-
.create_html_compressor(config) ⇒ HtmlCompressor::Compressor
Creates HTML compressor with configured CSS and JS compressors.
-
.create_js_compressor(config) ⇒ Terser
Creates JavaScript compressor based on configuration.
-
.create_js_compressor_uncached(config) ⇒ Terser
Internal method to create JS compressor without caching (avoids deadlock).
Class Method Details
.compress_css(content, config, file_path = 'unknown') ⇒ String
Compresses CSS content using appropriate compressor with validation
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/jekyll-minifier.rb', line 446 def compress_css(content, config, file_path = 'unknown') # Validate content before processing unless ValidationHelpers.validate_file_content(content, 'css', file_path) Jekyll.logger.warn("Jekyll Minifier:", "Skipping CSS compression for unsafe content: #{file_path}") return content end begin if config.css_enhanced_mode? && config. CSSminify2.compress_enhanced(content, config.) else compressor = create_css_compressor(config) # Pass nil to disable line breaks completely for performance (PR #61) compressor.compress(content, nil) end rescue => e Jekyll.logger.warn("Jekyll Minifier:", "CSS compression failed for #{file_path}: #{e.}. Using original content.") content end end |
.compress_js(content, config, file_path = 'unknown') ⇒ String
Compresses JavaScript content using Terser with validation
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 |
# File 'lib/jekyll-minifier.rb', line 472 def compress_js(content, config, file_path = 'unknown') # Validate content before processing unless ValidationHelpers.validate_file_content(content, 'js', file_path) Jekyll.logger.warn("Jekyll Minifier:", "Skipping JavaScript compression for unsafe content: #{file_path}") return content end begin compressor = create_js_compressor(config) compressor.compile(content) rescue => e Jekyll.logger.warn("Jekyll Minifier:", "JavaScript compression failed for #{file_path}: #{e.}. Using original content.") content end end |
.compress_json(content, file_path = 'unknown') ⇒ String
Compresses JSON content with validation
492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/jekyll-minifier.rb', line 492 def compress_json(content, file_path = 'unknown') # Validate content before processing unless ValidationHelpers.validate_file_content(content, 'json', file_path) Jekyll.logger.warn("Jekyll Minifier:", "Skipping JSON compression for unsafe content: #{file_path}") return content end begin JSON.minify(content) rescue => e Jekyll.logger.warn("Jekyll Minifier:", "JSON compression failed for #{file_path}: #{e.}. Using original content.") content end end |
.create_css_compressor(config) ⇒ Object
Creates CSS compressor based on configuration
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 |
# File 'lib/jekyll-minifier.rb', line 349 def create_css_compressor(config) # Generate cache key from configuration if config.css_enhanced_mode? && config. cache_key = CompressorCache.generate_cache_key({ enhanced_mode: true, options: config. }) else cache_key = CompressorCache.generate_cache_key({ enhanced_mode: false }) end # Use cache to get or create compressor CompressorCache.get_or_create(:css, cache_key) do if config.css_enhanced_mode? && config. CSSEnhancedWrapper.new(config.) else CSSminify2.new() end end end |
.create_css_compressor_uncached(config) ⇒ Object
Internal method to create CSS compressor without caching (avoids deadlock)
422 423 424 425 426 427 428 |
# File 'lib/jekyll-minifier.rb', line 422 def create_css_compressor_uncached(config) if config.css_enhanced_mode? && config. CSSEnhancedWrapper.new(config.) else CSSminify2.new() end end |
.create_html_compressor(config) ⇒ HtmlCompressor::Compressor
Creates HTML compressor with configured CSS and JS compressors
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 |
# File 'lib/jekyll-minifier.rb', line 394 def create_html_compressor(config) # Generate cache key from HTML compressor configuration html_args = config.html_compressor_args cache_key = CompressorCache.generate_cache_key({ html_args: html_args, css_enhanced: config.css_enhanced_mode?, css_options: config., terser_args: config.terser_args }) # Use cache to get or create HTML compressor # Avoid deadlock by creating sub-compressors outside the cache lock CompressorCache.get_or_create(:html, cache_key) do # Create sub-compressors first (outside the HTML cache lock) css_compressor = create_css_compressor_uncached(config) js_compressor = create_js_compressor_uncached(config) # Create fresh args hash for this instance fresh_html_args = html_args.dup fresh_html_args[:css_compressor] = css_compressor fresh_html_args[:javascript_compressor] = js_compressor HtmlCompressor::Compressor.new(fresh_html_args) end end |
.create_js_compressor(config) ⇒ Terser
Creates JavaScript compressor based on configuration
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
# File 'lib/jekyll-minifier.rb', line 373 def create_js_compressor(config) # Generate cache key from Terser configuration cache_key = if config.has_terser_args? CompressorCache.generate_cache_key({ terser_args: config.terser_args }) else CompressorCache.generate_cache_key({ terser_args: nil }) end # Use cache to get or create compressor CompressorCache.get_or_create(:js, cache_key) do if config.has_terser_args? ::Terser.new(config.terser_args) else ::Terser.new() end end end |
.create_js_compressor_uncached(config) ⇒ Terser
Internal method to create JS compressor without caching (avoids deadlock)
433 434 435 436 437 438 439 |
# File 'lib/jekyll-minifier.rb', line 433 def create_js_compressor_uncached(config) if config.has_terser_args? ::Terser.new(config.terser_args) else ::Terser.new() end end |