remove webpack

This commit is contained in:
2024-11-14 19:47:50 +08:00
parent 696be2a28c
commit 7ec8a001de
16 changed files with 1303 additions and 887 deletions

View File

@@ -1,8 +1,5 @@
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
export const getRequire = () => {
return eval('require') as NodeRequire;
};
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
@@ -22,14 +19,15 @@ export const defaultImportModules = [
export const dynamicImport = async (name: string) => {
// const nodeModules = path.resolve(directoryPath, 'node_modules');
// const nodeName = path.resolve(nodeModules, name);
const require = getRequire();
console.log(`Dynamic import module ${name}`);
try {
const nodeCache = require.cache[require.resolve(name)];
if (nodeCache) {
console.log(`${name} is cached`);
}
return require(name);
return await import(name);
// const require = getRequire();
// const nodeCache = require.cache[require.resolve(name)];
// if (nodeCache) {
// console.log(`${name} is cached`);
// }
// return require(name);
} catch (e) {
console.error(`Failed to import module ${name}: ${e.message}`);
throw `Failed to import module ${name}: ${e.message}`;

View File

@@ -2,6 +2,11 @@ import { exec } from 'child_process';
import path from 'path';
import fs from 'fs';
import { fileURLToPath } from 'url'
import { dirname } from 'path'
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
export const directoryPath = path.resolve(__dirname);
const packagePath = path.resolve(directoryPath, 'package.json');
const exists = (filePath: string) => {

View File

@@ -16,6 +16,7 @@ const cacheFilePath = useFileStore('cache-file', { needExists: true });
// -F "description=This is a test upload" \
// -F "username=testuser"
let clients = [];
export const uploadMiddleware = async (req: http.IncomingMessage, res: http.ServerResponse) => {
if (req.method === 'GET' && req.url === '/api/app/upload') {
res.writeHead(200, { 'Content-Type': 'text/plain' });
@@ -104,8 +105,19 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
allowEmptyFiles: true, // 允许空
minFileSize: 0, // 最小文件大小
createDirsFromUploads: false, // 根据上传的文件夹结构创建目录
keepExtensions: true, // 保留文件
hashAlgorithm: 'md5', // 文件哈希算法
});
form.on('progress', (bytesReceived, bytesExpected) => {
const progress = (bytesReceived / bytesExpected) * 100;
console.log(`Upload progress: ${progress.toFixed(2)}%`);
const data = {
progress: progress.toFixed(2),
message: `Upload progress: ${progress.toFixed(2)}%`,
};
// 向所有连接的客户端推送进度信息
clients.forEach((client) => client.write(`${JSON.stringify(data)}\n`));
});
// 解析上传的文件
form.parse(req, async (err, fields, files) => {
if (err) {
@@ -192,4 +204,17 @@ export const uploadMiddleware = async (req: http.IncomingMessage, res: http.Serv
res.end(JSON.stringify(data));
});
}
if (req.url === '/api/events') {
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
Connection: 'keep-alive',
});
clients.push(res);
// 移除客户端连接
req.on('close', () => {
clients = clients.filter((client) => client !== res);
});
}
};