Advertisement
Guest User

vite-config

a guest
Jul 30th, 2021
5,197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { defineConfig, loadEnv } from 'vite';
  2. import reactRefresh from '@vitejs/plugin-react-refresh';
  3. import { getAliases } from 'vite-aliases';
  4.  
  5. const aliases = getAliases({
  6.   path: 'src',
  7.   prefix: '@',
  8. });
  9.  
  10. export default ({ mode }) => {
  11.   process.env = { ...process.env, ...loadEnv(mode, process.cwd()) }; // <-
  12.  
  13.   // import.meta.env.VITE_NAME available here with: process.env.VITE_NAME
  14.   // import.meta.env.VITE_PORT available here with: process.env.VITE_PORT
  15.  
  16.   const isProd = mode === 'production';
  17.   let plugins = []
  18.  
  19.   const env = loadEnv(mode, process.cwd()); // <-
  20.   const envCut = Object.keys(env).map((v) => {
  21.     const keys =
  22.       'VITE_STRIPE_KEY, VITE_RECAPTCHA_KEY, VITE_MAPS_KEY, VITE_GMAPS_KEY, VITE_FACEBOOK_PIXEL_ID, VITE_GOOGLE_TAG_MANAGER_ID, VITE_TITLE, VITE_ICON_PATH';
  23.     const val = env[v];
  24.     let nval = val;
  25.     if (keys.split(', ').includes(v)) {
  26.       nval = val.slice(0, 3) + '****' + val.slice(val.length - 3, val.length);
  27.     }
  28.     return {
  29.       key: v,
  30.       value: nval,
  31.     };
  32.   });
  33.  
  34.   const highestKeyLength = envCut
  35.     .map((v) => v.key.length)
  36.     .sort((a, b) => b - a)[0];
  37.  
  38.   const highestValueLength = envCut
  39.     .map((v) => v.value.length)
  40.     .sort((a, b) => b - a)[0];
  41.  
  42.   const separator = '█';
  43.   const separatorTop = '▄';
  44.   const separatorBottom = '▀';
  45.   console.log('\nEnvironment: ' + mode);
  46.   console.log(separatorTop.repeat(highestKeyLength + highestValueLength + 7));
  47.   console.log(
  48.     separator +
  49.       ' '.repeat(highestKeyLength + 2) +
  50.       separator +
  51.       ' '.repeat(highestValueLength + 2) +
  52.       separator
  53.   );
  54.  
  55.   envCut.forEach((v) =>
  56.     console.log(
  57.       `${separator} ${v.key}` +
  58.         ' '.repeat(highestKeyLength - v.key.length) +
  59.         ` ${separator} ${v.value}` +
  60.         ' '.repeat(highestValueLength - v.value.length) +
  61.         ` ${separator}`
  62.     )
  63.   );
  64.   console.log(
  65.     separator +
  66.       ' '.repeat(highestKeyLength + 2) +
  67.       separator +
  68.       ' '.repeat(highestValueLength + 2) +
  69.       separator
  70.   );
  71.   console.log(
  72.     separatorBottom.repeat(highestKeyLength + highestValueLength + 7)
  73.   );
  74.  
  75.   const viteEnv = {};
  76.   if (isProd) {
  77.     Object.keys(process.env).forEach((key) => {
  78.       if (key.startsWith(`VITE_`)) {
  79.         viteEnv[`import.meta.env.${key}`] = process.env[key];  // <-
  80.       }
  81.     });
  82.   }
  83.  
  84.   if(mode === 'development') plugins.push(reactRefresh())
  85.  
  86.  
  87.   return defineConfig({
  88.     plugins,
  89.     publicDir: './public',
  90.     resolve: {
  91.       alias: aliases,
  92.     },
  93.     ...(isProd && { define: viteEnv }),
  94.     server: {
  95.       port: 3001,
  96.       host: true,
  97.     },
  98.     build: {
  99.       chunkSizeWarningLimit: 2500,
  100.     },
  101.     optimizeDeps: {
  102.       include: ["@material-ui/utils", "@react-icons/all-files"]
  103.     },
  104.   });
  105. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement