Overview
The bundling process is the core of how NativePHP transforms your Laravel application into a distributable desktop application. It involves copying files, optimizing dependencies, bundling the PHP runtime, and packaging everything with Electron.Build Directory Preparation
NativePHP uses theCopiesToBuildDirectory trait to handle file operations:
Clean Previous Builds
The build directory is completely removed and recreated to ensure a clean state.
Copy Application Files
Your Laravel application is copied to
build/app/ using a recursive iterator that respects exclusion patterns.Files matching patterns in cleanup_exclude_files are automatically skipped during the copy process.Placeholder
_native.json files prevent Electron Builder from removing empty but required Laravel directories.File Filtering
NativePHP uses PHP’sfnmatch() function for glob-style pattern matching:
Common Exclusion Patterns
| Pattern | What It Excludes |
|---|---|
build | The entire build directory |
node_modules | All Node.js dependencies |
*/tests | Test directories at any level |
.git | Git repository data |
*.log | All log files |
Dependency Optimization
NativePHP optimizes your Composer dependencies for production using thePrunesVendorDirectory trait:
Install Production Dependencies
Runs
composer install --no-dev to remove development dependencies like PHPUnit, test libraries, and development tools.Environment File Management
TheManagesEnvFile trait handles sensitive data cleanup:
Environment Cleanup Process
Remove Sensitive Keys
Environment variables matching patterns in
cleanup_env_keys are removed from the .env file.Environment Manipulation Methods
TheManagesEnvFile trait provides helpful methods:
PHP Binary Bundling
NativePHP automatically bundles platform-specific PHP binaries during the Electron build:The
beforePack hook in Electron Builder ensures the correct PHP binary is downloaded and bundled for the target platform and architecture.Pre and Post Processing
TheHasPreAndPostProcessing trait executes custom commands:
Command Features
- Live Output: Command output is streamed in real-time
- TTY Support: Commands run in TTY mode when supported
- Timeout Protection: Each command has a 300-second timeout
- Error Handling: Failed commands are logged but don’t stop the build
Certificate Authority Bundling
NativePHP includes theCopiesCertificateAuthority trait to bundle CA certificates for HTTPS requests:
CA certificates are automatically bundled to ensure your application can make secure HTTPS connections on all platforms.
Electron Packaging
The final bundling step uses Electron Builder with configuration fromelectron-builder.mjs:
Resource Bundling
Platform-Specific Packaging
- Windows
- macOS
- Linux
Bundle Size Optimization
Tips for reducing your application bundle size:Exclude Development Files
Exclude Development Files
Ensure
node_modules, tests, and development assets are in your cleanup_exclude_files configuration.Optimize Vendor Directory
Optimize Vendor Directory
Use
composer install --no-dev (automatic) and consider removing unused dependencies.Remove Build Artifacts
Remove Build Artifacts
Clean up compiled assets and temporary files in your
postbuild scripts.Compress Assets
Compress Assets
Use Vite/Laravel Mix to minify and compress JavaScript and CSS assets.
Troubleshooting
Missing Required Directories
Issue: Application crashes due to missing storage directories. Solution: NativePHP automatically creates_native.json placeholder files. Ensure you’re not manually removing these.
Large Bundle Size
Issue: Final executable is larger than expected. Solution: Review yourcleanup_exclude_files configuration and ensure development dependencies are excluded.
Composer Install Timeout
Issue: Composer install fails during bundling. Solution: The timeout is set to 300 seconds (5 minutes). Optimize your composer.json or increase the timeout inPrunesVendorDirectory.php:15.
Next Steps
Code Signing
Sign your bundled applications for distribution
Auto-Updates
Configure automatic updates for your bundled apps