webpack打包自动上传到指定目录
webpack打包生成一个静态目录文件,可以通过webpack将打包的文件移到指定目录文件进行提交,自动化构建上传
编写一个webpack插件
build.after.js
const pluginName = 'ConsoleLogOnBuildWebpackPlugin'; let fs = require('fs-extra'); const process = require('process'); const exeShell = require('./shell'); class ConsoleLogOnBuildWebpackPlugin { constructor(staticDir,staticPath,remoteConfig) { this.staticDir = staticDir || 'vue'; //指定文件名 this.staticPath = staticPath || 'E:/blog/my_vue/disttest';//指定目录 if(remoteConfig) { this.remoteConfig = remoteConfig; } else { this.remoteConfig = { "username":"xxx", "password":"xxx", "host":"xxx", "port":"xxx" }; } } apply(compiler) { compiler.hooks.run.tap(pluginName, compilation => { console.log("webpack 构建过程开始!"); }); compiler.hooks.done.tap(pluginName,compilation=>{ let distPath = compilation.compilation.outputOptions.path; console.log(distPath); console.log("webpack 构建结束,自动复制代码"); var buildCDN = ()=>{ console.log('开始处理cdn文件...'); process.chdir(this.staticPath); return exeShell("git",['pull','--rebase','origin','master']).then(()=>{ console.log('更新文件成功...'); return fs.remove(this.staticPath + '/' + this.staticDir) }).then(() => { return fs.copy(distPath, this.staticPath + '/' + this.staticDir); }).then(() => { console.log('文件复制成功!'); console.log('提交静态文件...'); process.chdir(this.staticPath); return exeShell('git', ['add', '.']) }).then(function () { console.log('add success'); return exeShell('git', ['commit', '-m', 'vue-build dev_test']); }).then(function () { console.log('commit success '); return exeShell("git", ['pull', '--rebase', 'origin', 'master']); }).then(function () { console.log('pull success '); return exeShell("git", ['push', 'origin', 'master']); }).then(function () { console.log('push success '); }) }; buildCDN(); }); } } module.exports = ConsoleLogOnBuildWebpackPlugin;
shell.js
function exeShell(path,params) { return new Promise(function (res,rej) { var spawn = require('child_process').spawn; var addProcess = spawn(path,params); var resultBuffer = []; addProcess.stderr.on('data',function (data) { resultBuffer.push(data); }); addProcess.stdout.on('data',function (data) { resultBuffer.push(data); }); addProcess.on('exit',function () { res(Buffer.concat(resultBuffer).toString()) }) }); } module.exports = exeShell;
vue.config.js配置使用
const BuildAfter = require('./src/build/build.after'); chainWebpack: config => { if (process.env.NODE_ENV === 'production') { config.plugin('build-after').use(BuildAfter, ['', '', '']); } },
路径根据自己的目录定义,实现一个简单的自动化构建
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通