forked from saleor/saleor-dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
187 lines (174 loc) · 4.94 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
/* eslint-disable no-console */
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
import path from "path";
import nodePolyfills from "rollup-plugin-polyfill-node";
import { defineConfig, loadEnv } from "vite";
import { createHtmlPlugin } from "vite-plugin-html";
import { VitePWA } from "vite-plugin-pwa";
import viteSentry from "vite-plugin-sentry";
import { swcReactRefresh } from "vite-plugin-swc-react-refresh";
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_URI,
SW_INTERVAL,
IS_CLOUD_INSTANCE,
MARKETPLACE_URL,
SALEOR_APPS_ENDPOINT,
APP_MOUNT_URI,
SENTRY_ORG,
SENTRY_PROJECT,
SENTRY_AUTH_TOKEN,
SENTRY_DSN,
ENVIRONMENT,
STATIC_URL,
SALEOR_APPS_PAGE_PATH,
SALEOR_APPS_JSON_PATH,
APP_TEMPLATE_GALLERY_PATH,
SKIP_SOURCEMAPS,
DEMO_MODE,
} = env;
const sourcemap = SKIP_SOURCEMAPS ? false : true;
const enableSentry =
SENTRY_ORG && SENTRY_PROJECT && SENTRY_DSN && SENTRY_AUTH_TOKEN;
const plugins = [
swcReactRefresh(),
createHtmlPlugin({
entry: "/index.tsx",
template: "index.html",
inject: {
data: {
API_URL: API_URI,
APP_MOUNT_URI,
SALEOR_APPS_PAGE_PATH,
SALEOR_APPS_JSON_PATH,
APP_TEMPLATE_GALLERY_PATH,
MARKETPLACE_URL,
},
},
}),
];
if (enableSentry) {
console.log("Enabling sentry...");
plugins.push(
viteSentry({
sourceMaps: {
include: ["./build/dashboard"],
urlPrefix: process.env.SENTRY_URL_PREFIX,
},
}),
);
}
if (!isDev) {
console.log("Enabling service worker...");
plugins.push(
VitePWA({
/*
We use 'register-service-worker' for registering sw.js.
*/
injectRegister: null,
strategies: "injectManifest",
/*
Since "src" is exposed as a root,
sw.js has to be moved above, to preventing loading in a dev mode.
*/
srcDir: "../",
filename: "sw.js",
}),
);
}
/*
"qs" package uses 'get-intrinsic' whish refers to the global object, we need to recreate it.
Issue presents only on development mode.
*/
const globals = isDev ? { global: {} } : {};
return {
root: "src",
base: STATIC_URL ?? "/",
envDir: "..",
server: {
port: 9000,
},
define: {
...globals,
/*
We still have references to process.env, we need to peserve them as workaround.
*/
"process.env": {
NODE_ENV,
API_URI,
SW_INTERVAL,
IS_CLOUD_INSTANCE,
MARKETPLACE_URL,
SALEOR_APPS_ENDPOINT,
APP_MOUNT_URI,
SENTRY_DSN,
ENVIRONMENT,
DEMO_MODE,
},
},
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()],
},
},
optimizeDeps: {
include: ["esm-dep > cjs-dep"],
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: {
alias: {
/*
Aliases from tsconfig, we need to preserve them.
Since @saleor points to "src", we need to add aliases for macaw and sdk as first.
*/
"@saleor/macaw-ui": path.resolve(
__dirname,
"node_modules/@saleor/macaw-ui",
),
"@saleor/app-sdk": path.resolve(
__dirname,
"node_modules/@saleor/app-sdk",
),
"@saleor/sdk": path.resolve(__dirname, "node_modules/@saleor/sdk"),
"@saleor": path.resolve(__dirname, "./src"),
"@assets": path.resolve(__dirname, "./assets"),
"@locale": path.resolve(__dirname, "./locale"),
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/moment.js"),
},
},
plugins,
esbuild: { jsx: "automatic" },
};
});