mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-11 10:31:47 +00:00
feat: plugin auto-importer with vite-plugin-resolve (#1385)
This commit is contained in:
28
vite-plugins/plugin-virtual-module-generator.ts
Normal file
28
vite-plugins/plugin-virtual-module-generator.ts
Normal file
@ -0,0 +1,28 @@
|
||||
import { existsSync } from 'node:fs';
|
||||
import { basename, relative, resolve } from 'node:path';
|
||||
|
||||
import { globSync } from 'glob';
|
||||
|
||||
const snakeToCamel = (text: string) => text.replace(/-(\w)/g, (_, letter: string) => letter.toUpperCase());
|
||||
|
||||
export const pluginVirtualModuleGenerator = (mode: 'back' | 'preload' | 'front') => {
|
||||
const srcPath = resolve(__dirname, '..', 'src');
|
||||
|
||||
const plugins = globSync(`${srcPath}/plugins/*`)
|
||||
.map((path) => ({ name: basename(path), path }))
|
||||
.filter(({ name, path }) => !name.startsWith('utils') && existsSync(resolve(path, `${mode}.ts`)));
|
||||
|
||||
let result = '';
|
||||
|
||||
for (const { name, path } of plugins) {
|
||||
result += `import ${snakeToCamel(name)}Plugin from "./${relative(resolve(srcPath, '..'), path).replace(/\\/g, '/')}/${mode}";\n`;
|
||||
}
|
||||
|
||||
result += 'export const pluginList = {\n';
|
||||
for (const { name } of plugins) {
|
||||
result += ` "${name}": ${snakeToCamel(name)}Plugin,\n`;
|
||||
}
|
||||
result += '};';
|
||||
|
||||
return result;
|
||||
};
|
||||
Reference in New Issue
Block a user