diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5583f83..664fc99 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,13 +45,27 @@ jobs: gemfile: "gemfiles/Gemfile.rails-6.0.x.sprockets-4.x" - ruby: 2.7 gemfile: "gemfiles/Gemfile.rails-6.1.x.sprockets-4.x" + - ruby: 2.7 + gemfile: "gemfiles/Gemfile.rails-7.0.x.sprockets-4.x" # Ruby 3.0.x - ruby: '3.0' gemfile: "gemfiles/Gemfile.rails-6.1.x.sprockets-4.x" + - ruby: '3.0' + gemfile: "gemfiles/Gemfile.rails-7.0.x.sprockets-4.x" + - ruby: '3.0' + gemfile: "gemfiles/Gemfile.rails-7.1.x.sprockets-4.x" # Ruby 3.1.x - ruby: 3.1 - gemfile: Gemfile + gemfile: "gemfiles/Gemfile.rails-6.1.x.sprockets-4.x" + - ruby: 3.1 + gemfile: "gemfiles/Gemfile.rails-7.0.x.sprockets-4.x" + - ruby: 3.1 + gemfile: "gemfiles/Gemfile.rails-7.1.x.sprockets-4.x" # Ruby 3.2.x + - ruby: 3.2 + gemfile: "gemfiles/Gemfile.rails-6.1.x.sprockets-4.x" + - ruby: 3.2 + gemfile: "gemfiles/Gemfile.rails-7.0.x.sprockets-4.x" - ruby: 3.2 gemfile: Gemfile diff --git a/CHANGELOG.md b/CHANGELOG.md index ecc2aba..8d5603d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## 1.1.0 (6 October 2023) + +- Ruby required >= 2.2 +- Logger for .map and .gzip generated files +- Rails 7.x support added to readme and ci + ## 1.0.0 (24 September 2023) - Initial release diff --git a/README.md b/README.md index 61c65cc..8916036 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,8 @@ Create source maps when compressing assets in your Rails applications. This gem uses Terser to create source maps for your concatenated javascripts in Rails. It is meant to be used as a replacement for javascript compressor. -This is a fork from [sprockets_uglifier_with_source_maps](https://github.com/AlexanderPavlenko/sprockets_uglifier_with_source_maps) +This is a fork +from [sprockets_uglifier_with_source_maps](https://github.com/AlexanderPavlenko/sprockets_uglifier_with_source_maps) to generate source maps using terser as compressor. ## Rails Supported Versions @@ -22,6 +23,10 @@ to generate source maps using terser as compressor. + + 7.x + ✓ + 6.x ✓ @@ -61,9 +66,10 @@ In your Rails applications environment configuration: If you need to pass options to terser: - config.assets.terser = {output: {beautify: true, indent_level: 2}, compress: {drop_console: true}} + config.assets.terser = {output: {beautify: true, indent_level: 2}} -Your assets will be built as normal, also maps and concatenated sources will be provided as well in `public/assets/maps` and `public/assets/sources`. +Your assets will be built as normal, also maps and concatenated sources will be provided as well in `public/assets/maps` +and `public/assets/sources`. These subdirs may be configured: config.assets.sourcemaps_prefix = 'my_maps' @@ -77,13 +83,16 @@ You can optionally skip gzipping your maps and sources: config.assets.sourcemaps_gzip = false -By default maps and sources are defined relatively and will be fetched from the same domain your js file is served from. If you are using a CDN you may not want this - instead you might want to use a direct link to your site so you can more easily implement IP or Basic Auth protection: +By default maps and sources are defined relatively and will be fetched from the same domain your js file is served from. +If you are using a CDN you may not want this - instead you might want to use a direct link to your site so you can more +easily implement IP or Basic Auth protection: # set to a url - js will be served from 'http://cdn.host.com' but source map links will use 'http://some.host.com/' config.assets.sourcemaps_url_root = 'http://some.host.com/' -If you use CloudFront you might want to generate a signed url for these files that limits access based on IP address. You can do that by setting sourcemaps_url_root to a Proc and handling your URL signing there: +If you use CloudFront you might want to generate a signed url for these files that limits access based on IP address. +You can do that by setting sourcemaps_url_root to a Proc and handling your URL signing there: # using a Proc - see the AWS SDK docs for everything required to make this work config.assets.sourcemaps_url_root = Proc.new { |file| MyApp.generate_a_signed_url_for file } @@ -112,8 +121,9 @@ If sourcemaps are not generated, try `rm -rf tmp/cache`. ## Contributing -Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sprockets_terser_with_source_maps. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/sprockets_terser_with_source_maps/blob/master/CODE_OF_CONDUCT.md). - +Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/sprockets_terser_with_source_maps. +This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to +the [code of conduct](https://github.com/[USERNAME]/sprockets_terser_with_source_maps/blob/master/CODE_OF_CONDUCT.md). ## License @@ -121,4 +131,6 @@ The gem is available as open source under the terms of the [MIT License](https:/ ## Code of Conduct -Everyone interacting in the SprocketsTerserWithSourceMaps project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/sprockets_terser_with_source_maps/blob/master/CODE_OF_CONDUCT.md). +Everyone interacting in the SprocketsTerserWithSourceMaps project's codebases, issue trackers, chat rooms and mailing +lists is expected to follow +the [code of conduct](https://github.com/[USERNAME]/sprockets_terser_with_source_maps/blob/master/CODE_OF_CONDUCT.md). diff --git a/gemfiles/Gemfile.rails-7.0.x.sprockets-4.x b/gemfiles/Gemfile.rails-7.0.x.sprockets-4.x new file mode 100644 index 0000000..1ea2a1a --- /dev/null +++ b/gemfiles/Gemfile.rails-7.0.x.sprockets-4.x @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +# Specify your gem's dependencies in sprockets_terser_with_source_maps.gemspec +gemspec path: '..' + +gem 'railties', '~> 7.0.0' +gem 'sprockets', '~> 4.0' diff --git a/gemfiles/Gemfile.rails-7.1.x.sprockets-4.x b/gemfiles/Gemfile.rails-7.1.x.sprockets-4.x new file mode 100644 index 0000000..724512e --- /dev/null +++ b/gemfiles/Gemfile.rails-7.1.x.sprockets-4.x @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +source 'https://rubygems.org' + +# Specify your gem's dependencies in sprockets_terser_with_source_maps.gemspec +gemspec path: '..' + +gem 'railties', '~> 7.1.0' +gem 'sprockets', '~> 4.0' diff --git a/lib/sprockets_terser_with_source_maps/compressor.rb b/lib/sprockets_terser_with_source_maps/compressor.rb index adb707e..ddeacf4 100644 --- a/lib/sprockets_terser_with_source_maps/compressor.rb +++ b/lib/sprockets_terser_with_source_maps/compressor.rb @@ -30,7 +30,7 @@ def call(input) sourcemap['sourcesContent'] = [data] else # Generate uncompressed asset - uncompressed_url, = generate_asset_file(name, data, Rails.application.config.assets.uncompressed_prefix) + uncompressed_url = generate_asset_file(name, data, Rails.application.config.assets.uncompressed_prefix) sourcemap['sources'] = [uncompressed_url] end @@ -39,12 +39,11 @@ def call(input) sourcemap_json = sourcemap.to_json # Generate sourcemap file - sourcemap_url, sourcemap_path = generate_asset_file( + sourcemap_url = generate_asset_file( name, sourcemap_json, Rails.application.config.assets.sourcemaps_prefix, 'js.map' ) - logger.info "Writing #{sourcemap_path}" compressed_js.concat "\n//# sourceMappingURL=#{sourcemap_url}\n" end @@ -53,14 +52,16 @@ def call(input) def generate_asset_file(name, data, prefix, extension = 'js') filename = File.join(Rails.application.config.assets.prefix, prefix, "#{name}-#{digest(data)}.#{extension}") - file_path = File.join(Rails.public_path, filename) + file_path = File.join(Rails.public_path.to_s, filename) file_url = filename_to_url(filename) + logger.info "Writing #{file_path}" if !File.exist?(file_path) && file_path.include?('.map') + FileUtils.mkdir_p File.dirname(file_path) File.write(file_path, data) gzip_file(file_path) if gzip? - [file_url, file_path] + file_url end def filename_to_url(filename) @@ -81,6 +82,8 @@ def gzip? end def gzip_file(path) + logger.info "Writing #{path}.gz" if !File.exist?("#{path}.gz") && path.include?('.map') + Zlib::GzipWriter.open("#{path}.gz") do |gz| gz.mtime = File.mtime(path) gz.orig_name = path diff --git a/spec/rails_app/config/application.rb b/spec/rails_app/config/application.rb index 798fa9c..ee1d5c1 100644 --- a/spec/rails_app/config/application.rb +++ b/spec/rails_app/config/application.rb @@ -13,3 +13,8 @@ class Application < Rails::Application config.eager_load = false end end + +# Cache disabled +Rails.application.config.assets.configure do |env| + env.cache = ActiveSupport::Cache.lookup_store(:null_store) +end diff --git a/spec/railtie_spec.rb b/spec/railtie_spec.rb index 6823310..386b357 100644 --- a/spec/railtie_spec.rb +++ b/spec/railtie_spec.rb @@ -32,8 +32,7 @@ prefix = Rails.application.config.assets.prefix source_prefix = Rails.application.config.assets.uncompressed_prefix source_path = File.join( - app.root, - 'public', + Rails.public_path.to_s, prefix, source_prefix, 'application-67edb0733821d001c895de576601b754165d9a2a350e6abd1e8c009d9ea24c1c.js' @@ -41,8 +40,7 @@ map_prefix = Rails.application.config.assets.sourcemaps_prefix map_path = File.join( - app.root, - 'public', + Rails.public_path.to_s, prefix, map_prefix, 'application-8e484580fdd7121444478bdee52a61a6e8d44684c6c7fe030bde5979a58a13c6.js.map' @@ -53,6 +51,7 @@ expect(File.exist?(path)).to be expect(File.exist?(source_path)).to be expect(File.exist?(map_path)).to be + expect(File.exist?("#{map_path}.gz")).to be end end end