Skip to content

Commit e073361

Browse files
authored
Merge pull request #47 from isBatak/feat/add-support-for-webp-illustration
feat: illustration extensions
2 parents b96a539 + 09f5365 commit e073361

10 files changed

Lines changed: 91 additions & 23 deletions

File tree

.changeset/sixty-masks-drop.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
"@ikona/cli": patch
3+
---
4+
5+
Add config for illustration file extension (webp)
6+
7+
```js
8+
{
9+
illustrations: {
10+
extensions: ['svg', 'png', 'jpg', 'jpeg', 'webp'],
11+
},
12+
}
13+
```

examples/nextjs/ikona.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ export default defineConfig({
1010
},
1111
illustrations: {
1212
inputDir: "public/illustrations",
13+
extensions: ["svg", "png", "jpg", "jpeg", "webp"],
1314
},
1415
});

packages/cli/__tests__/fixtures/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Config } from "../../src";
22

33
export const configFixture: Config = {
4+
verbose: false,
45
outputDir: "output",
56
icons: {
67
inputDir: "icons",
@@ -10,6 +11,7 @@ export const configFixture: Config = {
1011
},
1112
illustrations: {
1213
inputDir: "illustrations",
14+
extensions: ["svg", "png", "jpg", "jpeg", "webp"],
1315
},
1416
force: false,
1517
cwd: "",

packages/cli/__tests__/illustrations/types.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ describe("types", () => {
1818
"book.png": "png",
1919
"song.jpg": "jpg",
2020
"song.jpeg": "jpeg",
21+
"song.webp": "webp",
2122
},
2223
});
2324

@@ -35,6 +36,7 @@ describe("types", () => {
3536
"/illustrations/email.svg",
3637
"/illustrations/song.jpeg",
3738
"/illustrations/song.jpg",
39+
"/illustrations/song.webp",
3840
] satisfies Array<IllustrationPath>;
3941
"
4042
`);
@@ -49,7 +51,8 @@ describe("types", () => {
4951
| '/illustrations/book.png'
5052
| '/illustrations/email.svg'
5153
| '/illustrations/song.jpeg'
52-
| '/illustrations/song.jpg';
54+
| '/illustrations/song.jpg'
55+
| '/illustrations/song.webp';
5356
"
5457
`);
5558
});

