-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvite.config.js
67 lines (59 loc) · 2.13 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { fileURLToPath, URL } from 'node:url'
import { defineConfig } from 'vite'
import * as fs from 'node:fs'
import { resolve } from 'node:path'
import child_process from 'node:child_process'
import vue from '@vitejs/plugin-vue'
const outDir = process.env.MANIFEST_V3 == 'true' ? './dist-v3/' : './dist/'
const copyManifest = () => {
return {
name: 'copy-manifest',
buildStart() {
if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir);
}
// Read source manifest
const sourceFilename = process.env.MANIFEST_V3 == 'true' ? 'v3' : 'v2'
let source = fs.readFileSync(`./manifest/${sourceFilename}.json`, 'utf8')
// Replace __PKG_version__ with package version from package.json
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf8'))
source = source.replace(/__PKG_version__/g, packageJson.version)
// Replace __PKG_versionName__ with date, git branch, and commit hash
const date = new Date().toISOString().slice(0, 10).replace(/-/g, '')
let versionName = `${date} dev`
try {
const commitHash = child_process.execSync('git rev-parse --short HEAD').toString().trim()
const branchName = child_process.execSync('git rev-parse --abbrev-ref HEAD').toString().trim()
versionName = `${date} ${branchName}@${commitHash}`
} catch (e) {
console.log('Failed to get git branch and commit hash.')
}
source = source.replace(/__PKG_versionName__/g, `${packageJson.version} (${versionName})`)
// Write manifest to build directory
const destPath = resolve(outDir, 'manifest.json')
fs.writeFileSync(destPath, source)
console.log(`\nCopied ${sourceFilename} to ${destPath}\n`)
}
}
}
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue(), copyManifest()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
}
},
build: {
emptyOutDir: false,
outDir,
rollupOptions: {
input: {
index: new URL('./index.html', import.meta.url).pathname
},
output: {
entryFileNames: '[name].global.js',
}
}
}
})