如何用gulp打包并上传

本文介绍了如何利用gulp进行项目打包。首先需要安装gulp插件,接着在项目中创建gulpfile.js配置文件。在gulpfile.js中编写相应任务,然后在package.json中添加执行命令。最后通过npm run deploy命令即可完成打包部署。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.首先要安装gulp插件

	npm install gulp
    npm install --save-dev gulp-ssh

2.然后在你的项目里面创建一个文件gulpfile.js
在这里插入图片描述
gulpfile.js代码如下:

   const gulp = require('gulp')
const GulpSSH = require('gulp-ssh')

// 需要上传到服务器的路径
const remotePath = 'xxxxxx'
const config = {
  ssh: {
    host: 'xxxxx', //服务器host
    port: 22, //服务器端口,这个不用改
    username: 'xxxx', //登陆服务器账号
    password: 'xxxxxx' //登陆服务器密码
  },
  remotePath,
  commands: [
    // 删除现有文件
    `rm -rf ${remotePath}`
  ]
}
const gulpSSH = new GulpSSH({
  ignoreErrors: false,
  sshConfig: config.ssh
})

/**
 * 上传前先删除服务器上现有文件...
 */
gulp.task('execSSH', () => {
  console.log('删除服务器上现有文件...')
  return gulpSSH.shell(config.commands, { filePath: 'commands.log' })
    .pipe(gulp.dest('logs'))
})

/**
 * 上传文件到服务器
 */
gulp.task('upload', () => {
  console.log('开始上传文件到服务器...')
  return gulp.src('./dist/**')
    .pipe(gulpSSH.dest(config.remotePath))
})

/**
 * 上传完成
 */
gulp.task('default', gulp.series('execSSH', 'upload', done => {
  console.log('上传完成,真香')
  done()
}))

在package.json中,添加如下命令

"scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "eslint  --ext .js,.vue src",
    "lint-fix": "eslint --fix --ext .js,.vue src",
    "upload": "gulp",
    "deploy": "vue-cli-service build && gulp"
  },

然后在终端新建命令 npm run deploy就可以了.

效果如下:
在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值