优化
This commit is contained in:
34
client/web/scripts/deno-static-entry.ts
Normal file
34
client/web/scripts/deno-static-entry.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
// @ts-nocheck
|
||||
import { serve } from 'https://deno.land/std@0.120.0/http/server.ts';
|
||||
import { serveFile } from 'https://deno.land/std@0.120.0/http/file_server.ts';
|
||||
import { fromFileUrl } from 'https://deno.land/std@0.120.0/path/mod.ts';
|
||||
|
||||
const clientRoot = new URL('./', import.meta.url);
|
||||
serve(async (req) => {
|
||||
const url = new URL(req.url);
|
||||
const localPath = new URL('./' + url.pathname, clientRoot);
|
||||
const filePath = fromFileUrl(localPath);
|
||||
|
||||
const fileExists = await checkFileExists(filePath);
|
||||
if (fileExists) {
|
||||
return serveFile(req, filePath);
|
||||
} else {
|
||||
return await serveFile(
|
||||
req,
|
||||
fromFileUrl(new URL('./index.html', clientRoot))
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
async function checkFileExists(filePath: string): Promise<boolean> {
|
||||
try {
|
||||
await Deno.stat(filePath);
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (error instanceof Deno.errors.NotFound) {
|
||||
return false;
|
||||
} else {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
16
client/web/scripts/generate-plugin-declaration.babel.ts
Normal file
16
client/web/scripts/generate-plugin-declaration.babel.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { generateFunctionDeclare } from 'tailchat-plugin-declaration-generator';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* 这个工具主要是用来自动扫描入口文件导出内容
|
||||
*
|
||||
* 以减少缺失的内容获取
|
||||
*/
|
||||
|
||||
generateFunctionDeclare({
|
||||
entryPath: path.resolve(__dirname, '../src/plugin/common/index.ts'),
|
||||
}).then((code) => {
|
||||
console.log('预计输出代码如下:');
|
||||
console.log('-----------------');
|
||||
console.log(code);
|
||||
});
|
||||
15
client/web/scripts/generate-plugin-declaration.tsmorph.ts
Normal file
15
client/web/scripts/generate-plugin-declaration.tsmorph.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { parseDeclarationEntry } from 'tailchat-plugin-declaration-generator';
|
||||
import path from 'path';
|
||||
|
||||
// WIP
|
||||
function generateDeclarationFile() {
|
||||
const { exportDefs } = parseDeclarationEntry({
|
||||
entryPath: path.resolve(__dirname, './test-export.ts'),
|
||||
project: {
|
||||
tsConfigFilePath: path.resolve(__dirname, '../tsconfig.json'),
|
||||
},
|
||||
});
|
||||
console.log(exportDefs);
|
||||
}
|
||||
|
||||
generateDeclarationFile();
|
||||
99
client/web/scripts/generate-plugin-declaration.typescript.ts
Normal file
99
client/web/scripts/generate-plugin-declaration.typescript.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import {
|
||||
parseModuleDeclaration,
|
||||
parseExports,
|
||||
ExportModuleItem,
|
||||
DeclarationModuleItem,
|
||||
} from 'tailchat-plugin-declaration-generator';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
|
||||
const outputPath = path.resolve(__dirname, '../tailchat.d.ts');
|
||||
|
||||
function exportModulesTemplate(
|
||||
items: ExportModuleItem[],
|
||||
existedModules: DeclarationModuleItem[] = []
|
||||
) {
|
||||
return items
|
||||
.map((item) => {
|
||||
const findedModule = existedModules.find((m) => m.name === item.name);
|
||||
if (findedModule) {
|
||||
return `${
|
||||
findedModule.comment ? findedModule.comment + '\n ' : ''
|
||||
}export const ${findedModule.text};`;
|
||||
} else {
|
||||
return `export const ${item.name}: any;`;
|
||||
}
|
||||
})
|
||||
.join('\n\n ');
|
||||
}
|
||||
|
||||
function generateDeclarationFile() {
|
||||
const { exportModules: commonExportModules } = parseExports(
|
||||
path.resolve(__dirname, '../src/plugin/common/index.ts'),
|
||||
{}
|
||||
);
|
||||
|
||||
const { exportModules: commonRegExportModules } = parseExports(
|
||||
path.resolve(__dirname, '../src/plugin/common/reg.ts'),
|
||||
{}
|
||||
);
|
||||
|
||||
const { exportModules: commonContextExportModules } = parseExports(
|
||||
path.resolve(__dirname, '../src/plugin/common/context.ts'),
|
||||
{}
|
||||
);
|
||||
|
||||
const { exportModules: componentExportModules } = parseExports(
|
||||
path.resolve(__dirname, '../src/plugin/component/index.tsx'),
|
||||
{}
|
||||
);
|
||||
|
||||
const { modules: existedModules } = parseModuleDeclaration(outputPath, {});
|
||||
|
||||
const output = `/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
/// <reference types="react" />
|
||||
|
||||
/**
|
||||
* 该文件由 Tailchat 自动生成
|
||||
* 用于插件的类型声明
|
||||
* 生成命令: pnpm run plugins:declaration:generate
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tailchat 通用
|
||||
*/
|
||||
declare module '@capital/common' {
|
||||
${exportModulesTemplate(
|
||||
commonExportModules,
|
||||
existedModules['@capital/common']
|
||||
)}
|
||||
|
||||
${exportModulesTemplate(
|
||||
commonRegExportModules,
|
||||
existedModules['@capital/common']
|
||||
)}
|
||||
|
||||
${exportModulesTemplate(
|
||||
commonContextExportModules,
|
||||
existedModules['@capital/common']
|
||||
)}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tailchat 组件
|
||||
*/
|
||||
declare module '@capital/component' {
|
||||
${exportModulesTemplate(
|
||||
componentExportModules,
|
||||
existedModules['@capital/component']
|
||||
)}
|
||||
}
|
||||
`;
|
||||
|
||||
fs.writeFile(outputPath, output, {
|
||||
encoding: 'utf8',
|
||||
});
|
||||
}
|
||||
|
||||
generateDeclarationFile();
|
||||
24
client/web/scripts/plugin-declaration.ts
Normal file
24
client/web/scripts/plugin-declaration.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import dtsgen from 'dts-generator';
|
||||
import path from 'path';
|
||||
|
||||
/**
|
||||
* :WIP:
|
||||
* TODO: tailchat.d.ts的路径尚未完善
|
||||
* 等完善后再追踪改文件的变更
|
||||
*/
|
||||
|
||||
declare module 'dts-generator' {
|
||||
interface DtsGeneratorOptions {
|
||||
prefix?: string;
|
||||
}
|
||||
}
|
||||
|
||||
dtsgen({
|
||||
main: '__tailchat__/common/index',
|
||||
name: '@capital/commmon',
|
||||
out: 'tailchat.d.ts',
|
||||
prefix: '__tailchat__',
|
||||
baseDir: path.resolve(__dirname, '../src'),
|
||||
rootDir: path.resolve(__dirname, '../src'),
|
||||
files: [path.resolve(__dirname, '../src/plugin/common/index.ts')],
|
||||
});
|
||||
1
client/web/scripts/test-export.ts
Normal file
1
client/web/scripts/test-export.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { localTrans } from '../src/plugin/common/index';
|
||||
Reference in New Issue
Block a user