-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathvite.config.ts
More file actions
200 lines (189 loc) · 7.18 KB
/
vite.config.ts
File metadata and controls
200 lines (189 loc) · 7.18 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { createRequire } from "node:module";
import { dirname, join } from "node:path";
import tailwindcss from "@tailwindcss/vite";
import { devtools } from "@tanstack/devtools-vite";
import { tanstackStart } from "@tanstack/react-start/plugin/vite";
import viteReact from "@vitejs/plugin-react";
import { nitro } from "nitro/vite";
import { defineConfig, type Plugin } from "vite";
import viteTsConfigPaths from "vite-tsconfig-paths";
const require = createRequire(import.meta.url);
const convexEntry = require.resolve("convex");
const convexRoot = dirname(dirname(dirname(convexEntry)));
const convexReactPath = join(convexRoot, "dist/esm/react/index.js");
const convexBrowserPath = join(convexRoot, "dist/esm/browser/index.js");
const convexValuesPath = join(convexRoot, "dist/esm/values/index.js");
const convexAuthReactPath = require.resolve("@convex-dev/auth/react");
function handleRollupWarning(
warning: { code?: string; message: string; id?: string },
warn: (warning: { code?: string; message: string; id?: string }) => void,
) {
if (
warning.code === "MODULE_LEVEL_DIRECTIVE" &&
warning.id?.includes("node_modules") &&
/use client/i.test(warning.message)
) {
return;
}
if (
warning.code === "UNUSED_EXTERNAL_IMPORT" &&
/@tanstack\/start-|@tanstack\/router-core\/ssr\/(client|server)/.test(warning.message)
) {
return;
}
if (warning.code === "EMPTY_BUNDLE" || /Generated an empty chunk/i.test(warning.message)) {
return;
}
warn(warning);
}
type SourceReplacement = readonly [from: string, to: string];
const reflectHas = (target: string, key: string) => `Reflect.has(${target}, ${JSON.stringify(key)})`;
const arkSafariInOperatorFixes = [
{
suffix: "/node_modules/.vite/deps/arktype.js",
replacements: [
['"expression" in value', reflectHas("value", "expression")],
['"toJSON" in o', reflectHas("o", "toJSON")],
['"morphs" in schema', reflectHas("schema", "morphs")],
['"branches" in schema', reflectHas("schema", "branches")],
['"unit" in schema', reflectHas("schema", "unit")],
['"reference" in schema', reflectHas("schema", "reference")],
['"proto" in schema', reflectHas("schema", "proto")],
['"domain" in schema', reflectHas("schema", "domain")],
['"value" in transformedInner', reflectHas("transformedInner", "value")],
['"default" in this.inner', reflectHas("this.inner", "default")],
['"variadic" in schema', reflectHas("schema", "variadic")],
['"prefix" in schema', reflectHas("schema", "prefix")],
['"defaultables" in schema', reflectHas("schema", "defaultables")],
['"optionals" in schema', reflectHas("schema", "optionals")],
['"postfix" in schema', reflectHas("schema", "postfix")],
['"minVariadicLength" in schema', reflectHas("schema", "minVariadicLength")],
['"description" in ctx', reflectHas("ctx", "description")],
['"data" in input', reflectHas("input", "data")],
['"get" in desc', reflectHas("desc", "get")],
['"set" in desc', reflectHas("desc", "set")],
] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/util/out/serialize.js",
replacements: [
['"expression" in value', reflectHas("value", "expression")],
['"toJSON" in o', reflectHas("o", "toJSON")],
],
},
{
suffix: "/node_modules/@ark/schema/out/parse.js",
replacements: [
['"morphs" in schema', reflectHas("schema", "morphs")],
['"branches" in schema', reflectHas("schema", "branches")],
['"unit" in schema', reflectHas("schema", "unit")],
['"reference" in schema', reflectHas("schema", "reference")],
['"proto" in schema', reflectHas("schema", "proto")],
['"domain" in schema', reflectHas("schema", "domain")],
] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/schema/out/node.js",
replacements: [['"value" in transformedInner', reflectHas("transformedInner", "value")]] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/schema/out/scope.js",
replacements: [['"branches" in schema', reflectHas("schema", "branches")]] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/schema/out/structure/optional.js",
replacements: [['"default" in this.inner', reflectHas("this.inner", "default")]] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/schema/out/structure/sequence.js",
replacements: [
['"variadic" in schema', reflectHas("schema", "variadic")],
['"prefix" in schema', reflectHas("schema", "prefix")],
['"defaultables" in schema', reflectHas("schema", "defaultables")],
['"optionals" in schema', reflectHas("schema", "optionals")],
['"postfix" in schema', reflectHas("schema", "postfix")],
['"minVariadicLength" in schema', reflectHas("schema", "minVariadicLength")],
] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/schema/out/structure/prop.js",
replacements: [['"default" in this.inner', reflectHas("this.inner", "default")]] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/schema/out/shared/implement.js",
replacements: [['"description" in ctx', reflectHas("ctx", "description")]] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/schema/out/shared/errors.js",
replacements: [['"data" in input', reflectHas("input", "data")]] satisfies SourceReplacement[],
},
{
suffix: "/node_modules/@ark/util/out/clone.js",
replacements: [
['"get" in desc', reflectHas("desc", "get")],
['"set" in desc', reflectHas("desc", "set")],
] satisfies SourceReplacement[],
},
] as const;
function patchArkSafariInOperator(): Plugin {
return {
name: "patch-ark-safari-in-operator",
enforce: "pre",
transform(code, id) {
const normalizedId = id.split("?")[0].replace(/\\/g, "/");
const fix = arkSafariInOperatorFixes.find((entry) => normalizedId.endsWith(entry.suffix));
if (!fix) return null;
let nextCode = code;
for (const [from, to] of fix.replacements) {
if (!nextCode.includes(from)) {
this.error(`Expected to patch ${from} in ${normalizedId}`);
}
nextCode = nextCode.replaceAll(from, to);
}
return {
code: nextCode,
map: null,
};
},
};
}
const config = defineConfig({
resolve: {
dedupe: ["convex", "@convex-dev/auth", "react", "react-dom"],
alias: {
"convex/react": convexReactPath,
"convex/browser": convexBrowserPath,
"convex/values": convexValuesPath,
"@convex-dev/auth/react": convexAuthReactPath,
},
},
optimizeDeps: {
include: ["convex/react", "convex/browser"],
},
plugins: [
patchArkSafariInOperator(),
devtools(),
nitro({
serverDir: "server",
rollupConfig: {
onwarn: handleRollupWarning,
},
}),
// this is the plugin that enables path aliases
viteTsConfigPaths({
projects: ["./tsconfig.json"],
}),
tailwindcss(),
tanstackStart(),
viteReact(),
],
build: {
// Keep the shipped client bundle parseable in Safari/WebKit.
target: "safari15",
chunkSizeWarningLimit: 900,
rollupOptions: {
onwarn: handleRollupWarning,
},
},
});
export default config;