Skip to content

Commit

Permalink
Merge pull request #7 from jwilsjustin/6-register-custom-compressor-i…
Browse files Browse the repository at this point in the history
…n-railtie

register custom compressor in railtie
  • Loading branch information
Justin Wilson authored Dec 21, 2023
2 parents c333626 + f60b25c commit ec4ef43
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 30 deletions.
2 changes: 2 additions & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nodejs 20.9.0
ruby 3.2.2
14 changes: 12 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
136 changes: 117 additions & 19 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -46,6 +143,7 @@ PLATFORMS
DEPENDENCIES
escompress!
minitest
railties
rake (~> 13.0)
standard

Expand Down
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
```
Expand Down
1 change: 1 addition & 0 deletions escompress.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_development_dependency "minitest"
spec.add_development_dependency "railties"
end
6 changes: 6 additions & 0 deletions lib/escompress/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions lib/escompress/railtie.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/escompress/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Escompress
VERSION = "0.3.0"
VERSION = "1.0.0"
end
14 changes: 14 additions & 0 deletions test/test_escompress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ec4ef43

Please sign in to comment.