Repository navigation
Expand file tree
/
Copy pathvite.config.ts
More file actions
153 lines (147 loc) · 5.08 KB
/
Copy pathvite.config.ts
File metadata and controls
153 lines (147 loc) · 5.08 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
// vite build config for control UI
// widget has its own config, see src/pretix/static/pretixpresale/widget/vite.config.ts
import { defineConfig, type Plugin } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { execSync } from 'child_process'
import { readFileSync } from 'fs'
import { parse as parseToml } from 'smol-toml'
// Shared dependencies exposed to plugins via import map.
// Adding a dep here auto-generates a _vendor/{name} entry and
// makes it available in the import map.
const SHARED_DEPS = ['vue']
const { entries: pretixPluginEntries } = discoverPretixPlugins()
const pluginDirs = [...new Set(Object.values(pretixPluginEntries).map(p => path.dirname(p)))]
export default defineConfig({
plugins: [
vue(),
sharedDepsPlugin(),
pretixPluginDevEntries(),
],
resolve: {
// Pin shared deps to pretix's node_modules to prevent duplicate instances
// across plugins whose node_modules live in sibling directories
dedupe: [...SHARED_DEPS, '@vue/runtime-core', '@vue/reactivity', '@vue/shared'],
},
server: {
fs: {
// Allow serving source files from sibling plugin directories
allow: ['src', ...pluginDirs],
},
cors: {
origin: /^https?:\/\/(?:(?:[^:]+\.)?localhost|127\.0\.0\.1|\[::1\]|[^:]+\.pretix\.(dev|work))(?::\d+)?$/
},
},
build: {
manifest: true,
outDir: path.resolve(__dirname, 'src/pretix/static.dist/vite/control'),
rollupOptions: {
preserveEntrySignatures: 'exports-only',
input: {
'webcheckin/main': path.resolve(__dirname, 'src/pretix/plugins/webcheckin/static/pretixplugins/webcheckin/main.ts'),
'checkinrules/main': path.resolve(__dirname, 'src/pretix/static/pretixcontrol/js/ui/checkinrules/index.ts'),
...Object.fromEntries(SHARED_DEPS.map(dep => [`_vendor/${dep}`, `virtual:vendor/${dep}`])),
...pretixPluginEntries,
},
}
},
optimizeDeps: {
exclude: ['moment', 'jquery']
}
})
// Virtual module plugin: generates re-export entries for each shared dep
// In dev mode, serves /__pretix_importmap so the python template tag can build the import map without hardcoding dep names.
function sharedDepsPlugin (): Plugin {
const PREFIX = 'virtual:vendor/'
const RESOLVED = '\0virtual:vendor/'
return {
name: 'pretix-shared-deps',
resolveId (id) {
if (id.startsWith(PREFIX))
return RESOLVED + id.slice(PREFIX.length)
},
load (id) {
if (id.startsWith(RESOLVED)) {
const pkg = id.slice(RESOLVED.length)
return `export * from '${pkg}'`
}
},
// Serve the import map data so the Python template tag can fetch it in dev mode
configureServer (server) {
server.middlewares.use((req, res, next) => {
if (req.url === '/__pretix_importmap') {
const imports = Object.fromEntries(
SHARED_DEPS.map(dep => [
dep,
`/node_modules/.vite/deps/${dep.replace('/', '_')}.js`,
])
)
res.setHeader('Content-Type', 'application/json')
res.setHeader('Access-Control-Allow-Origin', '*')
res.end(JSON.stringify(imports))
return
}
next()
})
},
}
}
// TODO move to separate file?
function discoverPretixPlugins (): { entries: Record<string, string> } {
let manifestFiles: string[] = []
try {
const raw = execSync(`python -c "
import importlib_metadata as metadata, json, pathlib, tomllib
result = []
for ep in metadata.entry_points(group='pretix.plugin'):
dist = ep.dist
if not dist: continue
try:
url_info = json.loads(dist.read_text('direct_url.json') or '{}')
if not url_info.get('dir_info', {}).get('editable'):
continue # non-editable plugins build their own assets
p = pathlib.Path(url_info['url'].replace('file://', '')) / 'pretixplugin.toml'
if not p.exists():
continue
with p.open('rb') as f:
if 'vite' in tomllib.load(f):
result.append(str(p))
except Exception:
pass
print(json.dumps(result))
"`, { stdio: ['pipe', 'pipe', 'inherit'] }).toString().trim()
manifestFiles = JSON.parse(raw)
} catch (error) {
console.error('Failed to discover pretix plugins, skipping plugin entries:', error)
}
const entries: Record<string, string> = {}
for (const manifestFile of manifestFiles) {
const packageRoot = manifestFile.replace(/[/\\]pretixplugin\.toml$/, '')
const parsed = parseToml(readFileSync(manifestFile, 'utf8')) as {
vite: { entries: Record<string, string> }
}
for (const [name, rel] of Object.entries(parsed.vite.entries)) {
entries[name] = path.join(packageRoot, rel)
}
}
return { entries }
}
// In dev mode, the browser requests /{entryName} from the Vite dev server.
// Vite can't find these files since they live outside the project root.
// This plugin rewrites those URLs to /@fs{absPath} so Vite serves them directly.
function pretixPluginDevEntries (): Plugin {
return {
name: 'pretix-plugin-dev-entries',
configureServer (server) {
server.middlewares.use((req, _res, next) => {
const urlPath = req.url?.split('?')[0]
if (urlPath) {
const name = urlPath.slice(1) // strip leading /
if (name in pretixPluginEntries)
req.url = `/@fs${pretixPluginEntries[name]}`
}
next()
})
},
}
}