Skip to content

Commit d606736

Browse files
fix: correct src file order by file compression value
1 parent 7a6c01f commit d606736

File tree

3 files changed

+57
-28
lines changed

3 files changed

+57
-28
lines changed

index.test.js

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,53 @@ async function run (input, output, opts = {}) {
99
expect(result.warnings()).toHaveLength(0);
1010
}
1111

12-
test('sortFiles sorts files by extension alphabetically', () => {
13-
const files = [
14-
{name: 'file3', ext: 'c'},
15-
{name: 'file1', ext: 'a'},
16-
{name: 'file4', ext: 'd'},
17-
{name: 'file5', ext: 'd'},
18-
{name: 'file2', ext: 'b'},
19-
];
20-
const expectedOrder = [
21-
{name: 'file1', ext: 'a'},
22-
{name: 'file2', ext: 'b'},
23-
{name: 'file3', ext: 'c'},
24-
{name: 'file4', ext: 'd'},
25-
{name: 'file5', ext: 'd'},
26-
];
27-
const sortedFiles = files.sort(sortFiles);
28-
expect(sortedFiles).toEqual(expectedOrder);
12+
describe('sortFiles', () => {
13+
it('should sort files based on weights', () => {
14+
const files = [
15+
{ name: 'file1', ext: 'ttf' },
16+
{ name: 'file2', ext: 'woff2' },
17+
{ name: 'file3', ext: 'woff' },
18+
{ name: 'file4', ext: 'otf' },
19+
];
20+
21+
const sortedFiles = files.sort(sortFiles);
22+
23+
expect(sortedFiles).toEqual([
24+
{ name: 'file2', ext: 'woff2' },
25+
{ name: 'file3', ext: 'woff' },
26+
{ name: 'file1', ext: 'ttf' },
27+
{ name: 'file4', ext: 'otf' },
28+
]);
29+
});
30+
31+
it('should sort files with the same weight based on extension', () => {
32+
const files = [
33+
{ name: 'file1', ext: 'woff' },
34+
{ name: 'file2', ext: 'woff2' },
35+
{ name: 'file3', ext: 'otf' },
36+
{ name: 'file4', ext: 'ttf' },
37+
{ name: 'file5', ext: 'woff2' },
38+
{ name: 'file6', ext: 'ttf' },
39+
{ name: 'file7', ext: 'woff' },
40+
{ name: 'file8', ext: 'otf' },
41+
];
42+
43+
const sortedFiles = files.sort(sortFiles);
44+
45+
expect(sortedFiles).toEqual([
46+
{ name: 'file2', ext: 'woff2' },
47+
{ name: 'file5', ext: 'woff2' },
48+
{ name: 'file1', ext: 'woff' },
49+
{ name: 'file7', ext: 'woff' },
50+
{ name: 'file4', ext: 'ttf' },
51+
{ name: 'file6', ext: 'ttf' },
52+
{ name: 'file3', ext: 'otf' },
53+
{ name: 'file8', ext: 'otf' },
54+
]);
55+
});
2956
});
3057

58+
describe('Generate font-face', () => {
3159
it('The console should display a warning message when no font files are found in the given directory', async () => {
3260
const result = await postcss([plugin({ fontsDir: './fonts/empty/' })]).process('', { from: undefined });
3361
expect(result.warnings()).toHaveLength(1);
@@ -36,7 +64,7 @@ it('The console should display a warning message when no font files are found in
3664
it('Create default output from files in font folder', async () => {
3765
const output = `@font-face {
3866
font-family: "Prague";
39-
src: local("Prague"), url("/fonts/Prague-Black.woff") format("woff"), url("/fonts/Prague-Black.woff2") format("woff2");
67+
src: local("Prague"), url("/fonts/Prague-Black.woff2") format("woff2"), url("/fonts/Prague-Black.woff") format("woff");
4068
font-weight: 900;
4169
font-style: normal;
4270
font-display: swap
@@ -47,7 +75,7 @@ it('Create default output from files in font folder', async () => {
4775
it('Create output from files in font folder without swap option', async () => {
4876
const output = `@font-face {
4977
font-family: "Prague Special";
50-
src: local("Prague Special"), url("/fonts/PragueSpecial-ExtraLightItalic.ttf") format("ttf"), url("/fonts/PragueSpecial-ExtraLightItalic.woff") format("woff"), url("/fonts/PragueSpecial-ExtraLightItalic.woff2") format("woff2");
78+
src: local("Prague Special"), url("/fonts/PragueSpecial-ExtraLightItalic.woff2") format("woff2"), url("/fonts/PragueSpecial-ExtraLightItalic.woff") format("woff"), url("/fonts/PragueSpecial-ExtraLightItalic.ttf") format("ttf");
5179
font-weight: 200;
5280
font-style: italic
5381
}`;
@@ -57,9 +85,10 @@ it('Create output from files in font folder without swap option', async () => {
5785
it('Create output from files in font folder without swap and local option', async () => {
5886
const output = `@font-face {
5987
font-family: "Prague Special";
60-
src: url("/fonts/PragueSpecial-ExtraLightItalic.ttf") format("ttf"), url("/fonts/PragueSpecial-ExtraLightItalic.woff") format("woff"), url("/fonts/PragueSpecial-ExtraLightItalic.woff2") format("woff2");
88+
src: url("/fonts/PragueSpecial-ExtraLightItalic.woff2") format("woff2"), url("/fonts/PragueSpecial-ExtraLightItalic.woff") format("woff"), url("/fonts/PragueSpecial-ExtraLightItalic.ttf") format("ttf");
6189
font-weight: 200;
6290
font-style: italic
6391
}`;
6492
await run('', output, { fontsDir: './fonts/PragueSpecial/', swap: false, local: false });
6593
});
94+
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "postcss-fontify",
3-
"version": "1.0.1",
3+
"version": "1.0.2",
44
"description": "PostCSS plugin for generating font-face declarations from font files in a directory.",
55
"keywords": [
66
"postcss",

utils.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
const sortFiles = (a, b) => {
2-
if (a.ext < b.ext) {
3-
return -1;
4-
}
5-
if (a.ext > b.ext) {
6-
return 1;
2+
const weights = { woff2: 4, woff: 3, ttf: 2, otf: 1 };
3+
const weightA = weights[a.ext];
4+
const weightB = weights[b.ext];
5+
6+
if (weightA !== weightB) {
7+
return weightB - weightA;
78
}
8-
return 0;
9-
}
9+
};
1010

1111
module.exports = {
1212
sortFiles,

0 commit comments

Comments
 (0)