2022-11-28 13:56:46 +00:00
|
|
|
/* eslint-disable no-console */
|
2022-12-09 14:12:16 +00:00
|
|
|
import { NodeGlobalsPolyfillPlugin } from "@esbuild-plugins/node-globals-polyfill";
|
2022-11-28 13:56:46 +00:00
|
|
|
import path from "path";
|
2022-12-09 14:12:16 +00:00
|
|
|
import nodePolyfills from "rollup-plugin-polyfill-node";
|
2022-11-28 13:56:46 +00:00
|
|
|
import { defineConfig, loadEnv } from "vite";
|
|
|
|
import { createHtmlPlugin } from "vite-plugin-html";
|
2022-12-01 10:55:46 +00:00
|
|
|
import { VitePWA } from "vite-plugin-pwa";
|
2022-11-28 13:56:46 +00:00
|
|
|
import viteSentry from "vite-plugin-sentry";
|
|
|
|
import { swcReactRefresh } from "vite-plugin-swc-react-refresh";
|
|
|
|
|
|
|
|
export default defineConfig(({ command, mode }) => {
|
2022-12-01 10:55:46 +00:00
|
|
|
const isDev = command !== "build";
|
2022-11-28 13:56:46 +00:00
|
|
|
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,
|
2022-11-30 16:20:20 +00:00
|
|
|
SALEOR_APPS_PAGE_PATH,
|
|
|
|
SALEOR_APPS_JSON_PATH,
|
|
|
|
APP_TEMPLATE_GALLERY_PATH,
|
2023-01-10 10:04:30 +00:00
|
|
|
APPS_MARKETPLACE_API_URI,
|
|
|
|
APPS_TUNNEL_URL_KEYWORDS,
|
2022-12-07 08:25:44 +00:00
|
|
|
SKIP_SOURCEMAPS,
|
2022-12-08 15:34:46 +00:00
|
|
|
DEMO_MODE,
|
2022-11-28 13:56:46 +00:00
|
|
|
} = env;
|
|
|
|
|
2022-12-07 08:25:44 +00:00
|
|
|
const sourcemap = SKIP_SOURCEMAPS ? false : true;
|
|
|
|
|
2022-11-28 13:56:46 +00:00
|
|
|
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,
|
2022-11-30 16:20:20 +00:00
|
|
|
SALEOR_APPS_PAGE_PATH,
|
|
|
|
SALEOR_APPS_JSON_PATH,
|
|
|
|
APP_TEMPLATE_GALLERY_PATH,
|
|
|
|
MARKETPLACE_URL,
|
2023-01-10 10:04:30 +00:00
|
|
|
APPS_MARKETPLACE_API_URI,
|
|
|
|
APPS_TUNNEL_URL_KEYWORDS,
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
|
|
|
|
if (enableSentry) {
|
|
|
|
console.log("Enabling sentry...");
|
|
|
|
|
|
|
|
plugins.push(
|
|
|
|
viteSentry({
|
|
|
|
sourceMaps: {
|
2022-12-01 12:16:03 +00:00
|
|
|
include: ["./build/dashboard"],
|
2022-11-28 13:56:46 +00:00
|
|
|
urlPrefix: process.env.SENTRY_URL_PREFIX,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-12-01 10:55:46 +00:00
|
|
|
if (!isDev) {
|
|
|
|
console.log("Enabling service worker...");
|
|
|
|
|
|
|
|
plugins.push(
|
|
|
|
VitePWA({
|
2022-12-06 10:12:19 +00:00
|
|
|
/*
|
|
|
|
We use 'register-service-worker' for registering sw.js.
|
|
|
|
*/
|
|
|
|
injectRegister: null,
|
2022-12-01 10:55:46 +00:00
|
|
|
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",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-11-28 13:56:46 +00:00
|
|
|
/*
|
|
|
|
"qs" package uses 'get-intrinsic' whish refers to the global object, we need to recreate it.
|
|
|
|
Issue presents only on development mode.
|
|
|
|
*/
|
2022-12-01 10:55:46 +00:00
|
|
|
const globals = isDev ? { global: {} } : {};
|
2022-11-28 13:56:46 +00:00
|
|
|
|
|
|
|
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,
|
2022-12-08 15:34:46 +00:00
|
|
|
DEMO_MODE,
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
build: {
|
2022-12-07 08:25:44 +00:00
|
|
|
sourcemap,
|
2022-11-28 13:56:46 +00:00
|
|
|
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,
|
|
|
|
},
|
2022-12-09 14:12:16 +00:00
|
|
|
rollupOptions: {
|
|
|
|
plugins: [nodePolyfills()],
|
|
|
|
},
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
optimizeDeps: {
|
|
|
|
include: ["esm-dep > cjs-dep"],
|
2022-12-09 14:12:16 +00:00
|
|
|
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 }),
|
|
|
|
],
|
|
|
|
},
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
resolve: {
|
|
|
|
alias: {
|
|
|
|
"@assets": path.resolve(__dirname, "./assets"),
|
|
|
|
"@locale": path.resolve(__dirname, "./locale"),
|
2023-01-16 09:45:12 +00:00
|
|
|
"@dashboard": path.resolve(__dirname, "./src"),
|
2022-11-28 13:56:46 +00:00
|
|
|
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" },
|
|
|
|
};
|
|
|
|
});
|