Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions nativephp/electron/build/vite-env.test.mjs
Original file line number Diff line number Diff line change
@@ -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/);
});
12 changes: 10 additions & 2 deletions nativephp/electron/electron.vite.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
Expand All @@ -23,4 +31,4 @@ export default defineConfig({
},
plugins: [externalizeDepsPlugin()],
},
});
}));
Loading