Create a new copyWebPackplugin.js file with the following code:

const fs = require('fs')
const path = require('path')

/** * webpack script *@description After webpack is packaged and the resources are generated to the Output directory, the generated resources are copied to the SpringBoot project */
module.exports = class CopyWebpackPlugin {
  / * * * *@param { String } Options. dirFiles directory to copy all files (not folders) * in that directory@param { String } Options. dir, copy all files * in all folders in that directory@param { String } Options. dirTo copy to this directory *@param { String } Options. dirFilesTo copy to this directory */
  constructor(options) {
    this.options = options
  }

  // Webpack specifies that each instance of a plug-in must have a.apply() method, in which the plug-in can register its hooks before webpack calls all of its methods.
  apply(compiler) {
    compiler.hooks.afterEmit.tapAsync('CopyWebpackPlugin'.(compilation, cb) = > {
      try {
        if(! fs.copyFile) {console.error('Your nodejs version is too low, please upgrade! ')}else {
          if(! fs.existsSync(this.options.dirFilesTo)) {
            fs.mkdirSync(this.options.dirFilesTo, { recursive: true})}else {
            delDir(this.options.dirFilesTo)
          }
          if(! fs.existsSync(this.options.dirTo)) {
            fs.mkdirSync(this.options.dirTo, { recursive: true})}else {
            delDir(this.options.dirTo)
          }
          if (this.options.dirFiles) {
            const files = fs.readdirSync(this.options.dirFiles, { withFileTypes: true })
            files.forEach(file= > {
              if (file.isFile()) {
                fs.copyFile(path.resolve(this.options.dirFiles, file.name), path.resolve(this.options.dirFilesTo, file.name), error= > {
                  if (error) {
                    console.error(file.name + 'Failed to copy:' + error)
                  } else {
                    console.log(file.name + 'Copy succeeded! '}})}})}if (this.options.dir) {
            const files = fs.readdirSync(this.options.dir, { withFileTypes: true })
            files.forEach(file= > {
              if (file.isDirectory()) {
                copyDir(path.resolve(this.options.dir, file.name), path.resolve(this.options.dirTo, file.name), error= > {
                  console.error(file.name + 'Copy failed! ' + error)
                })
              }
            })
          }
        }
      } catch (error) {
        console.error(error)
      } finally {
        cb()
      }
    })
  }
}


@param SRC {String} The directory to copy * @param to {String} copy to the target directory */
function copyDir(src, to, callback) {
  fs.access(to, function (err) {
    if (err) {
      // Create a directory if the directory does not exist
      fs.mkdirSync(to)
    }
    _copy(null, src, to, callback)
  })
}

function _copy(err, src, to, callback) {
  if (err) {
    callback(err)
  } else {
    fs.readdir(src, function (err, paths) {
      if (err) {
        callback(err)
      } else {
        paths.forEach(function (path) {
          const _src = src + '/' + path
          const _to = to + '/' + path
          fs.stat(_src, function (err, stat) {
            if (err) {
              callback(err)
            } else {
              // Determine whether it is a file or a directory
              if (stat.isFile()) {
                console.log(path + 'Copy succeeded! ')
                fs.writeFileSync(_to, fs.readFileSync(_src))
              } else if (stat.isDirectory()) {
                // Copy the directory recursively
                copyDir(_src, _to, callback)
              }
            }
          })
        })
      }
    })
  }
}


function delDir(path) {
  let files = []
  if (fs.existsSync(path)) {
    files = fs.readdirSync(path)
    files.forEach((file) = > {
      const curPath = path + '/' + file
      if (fs.statSync(curPath).isDirectory()) {
        arguments.callee(curPath) // Delete folders recursively
      } else {
        fs.unlinkSync(curPath) // Delete files}}}})Copy the code

Then reference the plugin in the webpack.config.js file:

const path = require('path')
const CopyWebpackPlugin = require('./plugins/CopyWebpackPlugin')

module.exports = { ... .plugins: [
    new CopyWebpackPlugin({
      dirFiles: path.resolve(__dirname, 'dist'),
      dirFilesTo: 'E:/Workspace/idea/mx-blog-be-java/src/main/resources/templates'.dir: path.resolve(__dirname, 'dist'),
      dirTo: 'E:/Workspace/idea/mx-blog-be-java/src/main/resources/static',}),],... }Copy the code