Skip to content

Commit

Permalink
Fix CUSTOM_LEMMY_SERVERS when installed (aeharding#127)
Browse files Browse the repository at this point in the history
* add .prettierrc to keep original formatting

* define CUSTOM_LEMMY_SERVERS through vite.config

the CUSTOM_LEMMY_SERVER env variable is now
injected as a variable defined in vite.config.ts,
allowing it to persist even after the PWA has
been installed

* fix linting issue
  • Loading branch information
gianmarcotoso authored Jul 1, 2023
1 parent 9f9e8ba commit 8bde519
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 38 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@
{
"files": ["src/**"],
"globals": {
"APP_VERSION": true
"APP_VERSION": true,
"CUSTOM_LEMMY_SERVERS": true
}
}
]
Expand Down
5 changes: 5 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"tabWidth": 2,
"useTabs": false,
"semi": true
}
4 changes: 2 additions & 2 deletions src/helpers/lemmy.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Comment, CommentView, Community } from "lemmy-js-client";

export const LEMMY_SERVERS =
"CUSTOM_LEMMY_SERVERS" in window
? (window.CUSTOM_LEMMY_SERVERS as string[])
typeof CUSTOM_LEMMY_SERVERS !== "undefined"
? (CUSTOM_LEMMY_SERVERS as string).split(",")
: ["lemmy.world", "lemmy.ml", "beehaw.org", "sh.itjust.works"];

export interface LemmyJWT {
Expand Down
1 change: 1 addition & 0 deletions src/vite-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/// <reference types="vite-plugin-pwa/react" />

declare const APP_VERSION: string;
declare const CUSTOM_LEMMY_SERVERS: string;
76 changes: 41 additions & 35 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,42 +3,48 @@ import { defineConfig } from "vitest/config";
import { VitePWA } from "vite-plugin-pwa";
import svgr from "vite-plugin-svgr";
import legacy from "@vitejs/plugin-legacy";
import { loadEnv } from "vite";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
react({
jsxImportSource: "@emotion/react",
babel: {
plugins: ["@emotion/babel-plugin"],
},
}),
svgr(),
VitePWA({ registerType: "prompt" }),
legacy({
modernPolyfills: ["es.array.at"],
}),
],
// TODO: Outdated clients trying to access stale codesplit js chucks
// break. This breaks iOS transitions.
// Put everything into one chunk for now.
build: {
rollupOptions: {
output: {
manualChunks: () => "index.js",
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd(), "") };

return defineConfig({
plugins: [
react({
jsxImportSource: "@emotion/react",
babel: {
plugins: ["@emotion/babel-plugin"],
},
}),
svgr(),
VitePWA({ registerType: "prompt" }),
legacy({
modernPolyfills: ["es.array.at"],
}),
],
// TODO: Outdated clients trying to access stale codesplit js chucks
// break. This breaks iOS transitions.
// Put everything into one chunk for now.
build: {
rollupOptions: {
output: {
manualChunks: () => "index.js",
},
},
},
},
define: {
// eslint-disable-next-line no-undef
APP_VERSION: JSON.stringify(process.env.npm_package_version),
},
test: {
globals: true,
environment: "jsdom",
setupFiles: "./src/setupTests.ts",
},
optimizeDeps: {
exclude: ["mdast-util-gfm-autolink-literal-lemmy"],
},
});
define: {
// eslint-disable-next-line no-undef
APP_VERSION: JSON.stringify(process.env.npm_package_version),
CUSTOM_LEMMY_SERVERS: JSON.stringify(process.env.CUSTOM_LEMMY_SERVERS),
},
test: {
globals: true,
environment: "jsdom",
setupFiles: "./src/setupTests.ts",
},
optimizeDeps: {
exclude: ["mdast-util-gfm-autolink-literal-lemmy"],
},
});
};

0 comments on commit 8bde519

Please sign in to comment.