yacd/vite.config.ts

83 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-05-08 18:37:08 +08:00
import react from '@vitejs/plugin-react'
2021-05-30 16:33:27 +08:00
import * as path from 'path';
2023-01-30 20:52:43 +08:00
import { defineConfig } from 'vite';
import { VitePWA } from 'vite-plugin-pwa';
2021-05-30 16:33:27 +08:00
import * as pkg from './package.json';
// https://vitejs.dev/config/
2023-01-30 20:52:43 +08:00
export default defineConfig(async ({ mode }) => {
let hash = process.env.COMMIT_HASH;
if (!hash) {
try {
hash = await gitHash();
} catch (e) { }
}
if (!hash) hash = '';
return {
define: {
__VERSION__: JSON.stringify(pkg.version),
__COMMIT_HASH__: JSON.stringify(hash),
'process.env.NODE_ENV': JSON.stringify(mode),
'process.env.PUBLIC_URL': JSON.stringify('./'),
2022-05-08 18:37:08 +08:00
},
2023-01-30 20:52:43 +08:00
base: './',
resolve: {
alias: {
$src: path.resolve(__dirname, './src'),
src: path.resolve(__dirname, './src'),
},
},
publicDir: 'assets',
build: {
// sourcemap: true,
// the default value is 'dist'
// which make more sense
// but change this may break other people's tools
2021-05-30 16:33:27 +08:00
outDir: 'public',
2023-01-30 20:52:43 +08:00
},
plugins: [
react(),
VitePWA({
srcDir: 'src',
outDir: 'public',
filename: 'sw.ts',
strategies: 'injectManifest',
base: './',
}),
],
}
});
// non vite stuff
async function gitHash() {
try {
const mod = await import('node:child_process');
return await run(mod.spawn, 'git', ['rev-parse', '--short', 'HEAD']);
} catch (e) {
return;
}
}
function run(spawn: typeof import('node:child_process').spawn, cmd0: string, args0: string[]): Promise<string> {
const cmd = cmd0;
const args = args0;
return new Promise((resolve, reject) => {
const proc = spawn(cmd, args);
let out = Buffer.from('');
proc.stdout.on('data', (data) => {
out += data;
});
proc.on('error', (err) => {
reject(err);
});
proc.on('exit', (code) => {
if (code !== 0) reject(code);
resolve(out.toString());
});
});
}