In 2019, the simplest way to do this is to use the heroku/nodejs
buildpack (automatically selected if you have a package.json
at the root of the repository) and add a build
script to package json:
"build": "webpack --mode production --config ./webpack.prod.config.js"
Heroku will automatically run this script on deploy. Bonus points because this is the intuitive script to run to test the build locally or in CI.
This is described in Heroku's Best Practices for Node.js Development document:
npm’s lifecycle scripts make great hooks for automation. Heroku provides custom hooks that allow you to run custom commands before or after we install your dependencies. If you need to run something before building your app, you can use the heroku-prebuild
script. Need to build assets with grunt, gulp, browserify, or webpack? Do it in a build
script.