vue.jsvue-cli-3

Vue CLI delete the outputDir folder during serve


I'm trying to use VueCLI and delete the output directory during serve, so that I can use this in my php files and determine whether to load dist assets or load via localhost:8080.

So in my vue.config.js I have:

module.exports = {
  outputDir:'web',
  configureWebpack: config => {
    if (process.env.NODE_ENV !== 'production') {
        console.log("its serve") // this logs
        // here delete the outputDir
    }
  }
}

How do i delete a folder using the VueCLI since by default during serve my app is never deleted.


Solution

  • Use rimraf to remove the dist directory.

    const rimraf = require("rimraf");
    
    module.exports = {
      outputDir: 'web',
      configureWebpack: config => {
        if (process.env.NODE_ENV !== 'production') {
            rimraf.sync(config.outputDir);
            // rimraf.sync('web')
        }
      }
    };
    

    As an alternative you can append the rimraf to your serve script.

    // package.json
    
    "scripts": {
        "serve": "rimraf web && vue-cli-service serve",
        "build": "vue-cli-service build",
        "lint": "vue-cli-service lint"
    }