Skip to content

Commit cca493b

Browse files
committed
fix: build performance
1 parent f47262d commit cca493b

File tree

4 files changed

+156
-168
lines changed

4 files changed

+156
-168
lines changed

src/providers/decorators.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function throttle<T extends (...params: unknown[]) => unknown>(
7171
}) as T;
7272
}
7373

74-
function memoize<T extends (...params: unknown[]) => unknown>(fn: T): T {
74+
export function memoize<T extends (...params: unknown[]) => unknown>(fn: T): T {
7575
const cache = new Map();
7676

7777
return ((...args) => {
@@ -84,7 +84,7 @@ function memoize<T extends (...params: unknown[]) => unknown>(fn: T): T {
8484
}) as T;
8585
}
8686

87-
function retry<T extends (...params: unknown[]) => Promise<unknown>>(
87+
export function retry<T extends (...params: unknown[]) => Promise<unknown>>(
8888
fn: T,
8989
{ retries = 3, delay = 1000 } = {},
9090
) {
@@ -102,12 +102,3 @@ function retry<T extends (...params: unknown[]) => Promise<unknown>>(
102102
throw latestError;
103103
};
104104
}
105-
106-
export default {
107-
singleton,
108-
debounce,
109-
cache,
110-
throttle,
111-
memoize,
112-
retry,
113-
};

vite-plugins/i18n-importer.mts

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { Project } from 'ts-morph';
77
const snakeToCamel = (text: string) =>
88
text.replace(/-(\w)/g, (_, letter: string) => letter.toUpperCase());
99

10-
export const i18nImporter = () => {
11-
const __dirname = dirname(fileURLToPath(import.meta.url));
12-
const project = new Project({
13-
tsConfigFilePath: resolve(__dirname, '..', 'tsconfig.json'),
14-
skipAddingFilesFromTsConfig: true,
15-
skipLoadingLibFiles: true,
16-
skipFileDependencyResolution: true,
17-
});
10+
const __dirname = dirname(fileURLToPath(import.meta.url));
11+
const globalProject = new Project({
12+
tsConfigFilePath: resolve(__dirname, '..', 'tsconfig.json'),
13+
skipAddingFilesFromTsConfig: true,
14+
skipLoadingLibFiles: true,
15+
skipFileDependencyResolution: true,
16+
});
1817

18+
export const i18nImporter = () => {
1919
const srcPath = resolve(__dirname, '..', 'src');
2020
const plugins = globSync(['src/i18n/resources/*.json']).map((path) => {
2121
const nameWithExt = basename(path);
@@ -24,24 +24,28 @@ export const i18nImporter = () => {
2424
return { name, path };
2525
});
2626

27-
const src = project.createSourceFile('vm:i18n', (writer) => {
28-
// prettier-ignore
29-
for (const { name, path } of plugins) {
27+
const src = globalProject.createSourceFile(
28+
'vm:i18n',
29+
(writer) => {
30+
// prettier-ignore
31+
for (const { name, path } of plugins) {
3032
const relativePath = relative(resolve(srcPath, '..'), path).replace(/\\/g, '/');
3133
writer.writeLine(`import ${snakeToCamel(name)}Json from "./${relativePath}";`);
3234
}
3335

34-
writer.blankLine();
35-
36-
writer.writeLine('export const languageResources = {');
37-
for (const { name } of plugins) {
38-
writer.writeLine(` "${name}": {`);
39-
writer.writeLine(` translation: ${snakeToCamel(name)}Json,`);
40-
writer.writeLine(' },');
41-
}
42-
writer.writeLine('};');
43-
writer.blankLine();
44-
});
36+
writer.blankLine();
37+
38+
writer.writeLine('export const languageResources = {');
39+
for (const { name } of plugins) {
40+
writer.writeLine(` "${name}": {`);
41+
writer.writeLine(` translation: ${snakeToCamel(name)}Json,`);
42+
writer.writeLine(' },');
43+
}
44+
writer.writeLine('};');
45+
writer.blankLine();
46+
},
47+
{ overwrite: true },
48+
);
4549

4650
return src.getText();
4751
};

vite-plugins/plugin-importer.mts

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@ import { Project } from 'ts-morph';
77
const snakeToCamel = (text: string) =>
88
text.replace(/-(\w)/g, (_, letter: string) => letter.toUpperCase());
99

10+
const __dirname = dirname(fileURLToPath(import.meta.url));
11+
const globalProject = new Project({
12+
tsConfigFilePath: resolve(__dirname, '..', 'tsconfig.json'),
13+
skipAddingFilesFromTsConfig: true,
14+
skipLoadingLibFiles: true,
15+
skipFileDependencyResolution: true,
16+
});
17+
1018
export const pluginVirtualModuleGenerator = (
1119
mode: 'main' | 'preload' | 'renderer',
1220
) => {
13-
const __dirname = dirname(fileURLToPath(import.meta.url));
14-
const project = new Project({
15-
tsConfigFilePath: resolve(__dirname, '..', 'tsconfig.json'),
16-
skipAddingFilesFromTsConfig: true,
17-
skipLoadingLibFiles: true,
18-
skipFileDependencyResolution: true,
19-
});
20-
2121
const srcPath = resolve(__dirname, '..', 'src');
2222
const plugins = globSync([
2323
'src/plugins/*/index.{js,ts}',
@@ -35,35 +35,39 @@ export const pluginVirtualModuleGenerator = (
3535
return { name, path };
3636
});
3737

38-
const src = project.createSourceFile('vm:pluginIndexes', (writer) => {
39-
// prettier-ignore
40-
for (const { name, path } of plugins) {
38+
const src = globalProject.createSourceFile(
39+
'vm:pluginIndexes',
40+
(writer) => {
41+
// prettier-ignore
42+
for (const { name, path } of plugins) {
4143
const relativePath = relative(resolve(srcPath, '..'), path).replace(/\\/g, '/');
4244
writer.writeLine(`import ${snakeToCamel(name)}Plugin, { pluginStub as ${snakeToCamel(name)}PluginStub } from "./${relativePath}";`);
4345
}
4446

45-
writer.blankLine();
47+
writer.blankLine();
4648

47-
// Context-specific exports
48-
writer.writeLine(`export const ${mode}Plugins = {`);
49-
for (const { name } of plugins) {
50-
const checkMode = mode === 'main' ? 'backend' : mode;
51-
// HACK: To avoid situation like importing renderer plugins in main
52-
writer.writeLine(
53-
` ...(${snakeToCamel(name)}Plugin['${checkMode}'] ? { "${name}": ${snakeToCamel(name)}Plugin } : {}),`,
54-
);
55-
}
56-
writer.writeLine('};');
57-
writer.blankLine();
49+
// Context-specific exports
50+
writer.writeLine(`export const ${mode}Plugins = {`);
51+
for (const { name } of plugins) {
52+
const checkMode = mode === 'main' ? 'backend' : mode;
53+
// HACK: To avoid situation like importing renderer plugins in main
54+
writer.writeLine(
55+
` ...(${snakeToCamel(name)}Plugin['${checkMode}'] ? { "${name}": ${snakeToCamel(name)}Plugin } : {}),`,
56+
);
57+
}
58+
writer.writeLine('};');
59+
writer.blankLine();
5860

59-
// All plugins export (stub only) // Omit<Plugin, 'backend' | 'preload' | 'renderer'>
60-
writer.writeLine('export const allPlugins = {');
61-
for (const { name } of plugins) {
62-
writer.writeLine(` "${name}": ${snakeToCamel(name)}PluginStub,`);
63-
}
64-
writer.writeLine('};');
65-
writer.blankLine();
66-
});
61+
// All plugins export (stub only) // Omit<Plugin, 'backend' | 'preload' | 'renderer'>
62+
writer.writeLine('export const allPlugins = {');
63+
for (const { name } of plugins) {
64+
writer.writeLine(` "${name}": ${snakeToCamel(name)}PluginStub,`);
65+
}
66+
writer.writeLine('};');
67+
writer.blankLine();
68+
},
69+
{ overwrite: true },
70+
);
6771

6872
return src.getText();
6973
};

0 commit comments

Comments
 (0)