A local-first personal finance app
[AI] refactor(build): replace process.env with import.meta.env in browser code (#8212)
Stephen Brown II9f8433990441
diff --git a/packages/desktop-client/src/browser-preload.js b/packages/desktop-client/src/browser-preload.jsindex 797cc00184..9dc50ea45d 100644--- a/packages/desktop-client/src/browser-preload.js+++ b/packages/desktop-client/src/browser-preload.js@@ -14,10 +14,10 @@ const backendWorkerUrl = new URL('./browser-server.js', import.meta.url);// browser environment this is where we initialize the backend and// everything else.-const IS_DEV = process.env.NODE_ENV === 'development';+const IS_DEV = import.meta.env.DEV;const ACTUAL_VERSION = Platform.isPlaywright? '99.9.9'- : process.env.REACT_APP_REVIEW_ID+ : import.meta.env.REACT_APP_REVIEW_ID? '.preview': packageJson.version;@@ -33,8 +33,8 @@ const worker = startBrowserBackend({initPayload: {version: ACTUAL_VERSION,isDev: IS_DEV,- publicUrl: process.env.PUBLIC_URL,- hash: process.env.REACT_APP_BACKEND_WORKER_HASH,+ publicUrl: import.meta.env.BASE_URL.slice(0, -1),+ hash: import.meta.env.REACT_APP_BACKEND_WORKER_HASH,},createSharedWorker: () =>new SharedBrowserServerWorker({ name: 'actual-backend' }),diff --git a/packages/desktop-client/src/build-shims.js b/packages/desktop-client/src/build-shims.jsindex f7056affee..e7a9225025 100644--- a/packages/desktop-client/src/build-shims.js+++ b/packages/desktop-client/src/build-shims.js@@ -1,13 +1,3 @@const global = globalThis || this || self;-const process = {- env: {- ...import.meta.env,- NODE_ENV: import.meta.env.DEV ? 'development' : 'production',- PUBLIC_URL: import.meta.env.BASE_URL.slice(0, -1),- },-};--export { process };-export { global };diff --git a/packages/desktop-client/src/components/App.tsx b/packages/desktop-client/src/components/App.tsxindex 96994d96c2..be40c0c2b3 100644--- a/packages/desktop-client/src/components/App.tsx+++ b/packages/desktop-client/src/components/App.tsx@@ -219,7 +219,7 @@ export function App() {}}><ErrorBoundary FallbackComponent={ErrorFallback}>- {process.env.REACT_APP_REVIEW_ID && !isTestEnv && (+ {import.meta.env.REACT_APP_REVIEW_ID && !isTestEnv && (<DevelopmentTopBar />)}<AppInner />diff --git a/packages/desktop-client/src/components/DevelopmentTopBar.tsx b/packages/desktop-client/src/components/DevelopmentTopBar.tsxindex 1a57de3e13..9ef7c0c240 100644--- a/packages/desktop-client/src/components/DevelopmentTopBar.tsx+++ b/packages/desktop-client/src/components/DevelopmentTopBar.tsx@@ -23,9 +23,9 @@ export function DevelopmentTopBar() {<Linkvariant="external"linkColor="purple"- to={`https://github.com/actualbudget/actual/pull/${process.env.REACT_APP_REVIEW_ID}`}+ to={`https://github.com/actualbudget/actual/pull/${import.meta.env.REACT_APP_REVIEW_ID}`}>- Open the PR: #{process.env.REACT_APP_REVIEW_ID}+ Open the PR: #{import.meta.env.REACT_APP_REVIEW_ID}</Link></View></View>diff --git a/packages/desktop-client/src/components/FinancesApp.tsx b/packages/desktop-client/src/components/FinancesApp.tsxindex beb0120e73..19e0367dff 100644--- a/packages/desktop-client/src/components/FinancesApp.tsx+++ b/packages/desktop-client/src/components/FinancesApp.tsx@@ -157,7 +157,7 @@ export function FinancesApp() {type: 'message',title: t('A new version of Actual is available!'),message:- (process.env.REACT_APP_IS_PIKAPODS ?? '').toLowerCase() ===+ (import.meta.env.REACT_APP_IS_PIKAPODS ?? '').toLowerCase() ==='true'? t('A new version of Actual is available! Your Pikapods instance will be automatically updated in the next few days - no action needed.',diff --git a/packages/desktop-client/src/hooks/useThemeCatalog.ts b/packages/desktop-client/src/hooks/useThemeCatalog.tsindex db92ede285..45a4589a66 100644--- a/packages/desktop-client/src/hooks/useThemeCatalog.ts+++ b/packages/desktop-client/src/hooks/useThemeCatalog.ts@@ -2,7 +2,7 @@ import { useEffect, useState } from 'react';import type { CatalogTheme } from '#style/customThemes';-const CATALOG_URL = `https://raw.githubusercontent.com/actualbudget/actual/${process.env.REACT_APP_BRANCH || 'master'}/packages/desktop-client/src/data/customThemeCatalog.json`;+const CATALOG_URL = `https://raw.githubusercontent.com/actualbudget/actual/${import.meta.env.REACT_APP_BRANCH || 'master'}/packages/desktop-client/src/data/customThemeCatalog.json`;/*** Custom hook to fetch and manage the theme catalog from GitHub.diff --git a/packages/desktop-client/src/util/versions.ts b/packages/desktop-client/src/util/versions.tsindex 17df6476da..b13fd81623 100644--- a/packages/desktop-client/src/util/versions.ts+++ b/packages/desktop-client/src/util/versions.ts@@ -19,7 +19,7 @@ function cmpSemanticVersion(}export async function getLatestVersion(): Promise<string | 'unknown'> {- if (Platform.isPlaywright || process.env.REACT_APP_REVIEW_ID) {+ if (Platform.isPlaywright || import.meta.env.REACT_APP_REVIEW_ID) {return Promise.resolve('v99.9.9');}diff --git a/packages/desktop-client/vite.config.mts b/packages/desktop-client/vite.config.mtsindex df5261ec46..e75ae4fff6 100644--- a/packages/desktop-client/vite.config.mts+++ b/packages/desktop-client/vite.config.mts@@ -44,45 +44,18 @@ const injectPlugin = (options?: Parameters<typeof inject>[0]): Plugin => {// Inject build shims using the inject pluginconst injectShims = (): Plugin[] => {const buildShims = path.resolve('./src/build-shims.js');- const serveInject: {- exclude: string[];- global: [string, string];- } = {- exclude: ['src/setupTests.ts'],- global: [buildShims, 'global'],- };- const buildInject: {- global: [string, string];- } = {- global: [buildShims, 'global'],- };return [- {- name: 'define-build-process',- config: () => ({- // rename process.env in build mode so it doesn't get set to an empty object up by the vite:define plugin- // this isn't needed in serve mode, because vite:define doesn't empty it in serve mode. And defines also happen last anyways in serve mode.- environments: {- client: {- define: {- 'process.env': '_process.env',- },- },- },- }),- apply: 'build',- },{enforce: 'post',apply: 'serve',...injectPlugin({- ...serveInject,- process: [buildShims, 'process'],+ exclude: ['src/setupTests.ts'],+ global: [buildShims, 'global'],}),},{- name: 'inject-build-process',+ name: 'inject-build-global',enforce: 'post',apply: 'build',config: () => ({@@ -90,8 +63,7 @@ const injectShims = (): Plugin[] => {rolldownOptions: {transform: {inject: {- ...buildInject,- _process: [buildShims, 'process'],+ global: [buildShims, 'global'],},},},diff --git a/packages/loot-core/package.json b/packages/loot-core/package.jsonindex ee583a5714..e3f959caa6 100644--- a/packages/loot-core/package.json+++ b/packages/loot-core/package.json@@ -85,6 +85,11 @@"#platform/server/*": "./src/platform/server/*.ts","#platform/*": "./src/platform/*.ts","#server/*": "./src/server/*.ts",+ "#shared/environment": {+ "electron": "./src/shared/environment.ts",+ "api": "./src/shared/environment.ts",+ "default": "./src/shared/environment.browser.ts"+ },"#shared/*": "./src/shared/*.ts","#types/*": "./src/types/*.ts","#mocks/*": "./src/mocks/*.ts",@@ -155,6 +160,11 @@"api": "./src/shared/platform.api.ts","default": "./src/shared/platform.ts"},+ "./shared/environment": {+ "electron": "./src/shared/environment.ts",+ "api": "./src/shared/environment.ts",+ "default": "./src/shared/environment.browser.ts"+ },"./shared/*": "./src/shared/*.ts","./types/models": "./src/types/models/index.ts","./types/*": "./src/types/*.ts",diff --git a/packages/loot-core/src/shared/environment.browser.ts b/packages/loot-core/src/shared/environment.browser.tsnew file mode 100644index 0000000000..cd9132de18--- /dev/null+++ b/packages/loot-core/src/shared/environment.browser.ts@@ -0,0 +1,21 @@+export function isPreviewEnvironment() {+ return String(import.meta.env.REACT_APP_NETLIFY) === 'true';+}++export function isDevelopmentEnvironment() {+ return import.meta.env.DEV;+}++export function isNonProductionEnvironment() {+ return isPreviewEnvironment() || isDevelopmentEnvironment();+}++export function isElectron() {+ if (+ typeof navigator !== 'undefined' &&+ navigator.userAgent.indexOf('Electron') >= 0+ ) {+ return true;+ }+ return false;+}diff --git a/upcoming-release-notes/process-env.md b/upcoming-release-notes/process-env.mdnew file mode 100644index 0000000000..67b57b622a--- /dev/null+++ b/upcoming-release-notes/process-env.md@@ -0,0 +1,6 @@+---+category: Maintenance+authors: [StephenBrown2]+---++Use Vite's `import.meta.env` instead of `process.env` for build-time `REACT_APP_*` variables in client-only desktop-client code.