-
Notifications
You must be signed in to change notification settings - Fork 0
/
fonts.js
131 lines (112 loc) · 2.88 KB
/
fonts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import addToMapSet from './add-to-map-set.js'
import morphFontAsReactDom from './morph/react-dom/morph-font.js'
import morphFontAsReactNative from './morph/react-native/morph-fonts.js'
import path from 'path'
import sort from 'bubblesort'
import relativise from './relativise.js'
import ensureDir from './ensure-dir.js'
let morphFont = {
'react-dom': morphFontAsReactDom,
'react-native': morphFontAsReactNative,
'react-pdf': morphFontAsReactNative,
}
export async function ensureFontsDirectory(src) {
await ensureDir(path.join(src, 'DesignSystem', 'Fonts'))
}
export let getFontId = (file) => path.basename(file, path.extname(file))
let fontsOrder = ['eot', 'woff2', 'woff', 'ttf', 'svg', 'otf']
let sortFonts = (fonts) => {
return new Set(
sort(
[...fonts],
(a, b) => fontsOrder.indexOf(b.type) - fontsOrder.indexOf(a.type)
)
)
}
export function processCustomFonts({ customFonts, filesFontCustom }) {
for (let file of filesFontCustom) {
addToMapSet(customFonts, getFontId(file), file)
}
for (let [id, fonts] of customFonts) {
customFonts.set(id, sortFonts(fonts))
}
}
export function morphAllFonts({
as,
customFonts,
filesView,
src,
viewsToFiles,
}) {
let fontsDirectory = path.join(src, 'DesignSystem', 'Fonts')
let fontsInUse = new Set()
let mapCustomFont = (file) => ({
type: FONT_TYPES[path.extname(file)],
file: file.replace(fontsDirectory, '.'),
})
for (let file of filesView) {
let view = viewsToFiles.get(file)
if (view.custom) continue
view.parsed.fonts.forEach((font) => {
fontsInUse.add(font.id)
})
}
return [...fontsInUse].map((font) => {
let [family, weight, style = 'normal'] = font.split('-')
let customFontSources = []
if (customFonts.has(font)) {
customFontSources = [...customFonts.get(font)].map(mapCustomFont)
}
return {
file: path.join(
src,
'DesignSystem',
'Fonts',
`${font}${getFontFileExtension({ as })}`
),
content: morphFont[as](
{
id: font,
family,
style,
weight,
},
customFontSources
),
}
})
}
// let removeFont = file => {
// let id = getFontId(file)
// instance.customFonts = instance.customFonts.filter(font => font.id !== id)
// }
let FONT_TYPES = {
'.otf': 'opentype',
'.eot': 'eot',
'.svg': 'svg',
'.ttf': 'truetype',
'.woff': 'woff',
'.woff2': 'woff2',
}
export let makeGetFontImport = ({ as, src }) => (font, view) =>
`import "${relativise(
view.file,
path.join(
src,
'DesignSystem',
'Fonts',
`${font}${getFontFileExtension({ as })}`
),
src
)}"`
// let isFont = f => Object.keys(FONT_TYPES).includes(path.extname(f))
function getFontFileExtension({ as }) {
switch (as) {
case 'react-dom': {
return '.module.css'
}
default: {
return '.js'
}
}
}