source_maps_fixer 0.1.1 → 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: b46d1a5a92e0cfb9ce2cfa9fecc78ce1b16443677a4565fa9b7a0fee023bed8b
4
- data.tar.gz: 30b57467453f4c40690e453e71c7442fa6ac83fb769008da7b475765d26b96b0
3
+ metadata.gz: 8359d9c4a3b6c24bca36cc77d9f53cf762e5a14fc58bae51ca4354ca8e4ce0fb
4
+ data.tar.gz: 942a8bc352604d24148a0200eb9c27e40c7ea234d3f4f8de021a9f486aa8a633
5
5
  SHA512:
6
- metadata.gz: a8d4fed5ab9bfbab3d973dc102da6cba964b8505580169a9ba643405ed01e32741865bc5aac3e8ad4547daaffceb3fc843c14cc37fbe52e6c2e337e472b0627d
7
- data.tar.gz: 8736a808ddc9c31169502ba78718fe169c589760823ce03419a7a858a4f7dc9e4913f581bb90f5694afcd39b9fee1e7c7374b0a66e8febad4ea2b8ccee1ed5a8
6
+ metadata.gz: 4c27c8b97e5cd3a5e9b8c366c876feb3f1ed00d94a28b01609d4ad67ccf91b1c9b4d6aef26e4c5fc58dfeb5b53310f2bd8d753fa0938d72c90731210cdba14ef
7
+ data.tar.gz: 13877418eba6cd5a8e14281205b009b4ece82fc72f95db4844d58599459919552f6b5fc3e5c5500c3705e1201978e024994a32cd2f37f5c796f36267db5a44a7
data/README.md CHANGED
@@ -1,11 +1,37 @@
1
1
  # SourceMapsFixer
2
- Short description and motivation.
3
2
 
4
- ## Usage
5
- How to use my plugin.
3
+ SourceMapsFixer is a set of [Rake](https://ruby.github.io/rake/) tasks for fixing **sourceMappingURL**s.
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.
6
5
 
7
- ## Installation
8
- Add this line to your application's Gemfile:
6
+ ### Problem
7
+
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.
9
+ But when _Sprockets_ compile assets to `public/assets`, fingerprints are added to all asset filenames.
10
+ **sourceMappingURL**s are intact, which makes them point to invalid files without fingerprints.
11
+
12
+ ### Solution
13
+
14
+ _SourceMapsFixer_ fixes **sourceMappingURL**s inside bundles.
15
+
16
+
17
+ ## 🎮 Usage
18
+
19
+ Instead of running
20
+
21
+ ```bash
22
+ $ bin/rails assets:precompile
23
+ ```
24
+
25
+ run
26
+
27
+ ```bash
28
+ $ bin/rails assets:prepare
29
+ ```
30
+
31
+
32
+ ## 📥 Installation
33
+
34
+ Add *source\_maps\_fixer* to your application's Gemfile:
9
35
 
10
36
  ```ruby
11
37
  gem 'source_maps_fixer'
@@ -21,8 +47,26 @@ Or install it yourself as:
21
47
  $ gem install source_maps_fixer
22
48
  ```
23
49
 
24
- ## Contributing
25
- Contribution directions go here.
26
50
 
27
- ## License
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
+
63
+ ## 📜 License
28
64
  The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
65
+
66
+
67
+ ## 👨‍🏭 Author
68
+ Zbigniew Humeniuk from [Art of Code](https://artofcode.co)
69
+
70
+
71
+ ## 👀 See also
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.1'
4
+ VERSION = '0.2.0'
5
5
  end
@@ -3,46 +3,66 @@
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
 
18
- def self.source_mapping_url file_name
19
+ def self.source_mapping_url(file_name)
19
20
  case file_name.match?(/\.css/) ? :css : :js
20
21
  when :css
21
- "/*# sourceMappingURL=#{file_name}*/"
22
+ %(/*# sourceMappingURL=#{file_name}*/)
22
23
  when :js
23
- "//# sourceMappingURL=#{file_name}"
24
+ %(//# sourceMappingURL=#{file_name})
24
25
  end
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))
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)),
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.1
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: 2019-02-09 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: '6.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: '6.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.0.1
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: []