2022-12-15 09:42:34 +03:00
|
|
|
import { execSync } from 'child_process';
|
2022-12-28 08:47:11 +03:00
|
|
|
import fs from 'fs';
|
2022-12-13 15:42:09 +03:00
|
|
|
import { resolve } from 'path';
|
2024-07-08 12:40:16 +03:00
|
|
|
|
2024-08-13 10:26:23 +03:00
|
|
|
import { lingui } from '@lingui/vite-plugin';
|
2024-07-08 12:40:16 +03:00
|
|
|
import preact from '@preact/preset-vite';
|
2023-12-25 14:25:48 +03:00
|
|
|
import { uid } from 'uid/single';
|
2022-12-22 14:38:02 +03:00
|
|
|
import { defineConfig, loadEnv, splitVendorChunkPlugin } from 'vite';
|
2023-02-28 10:27:42 +03:00
|
|
|
import generateFile from 'vite-plugin-generate-file';
|
2022-12-28 08:47:11 +03:00
|
|
|
import htmlPlugin from 'vite-plugin-html-config';
|
2022-12-19 09:51:56 +03:00
|
|
|
import { VitePWA } from 'vite-plugin-pwa';
|
2023-01-02 10:09:31 +03:00
|
|
|
import removeConsole from 'vite-plugin-remove-console';
|
2024-08-16 06:25:59 +03:00
|
|
|
import { run } from 'vite-plugin-run';
|
2022-12-19 09:51:56 +03:00
|
|
|
|
2024-01-05 04:14:09 +03:00
|
|
|
const allowedEnvPrefixes = ['VITE_', 'PHANPY_'];
|
2023-01-02 09:22:01 +03:00
|
|
|
const { NODE_ENV } = process.env;
|
2023-05-24 12:18:22 +03:00
|
|
|
const {
|
2023-12-25 14:25:48 +03:00
|
|
|
PHANPY_CLIENT_NAME: CLIENT_NAME,
|
|
|
|
PHANPY_APP_ERROR_LOGGING: ERROR_LOGGING,
|
2024-01-05 04:14:09 +03:00
|
|
|
} = loadEnv('production', process.cwd(), allowedEnvPrefixes);
|
2022-12-10 12:14:48 +03:00
|
|
|
|
2023-02-28 10:27:42 +03:00
|
|
|
const now = new Date();
|
2023-12-25 14:25:48 +03:00
|
|
|
let commitHash;
|
2023-12-25 15:05:56 +03:00
|
|
|
let fakeCommitHash = false;
|
2023-12-25 14:25:48 +03: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 🤞
|
|
|
|
commitHash = uid();
|
2023-12-25 15:05:56 +03:00
|
|
|
fakeCommitHash = true;
|
2023-12-25 14:25:48 +03:00
|
|
|
}
|
2022-12-15 09:42:34 +03:00
|
|
|
|
2022-12-28 08:47:11 +03:00
|
|
|
const rollbarCode = fs.readFileSync(
|
|
|
|
resolve(__dirname, './rollbar.js'),
|
|
|
|
'utf-8',
|
|
|
|
);
|
|
|
|
|
2022-12-10 12:14:48 +03:00
|
|
|
// https://vitejs.dev/config/
|
|
|
|
export default defineConfig({
|
2023-08-30 12:46:22 +03:00
|
|
|
base: './',
|
2024-01-05 04:14:09 +03:00
|
|
|
envPrefix: allowedEnvPrefixes,
|
2024-03-02 08:53:53 +03:00
|
|
|
appType: 'mpa',
|
2022-12-19 09:51:56 +03:00
|
|
|
mode: NODE_ENV,
|
2022-12-15 09:42:34 +03:00
|
|
|
define: {
|
2023-02-28 10:27:42 +03:00
|
|
|
__BUILD_TIME__: JSON.stringify(now),
|
2022-12-15 09:42:34 +03:00
|
|
|
__COMMIT_HASH__: JSON.stringify(commitHash),
|
2023-12-25 15:05:56 +03:00
|
|
|
__FAKE_COMMIT_HASH__: fakeCommitHash,
|
2022-12-15 09:42:34 +03:00
|
|
|
},
|
2023-04-06 11:15:19 +03:00
|
|
|
server: {
|
|
|
|
host: true,
|
|
|
|
},
|
2024-02-09 15:07:06 +03:00
|
|
|
css: {
|
|
|
|
preprocessorMaxWorkers: 1,
|
|
|
|
},
|
2022-12-19 09:51:56 +03:00
|
|
|
plugins: [
|
2024-07-08 12:40:16 +03:00
|
|
|
preact({
|
|
|
|
// Force use Babel instead of ESBuild due to this change: https://github.com/preactjs/preset-vite/pull/114
|
|
|
|
// Else, a bug will happen with importing variables from import.meta.env
|
2024-08-13 10:26:23 +03:00
|
|
|
babel: {
|
|
|
|
plugins: ['macros'],
|
|
|
|
},
|
2024-07-08 12:40:16 +03:00
|
|
|
}),
|
2024-08-13 10:26:23 +03:00
|
|
|
lingui(),
|
2024-08-16 06:25:59 +03:00
|
|
|
run({
|
|
|
|
silent: false,
|
|
|
|
input: [
|
|
|
|
{
|
|
|
|
name: 'messages:extract:clean',
|
|
|
|
run: ['npm', 'run', 'messages:extract:clean'],
|
|
|
|
pattern: 'src/**/*.{js,jsx,ts,tsx}',
|
|
|
|
},
|
2024-08-21 18:48:08 +03:00
|
|
|
{
|
|
|
|
name: 'update-catalogs',
|
|
|
|
run: ['node', 'scripts/catalogs.js'],
|
|
|
|
pattern: 'src/locales/*.po',
|
|
|
|
},
|
2024-08-16 06:25:59 +03:00
|
|
|
],
|
|
|
|
}),
|
2022-12-19 09:51:56 +03:00
|
|
|
splitVendorChunkPlugin(),
|
2023-02-14 14:10:21 +03:00
|
|
|
removeConsole({
|
|
|
|
includes: ['log', 'debug', 'info', 'warn', 'error'],
|
|
|
|
}),
|
2022-12-28 08:47:11 +03:00
|
|
|
htmlPlugin({
|
2023-01-02 09:22:01 +03:00
|
|
|
headScripts: ERROR_LOGGING ? [rollbarCode] : [],
|
2022-12-28 08:47:11 +03:00
|
|
|
}),
|
2023-02-28 10:27:42 +03:00
|
|
|
generateFile([
|
|
|
|
{
|
|
|
|
type: 'json',
|
|
|
|
output: './version.json',
|
|
|
|
data: {
|
|
|
|
buildTime: now,
|
|
|
|
commitHash,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]),
|
2022-12-19 09:51:56 +03:00
|
|
|
VitePWA({
|
|
|
|
manifest: {
|
|
|
|
name: CLIENT_NAME,
|
|
|
|
short_name: CLIENT_NAME,
|
|
|
|
description: 'Minimalistic opinionated Mastodon web client',
|
2023-12-24 18:43:18 +03:00
|
|
|
// https://github.com/cheeaun/phanpy/issues/231
|
|
|
|
// theme_color: '#ffffff',
|
2022-12-19 09:51:56 +03:00
|
|
|
icons: [
|
|
|
|
{
|
|
|
|
src: 'logo-192.png',
|
|
|
|
sizes: '192x192',
|
|
|
|
type: 'image/png',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
src: 'logo-512.png',
|
|
|
|
sizes: '512x512',
|
|
|
|
type: 'image/png',
|
|
|
|
},
|
2022-12-20 14:12:25 +03:00
|
|
|
{
|
|
|
|
src: 'logo-maskable-512.png',
|
|
|
|
sizes: '512x512',
|
|
|
|
type: 'image/png',
|
|
|
|
purpose: 'maskable',
|
|
|
|
},
|
2022-12-19 09:51:56 +03:00
|
|
|
],
|
2024-03-06 09:25:46 +03:00
|
|
|
categories: ['social', 'news'],
|
2022-12-19 09:51:56 +03:00
|
|
|
},
|
|
|
|
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',
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2022-12-13 15:42:09 +03:00
|
|
|
build: {
|
2022-12-15 06:47:11 +03:00
|
|
|
sourcemap: true,
|
2024-03-24 05:08:09 +03:00
|
|
|
cssCodeSplit: false,
|
2022-12-13 15:42:09 +03:00
|
|
|
rollupOptions: {
|
2023-03-22 16:01:03 +03:00
|
|
|
treeshake: false,
|
2022-12-13 15:42:09 +03:00
|
|
|
input: {
|
|
|
|
main: resolve(__dirname, 'index.html'),
|
|
|
|
compose: resolve(__dirname, 'compose/index.html'),
|
|
|
|
},
|
2023-04-14 12:23:41 +03:00
|
|
|
output: {
|
2024-06-10 15:38:41 +03:00
|
|
|
manualChunks: {
|
|
|
|
// 'intl-segmenter-polyfill': ['@formatjs/intl-segmenter/polyfill'],
|
|
|
|
'tinyld-light': ['tinyld/light'],
|
|
|
|
},
|
2023-04-14 12:23:41 +03:00
|
|
|
chunkFileNames: (chunkInfo) => {
|
|
|
|
const { facadeModuleId } = chunkInfo;
|
|
|
|
if (facadeModuleId && facadeModuleId.includes('icon')) {
|
|
|
|
return 'assets/icons/[name]-[hash].js';
|
|
|
|
}
|
2024-08-14 12:17:34 +03:00
|
|
|
if (facadeModuleId && facadeModuleId.includes('locales')) {
|
|
|
|
return 'assets/locales/[name]-[hash].js';
|
|
|
|
}
|
2023-04-14 12:23:41 +03:00
|
|
|
return 'assets/[name]-[hash].js';
|
|
|
|
},
|
|
|
|
},
|
2022-12-13 15:42:09 +03:00
|
|
|
},
|
|
|
|
},
|
2022-12-10 12:14:48 +03:00
|
|
|
});
|