phanpy/vite.config.js

138 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-12-10 09:14:48 +00:00
import preact from '@preact/preset-vite';
import { execSync } from 'child_process';
import fs from 'fs';
import { resolve } from 'path';
2023-12-25 11:25:48 +00:00
import { uid } from 'uid/single';
import { defineConfig, loadEnv, splitVendorChunkPlugin } from 'vite';
2023-02-28 07:27:42 +00:00
import generateFile from 'vite-plugin-generate-file';
import htmlPlugin from 'vite-plugin-html-config';
import { VitePWA } from 'vite-plugin-pwa';
2023-01-02 07:09:31 +00:00
import removeConsole from 'vite-plugin-remove-console';
const allowedEnvPrefixes = ['VITE_', 'PHANPY_'];
2023-01-02 06:22:01 +00:00
const { NODE_ENV } = process.env;
2023-05-24 09:18:22 +00:00
const {
2023-12-25 11:25:48 +00:00
PHANPY_CLIENT_NAME: CLIENT_NAME,
PHANPY_APP_ERROR_LOGGING: ERROR_LOGGING,
} = loadEnv('production', process.cwd(), allowedEnvPrefixes);
2022-12-10 09:14:48 +00:00
2023-02-28 07:27:42 +00:00
const now = new Date();
2023-12-25 11:25:48 +00:00
let commitHash;
let fakeCommitHash = false;
2023-12-25 11:25:48 +00:00
try {
commitHash = execSync('git rev-parse --short HEAD').toString().trim();
} catch (error) {
// If error, means git is not installed or not a git repo (could be downloaded instead of git cloned)
// Fallback to random hash which should be different on every build run 🤞
2023-12-03 14:54:03 +00:00
if (process.env.PHANPY_COMMIT_HASH) {
commitHash = process.env.PHANPY_COMMIT_HASH;
} else {
commitHash = uid();
fakeCommitHash = true;
}
2023-12-25 11:25:48 +00:00
}
const rollbarCode = fs.readFileSync(
resolve(__dirname, './rollbar.js'),
'utf-8',
);
2022-12-10 09:14:48 +00:00
// https://vitejs.dev/config/
export default defineConfig({
2023-08-30 09:46:22 +00:00
base: './',
envPrefix: allowedEnvPrefixes,
2024-03-02 05:53:53 +00:00
appType: 'mpa',
mode: NODE_ENV,
define: {
2023-02-28 07:27:42 +00:00
__BUILD_TIME__: JSON.stringify(now),
__COMMIT_HASH__: JSON.stringify(commitHash),
__FAKE_COMMIT_HASH__: fakeCommitHash,
},
2023-04-06 08:15:19 +00:00
server: {
host: true,
},
2024-02-09 12:07:06 +00:00
css: {
preprocessorMaxWorkers: 1,
},
plugins: [
preact(),
splitVendorChunkPlugin(),
removeConsole({
includes: ['log', 'debug', 'info', 'warn', 'error'],
}),
htmlPlugin({
2023-01-02 06:22:01 +00:00
headScripts: ERROR_LOGGING ? [rollbarCode] : [],
}),
2023-02-28 07:27:42 +00:00
generateFile([
{
type: 'json',
output: './version.json',
data: {
buildTime: now,
commitHash,
},
},
]),
VitePWA({
manifest: {
name: CLIENT_NAME,
short_name: CLIENT_NAME,
description: 'Minimalistic opinionated Mastodon web client',
2023-12-24 15:43:18 +00:00
// https://github.com/cheeaun/phanpy/issues/231
// theme_color: '#ffffff',
icons: [
{
src: 'logo-192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: 'logo-512.png',
sizes: '512x512',
type: 'image/png',
},
2022-12-20 11:12:25 +00:00
{
src: 'logo-maskable-512.png',
sizes: '512x512',
type: 'image/png',
purpose: 'maskable',
},
],
2024-03-06 06:25:46 +00:00
categories: ['social', 'news'],
},
strategies: 'injectManifest',
injectRegister: 'inline',
injectManifest: {
// Prevent "Unable to find a place to inject the manifest" error
injectionPoint: undefined,
},
devOptions: {
enabled: NODE_ENV === 'development',
type: 'module',
},
}),
],
build: {
sourcemap: true,
rollupOptions: {
treeshake: false,
input: {
main: resolve(__dirname, 'index.html'),
compose: resolve(__dirname, 'compose/index.html'),
},
output: {
2024-03-04 16:51:53 +00:00
manualChunks: {
'intl-segmenter-polyfill': ['@formatjs/intl-segmenter/polyfill'],
},
chunkFileNames: (chunkInfo) => {
const { facadeModuleId } = chunkInfo;
if (facadeModuleId && facadeModuleId.includes('icon')) {
return 'assets/icons/[name]-[hash].js';
}
return 'assets/[name]-[hash].js';
},
},
},
},
2022-12-10 09:14:48 +00:00
});