fontawesome_subsetter 0.1.3 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3f3b31b1f9c9b187bead63801821cf4361d70254b9419b871a493871a75fb2d0
4
- data.tar.gz: 740a2f6547e6d10f3a8c5627f944a4869e263d856f11219a597a46a1e6367d8e
3
+ metadata.gz: fd9237f5e86d63481cb5cb18ab422008f36f5ae4f32f2116f24d1549e964b2a3
4
+ data.tar.gz: b55ca058bde22b4b7a454ae0fcb211dc2c6df33aa87d10de381ea09495c3608f
5
5
  SHA512:
6
- metadata.gz: e8c84e173d49c219764c998fc2249df1b18f398944cfd6855f5eabb5d09bf2e31f0a116b847cf118e8044cd01b893467957c5abb23a4d5852c7c1fad1fb11a47
7
- data.tar.gz: 923cb195f65901177ac1f1daf90b2c60a7fd6ae07fc54efdd4e75db193edd9c71cea8416cba1402fa7a94a93a186d3d8c8baeda8ef0d8540c65b783351b200d1
6
+ metadata.gz: 54c84abeeceb8dfdc1337358d4ff3c03022a578760e04dbea6746c5ac239bc92ac3fbd62b5a8074803c345e526caef01985a8d354cc721600605a1d8bd6a5f1c
7
+ data.tar.gz: 64b61927a5dae3874a74a9cdb2dfe6918e925d605c55612ec6577a950e757989010f792d2055821ed974ec00b8ee61c606c9b4587a433e39d3656adced3f8a88
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # FontAwesome Subsetter
2
2
 
3
- A Rails gem that tree-shakes FontAwesome — scans your views and helpers for `icon()` calls, subsets WOFF2 font files to include only used glyphs, and generates minimal CSS.
3
+ A Rails gem that tree-shakes FontAwesome 7 — scans your views and helpers for `icon()` calls, subsets WOFF2 font files to include only used glyphs, and generates minimal CSS.
4
4
 
5
5
  ## Requirements
6
6
 
@@ -47,8 +47,6 @@ vendor/
47
47
  fa-thin-100.woff2
48
48
  ```
49
49
 
50
- > **Note:** In `vendor/fontawesome/scss/variables.scss`, change `$icons` and `$brand-icons` to empty maps with `!default` so the subsetter can inject only the used icons.
51
-
52
50
  ## Features
53
51
 
54
52
  ### 1. Icon Helper
@@ -67,7 +65,7 @@ Add to your `config/puma.rb`:
67
65
 
68
66
  ```ruby
69
67
  if ENV.fetch("RAILS_ENV", "development") == "development"
70
- plugin :fontawesome
68
+ plugin :fontawesome_subsetter
71
69
  end
