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
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 |
# File 'lib/jekyll-minifier.rb', line 528 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.message}. Using original content.") content end end |
.compress_js(content, config, file_path = 'unknown') ⇒ String
Compresses JavaScript content using Terser with validation
554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 |
# File 'lib/jekyll-minifier.rb', line 554 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.message}. Using original content.") content end end |
.compress_json(content, file_path = 'unknown') ⇒ String
Compresses JSON content with validation
574 575 576 577 578 579 580 581 582 583 584 585 586 587 |
# File 'lib/jekyll-minifier.rb', line 574 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.message}. Using original content.") content end end |
.create_css_compressor(config) ⇒ Object
Creates CSS compressor based on configuration
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 |
# File 'lib/jekyll-minifier.rb', line 431 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)
504 505 506 507 508 509 510 |
# File 'lib/jekyll-minifier.rb', line 504 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
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 |
# File 'lib/jekyll-minifier.rb', line 476 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
455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'lib/jekyll-minifier.rb', line 455 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)
515 516 517 518 519 520 521 |
# File 'lib/jekyll-minifier.rb', line 515 def create_js_compressor_uncached(config) if config.has_terser_args? ::Terser.new(config.terser_args) else ::Terser.new() end end |