1 Star 3 Fork 4

ManerFan / vuesume

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
oss.js 3.01 KB
一键复制 编辑 原始数据 按行查看 历史
yongyong.fyy 提交于 2020-01-05 13:50 . ios不兼容的问题
/**!
* 上传webpack编译后的文件到阿里云OSS
*
* @author mfylee@163.com
* @since 2017-08-23
*/
const OSS = require('ali-oss');
const u = require('underscore');
const ConfigFileLoader = require('config-file-loader');
/**
* 读取全局配置
* ~/.aliyun
* @link https://github.com/reyesr/config-file-loader
*/
const aliyunConfig = new ConfigFileLoader.Loader().get('aliyun');
// 默认配置参数
const DEFAULT_OPTIONS = {
enable: false,
retry: 3,
filter: function (file) {
return true;
}
};
function WebpackAliyunOssPlugin(options) {
this.options = u.extend({}, DEFAULT_OPTIONS, options);
if (!this.options.enable) {
return;
}
const conf = aliyunConfig;
this.client = new OSS({
region: conf.region,
accessKeyId: conf.ak,
accessKeySecret: conf.sk,
bucket: conf.bucket
});
}
/**
* 定义插件的动作
* @param compiler
*/
WebpackAliyunOssPlugin.prototype.apply = function (compiler) {
const _this = this;
if (!_this.options.enable) {
console.log('[WebpackAliyunOssPlugin SUCCESS]: skip');
return;
}
/**
* 文件编译hook
*/
compiler.hooks.emit.tapAsync('WebpackAliyunOssPlugin', function (compilation, callback) {
// 需要上传的文件列表
const files = u.filter(u.keys(compilation.assets), _this.options.filter);
if (files.length === 0) {
return callback();
}
/**
* 上传文件
* @param file 文件(路径)
* @param times 重试次数
* @returns {Promise<OSS.PutObjectResult>}
*/
function upload(file, times) {
// 构造上传文件的buffer
const source = compilation.assets[file].source();
const body = Buffer.isBuffer(source) ? source : Buffer.from(source, 'utf8');
// 上传
return _this.client.put(file, body, {
timeout: 30 * 1000
}).then(function () {
console.log('[WebpackAliyunOssPlugin SUCCESS]:', file);
const next = files.shift();
if (next) {
// 递归
return upload(next, _this.options.retry);
}
}, function (e) {
if (times === 0) {
// 失败
throw new Error('[WebpackAliyunOssPlugin ERROR]: ', e);
} else {
// 失败重试
console.warn('[WebpackAliyunOssPlugin retry]:', times, file);
return upload(file, --times);
}
});
}
// 上传
upload(files.shift(), _this.options.retry).then(function () {
console.log('[WebpackAliyunOssPlugin FINISHED]', 'All Completed');
callback();
}).catch(function (e) {
console.error('[WebpackAliyunOssPlugin FAILED]', e);
return callback(e);
});
});
};
module.exports = WebpackAliyunOssPlugin;
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ManerFan/vuesume.git
git@gitee.com:ManerFan/vuesume.git
ManerFan
vuesume
vuesume
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891