72
70
  ```
73
71
 
@@ -3,9 +3,51 @@ require "open3"
3
3
  require "fileutils"
4
4
  require "yaml"
5
5
  require "set"
6
+ require "uri"
6
7
 
7
8
  module FontawesomeSubsetter
8
9
 
10
+ # Custom Sass importer that intercepts FontAwesome SCSS file loads.
11
+ # Strips the populated $icons and $brand-icons maps from variables.scss in memory,
12
+ # replacing them with empty !default maps so the subsetter can inject only used icons.
13
+ # This avoids requiring users to manually edit their vendor FontAwesome files.
14
+ class FontawesomeSassImporter
15
+
16
+ def initialize(scss_dir)
17
+ @scss_dir = File.expand_path(scss_dir.to_s)
18
+ end
19
+
20
+ def canonicalize(url, context)
21
+ if url.start_with?("file://")
22
+ path = URI.parse(url).path
23
+ elsif context.containing_url
24
+ base = File.dirname(URI.parse(context.containing_url.to_s).path)
25
+ path = File.expand_path(url, base)
26
+ else
27
+ return nil
28
+ end
29
+
30
+ candidates = [path, "#{path}.scss", File.join(File.dirname(path), "_#{File.basename(path)}.scss")]
31
+ match = candidates.find { |c| File.exist?(c) && c.start_with?(@scss_dir) }
32
+ return nil unless match
33
+
34
+ "file://#{match}"
35
+ end
36
+
37
+ def load(url)
38
+ path = URI.parse(url).path
39
+ content = File.read(path)
40
+
41
+ if File.basename(path, ".scss").delete_prefix("_") == "variables"
42
+ content = content.sub(/^\$icons\s*:\s*\(.*?^\)\s*;/m, "$icons: () !default;")
43
+ content = content.sub(/^\$brand-icons\s*:\s*\(.*?^\)\s*;/m, "$brand-icons: () !default;")
44
+ end
45
+
46
+ { contents: content, syntax: :scss }
47
+ end
48
+
49
+ end
50
+
9
51
  class Subsetter
10
52
 
11
53
  def initialize(root: nil)
@@ -116,8 +158,18 @@ module FontawesomeSubsetter
116
158
  default_scss_template(styles)
117
159
  end
118
160
 
119
- # Compile the dynamic SCSS. `load_paths` tells Sass where to find the imported files.
120
- compiled_css = Sass.compile_string(scss_string, style: :compressed, load_paths: [ @root ]).css
161
+ # Compile the dynamic SCSS with a custom importer that strips populated icon maps
162
+ # from variables.scss in memory, so users don't need to modify their vendor files.
163
+ scss_dir = @root.join("vendor", "fontawesome", "scss").to_s
164
+ fa_importer = FontawesomeSassImporter.new(scss_dir)
165
+
166
+ compiled_css = Sass.compile_string(
167
+ scss_string,
168
+ style: :compressed,
169
+ importer: fa_importer,
170
+ importers: [fa_importer],
171
+ url: "file://#{@root}/fontawesome_subsetter_input.scss"
172
+ ).css
121
173
 
122
174
  # Forcefully remove all comments, including "loud" /*! ... */ comments for licenses.
123
175
  compiled_css = compiled_css.gsub(/\/\*!.*?\*\//m, "")
@@ -184,7 +236,7 @@ module FontawesomeSubsetter
184
236
  def default_scss_template(styles)
185
237
  <<-SCSS
186
238
 
187
- // NOTE: $icons and $brand-icons were changed in variables to just be empty arrays with !default.
239
+ // The custom importer strips populated $icons/$brand-icons maps from variables.scss in memory.
188
240
  @use "./vendor/fontawesome/scss/variables" with (
189
241
  $font-path: "webfonts",
190
242
  $icons: (#{ @styles.except("brands").values.reduce(Set.new) { | all_icons, style | all_icons.merge(style[:sass_icon_cache]) }.join(", ") }),
@@ -1,5 +1,5 @@
1
1
  module FontawesomeSubsetter
2
2
 
3
- VERSION = "0.1.3"
3
+ VERSION = "0.1.5"
4
4
 
5
5
  end
@@ -1,6 +1,6 @@
1
1
  require "puma/plugin"
2
2
 
3
- class Fontawesome
3
+ class FontawesomeSubsetter
4
4
  end
5
5
 
6
6
  Puma::Plugin.create do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fontawesome_subsetter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - 16554289+optimuspwnius@users.noreply.github.com
@@ -95,7 +95,7 @@ files:
95
95
  - lib/fontawesome_subsetter/icon_helper.rb
96
96
  - lib/fontawesome_subsetter/subsetter.rb
97
97
  - lib/fontawesome_subsetter/version.rb
98
- - lib/puma/plugin/fontawesome.rb
98
+ - lib/puma/plugin/fontawesome_subsetter.rb
99
99
  - lib/tasks/fontawesome_subsetter.rake
100
100
  homepage: https://github.com/optimuspwnius/fontawesome-subsetter
101
101
  licenses: