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";
|
2023-03-17 09:44:00 +00:00
|
|
|
import react from "@vitejs/plugin-react-swc";
|
2023-05-12 11:06:27 +00:00
|
|
|
import { copyFileSync, mkdirSync } from "fs";
|
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";
|
2023-04-25 06:33:09 +00:00
|
|
|
import { defineConfig, loadEnv, searchForWorkspaceRoot } from "vite";
|
2022-11-28 13:56:46 +00:00
|
|
|
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";
|
|
|
|
|
2023-03-08 09:13:00 +00:00
|
|
|
const copyOgImage = () => ({
|
|
|
|
name: "copy-og-image",
|
|
|
|
apply: "build",
|
|
|
|
writeBundle: () => {
|
2023-05-12 11:06:27 +00:00
|
|
|
mkdirSync(path.resolve("build", "dashboard"), { recursive: true });
|
|
|
|
copyFileSync(
|
|
|
|
path.resolve("assets", "og.png"),
|
|
|
|
path.resolve("build", "dashboard", "og.png"),
|
|
|
|
);
|
2023-03-08 09:13:00 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-11-28 13:56:46 +00:00
|
|
|
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,
|
|
|
|
APP_MOUNT_URI,
|
|
|
|
SENTRY_ORG,
|
|
|
|
SENTRY_PROJECT,
|
|
|
|
SENTRY_AUTH_TOKEN,
|
|
|
|
SENTRY_DSN,
|
|
|
|
ENVIRONMENT,
|
|
|
|
STATIC_URL,
|
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,
|
2023-04-26 10:05:16 +00:00
|
|
|
CUSTOM_VERSION,
|
2023-01-16 13:55:38 +00:00
|
|
|
FLAGS_SERVICE_ENABLED,
|
2022-11-28 13:56:46 +00:00
|
|
|
} = env;
|
|
|
|
|
2023-03-08 09:13:00 +00:00
|
|
|
const base = STATIC_URL ?? "/";
|
2023-01-16 13:55:38 +00:00
|
|
|
const featureFlagsEnvs = Object.fromEntries(
|
|
|
|
Object.entries(env).filter(([flagKey]) => flagKey.startsWith("FF_")),
|
|
|
|
);
|
|
|
|
|
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 = [
|
2023-03-17 09:44:00 +00:00
|
|
|
react(),
|
2022-11-28 13:56:46 +00:00
|
|
|
createHtmlPlugin({
|
2023-05-12 11:06:27 +00:00
|
|
|
entry: path.resolve(__dirname, "src", "index.tsx"),
|
2022-11-28 13:56:46 +00:00
|
|
|
template: "index.html",
|
|
|
|
inject: {
|
|
|
|
data: {
|
|
|
|
API_URL: API_URI,
|
|
|
|
APP_MOUNT_URI,
|
2023-01-10 10:04:30 +00:00
|
|
|
APPS_MARKETPLACE_API_URI,
|
|
|
|
APPS_TUNNEL_URL_KEYWORDS,
|
2023-03-03 07:51:35 +00:00
|
|
|
IS_CLOUD_INSTANCE,
|
2023-03-08 09:13:00 +00:00
|
|
|
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/">
|
|
|
|
`,
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
2023-03-08 09:13:00 +00:00
|
|
|
copyOgImage(),
|
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.
|
|
|
|
*/
|
2023-05-12 11:06:27 +00:00
|
|
|
srcDir: path.resolve(__dirname),
|
2022-12-01 10:55:46 +00:00
|
|
|
filename: "sw.js",
|
|
|
|
}),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-16 13:55:38 +00:00
|
|
|
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),
|
|
|
|
};
|
2022-11-28 13:56:46 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
root: "src",
|
2023-03-08 09:13:00 +00:00
|
|
|
base,
|
2022-11-28 13:56:46 +00:00
|
|
|
envDir: "..",
|
|
|
|
server: {
|
|
|
|
port: 9000,
|
2023-04-25 06:33:09 +00:00
|
|
|
fs: {
|
|
|
|
allow: [searchForWorkspaceRoot(process.cwd()), "../.."],
|
|
|
|
},
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
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,
|
|
|
|
APP_MOUNT_URI,
|
|
|
|
SENTRY_DSN,
|
|
|
|
ENVIRONMENT,
|
2022-12-08 15:34:46 +00:00
|
|
|
DEMO_MODE,
|
2023-04-26 10:05:16 +00:00
|
|
|
CUSTOM_VERSION,
|
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()],
|
2023-03-17 10:28:17 +00:00
|
|
|
maxParallelFileOps: 2,
|
|
|
|
cache: false,
|
|
|
|
output: {
|
|
|
|
sourcemap,
|
|
|
|
manualChunks: id => {
|
|
|
|
if (id.includes("node_modules")) {
|
|
|
|
return "vendor";
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
2022-12-09 14:12:16 +00:00
|
|
|
},
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
optimizeDeps: {
|
2023-04-25 06:33:09 +00:00
|
|
|
include: ["esm-dep > cjs-dep", "@saleor/macaw-ui"],
|
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: {
|
2023-04-25 06:33:09 +00:00
|
|
|
dedupe: ["react", "react-dom", "clsx", "@material-ui/styles"],
|
2022-11-28 13:56:46 +00:00
|
|
|
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.
|
|
|
|
*/
|
2023-02-13 09:40:05 +00:00
|
|
|
moment: path.resolve(
|
|
|
|
__dirname,
|
|
|
|
"./node_modules/moment/min/moment-with-locales.js",
|
|
|
|
),
|
2022-11-28 13:56:46 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
plugins,
|
|
|
|
esbuild: { jsx: "automatic" },
|
|
|
|
};
|
|
|
|
});
|