@@ -17,6 +17,39 @@ const prod = process.env.ENVIRONMENT === "prod";
1717
1818const visualizeBundle = process . argv . includes ( "--visualize-bundle" ) ;
1919
20+ /*
21+ * Explanation of this gross hack:
22+ * We cannot allow the Prisma runtime to get bundled into the desktop build, else it will fail
23+ * to start. We do, however, need the TypeScript types of the Prisma models. This is normally
24+ * not a problem because of zod-prisma-types, which generates @badger/prisma/types, which we
25+ * can import (and forbid importing @badger/prisma/client).
26+ * However, zod-prisma-types still needs to import the actual Prisma client in one place,
27+ * transformJsonNull.ts, so that it can access Prisma.JsonNull/Prisma.DbNull.
28+ *
29+ * To fix this, we stub out this one import, which thereby ensures the Prisma client runtime
30+ * never gets bundled in. This is safe to do, because we will never need to interact with
31+ * Prisma.{Db,Json}Null in Desktop.
32+ */
33+ const jsonNullStub = "export const transformJsonNull = v => v; export default transformJsonNull;"
34+ const jsonNullStubPlaceholder = "\0ignore_prisma_placeholder"
35+ /** @type {import("vite").Plugin } */
36+ const IgnorePrismaJsonNullPlugin = {
37+ name : "ignorePrismaJsonNull" ,
38+ resolveId ( importee ) {
39+ if ( importee . includes ( "transformJsonNull" ) ) {
40+ return jsonNullStubPlaceholder ;
41+ }
42+ return null ;
43+ } ,
44+ load ( name ) {
45+ return name === jsonNullStubPlaceholder ? {
46+ code : jsonNullStub ,
47+ moduleSideEffects : false ,
48+ } : null ;
49+ } ,
50+ enforce : "pre"
51+ } ;
52+
2053const base = defineConfig ( {
2154 define : {
2255 "global.__APP_VERSION__" : JSON . stringify ( packageJSON . version ) ,
@@ -26,8 +59,6 @@ const base = defineConfig({
2659 "global.__ENVIRONMENT__" : JSON . stringify ( process . env . ENVIRONMENT ) ,
2760 } ,
2861 plugins : [
29- // Fix Prisma runtime trying to get bundled
30- ignore ( [ "../../client" ] ) ,
3162 sentryVitePlugin ( {
3263 org : "ystv" ,
3364 project : "badger-desktop" ,
@@ -76,45 +107,49 @@ const base = defineConfig({
76107 * @type {import('electron-vite').UserConfig }
77108 */
78109const config = {
79- main : mergeConfig ( base , {
110+ main : mergeConfig ( base , defineConfig ( {
80111 plugins : [
112+ IgnorePrismaJsonNullPlugin ,
81113 commonjs ( ) ,
82114 visualizeBundle &&
83- visualizer ( {
84- filename : "bundle-main.html" ,
85- } ) ,
115+ visualizer ( {
116+ filename : "bundle-main.html" ,
117+ } ) ,
86118 ] . filter ( Boolean ) ,
87119 resolve : {
88120 conditions : [ "node" ] ,
89121 browserField : false ,
90122 } ,
91- } ) ,
92- renderer : mergeConfig ( base , {
123+ build : {
124+ sourcemap : true ,
125+ }
126+ } ) ) ,
127+ renderer : mergeConfig ( base , defineConfig ( {
93128 plugins : [
94129 visualizeBundle &&
95- visualizer ( {
96- filename : "bundle-renderer.html" ,
97- } ) ,
130+ visualizer ( {
131+ filename : "bundle-renderer.html" ,
132+ } ) ,
98133 ] . filter ( Boolean ) ,
99134 build : {
100135 rollupOptions : {
101136 input : "./src/renderer/index.html" ,
102137 } ,
103138 } ,
104- } ) ,
105- preload : mergeConfig ( base , {
139+ } ) ) ,
140+ preload : mergeConfig ( base , defineConfig ( {
106141 plugins : [
107142 visualizeBundle &&
108- visualizer ( {
109- filename : "bundle-preload.html" ,
110- } ) ,
143+ visualizer ( {
144+ filename : "bundle-preload.html" ,
145+ } ) ,
111146 ] . filter ( Boolean ) ,
112147 build : {
113148 lib : {
114149 entry : "./src/common/preload.ts" ,
115150 } ,
116151 } ,
117- } ) ,
152+ } ) ) ,
118153} ;
119154
120155export default config ;
0 commit comments