In your vue.config.js
file (create one in the src
folder if it doesn't already exist), specify the target directory for the build:
const path = require("path");
module.exports = {
outputDir: path.resolve(__dirname, "path/to/server/directory/public")
};
After running npm run build
, the static files will be compiled there.
Next, you just need to direct your server to that particular folder.
For those using Node/Express as their backend:
// Handle production
if (process.env.NODE_ENV === 'production') {
// Static folder
app.use(express.static(__dirname + '/public/'));
}
Additionally, if your Vue app is a single-page application (SPA), include the following code inside the if
block, after
app.use(express.static(__dirname + '/public/'));
, to manage routing:
// Handle SPA
app.get(/.*/, (req, res) => res.sendFile(__dirname + '/public/index.html'));