-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
vite.config.js
227 lines (216 loc) · 6.78 KB
/
vite.config.js
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* eslint-disable no-console */
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import { sentryVitePlugin } from "@sentry/vite-plugin";
import react from "@vitejs/plugin-react-swc";
import { CodeInspectorPlugin } from "code-inspector-plugin";
import { copyFileSync, mkdirSync } from "fs";
import path from "path";
import nodePolyfills from "rollup-plugin-polyfill-node";
import { defineConfig, loadEnv, searchForWorkspaceRoot } from "vite";
import { createHtmlPlugin } from "vite-plugin-html";
const copyNoopSW = () => ({
name: "copy-noop-sw",
apply: "build",
writeBundle: () => {
mkdirSync(path.resolve("build", "dashboard"), { recursive: true });
copyFileSync(path.resolve("assets", "sw.js"), path.resolve("build", "dashboard", "sw.js"));
},
});
const copyOgImage = () => ({
name: "copy-og-image",
apply: "build",
writeBundle: () => {
mkdirSync(path.resolve("build", "dashboard"), { recursive: true });
copyFileSync(path.resolve("assets", "og.png"), path.resolve("build", "dashboard", "og.png"));
},
});
export default defineConfig(({ command, mode }) => {
const isDev = command !== "build";
const env = loadEnv(mode, process.cwd(), "");
/*
Using explicit env variables, there is no need to expose all of them (security).
*/
const {
NODE_ENV,
API_URL,
SW_INTERVAL,
IS_CLOUD_INSTANCE,
APP_MOUNT_URI,
SENTRY_DSN,
SENTRY_RELEASE,
ENVIRONMENT,
STATIC_URL,
APPS_MARKETPLACE_API_URL,
APPS_TUNNEL_URL_KEYWORDS,
SKIP_SOURCEMAPS,
DEMO_MODE,
CUSTOM_VERSION,
FLAGS_SERVICE_ENABLED,
LOCALE_CODE,
POSTHOG_KEY,
POSTHOG_HOST,
SENTRY_AUTH_TOKEN,
SENTRY_ORG,
SENTRY_PROJECT,
ONBOARDING_USER_JOINED_DATE_THRESHOLD,
// eslint-disable-next-line camelcase
npm_package_version,
} = env;
const base = STATIC_URL ?? "/";
const featureFlagsEnvs = Object.fromEntries(
Object.entries(env).filter(([flagKey]) => flagKey.startsWith("FF_")),
);
const sourcemap = !SKIP_SOURCEMAPS;
const plugins = [
react(),
CodeInspectorPlugin({
bundler: "vite",
}),
createHtmlPlugin({
entry: path.resolve(__dirname, "src", "index.tsx"),
template: "index.html",
inject: {
data: {
API_URL,
APP_MOUNT_URI,
APPS_MARKETPLACE_API_URL,
APPS_TUNNEL_URL_KEYWORDS,
IS_CLOUD_INSTANCE,
LOCALE_CODE,
POSTHOG_KEY,
POSTHOG_HOST,
ONBOARDING_USER_JOINED_DATE_THRESHOLD,
injectOgTags:
DEMO_MODE &&
`
<meta property="og:type" content="website">
<meta property="og:title" content="Sign in to the Saleor Dashboard">
<meta property="og:description" content="Sign in to the Saleor Dashboard to manage your orders, payments, products and more.">
<meta property="og:image" content="${base}og.png">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:title" content="Sign in to the Saleor Dashboard">
<meta name="twitter:description" content="Sign in to the Saleor Dashboard to manage your orders, payments, products and more.">
<meta name="twitter:image" content="${base}og.png">
<meta property="og:url" content="https://demo.saleor.io/dashboard/">
<meta property="twitter:domain" content="demo.saleor.io">
<meta property="twitter:url" content="https://demo.saleor.io/dashboard/">
`,
},
},
}),
copyOgImage(),
copyNoopSW(),
];
if (!isDev) {
console.log("Enabling service worker...");
plugins.push(
sentryVitePlugin({
authToken: SENTRY_AUTH_TOKEN,
org: SENTRY_ORG,
project: SENTRY_PROJECT,
}),
);
}
const globals = {
/*
"qs" package uses 'get-intrinsic' whish refers to the global object, we need to recreate it.
Issue presents only on development mode.
*/
...(isDev ? { global: {} } : {}),
FLAGS_SERVICE_ENABLED: FLAGS_SERVICE_ENABLED === "true",
// Keep all feature flags from env in global variable
FLAGS: JSON.stringify(featureFlagsEnvs),
};
return {
root: "src",
base,
envDir: "..",
server: {
port: 9000,
fs: {
allow: [searchForWorkspaceRoot(process.cwd()), "../.."],
},
},
define: {
...globals,
/*
We still have references to process.env, we need to peserve them as workaround.
*/
"process.env": {
NODE_ENV,
API_URL,
SW_INTERVAL,
IS_CLOUD_INSTANCE,
APP_MOUNT_URI,
SENTRY_DSN,
ENVIRONMENT,
DEMO_MODE,
CUSTOM_VERSION,
LOCALE_CODE,
SENTRY_RELEASE,
STATIC_URL,
POSTHOG_KEY,
POSTHOG_HOST,
ONBOARDING_USER_JOINED_DATE_THRESHOLD,
// eslint-disable-next-line camelcase
RELEASE_NAME: npm_package_version,
},
},
build: {
sourcemap,
minify: false,
emptyOutDir: true,
outDir: "../build/dashboard",
assetsDir: ".",
commonjsOptions: {
/*
Fix dynamic imports by "require", Necessary for react-editor-js
Ref: https://github.com/Jungwoo-An/react-editor-js/blob/e58b7ba5e66d07912bb78f65ac911e4018d363e1/packages/react-editor-js/src/factory.ts#L5
*/
transformMixedEsModules: true,
},
rollupOptions: {
plugins: [nodePolyfills()],
maxParallelFileOps: 2,
cache: false,
output: {
sourcemap,
manualChunks: id => {
if (id.includes("node_modules")) {
return "vendor";
}
},
},
},
},
optimizeDeps: {
include: ["esm-dep > cjs-dep", "@saleor/macaw-ui"],
esbuildOptions: {
plugins: [
/*
react-markdown and its dependency tried to call process.cwd().
Since it's not present in the browser, we need to polyfill that.
*/
NodeGlobalsPolyfillPlugin({ process: true }),
],
},
},
resolve: {
dedupe: ["react", "react-dom", "clsx", "@material-ui/styles"],
alias: {
"@assets": path.resolve(__dirname, "./assets"),
"@locale": path.resolve(__dirname, "./locale"),
"@dashboard": path.resolve(__dirname, "./src"),
src: path.resolve(__dirname, "./src"),
/*
Moment.js/react-moment does not fully suport ES modules.
Vite resolves it by using jsnext:main https://github.com/moment/moment/blob/develop/package.json#L26.
We enforce to use a different path, ignoring jsnext:main field.
*/
moment: path.resolve(__dirname, "./node_modules/moment/min/moment-with-locales.js"),
},
},
plugins,
esbuild: { jsx: "automatic" },
};
});