-
Notifications
You must be signed in to change notification settings - Fork 38
/
wxt.config.ts
120 lines (116 loc) · 4.07 KB
/
wxt.config.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import { execSync } from 'node:child_process';
import path from 'node:path';
import { lingui } from '@lingui/vite-plugin';
import mdx from '@mdx-js/rollup';
import react from '@vitejs/plugin-react-swc';
import Randomstring from 'randomstring';
import remarkMdxImages from 'remark-mdx-images';
import remarkUnwrapImages from 'remark-unwrap-images';
import preserveDirectives from 'rollup-preserve-directives';
import icons from 'unplugin-icons/vite';
import { chunkSplitPlugin } from 'vite-plugin-chunk-split';
import { defineConfig } from 'wxt';
import { version as versionMui } from './node_modules/@mui/material/package.json';
import { version as versionReact } from './node_modules/react/package.json';
import { version as versionThuLearnLib } from './node_modules/thu-learn-lib/package.json';
import { author, version } from './package.json';
const runCmd = (cmd: string) => execSync(cmd).toString().trim();
const randomSuffix = Randomstring.generate(4);
const helperVersion = version;
const gitVersion = runCmd('git describe --always --dirty');
const gitBranch = runCmd('git branch --show-current').replaceAll('/', '-');
const date = new Date();
// See https://wxt.dev/api/config.html
export default defineConfig({
vite: ({ mode }) => ({
define: {
__HELPER_VERSION__: JSON.stringify(helperVersion),
__GIT_VERSION__: JSON.stringify(gitVersion),
__GIT_COMMIT_HASH__: JSON.stringify(runCmd('git rev-parse HEAD')),
__GIT_COMMIT_DATE__: JSON.stringify(
runCmd('git log -1 --date=format:"%Y/%m/%d %T" --format="%ad"'),
),
__GIT_BRANCH__: JSON.stringify(runCmd('git rev-parse --abbrev-ref HEAD')),
__BUILD_HOSTNAME__: JSON.stringify(runCmd('hostname')),
__BUILD_TIME__: JSON.stringify(date.toLocaleString('zh-CN')),
__THU_LEARN_LIB_VERSION__: JSON.stringify(versionThuLearnLib),
__MUI_VERSION__: JSON.stringify(versionMui),
__REACT_VERSION__: JSON.stringify(versionReact),
__LEARN_HELPER_CSRF_TOKEN_PARAM__: JSON.stringify(
`__learn-helper-csrf-token-${randomSuffix}__`,
),
__LEARN_HELPER_CSRF_TOKEN_INJECTOR__: JSON.stringify(
`__learn_helper_csrf_token_injector_${randomSuffix}__`,
),
},
plugins: [
{
enforce: 'pre',
...mdx({
remarkPlugins: [remarkMdxImages, remarkUnwrapImages],
}),
},
icons({ compiler: 'jsx', jsx: 'react' }),
react({ plugins: [['@lingui/swc-plugin', {}]] }),
preserveDirectives(),
lingui(),
chunkSplitPlugin({
customSplitting: {
'thu-learn-lib-vendor': [/thu-learn-lib/],
'ui-vendor': [/@mui/, /@emotion/],
},
}),
],
resolve: {
alias: {
'~': path.resolve(__dirname, 'src'),
parse5: path.resolve(__dirname, 'node_modules/fake-parse5/'),
'parse5-htmlparser2-tree-adapter': path.resolve(__dirname, 'node_modules/fake-parse5/'),
},
},
build: {
target: 'esnext',
sourcemap: mode === 'development',
...(mode === 'production' && {
minify: 'terser',
terserOptions: {
format: {
comments: false,
ecma: 2018,
},
},
}),
},
}),
extensionApi: 'chrome',
manifestVersion: 3,
manifest: ({ browser }) => ({
name: '__MSG_appName__',
description: '__MSG_appDesc__',
default_locale: 'zh_CN',
author: author,
version,
action: {},
host_permissions: ['*://learn.tsinghua.edu.cn/*', '*://id.tsinghua.edu.cn/*'],
permissions:
browser === 'firefox'
? ['storage', 'downloads']
: ['storage', 'downloads', 'declarativeNetRequest'],
...(browser === 'firefox' && {
browser_specific_settings: {
gecko: {
update_url: 'https://harrychen.xyz/learn/updates.json',
id: '{b3a44052-5d0d-4ef9-9744-93b6f5ca7398}',
},
},
}),
}),
analysis: {
enabled: true,
},
zip: {
artifactTemplate: `{{name}}-{{version}}-${gitBranch}-${gitVersion}-{{browser}}.zip`,
sourcesTemplate: `{{name}}-{{version}}-${gitBranch}-${gitVersion}-sources.zip`,
},
outDir: 'dist',
});