使用node执行命令, 使前端项目打包后自动压缩成zip包

摘要:在 vue cli2 的项目中 找到 build/build.js,在webpack(webpackConfig, cb) 的 回调函数cb中添加;就会在webpack打包结束后将我们的打包出来的文件打包成zip包

在 vue cli2 的项目中 找到 build/build.js,在webpack(webpackConfig, cb) 的 回调函数cb中添加

const exec = require('child_process').exec

exec('cd .\\dist && del dist.zip', function(error) {
  if (error) {
    console.log(error)
  }
  // D:\\program\\WinRAR\\WinRAR.exe 需要改为你电脑上WinRAR的安装目录
  exec('cd .\\dist && D:\\program\\WinRAR\\WinRAR.exe a -r dist.zip .\\*.*', function(error) {
    if (error) {
      console.log(error)
    }
  })
})

就会在webpack打包结束后将我们的打包出来的文件打包成zip包

vue cli3 中要麻烦些,需要在 vue.config.js 中找到

configureWebpack

在这个选项下添加插件

configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      config.plugins.push({
        apply: (compiler) => {
          compiler.hooks.done.tap(pluginName, compilation => {
            
            // 这里执行我们的代码
            const exec = require('child_process').exec

            exec('cd .\\dist && del dist.zip', function(error) {
              if (error) {
                console.log(error)
              }
              // D:\\program\\WinRAR\\WinRAR.exe 需要改为你电脑上WinRAR的安装目录
              exec('cd .\\dist && D:\\program\\WinRAR\\WinRAR.exe a -r dist.zip .\\*.*', function(error) {
                if (error) {
                  console.log(error)
                }
              })
            })
            
          });
        }
      })
    } else {
      // 为开发环境修改配置...
    }

本文内容仅供个人学习、研究或参考使用,不构成任何形式的决策建议、专业指导或法律依据。未经授权,禁止任何单位或个人以商业售卖、虚假宣传、侵权传播等非学习研究目的使用本文内容。如需分享或转载,请保留原文来源信息,不得篡改、删减内容或侵犯相关权益。感谢您的理解与支持!

链接: https://shenqiku.cn/article/FLY_8293