forked from IBM/plex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-scss.js
More file actions
158 lines (138 loc) · 5.1 KB
/
Copy pathgenerate-scss.js
File metadata and controls
158 lines (138 loc) · 5.1 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* `scripts/export-css.js` is used to help generate all the sass files that we
* need for each font family, supported weight, and unicode range.
*/
const fs = require('fs-extra');
const path = require('path');
const rimraf = require('rimraf');
const families = require('./data/families');
const { formatFilename, createFontFace } = require('./tools');
const unicodes = require('./data/unicodes');
const weights = require('./data/weights');
const OUTPUT_DIRECTORY = path.resolve(__dirname, '../scss');
/**
* The general flow for this is to iterate through families, weights, and
* unicodes, using them to generate an array of objects that contains
* `filename` and `content` fields.
*
* We then use this array to write all the necessary files that we need to
* support our use-case.
*/
const filesToWrite = families
.map(family => {
// Don't generate scss if the font doesn't have italic weights
const filteredWeights = weights.filter(weight => {
const italicWeight =
weight.variant === 'Italic' || weight.type === 'Italic';
if (italicWeight) {
return family.hasItalic;
}
return true;
});
const files = filteredWeights
.map(weight => {
// When mapping over the unicodes, we'll create the actual @font-face
// declarations and the appropriate file labeled by the unicode's type
// field.
const innerFiles = unicodes
.filter(unicode => family.unicodes.includes(unicode.type))
.map(unicode => {
const fileNameRoot = family.preferredName || family.type;
const filename = formatFilename([
fileNameRoot,
weight.type,
weight.variant,
`_${unicode.type}.scss`,
]);
return {
filename: `${OUTPUT_DIRECTORY}/${filename.split(' ').join('-')}`,
content: createFontFace(family, weight, unicode),
unicode,
};
})
.filter(Boolean);
// console.log(innerFiles)
// Create a helpful `_index.scss` partial that imports all of the
// unicode files that were generated above.
const fileNameRoot = family.preferredName || family.type;
const filename = formatFilename([
fileNameRoot,
weight.type,
weight.variant,
'_index.scss',
]);
const order = {
Cyrillic: 1,
Pi: 2,
Latin3: 3,
Latin2: 4,
Latin1: 5,
};
const contentSplit = innerFiles
.sort((a, b) => order[a.unicode.type] - order[b.unicode.type])
.filter(({ unicode }) => family.unicodes.includes(unicode.type))
.map(({ unicode }) => {
const importPath = formatFilename([unicode.type]);
return `@import '${importPath}';`;
})
.join('\n');
const contentWhole = createFontFace(family, weight);
// We spread all the inner files, since they are valid files that we'll
// want to create in the future, and then reduce over the whole
// collection to flatten the array entries.
// String contentWhole (woff) goes before contentSplit (woff2) so modern
// browsers will look for split files.
return [
...innerFiles,
{
hasItalic: family.hasItalic,
filename: `${OUTPUT_DIRECTORY}/${filename.split(' ').join('-')}`,
content: `$font-prefix: '..' !default;\n${contentWhole}\n${contentSplit}`,
weight,
},
];
})
.filter(Boolean)
.reduce((acc, array) => acc.concat(array), []);
// Here we'll generate a `_index.scss` partial for a specific font family
// that includes all the various weight files generated for the font-family.
// If a family is split from the core stylesheet, we don't use the underscore
// since it isn't a partial
const fontFileRoot = family.preferredName || family.type;
const filename = `${OUTPUT_DIRECTORY}/${fontFileRoot.toLowerCase()
.split(' ')
.join('-')}/${family.ownStyleSheet ? 'index.scss' : '_index.scss'}`;
const content = files
.filter(file => file.weight)
.map(({ weight }) => {
const importPath = formatFilename([weight.type, weight.variant]);
return `@import '${importPath}/index';`;
})
.join('\n');
return [
...files,
{
filename,
content: `$font-prefix: '..' !default;\n${content}`,
},
];
})
.reduce((acc, array) => acc.concat(array));
// Write all the files that we created above
filesToWrite.forEach(({ filename, content }) => {
fs.outputFileSync(filename, content, 'utf8');
});
const familyStrings = families.map(family => {
if (family.ownStyleSheet) {
return;
}
const fontFileRoot = family.preferredName || family.type;
return `@import '${fontFileRoot
.split(' ')
.join('-')
.toLowerCase()}/index';`;
});
// Create partial for all families
const rootPartial =
`$font-prefix: '..' !default;\n\n` + familyStrings.join('\n');
fs.outputFileSync(`${OUTPUT_DIRECTORY}/ibm-plex.scss`, rootPartial, 'utf8');