From 8d94392cfba95e8ec86ff3ca7e7a77bf3b6fd6d6 Mon Sep 17 00:00:00 2001 From: kermieisinthehouse Date: Thu, 18 Nov 2021 00:38:19 -0800 Subject: [PATCH] Fix Vite issues (#2038) * Fix environment vars in Vite * Add types, remove process.env override * Temporarily Remove Statigz / gzip * Update ui/v2.5/src/core/createClient.ts Co-authored-by: peolic <66393006+peolic@users.noreply.github.com> * Update ui/v2.5/src/serviceWorker.ts Co-authored-by: peolic <66393006+peolic@users.noreply.github.com> * Ignore case rules in types * Add windows js workaround Co-authored-by: WithoutPants <53250216+WithoutPants@users.noreply.github.com> --- pkg/api/server.go | 11 +++++++++-- ui/v2.5/src/components/Changelog/Changelog.tsx | 4 ++-- .../components/Settings/SettingsAboutPanel.tsx | 6 +++--- ui/v2.5/src/core/createClient.ts | 6 +++--- ui/v2.5/src/globals.d.ts | 14 ++++++++++++++ ui/v2.5/src/serviceWorker.ts | 4 ++-- ui/v2.5/vite.config.js | 18 ++++++++---------- 7 files changed, 41 insertions(+), 22 deletions(-) diff --git a/pkg/api/server.go b/pkg/api/server.go index 7f348d618..f58049cb1 100644 --- a/pkg/api/server.go +++ b/pkg/api/server.go @@ -30,7 +30,6 @@ import ( "github.com/stashapp/stash/pkg/manager/config" "github.com/stashapp/stash/pkg/models" "github.com/stashapp/stash/pkg/utils" - "github.com/vearutop/statigz" ) var version string @@ -190,6 +189,12 @@ func Start(uiBox embed.FS, loginUIBox embed.FS) { ext := path.Ext(r.URL.Path) + // workaround for Windows systems where js files are set to plaintext in + // the registry. + if ext == ".js" { + w.Header().Set("Content-Type", "application/javascript") + } + if customUILocation != "" { if r.URL.Path == "index.html" || ext == "" { r.URL.Path = "/" @@ -215,7 +220,9 @@ func Start(uiBox embed.FS, loginUIBox embed.FS) { w.Header().Add("Cache-Control", "max-age=604800000") } r.URL.Path = uiRootDir + r.URL.Path - statigz.FileServer(uiBox).ServeHTTP(w, r) + + http.FileServer(http.FS(uiBox)).ServeHTTP(w, r) + // statigz.FileServer(uiBox).ServeHTTP(w, r) } }) diff --git a/ui/v2.5/src/components/Changelog/Changelog.tsx b/ui/v2.5/src/components/Changelog/Changelog.tsx index 9dd739a8d..e237f94d6 100644 --- a/ui/v2.5/src/components/Changelog/Changelog.tsx +++ b/ui/v2.5/src/components/Changelog/Changelog.tsx @@ -23,8 +23,8 @@ type Module = typeof V010; const Changelog: React.FC = () => { const [{ data, loading }, setOpenState] = useChangelogStorage(); - const stashVersion = process.env.VITE_APP_STASH_VERSION; - const buildTime = process.env.VITE_APP_DATE; + const stashVersion = import.meta.env.VITE_APP_STASH_VERSION; + const buildTime = import.meta.env.VITE_APP_DATE; let buildDate; if (buildTime) { diff --git a/ui/v2.5/src/components/Settings/SettingsAboutPanel.tsx b/ui/v2.5/src/components/Settings/SettingsAboutPanel.tsx index ef55a82a6..efff47b79 100644 --- a/ui/v2.5/src/components/Settings/SettingsAboutPanel.tsx +++ b/ui/v2.5/src/components/Settings/SettingsAboutPanel.tsx @@ -5,9 +5,9 @@ import { LoadingIndicator } from "src/components/Shared"; import { useLatestVersion } from "src/core/StashService"; export const SettingsAboutPanel: React.FC = () => { - const gitHash = process.env.VITE_APP_GITHASH; - const stashVersion = process.env.VITE_APP_STASH_VERSION; - const buildTime = process.env.VITE_APP_DATE; + const gitHash = import.meta.env.VITE_APP_GITHASH; + const stashVersion = import.meta.env.VITE_APP_STASH_VERSION; + const buildTime = import.meta.env.VITE_APP_DATE; const intl = useIntl(); diff --git a/ui/v2.5/src/core/createClient.ts b/ui/v2.5/src/core/createClient.ts index 0e605a4de..0d8aa48cb 100644 --- a/ui/v2.5/src/core/createClient.ts +++ b/ui/v2.5/src/core/createClient.ts @@ -90,10 +90,10 @@ export const getBaseURL = () => { export const getPlatformURL = (ws?: boolean) => { const platformUrl = new URL(window.location.origin + getBaseURL()); - if (!process.env.NODE_ENV || process.env.NODE_ENV === "development") { - platformUrl.port = process.env.VITE_APP_PLATFORM_PORT ?? "9999"; + if (import.meta.env.DEV) { + platformUrl.port = import.meta.env.VITE_APP_PLATFORM_PORT ?? "9999"; - if (process.env.VITE_APP_HTTPS === "true") { + if (import.meta.env.VITE_APP_HTTPS === "true") { platformUrl.protocol = "https:"; } } diff --git a/ui/v2.5/src/globals.d.ts b/ui/v2.5/src/globals.d.ts index 79228f836..1874dfb59 100644 --- a/ui/v2.5/src/globals.d.ts +++ b/ui/v2.5/src/globals.d.ts @@ -7,3 +7,17 @@ declare module "hamming-distance"; declare module "@formatjs/intl-pluralrules/locale-data/en"; declare module "@formatjs/intl-numberformat/locale-data/en"; declare module "@formatjs/intl-numberformat/locale-data/en-GB"; + +/* eslint-disable @typescript-eslint/naming-convention */ +interface ImportMetaEnv extends Readonly> { + readonly VITE_APP_GITHASH?: string; + readonly VITE_APP_STASH_VERSION?: string; + readonly VITE_APP_DATE?: string; + readonly VITE_APP_PLATFORM_PORT?: string; + readonly VITE_APP_HTTPS?: string; +} + +interface ImportMeta { + readonly env: ImportMetaEnv; +} +/* eslint-enable @typescript-eslint/no-unused-vars */ diff --git a/ui/v2.5/src/serviceWorker.ts b/ui/v2.5/src/serviceWorker.ts index 3b891b942..ca73860b0 100755 --- a/ui/v2.5/src/serviceWorker.ts +++ b/ui/v2.5/src/serviceWorker.ts @@ -101,7 +101,7 @@ function checkValidServiceWorker(swUrl: string, config?: IConfig) { } export function register(config?: IConfig) { - if (process.env.NODE_ENV === "production" && "serviceWorker" in navigator) { + if (import.meta.env.PROD && "serviceWorker" in navigator) { // The URL constructor is available in all browsers that support SW. const publicUrl = new URL( (process as { env: { [key: string]: string } }).env.PUBLIC_URL, @@ -115,7 +115,7 @@ export function register(config?: IConfig) { } window.addEventListener("load", () => { - const swUrl = `${process.env.PUBLIC_URL}/service-worker.js`; + const swUrl = `${import.meta.env.PUBLIC_URL}/service-worker.js`; if (isLocalhost) { // This is running on localhost. Let's check if a service worker still exists or not. diff --git a/ui/v2.5/vite.config.js b/ui/v2.5/vite.config.js index f7e3eefa8..a6c8e6cd4 100644 --- a/ui/v2.5/vite.config.js +++ b/ui/v2.5/vite.config.js @@ -15,14 +15,12 @@ export default defineConfig({ }, publicDir: 'public', assetsInclude: ['**/*.md'], - plugins: [tsconfigPaths(), viteCompression({ - algorithm: 'gzip', - disable: false, - deleteOriginFile: true, - filter: /\.(js|json|css|svg|md)$/i - })], - define: { - 'process.versions': {}, - 'process.env': {} - } + plugins: [tsconfigPaths() + // viteCompression({ + // algorithm: 'gzip', + // disable: false, + // deleteOriginFile: true, + // filter: /\.(js|json|css|svg|md)$/i + // }) +], })