source_maps_fixer 0.1.4 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d060fd00bf128c3a3e46a94660894159c052de0529e6a4f6056daa423bb24411
4
- data.tar.gz: 9fdd977e500e9fe2bdf5e61a495d57a7741a470f60a3c359d4db6a214448bb6e
3
+ metadata.gz: 8359d9c4a3b6c24bca36cc77d9f53cf762e5a14fc58bae51ca4354ca8e4ce0fb
4
+ data.tar.gz: 942a8bc352604d24148a0200eb9c27e40c7ea234d3f4f8de021a9f486aa8a633
5
5
  SHA512:
6
- metadata.gz: 90519081a6e19f8001219735174e703b563e3a1b3e0a6145ca7a92c3fca18f5f0dab26debcc083932f91b586a45b9e8adfd1e59d5430837407557a7181ebeb80
7
- data.tar.gz: 81f6b59dff7a5471a476f645aacfc2c10d28c2d65593a86dfd0c5144003adc657812b05a069f936cc2d9f1127076178bfdfdffcee870fd653116a0c4ef865a18
6
+ metadata.gz: 4c27c8b97e5cd3a5e9b8c366c876feb3f1ed00d94a28b01609d4ad67ccf91b1c9b4d6aef26e4c5fc58dfeb5b53310f2bd8d753fa0938d72c90731210cdba14ef
7
+ data.tar.gz: 13877418eba6cd5a8e14281205b009b4ece82fc72f95db4844d58599459919552f6b5fc3e5c5500c3705e1201978e024994a32cd2f37f5c796f36267db5a44a7
data/README.md CHANGED
@@ -1,17 +1,13 @@
1
1
  # SourceMapsFixer
2
2
 
