Just curious how other Forge users are handling production assets with Vite given that /public/build is in the default .gitignore file. Are you editing .gitignore, running npm run build locally, and pushing the bundled assets to your repo or are you running the npm commands via the deployment script in Forge? Is either approach clearly better? Have any best practices emerged?
@mikefolsom You should be running npm run build as part of your deployment script, to generate the assets for that particular version you’re deploying.
The /public/build directory is gitignore-d because you’re not meant to track it in your repository.
@martinbean Thanks for the reply. I know there are benefits/tradeoffs for each approach. I like not having to remember to npm run build locally before every deployment and appreciate having a clean(er) git repo. But have you ever run into issues with building on the development server (e.g., long deployment times/failed builds)? Do you npm install as part of your deployment script? What about managing npm versions, dev dependencies, and the node_modules directory? Do you remove that directory after every deployment or leave it there? I want to try this approach but am a bit nervous about it. :)
@mikefolsom Never had any issues. On deploy I run:
npm ci
npm run build
rm -rf node_modules
This installs NPM dependencies afresh; runs my NPM build script to build CSS and JavaScript using Vite; and then trashes the node_modules directory, as it’s not needed for the running and serving of a PHP-based application.
Like PHP, you want to make sure the versions of Node and NPM match across environments. You can enforce this if you really want to by adding an engines section to your package.json file: