{{ message }}
Repository navigation
Expand file tree
/
Copy pathvite.config.mts
More file actions
673 lines (647 loc) · 23.4 KB
/
Copy pathvite.config.mts
File metadata and controls
673 lines (647 loc) · 23.4 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import twemoji from '@discordapp/twemoji';
import tailwindcss from '@tailwindcss/vite';
import react from '@vitejs/plugin-react';
import checker from 'vite-plugin-checker';
import electron from 'vite-plugin-electron/simple';
import type { Plugin } from 'vite-plus';
import { defineConfig } from 'vite-plus';
import { Constants } from './src/renderer/constants.ts';
/**
* Vite plugin that copies static assets to disk on startup.
* Runs in both dev and build modes so the Electron main process can resolve
* asset file URLs without requiring a prior `pnpm build`.
*/
const copyStaticAssetsPlugin = (): Plugin => {
const flatten = (obj: object): string[] =>
Object.values(obj).flatMap((v) => (Array.isArray(v) ? v : flatten(v as object)));
const extractSvgFilename = (imgHtml: string) => /src="(.*)"/.exec(imgHtml)?.[1].split('/').pop();
let isBuild = false;
const copyAssets = () => {
// Copy the root assets/ directory (images, sounds, etc.) into build/
fs.cpSync(
fileURLToPath(new URL('assets', import.meta.url)),
fileURLToPath(new URL('build/assets', import.meta.url)),
{ recursive: true },
);
// Copy individual Twemoji SVGs needed by the app into build/assets/images/twemoji/
const twemojiSrcDir = fileURLToPath(
new URL('node_modules/@discordapp/twemoji/dist/svg', import.meta.url),
);
const twemojiDestDir = fileURLToPath(new URL('build/assets/images/twemoji', import.meta.url));
fs.mkdirSync(twemojiDestDir, { recursive: true });
const allEmojis = flatten(Constants.EMOJIS);
for (const emoji of allEmojis) {
const filename = extractSvgFilename(twemoji.parse(emoji, { folder: 'svg', ext: '.svg' }));
if (filename) {
fs.copyFileSync(path.join(twemojiSrcDir, filename), path.join(twemojiDestDir, filename));
}
}
};
return {
name: 'copy-static-assets',
configResolved(config) {
isBuild = config.command === 'build';
},
// In serve/dev mode, copy before the build starts (emptyOutDir doesn't run).
// In build mode, copy after all output is written — buildStart runs before
// Vite's emptyOutDir wipe, so assets copied there would be deleted.
buildStart() {
if (!isBuild) {
copyAssets();
}
},
closeBundle() {
if (isBuild) {
copyAssets();
}
},
};
};
/**
* Vite plugin that injects React DevTools connection script in dev mode only.
* This script connects the renderer process to the standalone react-devtools instance.
*/
const reactDevToolsPlugin = (): Plugin => ({
name: 'react-devtools',
apply: 'serve', // Only apply in dev mode (vite dev)
transformIndexHtml(html) {
// Inject devtools connection script and update CSP to allow localhost:8097
return html
.replace(
'<meta http-equiv="Content-Security-Policy" content="script-src \'self\';',
'<meta http-equiv="Content-Security-Policy" content="script-src \'self\' http://localhost:8097;',
)
.replace('</head>', ' <script src="http://localhost:8097"></script>\n </head>');
},
});
const isBuild = process.argv.includes('build');
const TEST_FILES = ['**/*.test.ts', '**/*.test.tsx'];
const TEST_SUPPORT_FILES = ['**/__mocks__/**', '**/__helpers__/**'];
const TEST_EXCLUDES = [...TEST_FILES, ...TEST_SUPPORT_FILES];
const RENDERER_SOURCE_FILES = ['src/renderer/**/*.ts', 'src/renderer/**/*.tsx'];
// TypeScript source globs for one or more renderer layers, so an override
// cannot accidentally omit `.tsx`.
const rendererLayerFiles = (...layers: string[]): string[] =>
layers.flatMap((layer) => [`src/renderer/${layer}/**/*.ts`, `src/renderer/${layer}/**/*.tsx`]);
// Match both a layer's directory and its contents, so a bare directory import
// (`../hooks`) cannot bypass the boundary rules once a barrel `index.ts` exists.
const forbiddenLayerGlobs = (layers: string[]): string[] =>
layers.flatMap((layer) => [`**/${layer}`, `**/${layer}/**`]);
const rendererBoundaryPattern = (layers: string[], message: string) => ({
group: forbiddenLayerGlobs(layers),
allowTypeImports: true,
message,
});
// Shared so a more specific override (for example the notification UI) can
// compose it instead of restating it and silently dropping the parent rule.
const componentRoutesBoundaryPattern = rendererBoundaryPattern(
['routes'],
'Renderer boundary: `components` may not import from routes (see ARCHITECTURE.md).',
);
const notificationQueryBoundaryPattern = {
group: [
'**/hooks/useNotifications',
'**/hooks/notifications/useNotificationsQuery',
'**/hooks/notifications/useNotificationsHost',
],
allowTypeImports: true,
message:
'Notification UI must not subscribe to the notifications query; it receives data via props. Use `useNotificationActions` for actions (see ARCHITECTURE.md).',
};
const processBoundaryPattern = {
group: [...forbiddenLayerGlobs(['main', 'preload']), 'electron', 'electron/**'],
allowTypeImports: true,
message:
'Renderer boundary: use `window.gitify` instead of importing the main process, preload, or Electron (see ARCHITECTURE.md).',
};
const rendererImportBoundaryPattern = {
group: forbiddenLayerGlobs(['renderer']),
allowTypeImports: true,
message: 'Process boundary: main and preload must not import the renderer (see ARCHITECTURE.md).',
};
const sharedImportBoundaryPattern = {
group: forbiddenLayerGlobs(['main', 'preload', 'renderer']),
allowTypeImports: true,
message:
'Process boundary: shared code must not import main, preload, or renderer modules (see ARCHITECTURE.md).',
};
const mainImportBoundaryPattern = {
group: forbiddenLayerGlobs(['main']),
allowTypeImports: true,
message: 'Process boundary: preload must not import main (see ARCHITECTURE.md).',
};
const preloadImportBoundaryPattern = {
group: forbiddenLayerGlobs(['preload']),
allowTypeImports: true,
message: 'Process boundary: main must not import preload (see ARCHITECTURE.md).',
};
const forgeSpecificBoundaryPattern = rendererBoundaryPattern(
['forges/github', 'forges/gitlab', 'forges/gitea', 'forges/bitbucket'],
'Renderer boundary: shared code must use the forge adapter instead of importing a forge-specific module (see ARCHITECTURE.md).',
);
const storeBarrelBoundaryPattern = {
group: ['**/stores', '**/stores/index', '**/stores/index.ts'],
allowTypeImports: true,
message:
'Renderer boundary: import the specific store module instead of the `stores` barrel (see ARCHITECTURE.md).',
};
export default defineConfig({
plugins: [
// only run the checker plugin in dev (not during `vite build`)
...(isBuild
? []
: [
checker({
oxlint: { lintCommand: 'oxlint', dev: { logLevel: ['error'] } },
}),
]),
reactDevToolsPlugin(),
react(),
tailwindcss(),
electron({
main: {
entry: fileURLToPath(new URL('src/main/index.ts', import.meta.url)),
onstart: async ({ startup }) => {
// vite-plugin-electron v1 starts Electron with `cwd: server.config.root`.
// Our Vite root is `src/renderer`, so we must override `cwd` back to the
// repository root or Electron will try to boot from `src/renderer`.
await startup(undefined, {
cwd: fileURLToPath(new URL('.', import.meta.url)),
});
},
vite: {
// The outer Vite config sets root:'src/renderer', so we must
// explicitly tell the main-process sub-build where to find .env files.
envDir: fileURLToPath(new URL('.', import.meta.url)),
build: {
outDir: fileURLToPath(new URL('build', import.meta.url)),
rolldownOptions: {
output: { entryFileNames: 'main.js' },
external: [
'electron',
// TODO - how many of these are truly needed?
'electron-log',
'electron-menubar',
'electron-updater',
],
},
},
},
},
preload: {
input: fileURLToPath(new URL('src/preload/index.ts', import.meta.url)),
vite: {
build: {
outDir: fileURLToPath(new URL('build', import.meta.url)),
rolldownOptions: { output: { entryFileNames: 'preload.js' } },
},
resolve: { conditions: ['node'] },
},
},
}),
copyStaticAssetsPlugin(),
],
root: 'src/renderer',
publicDir: false as const,
base: './',
build: {
outDir: fileURLToPath(new URL('build', import.meta.url)),
emptyOutDir: true,
},
// Define build-time replacements for the main process bundle.
// During CI builds `process.env.OAUTH_CLIENT_ID` will be injected via the environment.
define: isBuild
? {
'process.env.OAUTH_CLIENT_ID': JSON.stringify(process.env.OAUTH_CLIENT_ID ?? ''),
}
: {
// Development Keys - See CONTRIBUTING.md
'process.env.OAUTH_CLIENT_ID': JSON.stringify('Ov23liQIkFs5ehQLNzHF'),
},
lint: {
plugins: ['typescript', 'react', 'unicorn', 'oxc', 'import', 'vitest'],
// Enable the type-aware path so `vp check` runs TypeScript type checks
// natively (via the TS Go toolchain / tsgolint). `typeCheck` requires
// `typeAware`; enabling both also turns on the type-aware lint rules.
options: {
typeAware: true,
typeCheck: true,
},
categories: {
correctness: 'error',
},
ignorePatterns: ['**/generated/**', 'build/**', 'dist/**', 'coverage/**', 'node_modules/**'],
rules: {
'import/no-self-import': 'error',
'import/no-duplicates': 'error',
'import/no-empty-named-blocks': 'error',
'no-console': 'error',
'no-unused-vars': [
'error',
{
args: 'after-used',
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
caughtErrorsIgnorePattern: '^_',
},
],
'no-param-reassign': 'error',
'default-param-last': 'error',
'default-case': 'error',
curly: 'error',
'react/function-component-definition': [
'error',
{
namedComponents: 'function-declaration',
unnamedComponents: 'arrow-function',
},
],
'react/exhaustive-deps': 'error',
'react/rules-of-hooks': 'error',
'react/self-closing-comp': 'error',
'react/jsx-no-target-blank': 'error',
'react/no-danger': 'error',
'typescript/no-inferrable-types': 'error',
'typescript/prefer-as-const': 'error',
'typescript/prefer-enum-initializers': 'error',
'unicorn/prefer-number-properties': 'error',
},
overrides: [
{
// Renderer boundary: every renderer module reaches the main process
// through the preload bridge (`window.gitify`). Importing main/preload
// code directly would bypass that contract. Layer-specific overrides
// below compose this pattern so they cannot silently drop it.
// See ARCHITECTURE.md.
files: RENDERER_SOURCE_FILES,
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': ['error', { patterns: [processBoundaryPattern] }],
},
},
{
// Renderer boundary: `utils` and `stores` are peer domain layers; neither
// may import hooks, components, or routes. Shared utils also may not
// import a forge-specific module; they go through the forge adapter.
// See ARCHITECTURE.md.
files: rendererLayerFiles('utils'),
excludeFiles: ['src/renderer/utils/forges/**', ...TEST_FILES],
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
rendererBoundaryPattern(
['hooks', 'components', 'routes'],
'Renderer boundary: `utils` may not import from hooks, components, or routes (see ARCHITECTURE.md).',
),
processBoundaryPattern,
forgeSpecificBoundaryPattern,
storeBarrelBoundaryPattern,
],
},
],
},
},
{
// Forge adapter implementations are the one place allowed to import
// their own forge-specific modules. They remain `utils` and may not
// import hooks, components, or routes. See ARCHITECTURE.md.
files: rendererLayerFiles('utils/forges'),
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
rendererBoundaryPattern(
['hooks', 'components', 'routes'],
'Renderer boundary: `utils` may not import from hooks, components, or routes (see ARCHITECTURE.md).',
),
processBoundaryPattern,
storeBarrelBoundaryPattern,
],
},
],
},
},
{
files: rendererLayerFiles('stores'),
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
rendererBoundaryPattern(
['hooks', 'components', 'routes'],
'Renderer boundary: `stores` may not import from hooks, components, or routes (see ARCHITECTURE.md).',
),
processBoundaryPattern,
forgeSpecificBoundaryPattern,
],
},
],
},
},
{
files: rendererLayerFiles('hooks'),
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
rendererBoundaryPattern(
['components', 'routes'],
'Renderer boundary: `hooks` may not import from components or routes (see ARCHITECTURE.md).',
),
processBoundaryPattern,
forgeSpecificBoundaryPattern,
],
},
],
},
},
{
files: rendererLayerFiles('components'),
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
componentRoutesBoundaryPattern,
processBoundaryPattern,
forgeSpecificBoundaryPattern,
],
},
],
},
},
{
// Renderer boundary: the notification UI renders once per notification
// (rows, repository headers, account headers) and receives its data via
// props from the route. It must not subscribe to the notifications query:
// every observer re-runs `select` and the structural-sharing walk on each
// cache write, so subscribing here multiplies that cost by the number of
// mounted items. Action-only consumers use `useNotificationActions`.
// See ARCHITECTURE.md.
files: rendererLayerFiles('components/notifications'),
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
componentRoutesBoundaryPattern,
notificationQueryBoundaryPattern,
processBoundaryPattern,
forgeSpecificBoundaryPattern,
],
},
],
},
},
{
// Renderer boundary: `icons` is a presentational leaf. It may not
// depend on any implementation layer. See ARCHITECTURE.md.
files: rendererLayerFiles('icons'),
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
rendererBoundaryPattern(
['utils', 'stores', 'hooks', 'components', 'routes'],
'Renderer boundary: `icons` is a leaf and may not import utils, stores, hooks, components, or routes (see ARCHITECTURE.md).',
),
processBoundaryPattern,
forgeSpecificBoundaryPattern,
],
},
],
},
},
{
// Renderer boundary: `types`/`constants` are leaf modules. They may
// reference types (hence allowTypeImports) but not implementation
// layers. See ARCHITECTURE.md.
files: ['src/renderer/types.ts', 'src/renderer/constants.ts'],
rules: {
'no-restricted-imports': [
'error',
{
patterns: [
rendererBoundaryPattern(
['utils', 'stores', 'hooks', 'components', 'routes'],
'Renderer boundary: leaf modules (`types`, `constants`) may not import implementation layers (see ARCHITECTURE.md).',
),
processBoundaryPattern,
forgeSpecificBoundaryPattern,
],
},
],
},
},
{
// Renderer boundary: the renderer is sandboxed and must reach the main
// process through `window.gitify`, not by importing Node builtins.
// See ARCHITECTURE.md.
files: RENDERER_SOURCE_FILES,
excludeFiles: TEST_EXCLUDES,
rules: {
'import/no-nodejs-modules': 'error',
},
},
{
// Renderer boundary: shared renderer code outside the forge adapter
// implementations must go through the adapter API. See ARCHITECTURE.md.
files: ['src/renderer/App.tsx', 'src/renderer/index.tsx', ...rendererLayerFiles('routes')],
excludeFiles: TEST_FILES,
rules: {
'no-restricted-imports': [
'error',
{ patterns: [forgeSpecificBoundaryPattern, processBoundaryPattern] },
],
},
},
{
// `import/no-cycle` is enforced across the renderer. The pre-existing
// cycles in `utils`, `stores`, and the forge adapters were removed so
// the rule can be enabled here. See ARCHITECTURE.md.
files: RENDERER_SOURCE_FILES,
excludeFiles: TEST_EXCLUDES,
rules: {
'import/no-cycle': 'error',
},
},
{
// Process boundary: main must not reach into preload or the renderer.
files: ['src/main/**/*.ts', 'src/main/**/*.tsx'],
excludeFiles: TEST_EXCLUDES,
rules: {
'no-restricted-imports': [
'error',
{ patterns: [rendererImportBoundaryPattern, preloadImportBoundaryPattern] },
],
},
},
{
// Process boundary: preload must not reach into main or the renderer.
files: ['src/preload/**/*.ts', 'src/preload/**/*.tsx'],
excludeFiles: TEST_EXCLUDES,
rules: {
'no-restricted-imports': [
'error',
{ patterns: [rendererImportBoundaryPattern, mainImportBoundaryPattern] },
],
},
},
{
// Process boundary: shared code is imported by both processes and must
// stay independent of main, preload, and renderer modules. It is also
// bundled into the renderer, so it must not import Node builtins.
files: ['src/shared/**/*.ts', 'src/shared/**/*.tsx'],
excludeFiles: TEST_EXCLUDES,
rules: {
'no-restricted-imports': ['error', { patterns: [sharedImportBoundaryPattern] }],
'import/no-nodejs-modules': 'error',
},
},
{
// Cycle detection also covers the main, preload, and shared processes.
files: [
'src/main/**/*.ts',
'src/main/**/*.tsx',
'src/preload/**/*.ts',
'src/preload/**/*.tsx',
'src/shared/**/*.ts',
'src/shared/**/*.tsx',
],
excludeFiles: TEST_EXCLUDES,
rules: {
'import/no-cycle': 'error',
},
},
{
files: TEST_EXCLUDES,
rules: {
'vitest/no-focused-tests': 'error',
'vitest/no-disabled-tests': 'error',
'vitest/no-standalone-expect': 'error',
// Vitest supports expect(actual, message); the default maxArgs is 1.
'vitest/valid-expect': ['error', { maxArgs: 2 }],
// Snapshot hints are not enforced; snapshots are being phased out.
'vitest/prefer-snapshot-hint': 'off',
// Typing every `vi.fn()` mock is a separate, large test refactor.
'vitest/require-mock-type-parameters': 'off',
'typescript/no-explicit-any': 'off',
'no-console': 'off',
// Mocks are typed as the real Electron/interface classes, so
// `expect(dialog.showMessageBox)` reads as an unbound class method
// even though the runtime value is a `vi.fn()`. The rule is a false
// positive for test doubles, so scope it to non-test code.
'typescript/unbound-method': 'off',
},
},
{
files: ['**/*.visual.test.tsx'],
rules: {
// Visual assertions live in the `captureRoute` helper.
'vitest/expect-expect': [
'error',
{
assertFunctionNames: [
'expect',
'expectTypeOf',
'assert',
'assertType',
'captureRoute',
],
},
],
},
},
{
files: ['scripts/**', 'codegen.ts', 'vite.config.mts', 'vitest.config.mts'],
rules: {
'no-console': 'off',
},
},
],
},
fmt: {
tabWidth: 2,
useTabs: false,
singleQuote: true,
jsxSingleQuote: false,
ignorePatterns: [
'**/generated/**',
'build/**',
'dist/**',
'coverage/**',
'.context/**',
'index.html',
// Autogenerated by release-please; its formatting is not ours to enforce.
'CHANGELOG.md',
],
sortImports: {
newlinesBetween: true,
customGroups: [
{ groupName: 'react', elementNamePattern: ['react', 'react-*', '@testing-library/**'] },
{ groupName: 'electron', elementNamePattern: ['*electron*', 'menubar'] },
{ groupName: 'primer', elementNamePattern: ['@primer/**'] },
{ groupName: 'octokit', elementNamePattern: ['@octokit/**'] },
{
groupName: 'mocks-helpers',
elementNamePattern: ['**/__mocks__/**', '**/__helpers__/**'],
},
{ groupName: 'shared', elementNamePattern: ['**/shared/**'] },
{ groupName: 'constants', elementNamePattern: ['**/constants', '**/constants/**'] },
{
groupName: 'state',
elementNamePattern: ['**/hooks/**', '**/routes/**', '**/stores', '**/stores/**'],
},
{
groupName: 'ui',
elementNamePattern: [
'**/layout/**',
'**/components/**',
'**/fields/**',
'**/primitives/**',
],
},
{ groupName: 'types', elementNamePattern: ['**/types'] },
],
groups: [
'builtin',
'react',
'electron',
'primer',
'octokit',
'external',
'mocks-helpers',
'shared',
'constants',
'state',
'ui',
'types',
['internal', 'parent', 'sibling', 'index', 'subpath'],
'unknown',
],
},
},
staged: {
'*': 'vp fmt --no-error-on-unmatched-pattern',
'*.{js,jsx,ts,tsx}': 'vp lint --fix --no-error-on-unmatched-pattern',
'*.{js,ts,tsx}': [
'bash -c "tsc --noEmit"',
"vp test --changed --passWithNoTests --update --project 'happy-dom [preload, renderer]' --project 'node [main, shared]'",
],
},
});
You can’t perform that action at this time.
