This repository has been archived by the owner on Mar 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utoolPlugin.ts
98 lines (91 loc) · 2.63 KB
/
utoolPlugin.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
import { PluginOption, ResolvedConfig } from "vite";
import {
readJSONSync,
writeJsonSync,
ensureDirSync,
copyFileSync,
} from "fs-extra";
import path, { join, resolve } from "path";
import {
InputOptions,
OutputOptions,
rollup,
RollupBuild,
watch,
} from "rollup";
import globals from "rollup-plugin-node-globals";
import typescript from "@rollup/plugin-typescript";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import commonjs from "@rollup/plugin-commonjs";
async function bundleUtoolPreload(isDevelopment = false) {
const inputOptions: InputOptions = {
input: path.join(__dirname, "./preload/preload.ts"),
plugins: [
// builtins(),
globals(),
typescript({
// tsconfig: "tsconfig.preload.json"
tsconfig: false,
target: "ESNext",
importHelpers: false,
include: ["preload/**/*.ts", "src/global.d.ts"],
}),
nodeResolve(),
commonjs(),
],
external: ["electron"],
};
const outputOptions: OutputOptions = {
format: "commonjs",
file: path.join(__dirname, "./dist", "preload.js"),
};
if (isDevelopment) {
watch({
...inputOptions,
output: outputOptions,
}).on("event", (event) => {
if (event.code === "START") {
console.log("Starting preload build...");
} else if (event.code === "BUNDLE_END") {
console.log(`Preload build complete in ${event.duration}ms.`);
} else if (event.code === "ERROR") {
console.error("Error during preload build:", event.error);
}
});
} else {
let bundle: RollupBuild;
bundle = await rollup(inputOptions);
await bundle.write(outputOptions);
}
}
export default function utoolPlugin(): PluginOption {
let config: ResolvedConfig;
return {
name: "utool-plugin",
async writeBundle(options) {
const outputDir = options.dir || path.dirname(options.file);
const pluginJsonPath = path.join(outputDir, "./plugin.json");
const plugin = readJSONSync(pluginJsonPath);
plugin.main = "./index.html";
plugin.preload = "./preload.js";
writeJsonSync(pluginJsonPath, plugin);
await bundleUtoolPreload(false);
},
configResolved(resolvedConfig) {
config = resolvedConfig;
},
async buildStart(options) {
if (config.command === "serve") {
bundleUtoolPreload(true);
const files = ["logo.png", "plugin.json"];
ensureDirSync(join(__dirname, "./dist"));
files.forEach((file) => {
copyFileSync(
join(__dirname, `./public/${file}`),
join(__dirname, `./dist/${file}`)
);
});
}
},
};
}