Skip to content

Commit

Permalink
Merge branch 'main' into 1.x
Browse files Browse the repository at this point in the history
  • Loading branch information
javier-menendez committed Oct 6, 2023
2 parents a6bf7f3 + e80fa37 commit a14852b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 18 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
28 changes: 20 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +23,10 @@ to generate source maps using terser as compressor.
</tr>
</thead>
<tbody>
<tr>
<td>7.x</td>
<td>&checkmark;</td>
</tr>
<tr>
<td>6.x</td>
<td>&checkmark;</td>
Expand Down Expand Up @@ -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'
Expand All @@ -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 }
Expand Down Expand Up @@ -112,13 +121,16 @@ 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

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## 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).
9 changes: 9 additions & 0 deletions gemfiles/Gemfile.rails-7.0.x.sprockets-4.x
Original file line number Diff line number Diff line change
@@ -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'
9 changes: 9 additions & 0 deletions gemfiles/Gemfile.rails-7.1.x.sprockets-4.x
Original file line number Diff line number Diff line change
@@ -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'
13 changes: 8 additions & 5 deletions lib/sprockets_terser_with_source_maps/compressor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions spec/rails_app/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 3 additions & 4 deletions spec/railtie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,15 @@
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'
)

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'
Expand All @@ -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

0 comments on commit a14852b

Please sign in to comment.