-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
80 lines (78 loc) · 2.15 KB
/
vite.config.ts
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
import react from "@vitejs/plugin-react";
import inject from "@rollup/plugin-inject";
import { defineConfig } from "vite";
/**
* @see https://vitejs.dev/config/
*/
export default defineConfig(({ command }) => {
const define =
command === "serve"
? { global: "globalThis" }
: {
global: (() => {
let globalVariable = "globalThis";
try {
// Try to import @safe-global/safe-apps-provider
require.resolve("@safe-global/safe-apps-provider");
// Try to import @safe-global/safe-apps-sdk
require.resolve("@safe-global/safe-apps-sdk");
// If both modules are found, return the custom global variable
globalVariable = "global";
} catch (e) {
// If either module is not found, fallback to globalThis
globalVariable = "globalThis";
}
return globalVariable;
})(),
};
return {
/**
* Defines global constant replacments
* @see https://vitejs.dev/config/shared-options.html#define
*/
define,
build: {
rollupOptions: {
external: ["@safe-globalThis/safe-apps-provider"],
},
},
resolve: {
/**
* Polyfills nodejs imports
* @see https://vitejs.dev/config/shared-options.html#resolve-alias
*/
alias: {
process: "process/browser",
util: "util",
},
},
/**
* Enables react
* @see https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md
*/
plugins: [
[
react({
jsxRuntime: "classic",
}),
{
...inject({
global: [
require.resolve("node-stdlib-browser/helpers/esbuild/shim"),
"global",
],
process: [
require.resolve("node-stdlib-browser/helpers/esbuild/shim"),
"process",
],
Buffer: [
require.resolve("node-stdlib-browser/helpers/esbuild/shim"),
"Buffer",
],
}),
enforce: "post",
},
],
],
};
});