packages/cli/__tests__/utils/config.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@ jest.mock("path", () => ({
1616
describe("defaultConfig", () => {
1717
it("should have the correct default values", () => {
1818
expect(defaultConfig).toEqual({
19-
outputDir: ".ikona",
2019
cwd: process.cwd(),
20+
force: false,
21+
icons: {
22+
hash: false,
23+
inputDir: "icons",
24+
optimize: false,
25+
spriteOutputDir: "output",
26+
},
27+
illustrations: {
28+
extensions: ["svg", "png", "jpg", "jpeg", "webp"],
29+
inputDir: "illustrations",
30+
},
31+
outputDir: ".ikona",
32+
verbose: false,
2133
});
2234
});
2335
});

packages/cli/src/illustrations/types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { writeIfChanged } from "../utils/validations";
66
import type { Config } from "../types";
77
import { illustrationsTemplate } from "./templates/illustrations";
88
import { pathsTemplate } from "./templates/paths";
9+
import { getIllustrationsExtensionsGlobPattern } from "../utils/glob";
910

1011
interface GenerateIconFilesOptions {
1112
files: Array<string>;
@@ -99,9 +100,12 @@ export async function generateIllustrationTypes(config: Config) {
99100
]);
100101

101102
const files = glob
102-
.sync("**/*.{svg,png,jpg,jpeg}", {
103-
cwd: inputDir,
104-
})
103+
.sync(
104+
getIllustrationsExtensionsGlobPattern(config.illustrations.extensions),
105+
{
106+
cwd: inputDir,
107+
}
108+
)
105109
.sort((a, b) => a.localeCompare(b));
106110

107111
if (files.length === 0) {

packages/cli/src/types.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ export type Prettify<T> = {
22
[K in keyof T]: T[K];
33
} & unknown;
44

5+
type DeepPartial<T> = {
6+
[P in keyof T]?: T[P] extends Record<string, unknown>
7+
? DeepPartial<T[P]>
8+
: T[P];
9+
};
10+
511
export interface Config {
6-
verbose?: boolean;
12+
verbose: boolean;
713

814
/**
915
* Directory where the generated files
@@ -14,31 +20,28 @@ export interface Config {
1420
/**
1521
* Force generation of files
1622
*/
17-
force?: boolean;
23+
force: boolean;
1824

1925
icons: {
20-
optimize?: boolean;
26+
optimize: boolean;
2127
inputDir: string;
2228
spriteOutputDir: string;
2329

2430
/**
2531
* Hash sprite file name and export it as a JS constant.
2632
*/
27-
hash?: boolean;
33+
hash: boolean;
2834
};
2935

3036
illustrations: {
3137
inputDir: string;
38+
extensions: string[];
3239
};
3340

3441
cwd: string;
3542
}
3643

37-
export type DefaultConfig = Required<Pick<Config, "outputDir" | "cwd">>;
38-
39-
export type FileConfig = Prettify<
40-
Omit<Config, "outputDir" | "cwd"> & Partial<Pick<Config, "outputDir" | "cwd">>
41-
>;
44+
export type FileConfig = Prettify<DeepPartial<Config>>;
4245

4346
export interface CliConfig {
4447
verbose?: boolean;

packages/cli/src/utils/config.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
import { bundleNRequire } from "bundle-n-require";
22
import findUp from "escalade/sync";
33
import { resolve } from "path";
4-
import { DefaultConfig } from "../types";
4+
import { Config } from "../types";
55

6-
export const defaultConfig: DefaultConfig = {
6+
export const defaultConfig: Config = {
7+
verbose: false,
78
outputDir: ".ikona",
9+
force: false,
10+
icons: {
11+
optimize: false,
12+
inputDir: "icons",
13+
spriteOutputDir: "output",
14+
hash: false,
15+
},
16+
illustrations: {
17+
inputDir: "illustrations",
18+
extensions: ["svg", "png", "jpg", "jpeg", "webp"],
19+
},
820
cwd: process.cwd(),
921
};
1022

packages/cli/src/utils/glob.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function getIllustrationsExtensionsGlobPattern(extensions: string[]) {
2+
return `**/*.{${extensions.join(",")}}`;
3+
}

packages/cli/src/utils/merge-config.ts

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@ export interface MergeConfigProps {
99

1010
export const mergeConfigs = ({ cliConfig, fileConfig }: MergeConfigProps) => {
1111
return {
12-
verbose: cliConfig.v ?? cliConfig.verbose ?? fileConfig.verbose,
12+
verbose:
13+
cliConfig.v ??
14+
cliConfig.verbose ??
15+
fileConfig.verbose ??
16+
defaultConfig.verbose,
1317
outputDir:
1418
cliConfig["out-dir"] ?? fileConfig.outputDir ?? defaultConfig.outputDir,
15-
force: cliConfig.force ?? fileConfig.force,
19+
force: cliConfig.force ?? fileConfig.force ?? defaultConfig.force,
1620
icons: {
17-
optimize: cliConfig.optimize ?? fileConfig.icons.optimize,
18-
inputDir: fileConfig.icons.inputDir, // TODO add missing cli config flag
19-
spriteOutputDir: fileConfig.icons.spriteOutputDir, // TODO add missing cli config flag
20-
hash: cliConfig.hash ?? fileConfig.icons.hash,
21+
optimize:
22+
cliConfig.optimize ??
23+
fileConfig.icons?.optimize ??
24+
defaultConfig.icons.optimize,
25+
inputDir: fileConfig.icons?.inputDir ?? defaultConfig.icons.inputDir, // TODO add missing cli config flag
26+
spriteOutputDir:
27+
fileConfig.icons?.spriteOutputDir ??
28+
defaultConfig.icons.spriteOutputDir, // TODO add missing cli config flag
29+
hash:
30+
cliConfig.hash ?? fileConfig.icons?.hash ?? defaultConfig.icons.hash,
2131
},
2232
illustrations: {
23-
inputDir: fileConfig.illustrations.inputDir, // TODO add missing cli config flag
33+
inputDir:
34+
fileConfig.illustrations?.inputDir ??
35+
defaultConfig.illustrations.inputDir, // TODO add missing cli config flag
36+
extensions:
37+
fileConfig.illustrations?.extensions ??
38+
defaultConfig.illustrations.extensions, // TODO add missing cli config flag
2439
},
2540
cwd: cliConfig.cwd ?? fileConfig.cwd ?? defaultConfig.cwd,
2641
} satisfies Config;

0 commit comments

Comments
 (0)