-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathvite.config.ts
More file actions
152 lines (145 loc) · 5.01 KB
/
vite.config.ts
File metadata and controls
152 lines (145 loc) · 5.01 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
/// <reference types="vitest/config" />
import { storybookTest } from '@storybook/addon-vitest/vitest-plugin'
import tailwindcss from '@tailwindcss/vite'
import react from '@vitejs/plugin-react'
import { execSync } from 'node:child_process'
import { fileURLToPath } from 'node:url'
import path from 'path'
import { defineConfig } from 'vite'
import { analyzer } from 'vite-bundle-analyzer'
const dirname = typeof __dirname !== 'undefined' ? __dirname : path.dirname(fileURLToPath(import.meta.url))
// More info at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon
const host = process.env.TAURI_DEV_HOST
// Import will happen eagerly, but the plugin is only activated when requested.
// Detect whether we should run the bundle analyzer. We look for either:
// 1. The ANALYZE environment variable explicitly set to "true" (e.g. `ANALYZE=true bun run build`)
// 2. The `analyze` npm/bun script being executed (process arguments include the word "analyze")
const shouldAnalyze = process.env.ANALYZE?.toLowerCase() === 'true' || process.argv.includes('analyze')
// Source maps are disabled by default so forks don't accidentally expose proprietary code.
// Enable with ENABLE_SOURCEMAP=true (e.g. in CI) to upload maps to PostHog for error tracking.
const sourcemap = process.env.ENABLE_SOURCEMAP?.toLowerCase() === 'true' ? 'hidden' : false
// https://vitejs.dev/config/
export default defineConfig({
build: {
sourcemap,
rollupOptions: {
external: ['bun:sqlite'],
},
},
plugins: [
{
name: 'copy-powersync-assets',
buildStart() {
execSync('powersync-web copy-assets --output public', { stdio: 'inherit' })
},
},
tailwindcss(),
react(),
// Include the bundle analyzer plugin only when explicitly requested.
...(shouldAnalyze
? [
analyzer({
analyzerMode: 'static',
openAnalyzer: false,
}),
]
: []),
{
name: 'configure-response-headers',
configureServer: (server) => {
server.middlewares.use((req, res, next) => {
res.setHeader('Cross-Origin-Embedder-Policy', 'require-corp')
res.setHeader('Cross-Origin-Opener-Policy', 'same-origin')
// Set correct Content-Type for .well-known files (required for Universal Links / App Links)
// Parse pathname to ignore query parameters
const pathname = req.url?.split('?')[0]
if (pathname === '/.well-known/apple-app-site-association') {
res.setHeader('Content-Type', 'application/json')
} else if (pathname === '/.well-known/assetlinks.json') {
res.setHeader('Content-Type', 'application/json')
}
next()
})
},
},
],
resolve: {
dedupe: ['@powersync/common', '@powersync/react', 'react'],
alias: {
'@': path.resolve(__dirname, './src'),
'@shared': path.resolve(__dirname, './shared'),
// Exposes PowerSync internal lib path so our custom SharedWorker can extend
// SharedSyncImplementation (not in public exports map).
'powersync-web-internal': path.resolve(__dirname, 'node_modules/@powersync/web/lib/src'),
},
conditions: ['browser'],
},
// Vite options tailored for Tauri development and only applied in `tauri dev` or `tauri build`
//
// 1. prevent vite from obscuring rust errors
clearScreen: false,
// 2. tauri expects a fixed port, fail if that port is not available
server: {
port: 1420,
strictPort: true,
host: host || false,
hmr: host
? {
protocol: 'ws',
host,
port: 1421,
}
: undefined,
watch: {
// 3. tell vite to ignore watching `src-tauri`
ignored: ['**/src-tauri/**'],
},
fs: {
strict: true,
allow: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'shared'),
path.resolve(__dirname, 'public'),
path.resolve(__dirname, 'node_modules'),
path.resolve(__dirname, 'dist-isolation'),
path.resolve(__dirname, '.storybook'),
// Vite's HTML middleware checks checkLoadingAccess() for index.html
path.resolve(__dirname, 'index.html'),
],
},
},
optimizeDeps: {
exclude: ['@journeyapps/wa-sqlite', '@powersync/web'],
},
worker: {
format: 'es',
},
test: {
projects: [
{
extends: true,
plugins: [
// The plugin will run tests for the stories defined in your Storybook config
// See options at: https://storybook.js.org/docs/next/writing-tests/integrations/vitest-addon#storybooktest
storybookTest({
configDir: path.join(dirname, '.storybook'),
}),
],
test: {
name: 'storybook',
browser: {
enabled: true,
headless: true,
provider: 'playwright',
instances: [
{
browser: 'chromium',
},
],
},
setupFiles: ['.storybook/vitest.setup.ts'],
},
},
],
},
})