-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvite.config.js
49 lines (47 loc) · 1.36 KB
/
vite.config.js
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
import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import path from 'path'
import fs from 'fs'
// https://vitejs.dev/config/
const FILE_NAME = "main.user.js";
const headerText = fs.readFileSync("./src/header.js").toString()
export default ({mode}) => {
return defineConfig({
plugins: [
svelte(),
header(headerText, mode==="dev")
],
build: {
outDir: "dist",
cssCodeSplit: true,
minify: false,
lib: {
entry: "src/main.js",
formats: ["iife"],
fileName: () => {
return FILE_NAME;
},
name: FILE_NAME
},
watch: mode === "dev"
}
})
}
function header(text, dev=true) {
return {
name: "vite-plugin-header",
generateBundle(OutputOptions, ChunkInfo) {
let filename = String(OutputOptions.name).replace("dist/", "");
let newCode = text + "\n" + ChunkInfo[filename].code;
ChunkInfo[filename].code = newCode;
if (dev) {
let index = String(text).lastIndexOf("\n");
let newText = text.slice(0, index) + `\n// @require file:///${path.join(__dirname, "/dist/" + FILE_NAME)}` + text.slice(index);
if (!fs.existsSync("dist")) {
fs.mkdirSync("dist");
}
fs.writeFileSync(path.join(__dirname, "/dist/" + FILE_NAME.replace(".js", ".dev.js")), newText);
}
}
}
}