-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.ts
148 lines (133 loc) · 4.03 KB
/
vite.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { svelte } from "@sveltejs/vite-plugin-svelte";
import archiver from "archiver";
import { createWriteStream } from "fs";
import { readFile, rm, writeFile } from "fs/promises";
import { execSync } from "node:child_process";
import { join, resolve } from "path";
import semver from "semver";
import { defineConfig } from "vite";
async function writeManifest() {
console.log("Setting up extension manifest");
const manifestBuffer = await readFile(
resolve(__dirname, "src/manifest.json"),
);
const manifest = JSON.parse(manifestBuffer.toString());
if (process.env.NODE_ENV !== "production") {
manifest.key = "developmentkalohmonpfgdhimepifhl";
manifest.name = `Dev: ${manifest.name}`;
for (const k of Object.keys(manifest.icons)) {
const parts = manifest.icons[k].split(".");
manifest.icons[k] = `${parts[0]}-dev.${parts[1]}`;
}
for (const k of Object.keys(manifest.action.default_icon)) {
const parts = manifest.action.default_icon[k].split(".");
manifest.action.default_icon[k] = `${parts[0]}-dev.${parts[1]}`;
}
} else {
const newVersion = semver.parse(process.env.EXTENSION_VERSION);
if (!newVersion) {
throw new Error(
`Version could not be parsed by semver: '${process.env.EXTENSION_VERSION}'`,
);
}
manifest.version = newVersion.version;
}
await writeFile(
resolve(__dirname, "dist/manifest.json"),
JSON.stringify(manifest, null, 2),
);
console.log(`written manifest`);
}
function injectSentryDebugIDs() {
console.log("Injecting sentry sourcemaps");
const sentryCli = resolve(__dirname, "node_modules/.bin/sentry-cli");
execSync(`${sentryCli} sourcemaps inject ${resolve(__dirname, "dist")}`);
}
async function bundleExtension() {
console.log("Bundling zip");
const zipPath = join(resolve(__dirname), "gauntface-pin-it-extension.zip");
try {
await rm(zipPath);
} catch (e) {
// NOOP
}
const output = createWriteStream(zipPath);
// eslint-disable-next-line new-cap
const archive = archiver("zip", {
zlib: {
// Sets the compression level
level: 9,
},
});
const distDir = resolve(__dirname, "dist");
try {
await new Promise((resolve, reject) => {
output.on("close", () => {
resolve(null);
});
output.on("end", () => {
console.log("Output End event");
});
// good practice to catch warnings (ie stat failures and other
// non-blocking errors)
archive.on("warning", (err) => {
if (err.code === "ENOENT") {
console.warn(`Archiver warning: ${err.message}`);
} else {
reject(err);
}
});
// good practice to catch this error explicitly
archive.on("error", (err) => {
reject(err);
});
// pipe archive data to the file
archive.pipe(output);
// append files from a sub-directory and naming it `gauntface-pin-it-extension`
// within the archive
archive.directory(distDir, false);
// finalize the archive (ie we are done appending files but streams have to
// finish yet)
// 'close', 'end' or 'finish' may be fired right after calling this method
// so register to them beforehand
archive.finalize();
});
} catch (err) {
console.error(`Error while zipping: ${err}`);
throw err;
}
}
// https://vitejs.dev/config/
export default defineConfig({
build: {
rollupOptions: {
input: {
options: resolve(__dirname, "options.html"),
sw: resolve(__dirname, "src/background/sw.ts"),
},
output: {
entryFileNames: (file) => {
if (file.name === "sw") {
return "scripts/background/sw.js";
}
return "assets/[name]-[hash].js";
},
},
},
},
plugins: [
svelte(),
{
name: "bundle-extension",
closeBundle: async () => {
await writeManifest();
await injectSentryDebugIDs();
await bundleExtension();
},
},
],
test: {
root: "src",
setupFiles: ["./vitestSetupMocks.ts"],
},
});