-
Notifications
You must be signed in to change notification settings - Fork 11
/
index.js
151 lines (125 loc) · 4.24 KB
/
index.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import color from 'picocolors'
import { globSync } from 'tinyglobby'
import path from 'path'
import OSS from 'ali-oss'
import { URL } from 'node:url'
import { normalizePath } from 'vite'
const retry = async (fn, time) => {
while (true) {
try {
await fn()
break
} catch (error) {
if (time > 0) {
time -= 1
console.error(color.red(error))
await new Promise(resolve => setTimeout(resolve, 500))
console.log('')
console.log('')
console.log(`[vite-plugin-ali-oss] retry upload after 0.5s, ${time} times left`)
console.log('')
} else {
throw new Error(error)
}
}
}
}
export default function vitePluginAliOss (options) {
let baseConfig = '/'
let buildConfig = {}
if (options.enabled !== void 0 && !options.enabled) {
return
}
return {
name: 'vite-plugin-ali-oss',
enforce: 'post',
apply: 'build',
configResolved (config) {
baseConfig = config.base
buildConfig = config.build
},
closeBundle: {
sequential: true,
order: 'post',
async handler () {
if (!/^http/i.test(baseConfig)) {
throw Error('[vite-plugin-ali-oss] base must be a url')
}
const outDirPath = normalizePath(path.resolve(normalizePath(buildConfig.outDir)))
const {pathname: ossBasePath, origin: ossOrigin} = new URL(baseConfig)
const createOssOption = Object.assign({}, options)
delete createOssOption.overwrite
delete createOssOption.ignore
delete createOssOption.headers
delete createOssOption.test
delete createOssOption.enabled
delete createOssOption.retry
const client = new OSS(createOssOption)
const ssrClient = buildConfig.ssrManifest
const ssrServer = buildConfig.ssr
const files = globSync(
outDirPath + '/**/*',
{
absolute: true,
dot: true,
ignore:
// custom ignore
options.ignore !== undefined ? options.ignore :
// ssr client ignore
ssrClient ? ['**/ssr-manifest.json', '**/*.html'] :
// ssr server ignore
ssrServer ? ['**'] :
// default ignore
'**/*.html'
}
)
console.log('')
console.log('ali oss upload start' + (ssrClient ? ' (ssr client)' : ssrServer ? ' (ssr server)' : ''))
console.log('')
const startTime = new Date().getTime()
for (const fileFullPath of files) {
const filePath = normalizePath(fileFullPath).split(outDirPath)[1] // eg: '/assets/vendor.bfb92b77.js'
const ossFilePath = ossBasePath.replace(/\/$/, '') + filePath // eg: '/base/assets/vendor.bfb92b77.js'
const completePath = ossOrigin + ossFilePath // eg: 'https://foo.com/base/assets/vendor.bfb92b77.js'
const output = `${buildConfig.outDir + filePath} => ${color.green(completePath)}`
if (options.test) {
console.log(`test upload path: ${output}`)
continue
}
if (options.overwrite) {
await retry(async () => {
await client.put(
ossFilePath,
fileFullPath,
{
headers: options.headers || {}
}
)
console.log(`upload complete: ${output}`)
}, Number(options.retry || 0))
} else {
try {
await client.head(ossFilePath);
console.log(`${color.gray('files exists')}: ${output}`)
} catch (error) {
await retry(async () => {
await client.put(
ossFilePath,
fileFullPath,
{
headers: Object.assign(options.headers || {}, { 'x-oss-forbid-overwrite': true })
}
)
console.log(`upload complete: ${output}`)
}, Number(options.retry || 0))
}
}
}
const duration = (new Date().getTime() - startTime) / 1000
console.log('')
console.log(`ali oss upload complete ^_^, cost ${duration.toFixed(2)}s`)
console.log('')
}
}
}
}