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 +4 -4
- data/README.md +1 -1
- data/lib/fontawesome_subsetter/subsetter.rb +55 -3
- data/lib/fontawesome_subsetter/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 5d4899f684cf0f8abb56aa022c5becbf72ff17dcb93c6120ae30825739ac515b
|
|
4
|
+
data.tar.gz: 077a8cf27d8fdf860fe5e6f5ce43b06bf5ba5e87616f9912c823d72a169ade0d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
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
|
|
120
|
-
|
|
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
|
-
//
|
|
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(", ") }),
|