Skip to content

Commit 1d5259a

Browse files
committed
Update vite plugins
- Add 'cwd' option for react native plugins - Allow multiple configs for MSDF plugin
1 parent 1ad24e8 commit 1d5259a

File tree

5 files changed

+130
-89
lines changed

5 files changed

+130
-89
lines changed

.changeset/clean-flies-yawn.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 configs, and add charsetFile option

.changeset/stupid-needles-take.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@plexinc/vite-plugin-react-reanimated-lightning": patch
3+
"@plexinc/vite-plugin-react-native-lightning": patch
4+
---
5+
6+
Add "cwd" option

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

Lines changed: 68 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@ interface Options {
2424
*/
2525
inputDir: string | string[];
2626
/**
27-
* Path to the output directory. All fonts awill be placed into this directory,
27+
* Path to the output directory. All fonts will be placed into this directory,
2828
* even if multiple directories are specified as inputs. Subdirectories will
29-
* be retained from the input. If you want to output to multiple directories,
30-
* you can use a second plugin instance with a different output directory.
29+
* be retained from the input.
3130
*/
3231
outDir: string;
32+
/**
33+
* If true, copies the original font files to the output directory. Defaults to false
34+
*/
35+
copyOriginalToOutDir?: boolean;
3336
/**
3437
* Type of font to generate. Defaults to ['msdf']
3538
*/
@@ -45,11 +48,16 @@ interface Options {
4548
*/
4649
force?: boolean;
4750
/**
48-
* Charset to include in the generated font. If there is a charset.config.json file in
49-
* the input directory, that will be used instead. Defaults to
51+
* Charset to include in the generated font. If there is a charset.config.json
52+
* file in the input directory, or if the charsetFile option is set, they will
53+
* be used instead. Defaults to
5054
* ' !\\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~’“”'
5155
*/
5256
charset?: string;
57+
/**
58+
* Path to a charset file. If this is set, it will be used instead of the charset option.
59+
*/
60+
charsetFile?: string;
5361

5462
/**
5563
* Overrides for font generation. This allows you to specify custom font sizes
@@ -59,56 +67,77 @@ interface Options {
5967
overrides?: Override;
6068
}
6169

62-
export default function msdfFontGen({
63-
inputDir,
64-
outDir,
65-
force,
66-
types = ['msdf'],
67-
extensions = ['ttf', 'otf', 'woff', 'woff2'],
68-
charset = ' !\\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~’“”',
69-
overrides,
70-
}: Options): PluginOption {
70+
export default function msdfFontGen(
71+
options: Options | Options[],
72+
): PluginOption {
7173
return {
7274
name: 'msdf-fontgen',
7375

7476
async buildStart() {
7577
console.log('Building fonts...');
7678

7779
const cleanup: string[] = [];
80+
const optionsArray = Array.isArray(options) ? options : [options];
81+
82+
for (const option of optionsArray) {
83+
const {
84+
inputDir,
85+
outDir,
86+
force,
87+
types = ['msdf'],
88+
extensions = ['ttf', 'otf', 'woff', 'woff2'],
89+
charset = ' !\\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmnopqrstuvwxyz{|}~’“”',
90+
charsetFile,
91+
overrides,
92+
copyOriginalToOutDir,
93+
} = option;
94+
95+
if (types.length === 0) {
96+
console.log('No font types specified');
97+
return;
98+
}
7899

79-
if (types.length === 0) {
80-
console.log('No font types specified');
81-
return;
82-
}
100+
console.log('Looking for fonts...');
83101

84-
console.log('Looking for fonts...');
102+
const inputDirs = Array.isArray(inputDir) ? inputDir : [inputDir];
85103

86-
if (!Array.isArray(inputDir)) {
87-
inputDir = [inputDir];
88-
}
104+
const fontFolders = getFolders(inputDirs, outDir, extensions);
89105

90-
const fontFolders = getFolders(inputDir, outDir, extensions);
106+
for (const { input, output, files } of fontFolders) {
107+
console.log(`Generating fonts in folder ${input}...`);
91108

92-
for (const { input, output, files } of fontFolders) {
93-
console.log(`Generating fonts in folder ${input}...`);
109+
setGeneratePaths(input, output, charsetFile);
94110

95-
setGeneratePaths(input, output);
111+
if (!charsetFile || !fs.existsSync(charsetFile)) {
112+
const charsetPath = `${input}/charset.config.json`;
96113

97-
const charsetPath = `${input}/charset.config.json`;
98-
if (!fs.existsSync(charsetPath) && charset) {
99-
fs.writeFileSync(charsetPath, JSON.stringify({ charset }), 'utf8');
100-
cleanup.push(charsetPath);
101-
}
114+
if (!fs.existsSync(charsetPath) && charset) {
115+
fs.writeFileSync(
116+
charsetPath,
117+
JSON.stringify({ charset }),
118+
'utf8',
119+
);
120+
cleanup.push(charsetPath);
121+
}
122+
}
102123

103-
const overridePath = `${input}/overrides.txt`;
104-
if (!fs.existsSync(overridePath) && overrides) {
105-
fs.writeFileSync(overridePath, JSON.stringify(overrides, null, 2));
106-
cleanup.push(overridePath);
107-
}
124+
const overridePath = `${input}/overrides.txt`;
125+
if (!fs.existsSync(overridePath) && overrides) {
126+
fs.writeFileSync(overridePath, JSON.stringify(overrides, null, 2));
127+
cleanup.push(overridePath);
128+
}
108129

109-
for (const file of files) {
110-
for (const type of new Set(types)) {
111-
await generateFont(output, file, type, force);
130+
for (const file of files) {
131+
for (const type of new Set(types)) {
132+
await generateFont(output, file, type, force);
133+
134+
if (copyOriginalToOutDir) {
135+
fs.copyFileSync(
136+
path.join(input, file),
137+
path.join(output, file),
138+
);
139+
}
140+
}
112141
}
113142
}
114143
}
Lines changed: 29 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { createRequire } from 'node:module';
2-
import { dirname, resolve } from 'node:path';
3-
import { fileURLToPath } from 'node:url';
42
import react from '@vitejs/plugin-react';
53
import type { PluginOption } from 'vite';
64

@@ -20,39 +18,40 @@ const extensions = [
2018
'.json',
2119
];
2220

23-
const __dirname = dirname(fileURLToPath(import.meta.url));
24-
const rootPath = resolve(__dirname, '..');
25-
const require = createRequire(rootPath);
26-
const reactNativeLightningPath = require.resolve(
27-
'@plexinc/react-native-lightning',
28-
);
29-
3021
type Options = {
22+
cwd?: string;
3123
reactOptions?: Parameters<typeof react>[0];
3224
};
3325

34-
const vitePlugin = (options?: Options): PluginOption => [
35-
react(options?.reactOptions),
36-
{
37-
name: 'vite-react-native-lightning',
38-
enforce: 'pre',
39-
config: () => ({
40-
define: {
41-
global: 'window',
42-
},
43-
optimizeDeps: {
44-
esbuildOptions: {
45-
resolveExtensions: extensions,
26+
const vitePlugin = (options?: Options): PluginOption => {
27+
const require = createRequire(options?.cwd ?? process.cwd());
28+
const reactNativeLightningPath = require.resolve(
29+
'@plexinc/react-native-lightning',
30+
);
31+
32+
return [
33+
react(options?.reactOptions),
34+
{
35+
name: 'vite-react-native-lightning',
36+
enforce: 'pre',
37+
config: () => ({
38+
define: {
39+
global: 'window',
4640
},
47-
},
48-
resolve: {
49-
extensions,
50-
alias: {
51-
'react-native': reactNativeLightningPath,
41+
optimizeDeps: {
42+
esbuildOptions: {
43+
resolveExtensions: extensions,
44+
},
5245
},
53-
},
54-
}),
55-
},
56-
];
46+
resolve: {
47+
extensions,
48+
alias: {
49+
'react-native': reactNativeLightningPath,
50+
},
51+
},
52+
}),
53+
},
54+
];
55+
};
5756

5857
export default vitePlugin;
Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
import { createRequire } from 'node:module';
2-
import { dirname, resolve } from 'node:path';
3-
import { fileURLToPath } from 'node:url';
42
import type { Plugin } from 'vite';
53

6-
const __dirname = dirname(fileURLToPath(import.meta.url));
7-
const rootPath = resolve(__dirname, '..');
8-
const require = createRequire(rootPath);
9-
const reactNativeLightningReanimatedPath = require.resolve(
10-
'@plexinc/react-lightning-plugin-reanimated',
11-
);
12-
const reactReanimatedPath = require.resolve('react-native-reanimated');
4+
type Options = {
5+
cwd?: string;
6+
};
137

14-
const plugin = (): Plugin => ({
15-
name: 'vite-react-reanimated-lightning',
16-
enforce: 'pre',
17-
config: () => ({
18-
resolve: {
19-
alias: {
20-
'react-native-reanimated': reactNativeLightningReanimatedPath,
21-
'react-native-reanimated-original': reactReanimatedPath,
8+
const plugin = (options?: Options): Plugin => {
9+
const require = createRequire(options?.cwd ?? process.cwd());
10+
const reactNativeLightningReanimatedPath = require.resolve(
11+
'@plexinc/react-lightning-plugin-reanimated',
12+
);
13+
const reactReanimatedPath = require.resolve('react-native-reanimated');
14+
15+
return {
16+
name: 'vite-react-reanimated-lightning',
17+
enforce: 'pre',
18+
config: () => ({
19+
resolve: {
20+
alias: {
21+
'react-native-reanimated': reactNativeLightningReanimatedPath,
22+
'react-native-reanimated-original': reactReanimatedPath,
23+
},
2224
},
23-
},
24-
}),
25-
});
25+
}),
26+
};
27+
};
2628

2729
export default plugin;

0 commit comments

Comments
 (0)