From b9e4b8d70b40879b4d1ef4fc2ee0c7fea55a596c Mon Sep 17 00:00:00 2001 From: Luis Amorim Date: Tue, 17 Nov 2026 13:38:33 -0300 Subject: [PATCH] fix(desktop): define Electron build path in electron-vite config The main bundle reads MAIN_VITE_NATIVEPHP_BUILD_PATH from nativephp/electron/.env.production, which was never committed because the root .gitignore ignores .env.*. Vite then inlined undefined and path.resolve crashed the packaged app on launch (ERR_INVALID_ARG_TYPE). Define the value in electron.vite.config.mjs instead and add a test that builds the bundle and fails if the path is undefined. Fixes #12 Signed-off-by: Luis Amorim --- nativephp/electron/build/vite-env.test.mjs | 18 ++++++++++++++++++ nativephp/electron/electron.vite.config.mjs | 12 ++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 nativephp/electron/build/vite-env.test.mjs diff --git a/nativephp/electron/build/vite-env.test.mjs b/nativephp/electron/build/vite-env.test.mjs new file mode 100644 index 0000000..a087337 --- /dev/null +++ b/nativephp/electron/build/vite-env.test.mjs @@ -0,0 +1,18 @@ +import assert from 'node:assert/strict'; +import { execFileSync } from 'node:child_process'; +import { readFileSync } from 'node:fs'; +import { join } from 'node:path'; +import { fileURLToPath } from 'node:url'; +import test from 'node:test'; + +// src/main/index.js resolves the bundled app from import.meta.env.MAIN_VITE_NATIVEPHP_BUILD_PATH. +// If Vite inlines `undefined`, the packaged app crashes on launch (ERR_INVALID_ARG_TYPE). +test('production main bundle embeds a defined build path', () => { + const root = fileURLToPath(new URL('..', import.meta.url)); + // Run the local CLI with Node directly: `npx` is a .cmd shim on Windows. + const cli = join(root, 'node_modules', 'electron-vite', 'bin', 'electron-vite.js'); + execFileSync(process.execPath, [cli, 'build'], { cwd: root, stdio: 'pipe' }); + const bundle = readFileSync(join(root, 'out', 'main', 'index.js'), 'utf8'); + assert.match(bundle, /\.\.\/\.\.\/\.\.\/build/); + assert.doesNotMatch(bundle, /import\.meta\.dirname, void 0/); +}); diff --git a/nativephp/electron/electron.vite.config.mjs b/nativephp/electron/electron.vite.config.mjs index 54d4dae..b2e390d 100644 --- a/nativephp/electron/electron.vite.config.mjs +++ b/nativephp/electron/electron.vite.config.mjs @@ -5,8 +5,16 @@ import { fileURLToPath } from 'node:url'; // NativePHP sets APP_PATH in packaged builds; local developer builds do not. const appPath = process.env.APP_PATH || fileURLToPath(new URL('../../', import.meta.url)); -export default defineConfig({ +// The main bundle locates the prepared app through this value. Define it here +// rather than in .env.* files, which are gitignored and rejected by the +// publication check. Production mirrors NativePHP's own .env.production. +export default defineConfig(({ mode }) => ({ main: { + define: { + 'import.meta.env.MAIN_VITE_NATIVEPHP_BUILD_PATH': JSON.stringify( + mode === 'production' ? '../../../build' : process.env.NATIVEPHP_BUILD_PATH, + ), + }, build: { rollupOptions: { plugins: [ @@ -23,4 +31,4 @@ export default defineConfig({ }, plugins: [externalizeDepsPlugin()], }, -}); +}));