Skip to content

Commit 8191815

Browse files
authored
fix: include subdirectory in filePath for nested fonts (#5)
Previously fonts in subdirectories only had the basename in filePath. Now the relative path from the input directory is preserved. Example: my-fonts/subfolder/Font.ttf → filePath: 'subfolder/Font.ttf'
1 parent 11ae93a commit 8191815

2 files changed

Lines changed: 28 additions & 5 deletions

File tree

lib/bundle.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,22 @@ function toArrayBuffer(buffer) {
99
return buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength);
1010
}
1111

12-
async function findFontFiles(dir) {
12+
async function findFontFiles(dir, baseDir = dir) {
1313
const fontFiles = [];
1414
const entries = await fs.readdir(dir, { withFileTypes: true });
1515

1616
for (const entry of entries) {
1717
const fullPath = path.join(dir, entry.name);
1818

1919
if (entry.isDirectory()) {
20-
const subFiles = await findFontFiles(fullPath);
20+
const subFiles = await findFontFiles(fullPath, baseDir);
2121
fontFiles.push(...subFiles);
2222
} else if (entry.isFile()) {
2323
const ext = path.extname(entry.name).toLowerCase();
2424
if (FONT_EXTENSIONS.has(ext)) {
25-
fontFiles.push({ path: fullPath, name: entry.name });
25+
// Store relative path from base input directory
26+
const relativePath = path.relative(baseDir, fullPath);
27+
fontFiles.push({ path: fullPath, relativePath });
2628
}
2729
}
2830
}
@@ -36,9 +38,9 @@ export async function createBundle(inputDir, outputPath, options = {}) {
3638
const fontFiles = await findFontFiles(inputDir);
3739
const availableFonts = [];
3840

39-
for (const { path: fontPath, name } of fontFiles) {
41+
for (const { path: fontPath, relativePath } of fontFiles) {
4042
const fileBuffer = await fs.readFile(fontPath);
41-
const result = await processFont(toArrayBuffer(fileBuffer), name);
43+
const result = await processFont(toArrayBuffer(fileBuffer), relativePath);
4244

4345
if (Array.isArray(result)) {
4446
availableFonts.push(...result);

test/bundle.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,25 @@ describe('createBundle', () => {
7474

7575
expect(result.fontsProcessed).toBe(1);
7676
});
77+
78+
it('should include subdirectory in filePath for nested fonts', async () => {
79+
// Create nested structure with known subdirectory
80+
const nestedDir = path.join(__dirname, 'output', 'nested-path-test');
81+
const subDir = path.join(nestedDir, 'my-subfolder');
82+
await fs.mkdir(subDir, { recursive: true });
83+
await fs.copyFile(
84+
path.join(fixturesDir, 'Anton-Regular.ttf'),
85+
path.join(subDir, 'Anton-Regular.ttf')
86+
);
87+
88+
const nestedOutput = path.join(__dirname, 'output', 'nested-path-fonts.json');
89+
await createBundle(nestedDir, nestedOutput);
90+
91+
const content = await fs.readFile(nestedOutput, 'utf-8');
92+
const json = JSON.parse(content);
93+
94+
// filePath should include the subdirectory
95+
const font = json.availableFonts[0];
96+
expect(font.filePath).toBe('my-subfolder/Anton-Regular.ttf');
97+
});
7798
});

0 commit comments

Comments
 (0)