3
3
  SourceMapsFixer is a set of [Rake](https://ruby.github.io/rake/) tasks for fixing **sourceMappingURL**s.
4
- It is useful in [Rails](https://rubyonrails.org) apps if you like to have your main front-end code separated in `frontend` directory, for example. But you also use [Sprockets](https://github.com/rails/sprockets-rails) to process additional assets and compile them to `public/assets`. Fingerprints are added to asset filenames during compilation, by default. It is useful in conjunction with far-future headers.
5
-
6
- Common setup looks like this:
7
-
8
- Module bundler like [webpack](https://webpack.js.org) outputs bundles to `app/assets/bundles`, for example. By running `bin/rails assets:precompile` _Sprockets_ compiles all assets to `public/assets`
4
+ It is helpful for [Rails](https://rubyonrails.org) apps where the main front-end code lives in the separate directory like `frontend`. But the app also uses [Sprockets](https://github.com/rails/sprockets-rails) to process outputted bundles and additional assets inside `app/assets` directory. `bin/rails assets:precompile` command compiles assets to `public/assets` directory. Fingerprints are added to asset filenames during compilation by default. It is useful in conjunction with far-future headers.
9
5
 
10
6
  ### Problem
11
7
 
12
- When _webpack_ produces bundles to `app/assets/bundles`, each contains **sourceMappingURL** at the bottom (if configured). This URL links to the corresponding source map.
8
+ When [webpack](https://webpack.js.org) produces bundles to `app/assets/bundles`, each contains **sourceMappingURL** at the bottom (if configured). This URL links to the corresponding source map.
13
9
  But when _Sprockets_ compile assets to `public/assets`, fingerprints are added to all asset filenames.
14
- **sourceMappingURL**s are intact which makes them pointing to invalid files without fingerprints.
10
+ **sourceMappingURL**s are intact, which makes them point to invalid files without fingerprints.
15
11
 
16
12
  ### Solution
17
13
 
@@ -35,7 +31,7 @@ $ bin/rails assets:prepare
35
31
 
36
32
  ## 📥 Installation
37
33
 
38
- Add this line to your application's Gemfile:
34
+ Add *source\_maps\_fixer* to your application's Gemfile:
39
35
 
40
36
  ```ruby
41
37
  gem 'source_maps_fixer'
@@ -52,6 +48,18 @@ $ gem install source_maps_fixer
52
48
  ```
53
49
 
54
50
 
51
+ # 📈 Changelog
52
+
53
+ ## Major releases 🎙
54
+
55
+ ### 0.2 _(2022-02-11)_
56
+ #### 💥 breaking changes
57
+
58
+ * *source\_maps\_fixer* works with Rails 7 and Ruby 3.1.
59
+ * it drops support for Ruby 2.6
60
+ * it supports sprockets-rails ~> 3.3.0. Version 3.4.0 works differently. `bin/rails assets:precompile` correctly replaces *sourceMappingURL*s for JS files but adds unnecessary comments
61
+
62
+
55
63
  ## 📜 License
56
64
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
57
65
 
@@ -61,4 +69,4 @@ Zbigniew Humeniuk from [Art of Code](https://artofcode.co)
61
69
 
62
70
 
63
71
  ## 👀 See also
64
- If you want to make your life easier in other areas of web app development, I strongly recommend you to take a look at my other project called [Loco framework](http://locoframework.org) 🙂. It is quite powerful and makes a front-end <-> back-end communication a breeze (among other things).
72
+ If you want to make your life easier in other areas of web app development, I strongly recommend you to take a look at my other project called the [Loco framework](http://locoframework.org) 🙂. It is pretty powerful and makes a front-end <-> back-end communication a breeze (among other things).
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SourceMapsFixer
4
- VERSION = '0.1.4'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -3,15 +3,16 @@
3
3
  require 'source_maps_fixer/railtie'
4
4
 
5
5
  module SourceMapsFixer
6
- class Path
7
- def self.files_with_source_maps
6
+ module Path
7
+ module_function
8
+
9
+ def files_with_source_maps(filter = '')
8
10
  Dir.glob("#{Rails.root.join 'app', 'assets'}/**/*")
9
- .find_all { |name| name =~ /\.map\Z/ }
10
- .map { |name| [name.sub('.map', ''), name] }
11
- .to_h
11
+ .grep(/#{filter}\.map\Z/)
12
+ .to_h { |name| [name.sub('.map', ''), name] }
12
13
  end
13
14
 
14
- def self.digest_path(file_name)
15
+ def digest_path(file_name)
15
16
  Rails.application.assets.find_asset(file_name).digest_path
16
17
  end
17
18
 
@@ -25,24 +26,43 @@ module SourceMapsFixer
25
26
  end
26
27
  end
27
28
 
28
- class Executor
29
- def self.call
30
- Path.files_with_source_maps.each do |file_name, sm_name|
31
- new_content = File.read(file_name).sub(
32
- Path.source_mapping_url(File.basename(sm_name)),
33
- Path.source_mapping_url(Path.digest_path(sm_name))+"\n\n"
29
+ module ReplaceLinksWithPredicted
30
+ module_function
31
+
32
+ def call
33
+ Path.files_with_source_maps.each do |asset_path, sm_path|
34
+ new_content = File.read(asset_path).sub(
35
+ Path.source_mapping_url(File.basename(sm_path)),
36
+ "#{Path.source_mapping_url(Path.digest_path(sm_path))}\n\n"
34
37
  )
35
- File.open(file_name, 'w') { |file| file.write new_content }
38
+ File.write(asset_path, new_content)
36
39
  end
37
40
  end
38
41
 
39
- def self.undo
40
- Path.files_with_source_maps.each do |file_name, sm_name|
41
- new_content = File.read(file_name).sub(
42
- Path.source_mapping_url(Path.digest_path(sm_name))+"\n\n",
43
- Path.source_mapping_url(File.basename(sm_name))
42
+ def undo
43
+ Path.files_with_source_maps.each do |asset_path, sm_path|
44
+ new_content = File.read(asset_path).sub(
45
+ "#{Path.source_mapping_url(Path.digest_path(sm_path))}\n\n",
46
+ Path.source_mapping_url(File.basename(sm_path))
44
47
  )
45
- File.open(file_name, 'w') { |file| file.write new_content }
48
+ File.write(asset_path, new_content)
49
+ end
50
+ end
51
+ end
52
+
53
+ module FixCompiled
54
+ module_function
55
+
56
+ def call
57
+ Path.files_with_source_maps('js').each do |asset_path, _sm_path|
58
+ public_path = Dir.glob("#{Rails.root.join 'public', 'assets'}/**/*")
59
+ .find do |path|
60
+ File.basename(path) == Path.digest_path(asset_path)
61
+ end
62
+ lines = File.readlines(public_path)
63
+ next unless lines[-2] == "//!\n" && lines[-1] == ";\n"
64
+
65
+ File.write(public_path, lines[0..-3].join)
46
66
  end
47
67
  end
48
68
  end
@@ -4,14 +4,21 @@ namespace :assets do
4
4
  desc 'Fix URLs to source maps'
5
5
  task fix_source_maps: [:environment] do
6
6
  puts 'Fixing URLs to source maps...'
7
- SourceMapsFixer::Executor.call
7
+ SourceMapsFixer::ReplaceLinksWithPredicted.call
8
8
  puts 'URLs to source maps have been fixed!'
9
9
  end
10
10
 
11
11
  desc 'Revert fixed URLs to source maps'
12
12
  task undo_fix_source_maps: [:environment] do
13
13
  puts 'Reverting fixed URLs to source maps...'
14
- SourceMapsFixer::Executor.undo
14
+ SourceMapsFixer::ReplaceLinksWithPredicted.undo
15
15
  puts 'URLs to source maps have been reverted!'
16
16
  end
17
+
18
+ desc 'Fix generated URLs to source maps'
19
+ task fix_compiled: [:environment] do
20
+ puts 'Fixing URLs to source maps...'
21
+ SourceMapsFixer::ReplaceLinksWithPredicted.call
22
+ puts 'URLs to source maps have been fixed!'
23
+ end
17
24
  end
@@ -3,9 +3,9 @@
3
3
  namespace :assets do
4
4
  desc 'Fix URLs to source maps and compile assets'
5
5
  task prepare: [:environment] do
6
- SourceMapsFixer::Executor.call
6
+ SourceMapsFixer::ReplaceLinksWithPredicted.call
7
7
  Rake::Task['assets:precompile'].invoke
8
- SourceMapsFixer::Executor.undo
8
+ SourceMapsFixer::ReplaceLinksWithPredicted.undo
9
9
  puts 'Assets have been prepared!'
10
10
  end
11
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: source_maps_fixer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zbigniew Humeniuk
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-22 00:00:00.000000000 Z
11
+ date: 2022-02-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '7.0'
22
+ version: '8.0'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,49 @@ dependencies:
29
29
  version: '5.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '7.0'
32
+ version: '8.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: sprockets-rails
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 3.3.0
40
+ type: :runtime
41
+ prerelease: false
42
+ version_requirements: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - "~>"
45
+ - !ruby/object:Gem::Version
46
+ version: 3.3.0
47
+ - !ruby/object:Gem::Dependency
48
+ name: listen
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rubocop
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
33
75
  description: Rake tasks that fix sourceMappingURL in JavaScript and CSS bundles generated
34
76
  by webpack to match the final fingerprinted names generated by Sprockets after assets'
35
77
  precompilation
@@ -50,8 +92,9 @@ files:
50
92
  homepage: https://artofcode.co
51
93
  licenses:
52
94
  - MIT
53
- metadata: {}
54
- post_install_message:
95
+ metadata:
96
+ rubygems_mfa_required: 'true'
97
+ post_install_message:
55
98
  rdoc_options: []
56
99
  require_paths:
57
100
  - lib
@@ -59,15 +102,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
59
102
  requirements:
60
103
  - - ">="
61
104
  - !ruby/object:Gem::Version
62
- version: '0'
105
+ version: 2.7.0
63
106
  required_rubygems_version: !ruby/object:Gem::Requirement
64
107
  requirements:
65
108
  - - ">="
66
109
  - !ruby/object:Gem::Version
67
110
  version: '0'
68
111
  requirements: []
69
- rubygems_version: 3.1.2
70
- signing_key:
112
+ rubygems_version: 3.3.3
113
+ signing_key:
71
114
  specification_version: 4
72
115
  summary: Rake tasks for fixing URLs to source maps
73
116
  test_files: []