diff --git a/.tool-versions b/.tool-versions new file mode 100644 index 0000000..9a12234 --- /dev/null +++ b/.tool-versions @@ -0,0 +1,2 @@ +nodejs 20.9.0 +ruby 3.2.2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 7920558..bc2598b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,15 @@ -## [Unreleased] +## [1.0.0] - 2023-12-21 -## [0.1.0] - 2022-07-22 +- v1.0.0 🎉 +- Now you can specify your css compression with a symbol + ```ruby + config.assets.css_compressor = :escompress + ``` + +## [0.3.0] - 2022-12-22 + +## [0.2.0] - 2022-12-22 + +## [0.1.0] - 2022-07-29 - Initial release diff --git a/Gemfile.lock b/Gemfile.lock index 34dd0be..6fc881b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,43 +1,140 @@ PATH remote: . specs: - escompress (0.3.0) + escompress (1.0.0) GEM remote: https://rubygems.org/ specs: + actionpack (7.1.2) + actionview (= 7.1.2) + activesupport (= 7.1.2) + nokogiri (>= 1.8.5) + racc + rack (>= 2.2.4) + rack-session (>= 1.0.1) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + actionview (7.1.2) + activesupport (= 7.1.2) + builder (~> 3.1) + erubi (~> 1.11) + rails-dom-testing (~> 2.2) + rails-html-sanitizer (~> 1.6) + activesupport (7.1.2) + base64 + bigdecimal + concurrent-ruby (~> 1.0, >= 1.0.2) + connection_pool (>= 2.2.5) + drb + i18n (>= 1.6, < 2) + minitest (>= 5.1) + mutex_m + tzinfo (~> 2.0) ast (2.4.2) - json (2.6.3) - language_server-protocol (3.17.0.2) + base64 (0.2.0) + bigdecimal (3.1.4) + builder (3.2.4) + concurrent-ruby (1.2.2) + connection_pool (2.4.1) + crass (1.0.6) + drb (2.2.0) + ruby2_keywords + erubi (1.12.0) + i18n (1.14.1) + concurrent-ruby (~> 1.0) + io-console (0.6.0) + irb (1.10.1) + rdoc + reline (>= 0.3.8) + json (2.7.1) + language_server-protocol (3.17.0.3) + lint_roller (1.1.0) + loofah (2.22.0) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) minitest (5.16.3) - parallel (1.22.1) - parser (3.1.3.0) + mutex_m (0.2.0) + nokogiri (1.15.5-x86_64-darwin) + racc (~> 1.4) + nokogiri (1.15.5-x86_64-linux) + racc (~> 1.4) + parallel (1.23.0) + parser (3.2.2.4) ast (~> 2.4.1) + racc + psych (5.1.1.1) + stringio + racc (1.7.3) + rack (3.0.8) + rack-session (2.0.0) + rack (>= 3.0.0) + rack-test (2.1.0) + rack (>= 1.3) + rackup (2.1.0) + rack (>= 3) + webrick (~> 1.8) + rails-dom-testing (2.2.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.0) + loofah (~> 2.21) + nokogiri (~> 1.14) + railties (7.1.2) + actionpack (= 7.1.2) + activesupport (= 7.1.2) + irb + rackup (>= 1.0.0) + rake (>= 12.2) + thor (~> 1.0, >= 1.2.2) + zeitwerk (~> 2.6) rainbow (3.1.1) rake (13.0.6) - regexp_parser (2.6.1) - rexml (3.2.5) - rubocop (1.40.0) + rdoc (6.6.1) + psych (>= 4.0.0) + regexp_parser (2.8.3) + reline (0.4.1) + io-console (~> 0.5) + rexml (3.2.6) + rubocop (1.57.2) json (~> 2.3) + language_server-protocol (>= 3.17.0) parallel (~> 1.10) - parser (>= 3.1.2.1) + parser (>= 3.2.2.4) rainbow (>= 2.2.2, < 4.0) regexp_parser (>= 1.8, < 3.0) rexml (>= 3.2.5, < 4.0) - rubocop-ast (>= 1.23.0, < 2.0) + rubocop-ast (>= 1.28.1, < 2.0) ruby-progressbar (~> 1.7) - unicode-display_width (>= 1.4.0, < 3.0) - rubocop-ast (1.24.0) - parser (>= 3.1.1.0) - rubocop-performance (1.15.1) + unicode-display_width (>= 2.4.0, < 3.0) + rubocop-ast (1.30.0) + parser (>= 3.2.1.0) + rubocop-performance (1.19.1) rubocop (>= 1.7.0, < 2.0) rubocop-ast (>= 0.4.0) - ruby-progressbar (1.11.0) - standard (1.20.0) + ruby-progressbar (1.13.0) + ruby2_keywords (0.0.5) + standard (1.32.1) language_server-protocol (~> 3.17.0.2) - rubocop (= 1.40.0) - rubocop-performance (= 1.15.1) - unicode-display_width (2.3.0) + lint_roller (~> 1.0) + rubocop (~> 1.57.2) + standard-custom (~> 1.0.0) + standard-performance (~> 1.2) + standard-custom (1.0.2) + lint_roller (~> 1.0) + rubocop (~> 1.50) + standard-performance (1.2.1) + lint_roller (~> 1.1) + rubocop-performance (~> 1.19.1) + stringio (3.1.0) + thor (1.3.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (2.5.0) + webrick (1.8.1) + zeitwerk (2.6.12) PLATFORMS x86_64-darwin-20 @@ -46,6 +143,7 @@ PLATFORMS DEPENDENCIES escompress! minitest + railties rake (~> 13.0) standard diff --git a/README.md b/README.md index 0949384..fc8aca0 100644 --- a/README.md +++ b/README.md @@ -18,10 +18,6 @@ Install the gem and add to the application's Gemfile by executing: $ bundle add escompress -If bundler is not being used to manage dependencies, install the gem by executing: - - $ gem install escompress - ## Usage Make sure you have `esbuild` in your package.json file. If you are using Rails, escompress will look in your node_modules folder for the esbuild executable. If not using Rails, the gem will assume the executable is located at `/usr/local/bin/esbuild` on the underlying machine. @@ -35,8 +31,8 @@ Make sure you have `esbuild` in your package.json file. If you are using Rails, module MyApp class Application < Rails::Application # ... other code - config.assets.css_compressor = Escompress::Compressor.new(loader: :css) - # config.assets.js_compressor = Escompress::Compressor.new(loader: :js) + config.assets.css_compressor = :escompress + # config.assets.js_compressor = :escompress end end ``` diff --git a/escompress.gemspec b/escompress.gemspec index 954a26d..504dbb5 100644 --- a/escompress.gemspec +++ b/escompress.gemspec @@ -30,4 +30,5 @@ Gem::Specification.new do |spec| spec.require_paths = ["lib"] spec.add_development_dependency "minitest" + spec.add_development_dependency "railties" end diff --git a/lib/escompress/compressor.rb b/lib/escompress/compressor.rb index 5205dcf..4cf7890 100644 --- a/lib/escompress/compressor.rb +++ b/lib/escompress/compressor.rb @@ -6,6 +6,12 @@ def initialize(loader:) @loader = loader end + def call(input) + compress(input[:data]) + end + + private + def compress(input) cmd = "#{Escompress.esbuild_executable} --loader=#{@loader} --minify" output, err, status = Open3.capture3(cmd, stdin_data: input) diff --git a/lib/escompress/railtie.rb b/lib/escompress/railtie.rb index 7a7f679..8efcd0b 100644 --- a/lib/escompress/railtie.rb +++ b/lib/escompress/railtie.rb @@ -1,7 +1,21 @@ +require "rails/railtie" + module Escompress - class Railtie < Rails::Railtie - initializer "escompress_railtie.configure_rails_initialization" do + class Railtie < ::Rails::Railtie + initializer "escompress_railtie.configure_rails_initialization", group: :all do Escompress.esbuild_executable = Rails.root.join("node_modules/esbuild/bin/esbuild") + + # Adapted from https://github.com/ahorek/terser-ruby/blob/46a23ba/lib/terser/railtie.rb + if config.respond_to?(:assets) # Ensure asset pipeline is there + config.assets.configure do |env| + # A processor is expected to respond to call() and it accepts a hash of file contents. + # It is expected to return a hash that includes a :data key. + # https://github.com/rails/sprockets/blob/main/guides/extending_sprockets.md#compressors + # https://github.com/rails/sprockets/blob/main/guides/extending_sprockets.md#extension-interface + env.register_compressor "text/css", :escompress, Escompress::Compressor.new(loader: :css) + env.register_compressor "application/javascript", :escompress, Escompress::Compressor.new(loader: :js) + end + end end end end diff --git a/lib/escompress/version.rb b/lib/escompress/version.rb index 8968277..3a03a90 100644 --- a/lib/escompress/version.rb +++ b/lib/escompress/version.rb @@ -1,3 +1,3 @@ module Escompress - VERSION = "0.3.0" + VERSION = "1.0.0" end diff --git a/test/test_escompress.rb b/test/test_escompress.rb index f5bd501..c57ea69 100644 --- a/test/test_escompress.rb +++ b/test/test_escompress.rb @@ -12,4 +12,18 @@ def test_configurable_executable_location Escompress.esbuild_executable = Pathname.new("/Users/justin/Sites/example/node_modules/esbuild/bin/esbuild") assert_equal Escompress.esbuild_executable, Pathname.new("/Users/justin/Sites/example/node_modules/esbuild/bin/esbuild") end + + def test_css_compressor + Escompress.esbuild_executable = Pathname.new("/usr/local/bin/esbuild") + compressor = Escompress::Compressor.new(loader: :css) + output = compressor.call(data: "a { color: #ff0000 }") + assert_equal output, "a{color:red}\n" + end + + def test_js_compressor + Escompress.esbuild_executable = Pathname.new("/usr/local/bin/esbuild") + compressor = Escompress::Compressor.new(loader: :js) + output = compressor.call(data: "const hello = (name) => { console.log(name) }") + assert_equal output, "const hello=o=>{console.log(o)};\n" + end end