mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-12 11:01:45 +00:00
change plugin system
This commit is contained in:
105
vite-plugins/plugin-loader.ts
Normal file
105
vite-plugins/plugin-loader.ts
Normal file
@ -0,0 +1,105 @@
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { resolve, basename } from 'node:path';
|
||||
|
||||
import { createFilter } from 'vite';
|
||||
import { Project, ts, ObjectLiteralExpression } from 'ts-morph';
|
||||
|
||||
import type { PluginOption } from 'vite';
|
||||
|
||||
export default function (mode: 'backend' | 'preload' | 'renderer' | 'none') {
|
||||
const pluginFilter = createFilter([
|
||||
'src/plugins/*/index.{js,ts}',
|
||||
'src/plugins/*',
|
||||
]);
|
||||
|
||||
return <PluginOption>{
|
||||
name: 'ytm-plugin-loader',
|
||||
async load(id) {
|
||||
if (!pluginFilter(id)) return null;
|
||||
|
||||
const project = new Project({
|
||||
tsConfigFilePath: resolve(__dirname, '..', 'tsconfig.json'),
|
||||
skipAddingFilesFromTsConfig: true,
|
||||
skipLoadingLibFiles: true,
|
||||
skipFileDependencyResolution: true,
|
||||
});
|
||||
|
||||
const src = project.createSourceFile(
|
||||
'_pf' + basename(id),
|
||||
await readFile(id, 'utf8'),
|
||||
);
|
||||
const exports = src.getExportedDeclarations();
|
||||
let objExpr: ObjectLiteralExpression | undefined = undefined;
|
||||
|
||||
for (const [name, [expr]] of exports) {
|
||||
if (name !== 'default') continue;
|
||||
|
||||
switch (expr.getKind()) {
|
||||
case ts.SyntaxKind.ObjectLiteralExpression: {
|
||||
objExpr = expr.asKindOrThrow(ts.SyntaxKind.ObjectLiteralExpression);
|
||||
break;
|
||||
}
|
||||
case ts.SyntaxKind.CallExpression: {
|
||||
const callExpr = expr.asKindOrThrow(ts.SyntaxKind.CallExpression);
|
||||
if (callExpr.getArguments().length !== 1) continue;
|
||||
|
||||
const name = callExpr.getExpression().getText();
|
||||
if (name !== 'createPlugin') continue;
|
||||
|
||||
const arg = callExpr.getArguments()[0];
|
||||
if (arg.getKind() !== ts.SyntaxKind.ObjectLiteralExpression)
|
||||
continue;
|
||||
|
||||
objExpr = arg.asKindOrThrow(ts.SyntaxKind.ObjectLiteralExpression);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!objExpr) return null;
|
||||
|
||||
const properties = objExpr.getProperties();
|
||||
const propertyNames = properties.map((prop) => {
|
||||
switch (prop.getKind()) {
|
||||
case ts.SyntaxKind.PropertyAssignment:
|
||||
return prop
|
||||
.asKindOrThrow(ts.SyntaxKind.PropertyAssignment)
|
||||
.getName();
|
||||
case ts.SyntaxKind.ShorthandPropertyAssignment:
|
||||
return prop
|
||||
.asKindOrThrow(ts.SyntaxKind.ShorthandPropertyAssignment)
|
||||
.getName();
|
||||
case ts.SyntaxKind.MethodDeclaration:
|
||||
return prop
|
||||
.asKindOrThrow(ts.SyntaxKind.MethodDeclaration)
|
||||
.getName();
|
||||
default:
|
||||
throw new Error('Not implemented');
|
||||
}
|
||||
});
|
||||
|
||||
const contexts = ['backend', 'preload', 'renderer'];
|
||||
for (const ctx of contexts) {
|
||||
if (mode === 'none') {
|
||||
const index = propertyNames.indexOf(ctx);
|
||||
if (index === -1) continue;
|
||||
|
||||
objExpr.getProperty(propertyNames[index])?.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (ctx === mode) continue;
|
||||
|
||||
const index = propertyNames.indexOf(ctx);
|
||||
if (index === -1) continue;
|
||||
|
||||
objExpr.getProperty(propertyNames[index])?.remove();
|
||||
}
|
||||
|
||||
return {
|
||||
code: src.getText(),
|
||||
ast: src,
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user