This repository has been archived by the owner on May 10, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.ts
130 lines (119 loc) · 2.8 KB
/
build.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
121
122
123
124
125
126
127
128
129
130
import { build } from "@ourongxing/estrella"
import { Plugin } from "esbuild"
import { mainfest } from "./mainfest"
import copy from "esbuild-plugin-mxn-copy"
import autoImport from "unplugin-auto-import/esbuild"
import { homedir } from "os"
import fs from "fs-extra"
import { exec } from "child_process"
const isProd = process.env.NODE_ENV === "production"
const bannerText = `
/*
THIS IS A GENERATED/BUNDLED FILE BY ESBuild
MIT License
Copyright (c) 2022 MarginNote
If you want to view the source code, please visit the github repository.
Github: ${mainfest.github}
Welcome to contribute to this project!
*/
try {
`
const footerText = `
} catch (e) {
Application.sharedInstance().alert("${mainfest.title}-"+String(e))
}
`
const outDir = isProd
? "./dist/"
: homedir() +
`/Library/Containers/QReader.MarginStudyMac/Data/Library/MarginNote Extensions/marginnote.extension.${mainfest.key}/`
function clear(): Plugin {
return {
name: "Clear",
setup(build) {
build.onStart(() => {
if (fs.existsSync(outDir)) {
fs.rmSync(outDir, { recursive: true })
}
})
}
}
}
function zip(): Plugin {
return {
name: "Zip",
setup(build) {
build.onEnd(() => {
if (isProd && fs.existsSync(outDir)) {
const fileName = `${mainfest.key} v${mainfest.version}`.replace(
/[ .]/g,
"_"
)
exec(`cd ${outDir} && zip -qr ${fileName}.mnaddon *`)
console.log(`Generated at ${outDir}${fileName}.mnaddon`)
}
})
}
}
}
function genMainfest(): Plugin {
return {
name: "GenMainfest",
setup(build) {
build.onEnd(() => {
const mnaddon = {
addonid: `marginnote.extension.${mainfest.key}`,
// author: mainfest.author,
author: "MN(ourongxing",
title: mainfest.title,
version: mainfest.version,
marginnote_version_min: mainfest.minMarginNoteVersion,
cert_key: ""
}
if (fs.existsSync(outDir))
fs.writeJSONSync(outDir + "mnaddon.json", mnaddon, { spaces: 2 })
})
}
}
}
const plugins: Plugin[] = [
clear(),
!isProd &&
autoImport({
imports: [
{
marginnote: ["console"]
}
],
dts: false
}),
mainfest.files?.length &&
copy({
copy: mainfest.files.map(k => ({
from: k,
to: outDir
}))
}),
genMainfest(),
isProd && zip()
].filter(k => k)
build({
entry: "src/main.ts",
tslint: !isProd,
outfile: outDir + "main.js",
splitting: false,
sourcemap: false,
clear: true,
watch: !isProd,
minify: isProd,
banner: {
js: bannerText
},
footer: {
js: footerText
},
pure: ["console.log", "console.error", "console.assert", "console.warn"],
bundle: true,
target: "safari13",
plugins
})