From 3b78ac8cc6521f4df00814d968e7eedb12d75722 Mon Sep 17 00:00:00 2001
From: Kasper Seweryn <github@wvffle.net>
Date: Tue, 22 Feb 2022 12:57:30 +0100
Subject: [PATCH] Workaround axios-auth-refresh in production

axios-auth-refresh has only prebuilt files, that might be the case when
vite is trying to bundle it. It seems like vite thinks that it's
constructed like:
```
export default {
    default () {
        // ...
    }
}
```

This also fixes `jQuery is not defined` in dev
---
 front/src/jquery.js  |  8 ++++++++
 front/src/main.js    | 11 +++++++++--
 front/vite.config.js |  3 ++-
 3 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100644 front/src/jquery.js

diff --git a/front/src/jquery.js b/front/src/jquery.js
new file mode 100644
index 000000000..84ad6d83c
--- /dev/null
+++ b/front/src/jquery.js
@@ -0,0 +1,8 @@
+import jQuery from 'jquery'
+
+// NOTE: Workaround for fomantic-ui-css
+if (import.meta.env.DEV) {
+  window.$ = window.jQuery = jQuery
+}
+
+export default jQuery
diff --git a/front/src/main.js b/front/src/main.js
index b62cc1343..ea98465ca 100644
--- a/front/src/main.js
+++ b/front/src/main.js
@@ -1,7 +1,7 @@
 // The Vue build version to load with the `import` command
 // (runtime-only or standalone) has been set in webpack.base.conf with an alias.
 import logger from '@/logging'
-import jQuery from 'jquery'
+import jQuery from '@/jquery'
 
 import Vue from 'vue'
 import moment from 'moment'
@@ -13,7 +13,7 @@ import store from './store'
 import GetTextPlugin from 'vue-gettext'
 import { sync } from 'vuex-router-sync'
 import locales from '@/locales'
-import createAuthRefreshInterceptor from 'axios-auth-refresh'
+import axiosAuthRefresh from 'axios-auth-refresh'
 
 import filters from '@/filters' // eslint-disable-line
 import { parseAPIErrors } from '@/utils'
@@ -158,6 +158,13 @@ const refreshAuth = (failedRequest) => {
   }
 }
 
+// TODO: This seems like a vite error, in production it thinks that
+//       axiosAuthRefresh is a following object: { default () { /* ... */} }
+//       Maybe we need to tweak the config?
+const createAuthRefreshInterceptor = import.meta.env.DEV
+  ? axiosAuthRefresh
+  : axiosAuthRefresh.default
+
 createAuthRefreshInterceptor(axios, refreshAuth)
 
 store.dispatch('instance/fetchFrontSettings').finally(() => {
diff --git a/front/vite.config.js b/front/vite.config.js
index 8202460a7..17a940d01 100644
--- a/front/vite.config.js
+++ b/front/vite.config.js
@@ -29,7 +29,8 @@ export default defineConfig({
   build: {
     // NOTE: For debugging builds
     // TODO: Remove before #1664 is merged
-    sourcemap: 'inline'
+    sourcemap: 'inline',
+    transformMixedEsModules: true
   },
   resolve: {
     alias: {
-- 
GitLab