-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtsup.tampermonkey.config.ts
More file actions
76 lines (69 loc) · 2.21 KB
/
Copy pathtsup.tampermonkey.config.ts
File metadata and controls
76 lines (69 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { defineConfig } from 'tsup';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// 读取 package.json 并解析为对象
const pkg = JSON.parse(
fs.readFileSync(path.resolve(__dirname, 'package.json'), 'utf-8')
);
// 读取 jsPDF 源码
const jspdfPath = path.resolve(__dirname, 'node_modules/jspdf/dist/jspdf.umd.min.js');
const jspdfSource = fs.readFileSync(jspdfPath, 'utf-8');
const {
version: pkgVersion,
description: pkgDescription,
author: pkgAuthor,
license: pkgLicense,
tampermonkey: tmConfig
} = pkg;
const userScriptMeta = `// ==UserScript==
// @name 学习通 PPT 下载器
// @namespace ${tmConfig.namespace}
// @version ${pkgVersion}
// @description ${pkgDescription}
// @author ${pkgAuthor}
${tmConfig.match.map((match: any) => `// @match ${match}`).join('\n')}
${tmConfig.grant.map((grant: any) => `// @grant ${grant}`).join('\n')}
// @run-at ${tmConfig.runAt}
// @updateURL ${tmConfig.updateURL}
// @downloadURL ${tmConfig.downloadURL}
// @homepageURL ${tmConfig.homepageURL}
// @license ${pkgLicense}
// ==/UserScript==
`;
export default defineConfig({
entry: {
'xuexitong-ppt-downloader.user': 'src/index.ts',
},
format: ['iife'],
outDir: 'dist/tampermonkey',
outExtension() {
return {
js: '.js',
};
},
minify: true,
sourcemap: false,
define: {
'process.env.JSPDF_SOURCE': JSON.stringify(jspdfSource)
},
banner: {
js: userScriptMeta
},
onSuccess: async () => {
const legacyDir = path.join(__dirname, 'dist', 'tampermonkey', 'legacy');
if (!fs.existsSync(legacyDir)) {
fs.mkdirSync(legacyDir, { recursive: true });
}
// 这里的文件名需要跟 entry key + outExtension 对应
const baseName = 'xuexitong-ppt-downloader.user.js';
const srcPath = path.join(__dirname, 'dist', 'tampermonkey', baseName);
const destPath = path.join(legacyDir, baseName.replace('.user.js', `_${pkgVersion}.user.js`));
if (fs.existsSync(srcPath)) {
fs.copyFileSync(srcPath, destPath);
console.log(`✅ Backup created: ${destPath}`);
}
}
});