aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/frontend/vite.config.ts
blob: 0c13f133b748c782c71c1ac8dede72a7e26a58c2 (plain) (blame)
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

import { lezer } from '@lezer/generator/rollup';
import react from '@vitejs/plugin-react';
import { minify } from 'html-minifier-terser';
import { defineConfig, PluginOption } from 'vite';
import injectPreload from 'vite-plugin-inject-preload';
import { VitePWA } from 'vite-plugin-pwa';

const thisDir = path.dirname(fileURLToPath(import.meta.url));

const mode = process.env.MODE || 'development';
const isDevelopment = mode === 'development';
process.env.NODE_ENV ??= mode;

function portNumberOrElse(envName: string, fallback: number): number {
  const value = process.env[envName];
  return value ? parseInt(value, 10) : fallback;
}

const listenHost = process.env.LISTEN_HOST || 'localhost';
const listenPort = portNumberOrElse('LISTEN_PORT', 1313);
const apiHost = process.env.API_HOST || listenHost;
const apiPort = portNumberOrElse('API_PORT', 1312);
const apiSecure = apiPort === 443;
const publicHost = process.env.PUBLIC_HOST || listenHost;
const publicPort = portNumberOrElse('PUBLIC_PORT', listenPort);

const { name: packageName, version: packageVersion } = JSON.parse(
  readFileSync(path.join(thisDir, 'package.json'), 'utf8'),
) as { name: string; version: string };
process.env.VITE_PACKAGE_NAME ??= packageName;
process.env.VITE_PACKAGE_VERSION ??= packageVersion;

const minifyPlugin: PluginOption = {
  name: 'minify-html',
  enforce: 'post',
  async transformIndexHtml(html) {
    if (isDevelopment) {
      return html;
    }
    return minify(html, {
      collapseWhitespace: true,
      collapseBooleanAttributes: true,
      minifyCSS: true,
      removeComments: true,
      removeAttributeQuotes: true,
      removeRedundantAttributes: true,
      sortAttributes: true,
    });
  },
};

export default defineConfig({
  logLevel: 'info',
  mode,
  root: thisDir,
  cacheDir: path.join(thisDir, 'build/vite/cache'),
  plugins: [
    minifyPlugin,
    react({
      babel: {
        // Gets rid of deoptimization warnings for large chunks.
        // We don't need to minify here, because the output of Babel
        // will get passed to esbuild anyways.
        compact: false,
        minified: false,
      },
    }),
    injectPreload({
      files: [
        {
          match:
            /(?:jetbrains-mono-latin-variable-wghtOnly-(?:italic|normal)|roboto-latin-(?:400|500)-normal).+\.woff2$/,
          attributes: {
            type: 'font/woff2',
            as: 'font',
            crossorigin: 'anonymous',
          },
        },
      ],
    }),
    lezer(),
    VitePWA({
      strategies: 'generateSW',
      registerType: 'prompt',
      injectRegister: null,
      devOptions: {
        enabled: true,
      },
      // Unregister service worker installed in production mode
      // if Vite is started in development mode on the same domain.
      selfDestroying: isDevelopment,
      workbox: {
        globPatterns: [
          '**/*.{css,html,js}',
          'roboto-latin-{300,400,500,700}-normal.*.woff2',
          'jetbrains-mono-latin-variable-wghtOnly-{normal,italic}.*.woff2',
        ],
        dontCacheBustURLsMatching: /\.(?:css|js|woff2?)$/,
        navigateFallbackDenylist: [/^\/xtext-service/],
        sourcemap: isDevelopment,
      },
      includeAssets: ['apple-touch-icon.png', 'favicon.svg', 'mask-icon.svg'],
      manifest: {
        lang: 'en-US',
        name: 'Refinery',
        short_name: 'Refinery',
        description:
          'An efficient graph sovler for generating well-formed models',
        theme_color: '#f5f5f5',
        display_override: ['window-controls-overlay'],
        display: 'standalone',
        background_color: '#21252b',
        icons: [
          {
            src: 'icon-192x192.png',
            sizes: '192x192',
            type: 'image/png',
            purpose: 'any maskable',
          },
          {
            src: 'icon-512x512.png',
            sizes: '512x512',
            type: 'image/png',
            purpose: 'any maskable',
          },
          {
            src: 'icon-any.svg',
            sizes: 'any',
            type: 'image/svg+xml',
            purpose: 'any maskable',
          },
          {
            src: 'mask-icon.svg',
            sizes: 'any',
            type: 'image/svg+xml',
            purpose: 'monochrome',
          },
        ],
      },
    }),
  ],
  base: '',
  define: {
    __DEV__: JSON.stringify(isDevelopment), // For MobX
  },
  build: {
    assetsDir: '.',
    // If we don't control inlining manually,
    // web fonts will randomly get inlined into the CSS, degrading performance.
    assetsInlineLimit: 0,
    outDir: path.join('build/vite', mode),
    emptyOutDir: true,
    sourcemap: isDevelopment,
    minify: !isDevelopment,
  },
  server: {
    host: listenHost,
    port: listenPort,
    strictPort: true,
    proxy: {
      '/xtext-service': {
        target: `${apiSecure ? 'https' : 'http'}://${apiHost}:${apiPort}`,
        ws: true,
        secure: apiSecure,
      },
    },
    hmr: {
      host: publicHost,
      clientPort: publicPort,
      path: '/vite',
    },
  },
});