Skip to content

Commit c249215

Browse files
committed
MSDF plugin fixes
- Fix plugin not finding any files when only one extension is specified - Handle multiple extensions for the same filename - Only one file will be used - The preferred file will be whatever is specified first in the extensions array
1 parent 81e885f commit c249215

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

.changeset/clean-hairs-begin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@plexinc/vite-plugin-msdf-fontgen": patch
3+
---
4+
5+
Fix plugin not finding files when only one extension is specified

.changeset/dirty-kiwis-scream.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@plexinc/vite-plugin-msdf-fontgen": patch
3+
---
4+
5+
Add support for multiple extensions of the same font file

packages/vite-plugin-msdf-fontgen/src/index.ts

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ interface Options {
3535
*/
3636
types?: ('msdf' | 'ssdf')[];
3737
/**
38-
* Font extensions to include. Defaults to ttf, otf, woff, and woff2
38+
* Font extensions to include. If multiple font files with the same name but
39+
* with different extensions are found, it will use the first one it finds
40+
* based on the order of this array. Defaults to ttf, otf, woff, and woff2
3941
*/
4042
extensions?: string[];
4143
/**
@@ -86,7 +88,7 @@ export default function msdfFontGen({
8688
}
8789

8890
const fontFolders = getFolders(inputDir, outDir, extensions);
89-
91+
console.log(fontFolders);
9092
for (const { input, output, files } of fontFolders) {
9193
console.log(`Generating fonts in folder ${input}...`);
9294

@@ -146,28 +148,53 @@ async function generateFont(
146148
}
147149

148150
function getFolders(inputDir: string[], outDir: string, extensions: string[]) {
151+
const extensionGlob =
152+
extensions.length > 1 ? `{${extensions.join(',')}}` : extensions[0];
149153
return Object.values(
150154
inputDir.reduce<
151-
Record<string, { input: string; output: string; files: string[] }>
155+
Record<
156+
string,
157+
{
158+
input: string;
159+
output: string;
160+
fontNames: Set<string>;
161+
files: string[];
162+
}
163+
>
152164
>((acc, input) => {
153-
const fontGlob = `${input}/**/*.{${extensions.join(',')}}`;
154-
const files = globSync(fontGlob);
165+
const files = globSync(`${input}/**/*.${extensionGlob}`);
155166

156-
for (const file of files) {
167+
// Sort files by extension so it follows the order of the extensions option
168+
for (const file of files.sort(sortByExtension(extensions))) {
157169
const baseFolder = path.dirname(file);
158170

159171
if (!acc[baseFolder]) {
160172
acc[baseFolder] = {
161173
input: baseFolder,
162174
output: path.join(outDir, path.relative(input, baseFolder)),
175+
fontNames: new Set(),
163176
files: [],
164177
};
165178
}
166179

167-
acc[baseFolder].files.push(path.relative(baseFolder, file));
180+
const fontName = path.basename(file, path.extname(file));
181+
182+
// Don't add the same font twice
183+
if (!acc[baseFolder].fontNames.has(fontName)) {
184+
acc[baseFolder].files.push(path.relative(baseFolder, file));
185+
acc[baseFolder].fontNames.add(fontName);
186+
}
168187
}
169188

170189
return acc;
171190
}, {}),
172191
);
173192
}
193+
194+
function sortByExtension(extensions: string[]) {
195+
return (a: string, b: string) => {
196+
const extA = a.split('.').pop() as string;
197+
const extB = b.split('.').pop() as string;
198+
return extensions.indexOf(extA) - extensions.indexOf(extB);
199+
};
200+
}

0 commit comments

Comments
 (0)