-
-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathvite.chrome.config.ts
87 lines (80 loc) · 2.47 KB
/
vite.chrome.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
import { crx } from "@crxjs/vite-plugin"
import { defineConfig, mergeConfig, UserConfig, PluginOption } from "vite"
import zipPack from "vite-plugin-zip-pack"
import manifest from "./manifest.chrome.config"
import packageJson from "./package.json" with { type: "json" }
import baseConfig from "./vite.config"
import chalk from "chalk"
const IS_DEV = process.env.NODE_ENV === "development"
const browser = "chrome"
const outDir = "dist"
const browserOutDir = `${outDir}/${browser}`
const outFileName = `${browser}-${packageJson.version}.zip`
const printMessage = (isDev: boolean): void => {
setTimeout(() => {
console.info("\n")
console.info(chalk.greenBright(`✅ Successfully built for ${browser}.`))
if (isDev) {
console.info(
chalk.greenBright(
`🚀 Load the extension via ${browser}://extensions/, enable "Developer mode", click "Load unpacked", and select the directory:`,
),
)
console.info(chalk.greenBright(`📂 ${browserOutDir}`))
} else {
console.info(
chalk.greenBright(
`📦 Zip File: ${outDir}/${outFileName} (Upload to the store)`,
),
)
console.info(chalk.greenBright(`🚀 Load manually from ${browserOutDir}`))
}
console.info("\n")
}, 50)
}
// Create build message plugin
const createBuildMessagePlugin = (isDev: boolean): PluginOption => ({
name: "vite-plugin-build-message",
enforce: "post" as const,
...(isDev
? {
configureServer(server) {
server.httpServer?.once("listening", () => printMessage(true))
},
}
: {}),
closeBundle: { sequential: true, handler: () => printMessage(isDev) },
})
// Define browser-specific configuration
export default defineConfig(() => {
// Create plugins based on environment
const browserPlugins: PluginOption[] = [
crx({
manifest,
browser,
contentScripts: { injectCss: true },
}),
createBuildMessagePlugin(IS_DEV),
]
// Add zip plugin for production builds
if (!IS_DEV) {
browserPlugins.push(
zipPack({
inDir: browserOutDir,
outDir,
outFileName,
filter: (_, filePath, isDirectory) =>
!(isDirectory && filePath.includes(".vite")),
}) as PluginOption,
)
}
// Create browser-specific config
const browserConfig: UserConfig = {
build: {
outDir: browserOutDir,
},
plugins: browserPlugins,
}
// Merge with base config and return
return mergeConfig(baseConfig, browserConfig)
})