source_maps_fixer 0.1.4 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +17 -9
- data/lib/source_maps_fixer/version.rb +1 -1
- data/lib/source_maps_fixer.rb +39 -19
- data/lib/tasks/fix_source_maps.rake +9 -2
- data/lib/tasks/prepare.rake +2 -2
- metadata +53 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3808cb69c2e1c696147484defa76811e3830dff5815c80026ba2b11546f01708
|
4
|
+
data.tar.gz: f74ed786448bde73d78d474eb77de3f2c8f167ffe78bcc6af1ee30025a221bfe
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45e3c83e12804cc4be8781f3398cd49ebe45816dcbdc1c8663a7714283ca10988c4ca20d3cb9867b59bcfd8403529d950b42b162455bf0d5d8e171b558658746
|
7
|
+
data.tar.gz: b250a720bb027c9cf38e54c816df8e0e5c1858ffc3d59b253b97f6c4ac8b1a73d6db810263e9898641fa5a80fc2d6de9df789c9eec760a09cc2efe9ffb1444a8
|
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
|
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
|
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
|
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
|
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
|
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/lib/source_maps_fixer.rb
CHANGED
@@ -3,15 +3,16 @@
|
|
3
3
|
require 'source_maps_fixer/railtie'
|
4
4
|
|
5
5
|
module SourceMapsFixer
|
6
|
-
|
7
|
-
|
6
|
+
module Path
|
7
|
+
module_function
|
8
|
+
|
9
|
+
def files_with_source_maps(filter = '')
|
8
10
|
Dir.glob("#{Rails.root.join 'app', 'assets'}/**/*")
|
9
|
-
.
|
10
|
-
.
|
11
|
-
.to_h
|
11
|
+
.grep(/#{filter}\.map\Z/)
|
12
|
+
.to_h { |name| [name.sub('.map', ''), name] }
|
12
13
|
end
|
13
14
|
|
14
|
-
def
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
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.
|
38
|
+
File.write(asset_path, new_content)
|
36
39
|
end
|
37
40
|
end
|
38
41
|
|
39
|
-
def
|
40
|
-
Path.files_with_source_maps.each do |
|
41
|
-
new_content = File.read(
|
42
|
-
Path.source_mapping_url(Path.digest_path(
|
43
|
-
Path.source_mapping_url(File.basename(
|
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.
|
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::
|
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::
|
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
|
data/lib/tasks/prepare.rake
CHANGED
@@ -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::
|
6
|
+
SourceMapsFixer::ReplaceLinksWithPredicted.call
|
7
7
|
Rake::Task['assets:precompile'].invoke
|
8
|
-
SourceMapsFixer::
|
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
|
+
version: 0.2.1
|
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:
|
11
|
+
date: 2023-03-29 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: '
|
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: '
|
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: 3.8.0
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 3.8.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: 1.48.1
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 1.48.1
|
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
|
-
|
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:
|
105
|
+
version: 3.0.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.
|
70
|
-
signing_key:
|
112
|
+
rubygems_version: 3.4.6
|
113
|
+
signing_key:
|
71
114
|
specification_version: 4
|
72
115
|
summary: Rake tasks for fixing URLs to source maps
|
73
116
|
test_files: []
|