working vite widget setup for prod (untested), local dev (with or without dev server) and pytests, with flags for running the old version or the vite version

This commit is contained in:
rash
2026-02-22 17:40:25 +01:00
parent 3f92868dba
commit b1b2a688a8
8 changed files with 139 additions and 49 deletions
@@ -1,9 +1,8 @@
// Internationalization strings for the pretix widget
// Django's i18n file expects `this` to be the global object, but ES modules
// have `this` as undefined. Import as raw text and execute with a local context.
// TODO hack
import djangoI18nScript from '../../../jsi18n/en/djangojs.js?raw'
// In production, widget.py injects the `django` global before this script loads.
// In dev mode, Django's i18n file expects `this` to be the global object, but
// ES modules have `this` as undefined — so we import as raw text and execute
// with a local context.
interface Django {
pgettext: (context: string, text: string) => string
@@ -12,10 +11,17 @@ interface Django {
get_format: (formatType: string) => string | number
}
// Create a local context object to capture django without polluting window
const context: { django?: Django } = {}
new Function(djangoI18nScript).call(context)
const django = context.django!
let django: Django
if (import.meta.env.DEV) {
// TODO this does not actually grab the correct language strings
const raw = (await import(`../../../jsi18n/${LANG}/djangojs.js?raw`)).default
const context: { django?: Django } = {}
new Function(raw).call(context)
django = context.django!
} else {
django = (globalThis as any).django
}
export const STRINGS = {
quantity: django.pgettext('widget', 'Quantity'),
@@ -1,7 +1,10 @@
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
export default defineConfig(({ mode }) => ({
server: {
port: 5180
},
plugins: [
vue()
],
@@ -11,8 +14,8 @@ export default defineConfig({
},
},
build: {
manifest: true,
outDir: import.meta.dirname + '/../../../../../static.dist/vite/widget',
minify: false, // django will do minification
outDir: import.meta.dirname + '/../../../static.dist/vite/widget',
rollupOptions: {
input: {
main: import.meta.dirname + '/src/main.ts',
@@ -28,6 +31,8 @@ export default defineConfig({
exclude: ['moment', 'jquery']
},
define: {
LANG: JSON.stringify(process.env.PRETIX_WIDGET_LANG || 'en')
}
})
...(mode === 'development' && {
LANG: JSON.stringify(process.env.PRETIX_WIDGET_LANG || 'en'),
}),
},
}))