rake_source_maps_fixer 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +72 -0
- data/Rakefile +29 -0
- data/lib/rake_source_maps_fixer/railtie.rb +11 -0
- data/lib/rake_source_maps_fixer/version.rb +5 -0
- data/lib/rake_source_maps_fixer.rb +69 -0
- data/lib/tasks/fix_source_maps.rake +24 -0
- data/lib/tasks/prepare.rake +11 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c54ebb9354aa3d2256298c98513c7168c36a03e1f37a5dca4bfd2ef0ecdf05d2
|
4
|
+
data.tar.gz: 1c59da177bf8833c84a3db5925ecceff3f7deb68c846e936dd21ed39ec0bded1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3fcd892fe68beaa35699d88ba7a3588777372a97f5206cbe66ee9783e48f1f27051d6520806b511ea0e483986bd3a30dceb1b0f4e0cc0a499efb22c837bf7389
|
7
|
+
data.tar.gz: c4e77f657e9576c918821cfb655ccc08d9ade659af88703fa3b31dddb447f649385803d30b4cecb634dc7fbec04f9182ee5ad9e5bb92369bc9aff2da4ba74a6d
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2018 Zbigniew Humeniuk
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# RakeSourceMapsFixer
|
2
|
+
|
3
|
+
RakeSourceMapsFixer 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.
|
5
|
+
|
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
|
+
_RakeSourceMapsFixer_ 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:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
gem 'rake_source_maps_fixer'
|
38
|
+
```
|
39
|
+
|
40
|
+
And then execute:
|
41
|
+
```bash
|
42
|
+
$ bundle
|
43
|
+
```
|
44
|
+
|
45
|
+
Or install it yourself as:
|
46
|
+
```bash
|
47
|
+
$ gem install rake_source_maps_fixer
|
48
|
+
```
|
49
|
+
|
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
|
+
|
63
|
+
## 📜 License
|
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).
|
data/Rakefile
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
rescue LoadError
|
6
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
7
|
+
end
|
8
|
+
|
9
|
+
require 'rdoc/task'
|
10
|
+
|
11
|
+
RDoc::Task.new(:rdoc) do |rdoc|
|
12
|
+
rdoc.rdoc_dir = 'rdoc'
|
13
|
+
rdoc.title = 'RakeSourceMapsFixer'
|
14
|
+
rdoc.options << '--line-numbers'
|
15
|
+
rdoc.rdoc_files.include('README.md')
|
16
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'bundler/gem_tasks'
|
20
|
+
|
21
|
+
require 'rake/testtask'
|
22
|
+
|
23
|
+
Rake::TestTask.new(:test) do |t|
|
24
|
+
t.libs << 'test'
|
25
|
+
t.pattern = 'test/**/*_test.rb'
|
26
|
+
t.verbose = false
|
27
|
+
end
|
28
|
+
|
29
|
+
task default: :test
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rake_source_maps_fixer/railtie'
|
4
|
+
|
5
|
+
module RakeSourceMapsFixer
|
6
|
+
module Path
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def files_with_source_maps(filter = '')
|
10
|
+
Dir.glob("#{Rails.root.join 'app', 'assets'}/**/*")
|
11
|
+
.grep(/#{filter}\.map\Z/)
|
12
|
+
.to_h { |name| [name.sub('.map', ''), name] }
|
13
|
+
end
|
14
|
+
|
15
|
+
def digest_path(file_name)
|
16
|
+
Rails.application.assets.find_asset(file_name).digest_path
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.source_mapping_url(file_name)
|
20
|
+
case file_name.match?(/\.css/) ? :css : :js
|
21
|
+
when :css
|
22
|
+
%(/*# sourceMappingURL=#{file_name}*/)
|
23
|
+
when :js
|
24
|
+
%(//# sourceMappingURL=#{file_name})
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
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"
|
37
|
+
)
|
38
|
+
File.write(asset_path, new_content)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
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))
|
47
|
+
)
|
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)
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :assets do
|
4
|
+
desc 'Fix URLs to source maps'
|
5
|
+
task fix_source_maps: [:environment] do
|
6
|
+
puts 'Fixing URLs to source maps...'
|
7
|
+
RakeSourceMapsFixer::ReplaceLinksWithPredicted.call
|
8
|
+
puts 'URLs to source maps have been fixed!'
|
9
|
+
end
|
10
|
+
|
11
|
+
desc 'Revert fixed URLs to source maps'
|
12
|
+
task undo_fix_source_maps: [:environment] do
|
13
|
+
puts 'Reverting fixed URLs to source maps...'
|
14
|
+
RakeSourceMapsFixer::ReplaceLinksWithPredicted.undo
|
15
|
+
puts 'URLs to source maps have been reverted!'
|
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
|
+
RakeSourceMapsFixer::ReplaceLinksWithPredicted.call
|
22
|
+
puts 'URLs to source maps have been fixed!'
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
namespace :assets do
|
4
|
+
desc 'Fix URLs to source maps and compile assets'
|
5
|
+
task prepare: [:environment] do
|
6
|
+
RakeSourceMapsFixer::ReplaceLinksWithPredicted.call
|
7
|
+
Rake::Task['assets:precompile'].invoke
|
8
|
+
RakeSourceMapsFixer::ReplaceLinksWithPredicted.undo
|
9
|
+
puts 'Assets have been prepared!'
|
10
|
+
end
|
11
|
+
end
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rake_source_maps_fixer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Zbigniew Humeniuk
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2022-03-07 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '8.0'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '5.0'
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
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.4.2
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: 3.4.2
|
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'
|
75
|
+
description: Rake tasks that fix sourceMappingURL in JavaScript and CSS bundles generated
|
76
|
+
by webpack to match the final fingerprinted names generated by Sprockets after assets'
|
77
|
+
precompilation
|
78
|
+
email:
|
79
|
+
- hello@artofcode.co
|
80
|
+
executables: []
|
81
|
+
extensions: []
|
82
|
+
extra_rdoc_files: []
|
83
|
+
files:
|
84
|
+
- MIT-LICENSE
|
85
|
+
- README.md
|
86
|
+
- Rakefile
|
87
|
+
- lib/rake_source_maps_fixer.rb
|
88
|
+
- lib/rake_source_maps_fixer/railtie.rb
|
89
|
+
- lib/rake_source_maps_fixer/version.rb
|
90
|
+
- lib/tasks/fix_source_maps.rake
|
91
|
+
- lib/tasks/prepare.rake
|
92
|
+
homepage: https://artofcode.co
|
93
|
+
licenses:
|
94
|
+
- MIT
|
95
|
+
metadata:
|
96
|
+
rubygems_mfa_required: 'true'
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: 2.7.0
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubygems_version: 3.2.3
|
113
|
+
signing_key:
|
114
|
+
specification_version: 4
|
115
|
+
summary: Rake tasks for fixing URLs to source maps
|
116
|
+
test_files: []
|