-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathconfig.ts
More file actions
127 lines (123 loc) · 4.78 KB
/
config.ts
File metadata and controls
127 lines (123 loc) · 4.78 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
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
121
122
123
124
125
126
127
import fs from 'fs';
import path from 'path';
import lodash from 'lodash';
import { build } from 'vite';
import { Config } from 'zotero-plugin-scaffold';
//@ts-expect-error no types
import svg from 'esbuild-plugin-svg';
import { sassPlugin } from 'esbuild-sass-plugin';
import pkg from '../package.json' with { type: 'json' };
import type { BuildOptions } from 'esbuild';
import type { AliasOptions, InlineConfig } from 'vite';
const buildDir = 'build';
export default function loadConfig(isDevBuild: boolean = false, isFullBuild: boolean = false) {
const esbuildConfig: BuildOptions = {
target: 'firefox140',
define: { __dev__: String(isDevBuild) },
bundle: true,
minify: !isDevBuild,
external: ['resource://*', 'chrome://*'],
outdir: path.join(buildDir, 'addon/content'),
},
sandboxConfig: BuildOptions = {
...esbuildConfig,
plugins: [svg(), sassPlugin({ type: 'css-text', style: 'compressed' })],
entryPoints: [{ in: 'src/bootstrap/index.ts', out: pkg.config.addonName }]
},
workerConfig: BuildOptions = {
...esbuildConfig,
format: 'esm',
outExtension: { '.js': '.mjs' },
entryPoints: [{ in: 'src/worker/index.ts', out: `${pkg.config.addonName}-worker` }],
},
viteResolveOptions: AliasOptions = [
{
find: /^highcharts\/(.*)(?<!\.css)$/,
replacement: 'highcharts/$1.src',
},
{
find: 'highcharts-vue',
replacement: 'highcharts-vue/dist/highcharts-vue.js',
},
],
viteConfig: InlineConfig = {
root: path.join(buildDir, '../src/vue'),
build: {
minify: isDevBuild ? false : 'esbuild',
emptyOutDir: false,
chunkSizeWarningLimit: 8192,
},
define: { __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: String(isDevBuild) },
resolve: isDevBuild ? { alias: viteResolveOptions } : undefined,
},
prefs = Object.keys(pkg.config.defaultSettings).reduce(
(obj, key) => {
obj[key] = `id='${pkg.name}-${key}' preference='${pkg.config.addonPref}.${key}'`;
return obj;
},
{} as Record<string, string>,
);
return Config.loadConfig({
name: pkg.config.addonName,
id: pkg.config.addonID,
namespace: pkg.name,
dist: buildDir,
updateURL: 'https://gitee.com/const_volatile/chartero/releases/download/update/update.json',
xpiDownloadLink:
'https://gitee.com/const_volatile/chartero/releases/download/v{{version}}/{{xpiName}}.xpi',
build: {
assets: 'addon',
define: {
...pkg.config,
author: pkg.author,
homepage: pkg.homepage,
releasepage: pkg.releasepage,
description: 'Charts for Zotero',
defaultSettings: '',
devBuild: isDevBuild
? '<html:h2>Development Build, <html:span style="color: red;">Do NOT</html:span> Use!</html:h2>'
: '',
buildVersion: pkg.version + (isDevBuild ? '-dev' : ''),
buildTime: '{{buildTime}}',
...prefs,
},
esbuildOptions: [sandboxConfig, workerConfig],
fluent: { dts: false },
hooks: {
'build:bundle': async () => {
buildPrefs();
patchLocaleStrings();
isFullBuild && (await build(viteConfig));
},
},
},
watchIgnore: /src[\/\\]vue[\/\\].*\.d\.ts$/,
release: {
bumpp: {
commit: '🔖 Release v',
},
},
});
}
function patchLocaleStrings() {
const standard = JSON.parse(fs.readFileSync('addon/locale/zh-CN/chartero.json', { encoding: 'utf-8' }));
for (const locale of ['en-US', 'it-IT', 'ja-JP']) {
const localeFile = path.join(buildDir, `addon/locale/${locale}/chartero.json`),
json = JSON.parse(fs.readFileSync(localeFile, { encoding: 'utf-8' })),
merged = lodash.defaultsDeep(json, standard);
fs.writeFileSync(localeFile, JSON.stringify(merged));
}
}
function buildPrefs() {
function stringifyObj(val: unknown) {
if (typeof val == 'string') return `'${val}'`;
else if (typeof val == 'object') return `'${JSON.stringify(val)}'`;
return val;
}
fs.writeFileSync(
path.join(buildDir, 'addon/prefs.js'),
Object.entries(pkg.config.defaultSettings)
.map(([k, v]) => `pref('${pkg.config.addonPref}.${k}', ${stringifyObj(v)});`)
.join('\n'),
);
}