Skip to content

Commit c84c57c

Browse files
committed
Gross hack to prevent Prisma runtime getting bundled
1 parent 9015171 commit c84c57c

File tree

5 files changed

+59
-20
lines changed

5 files changed

+59
-20
lines changed

desktop/.env

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
DESKTOP_SENTRY_DSN=
1+
DESKTOP_SENTRY_DSN=https://[email protected]/789x`
22
VITE_DESKTOP_SENTRY_DSN=$DESKTOP_SENTRY_DSN
33
ENVIRONMENT=localhost
44
VITE_ENVIRONMENT=$ENVIRONMENT

desktop/.eslintrc.cjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ module.exports = {
4848
message:
4949
"Use main/safeStorage.ts instead to avoid E2E test failures.",
5050
},
51+
{
52+
group: [`@badger/prisma/client`],
53+
message: "Use @badger/prisma/types instead"
54+
}
5155
],
5256
},
5357
],

desktop/electron.vite.config.mjs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,39 @@ const prod = process.env.ENVIRONMENT === "prod";
1717

1818
const 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+
2053
const 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
*/
78109
const 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

120155
export default config;

desktop/src/main/obs/obsHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
SceneItem,
66
} from "./obs";
77
import invariant from "../../common/invariant";
8-
import type { Media, ContinuityItem } from "@badger/prisma/client";
8+
import type { Media, ContinuityItem } from "@badger/prisma/types";
99
import { getLogger } from "../base/logging";
1010
import { selectedShow } from "../base/selectedShow";
1111
import { getLocalMedia } from "../media/mediaManagement";

desktop/src/main/vmix/vmixHelpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import invariant from "../../common/invariant";
22
import { getLocalMedia } from "../media/mediaManagement";
33
import { getVMixConnection } from "./vmix";
44
import { InputType, ListInput, ListItem } from "./vmixTypes";
5-
import type { Asset, Media } from "@badger/prisma/client";
5+
import type { Asset, Media } from "@badger/prisma/types";
66

77
export async function reconcileList(listName: string, elements: string[]) {
88
const conn = getVMixConnection();

0 commit comments

Comments
 (0)