-
Notifications
You must be signed in to change notification settings - Fork 72
/
oss.js
111 lines (97 loc) · 3.01 KB
/
oss.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/**!
* 上传webpack编译后的文件到阿里云OSS
*
* @author [email protected]
* @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;