fontawesome_subsetter 0.1.3 → 0.1.4

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: 5d4899f684cf0f8abb56aa022c5becbf72ff17dcb93c6120ae30825739ac515b
4
+ data.tar.gz: 077a8cf27d8fdf860fe5e6f5ce43b06bf5ba5e87616f9912c823d72a169ade0d
5
5
  SHA512:
6
- metadata.gz: e8c84e173d49c219764c998fc2249df1b18f398944cfd6855f5eabb5d09bf2e31f0a116b847cf118e8044cd01b893467957c5abb23a4d5852c7c1fad1fb11a47
7
- data.tar.gz: 923cb195f65901177ac1f1daf90b2c60a7fd6ae07fc54efdd4e75db193edd9c71cea8416cba1402fa7a94a93a186d3d8c8baeda8ef0d8540c65b783351b200d1
6
+ metadata.gz: ca16888526892dcf518fc911d40a8a380f3085d391ae773b88ba8854ab390533a77a5225852a392de132d24805772c41f0490456962f2472a1cb854ef714ba34
7
+ data.tar.gz: 817c69ec6b911debdd143c902294e18aeb019eb539aa8164de6beae19ff16eaa6419381d0d15d8f9907e064e8db9a9f6929f65f08769cd5c753e750c7eebde1d
data/README.md CHANGED
@@ -47,7 +47,7 @@ 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.
50
+ The gem's custom Sass importer automatically handles FontAwesome's `variables.scss` in memory — it strips the large populated `$icons` and `$brand-icons` maps and replaces them with empty `!default` maps at compile time. **No manual edits to your vendor FontAwesome files are needed.**
51
51
 
52
52
  ## Features
53
53
 
@@ -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.4"
4
4
 
5
5
  end
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.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - 16554289+optimuspwnius@users.noreply.github.com