diff --git a/.eslintrc.js b/.eslintrc.js index 611c24f5d61..05b7df1ac79 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -43,6 +43,9 @@ module.exports = { parser: '@typescript-eslint/parser', parserOptions: { project: [path.join(__dirname, './tsconfig.json')] }, reportUnusedDisableDirectives: true, + globals: { + globalThis: false, + }, rules: { 'prettier/prettier': 'warn', diff --git a/packages/desktop-client/.gitignore b/packages/desktop-client/.gitignore index 7a5faec2c43..08fad3425bd 100644 --- a/packages/desktop-client/.gitignore +++ b/packages/desktop-client/.gitignore @@ -10,11 +10,13 @@ test-results # production build build-stats +stats.json # misc .DS_Store .env npm-debug.log +.swc *kcab.* public/kcab diff --git a/packages/desktop-client/package.json b/packages/desktop-client/package.json index 1a23cf8a515..f36a447cb90 100644 --- a/packages/desktop-client/package.json +++ b/packages/desktop-client/package.json @@ -14,6 +14,7 @@ "@react-aria/utils": "^3.19.0", "@react-stately/collections": "^3.10.0", "@react-stately/list": "^3.9.1", + "@rollup/plugin-inject": "^5.0.5", "@svgr/cli": "^8.0.1", "@swc/core": "^1.3.82", "@swc/helpers": "^0.5.1", diff --git a/packages/desktop-client/src/build-shims.js b/packages/desktop-client/src/build-shims.js new file mode 100644 index 00000000000..e9348c2dd80 --- /dev/null +++ b/packages/desktop-client/src/build-shims.js @@ -0,0 +1,14 @@ +const global = globalThis || this || self; + +const process = { + env: { + ...import.meta.env, + NODE_ENV: import.meta.env.MODE, + PUBLIC_URL: import.meta.env.BASE_URL.slice(0, -1), + }, +}; + +// eslint-disable-next-line import/no-unused-modules +export { process }; +// eslint-disable-next-line import/no-unused-modules +export { global }; diff --git a/packages/desktop-client/src/index.tsx b/packages/desktop-client/src/index.tsx index dc7680bea77..7a01a023e7c 100644 --- a/packages/desktop-client/src/index.tsx +++ b/packages/desktop-client/src/index.tsx @@ -1,5 +1,3 @@ -import './init'; - // This file will initialize the app if we are in a real browser // environment (not electron) import './browser-preload'; diff --git a/packages/desktop-client/src/init.js b/packages/desktop-client/src/init.js deleted file mode 100644 index 21760550aa1..00000000000 --- a/packages/desktop-client/src/init.js +++ /dev/null @@ -1,8 +0,0 @@ -window.global ||= window; -window.process ||= { - env: { - ...import.meta.env, - NODE_ENV: import.meta.env.MODE, - PUBLIC_URL: import.meta.env.BASE_URL.slice(0, -1), - }, -}; diff --git a/packages/desktop-client/vite.config.mts b/packages/desktop-client/vite.config.mts index a7053b066e1..4a0f52e02b2 100644 --- a/packages/desktop-client/vite.config.mts +++ b/packages/desktop-client/vite.config.mts @@ -1,5 +1,6 @@ import * as path from 'path'; +import inject from '@rollup/plugin-inject'; import react from '@vitejs/plugin-react-swc'; import { visualizer } from 'rollup-plugin-visualizer'; /// @@ -22,8 +23,44 @@ const addWatchers = (): Plugin => ({ }, }); +const injectShims = (): Plugin[] => { + const buildShims = path.resolve('./src/build-shims.js'); + const commonInject = { + exclude: ['src/setupTests.jsx'], + global: [buildShims, 'global'], + }; + + return [ + { + name: 'inject-build-process', + config: () => ({ + define: { + 'process.env': `_process.env`, + }, + }), + apply: 'build', + }, + { + ...inject({ + ...commonInject, + process: [buildShims, 'process'], + }), + enforce: 'post', + apply: 'serve', + }, + { + ...inject({ + ...commonInject, + _process: [buildShims, 'process'], + }), + enforce: 'post', + apply: 'build', + }, + ]; +}; + // https://vitejs.dev/config/ -export default defineConfig(({ mode }) => { +export default defineConfig(async ({ mode }) => { const env = loadEnv(mode, process.cwd(), ''); const devHeaders = { 'Cross-Origin-Opener-Policy': 'same-origin', @@ -120,6 +157,7 @@ export default defineConfig(({ mode }) => { extensions: resolveExtensions, }, plugins: [ + injectShims(), addWatchers(), visualizer({ template: 'raw-data' }), react({ diff --git a/yarn.lock b/yarn.lock index 0fa16215168..de9464d677b 100644 --- a/yarn.lock +++ b/yarn.lock @@ -65,6 +65,7 @@ __metadata: "@react-aria/utils": "npm:^3.19.0" "@react-stately/collections": "npm:^3.10.0" "@react-stately/list": "npm:^3.9.1" + "@rollup/plugin-inject": "npm:^5.0.5" "@svgr/cli": "npm:^8.0.1" "@swc/core": "npm:^1.3.82" "@swc/helpers": "npm:^0.5.1" @@ -3212,6 +3213,38 @@ __metadata: languageName: node linkType: hard +"@rollup/plugin-inject@npm:^5.0.5": + version: 5.0.5 + resolution: "@rollup/plugin-inject@npm:5.0.5" + dependencies: + "@rollup/pluginutils": "npm:^5.0.1" + estree-walker: "npm:^2.0.2" + magic-string: "npm:^0.30.3" + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + checksum: 1d0e68dff0a8785398a1b6a7dac0dc0a7f2ded22319c0b4c411053f34cbe237ca897d1fc97e5150fddbc3486480f21cbeeb69f0ae7f44ab1ae7307c164c7e704 + languageName: node + linkType: hard + +"@rollup/pluginutils@npm:^5.0.1": + version: 5.1.0 + resolution: "@rollup/pluginutils@npm:5.1.0" + dependencies: + "@types/estree": "npm:^1.0.0" + estree-walker: "npm:^2.0.2" + picomatch: "npm:^2.3.1" + peerDependencies: + rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0 + peerDependenciesMeta: + rollup: + optional: true + checksum: abb15eaec5b36f159ec351b48578401bedcefdfa371d24a914cfdbb1e27d0ebfbf895299ec18ccc343d247e71f2502cba21202bc1362d7ef27d5ded699e5c2b2 + languageName: node + linkType: hard + "@rollup/rollup-android-arm-eabi@npm:4.8.0": version: 4.8.0 resolution: "@rollup/rollup-android-arm-eabi@npm:4.8.0" @@ -8227,6 +8260,13 @@ __metadata: languageName: node linkType: hard +"estree-walker@npm:^2.0.2": + version: 2.0.2 + resolution: "estree-walker@npm:2.0.2" + checksum: b02109c5d46bc2ed47de4990eef770f7457b1159a229f0999a09224d2b85ffeed2d7679cffcff90aeb4448e94b0168feb5265b209cdec29aad50a3d6e93d21e2 + languageName: node + linkType: hard + "esutils@npm:^2.0.2": version: 2.0.3 resolution: "esutils@npm:2.0.3" @@ -11565,7 +11605,7 @@ __metadata: languageName: node linkType: hard -"magic-string@npm:^0.30.5": +"magic-string@npm:^0.30.3, magic-string@npm:^0.30.5": version: 0.30.5 resolution: "magic-string@npm:0.30.5" dependencies: