vite-plugin-deploy-oss 3.3.1 → 3.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,75 +1,130 @@
1
1
  # vite-plugin-deploy-oss
2
2
 
3
- dist 目录上传到 OSS
3
+ [![npm version](https://img.shields.io/npm/v/vite-plugin-deploy-oss.svg?style=flat-square)](https://www.npmjs.com/package/vite-plugin-deploy-oss)
4
+ [![npm downloads](https://img.shields.io/npm/dm/vite-plugin-deploy-oss.svg?style=flat-square)](https://www.npmjs.com/package/vite-plugin-deploy-oss)
5
+ [![npm license](https://img.shields.io/npm/l/vite-plugin-deploy-oss.svg?style=flat-square)](https://www.npmjs.com/package/vite-plugin-deploy-oss)
4
6
 
5
- ## 介绍
7
+ Upload Vite build artifacts to Aliyun OSS.
8
+ [![npm version](https://img.shields.io/npm/v/vite-plugin-deploy-oss.svg?style=flat-square)](https://www.npmjs.com/package/vite-plugin-deploy-oss)
9
+ [![npm downloads](https://img.shields.io/npm/dm/vite-plugin-deploy-oss.svg?style=flat-square)](https://www.npmjs.com/package/vite-plugin-deploy-oss)
10
+ [![npm license](https://img.shields.io/npm/l/vite-plugin-deploy-oss.svg?style=flat-square)](https://www.npmjs.com/package/vite-plugin-deploy-oss)
6
11
 
7
- `vite-plugin-deploy-oss` 是一个 Vite 插件,它可以将你的 dist 目录上传到 OSS 上。
12
+ Upload Vite build artifacts to Aliyun OSS.
8
13
 
9
- ## 安装
14
+ ## Installation
15
+
16
+ ## Installation
10
17
 
11
18
  ```bash
12
19
  pnpm add vite-plugin-deploy-oss -D
13
20
  ```
14
21
 
15
- ## 调试模式
16
-
17
- ```bash
18
- pnpm run build:test:debug
19
- ```
22
+ ## Quick Start
20
23
 
21
- 这会进入带调试信息的上传模式,构建结束后额外输出每个关键步骤花了多久。
24
+ ## Quick Start
22
25
 
23
- ## 使用
26
+ It is recommended to control the deployment using environment variables to avoid accidental uploads during local builds.
27
+ It is recommended to control the deployment using environment variables to avoid accidental uploads during local builds.
24
28
 
25
29
  ```ts
26
30
  // vite.config.ts
31
+ import { defineConfig } from 'vite'
27
32
  import vitePluginDeployOss from 'vite-plugin-deploy-oss'
28
33
 
29
- // ...existing code...
30
- export default {
31
- // ...existing code...
34
+ export default defineConfig({
32
35
  plugins: [
33
- // 在最后一个插件中使用
34
36
  vitePluginDeployOss({
35
- // 建议按环境变量开关上传,避免本地/CI误上传
36
37
  open: process.env.DEPLOY_OSS === '1',
37
- // 输出调试耗时信息,方便排查慢在哪里
38
- debug: process.env.DEPLOY_OSS_DEBUG === '1',
39
- // 终端实时动效进度面板(默认 true)
40
- fancy: true,
41
-
42
- accessKeyId: '***',
43
- accessKeySecret: '***',
44
- bucket: '***',
45
- region: '***',
46
- uploadDir: `H5/zz/test`,
47
- skip: ['**/index.html'],
48
-
49
- // 默认 true:有上传失败时抛错并让构建失败
50
- failOnError: true,
51
38
 
52
- // 生成并上传 OSS 汇总文件
53
- manifest: true,
39
+ accessKeyId: process.env.OSS_ACCESS_KEY_ID || '',
40
+ accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET || '',
41
+ bucket: process.env.OSS_BUCKET || '',
42
+ region: process.env.OSS_REGION || '',
54
43
 
55
- // 修改打包后的资源路径
56
- configBase: `https://oss.eventnet.cn/H5/zz/test/`,
44
+ uploadDir: 'H5/demo/prod',
45
+ configBase: 'https://example.com/H5/demo/prod/',
46
+
47
+ manifest: true,
48
+ failOnError: true,
57
49
  }),
58
50
  ],
59
- }
51
+ })
52
+ ```
53
+
54
+ Run build and deploy:
55
+ Run build and deploy:
56
+
57
+ ```bash
58
+ DEPLOY_OSS=1 pnpm build
59
+ ```
60
+
61
+ ## Configuration
62
+
63
+ ## Configuration
64
+
65
+ | Option | Default | Description |
66
+ | :---------------- | :---------------- | :--------------------------------------------------------------------------- |
67
+ | `open` | `true` | Whether to enable upload. Recommended to control with environment variables. |
68
+ | `accessKeyId` | - | OSS access key ID. |
69
+ | `accessKeySecret` | - | OSS access key secret. |
70
+ | `bucket` | - | OSS bucket name. |
71
+ | `region` | - | OSS region, e.g., `oss-cn-beijing`. |
72
+ | `uploadDir` | - | Target directory in OSS to upload files. |
73
+ | `configBase` | - | Modifies Vite's asset base path synchronously. |
74
+ | `skip` | `'**/index.html'` | Glob pattern for files to skip uploading. |
75
+ | `overwrite` | `true` | Whether to overwrite existing files on OSS with the same name. |
76
+ | `autoDelete` | `false` | Whether to delete local build files after successful upload. |
77
+ | `manifest` | `false` | Whether to generate and upload a build manifest file. |
78
+ | `failOnError` | `true` | Whether to abort the build process if upload fails. |
79
+ | `debug` | `false` | Whether to output time cost information for debugging. |
80
+ | `fancy` | `true` | Whether to display a styled terminal progress bar. |
81
+
82
+ ## Important Behaviors
83
+
84
+ - If `open: true` and any of the required options (`accessKeyId`, `accessKeySecret`, `bucket`, `region`, or `uploadDir`) are missing, the build process will fail and terminate.
85
+ - When `manifest` is enabled, all built files are uploaded automatically, and local build files are retained.
86
+ - When `manifest` is enabled, `skip` defaults to an empty array and `autoDelete` is forced to `false`.
87
+ - `oss-manifest.json` only tracks successfully uploaded files in the current build and does not include the manifest file itself.
88
+ - `configBase` affects both Vite's output asset paths and the URL addresses inside the manifest.
89
+ - `alias` only affects the URLs generated inside the manifest; it does not change the actual upload destination on OSS.
90
+
91
+ ## Important Behaviors
92
+
93
+ - If `open: true` and any of the required options (`accessKeyId`, `accessKeySecret`, `bucket`, `region`, or `uploadDir`) are missing, the build process will fail and terminate.
94
+ - When `manifest` is enabled, all built files are uploaded automatically, and local build files are retained.
95
+ - When `manifest` is enabled, `skip` defaults to an empty array and `autoDelete` is forced to `false`.
96
+ - `oss-manifest.json` only tracks successfully uploaded files in the current build and does not include the manifest file itself.
97
+ - `configBase` affects both Vite's output asset paths and the URL addresses inside the manifest.
98
+ - `alias` only affects the URLs generated inside the manifest; it does not change the actual upload destination on OSS.
99
+
100
+ ## Manifest
101
+
102
+ Enable manifest:
103
+ Enable manifest:
104
+
105
+ ```ts
106
+ vitePluginDeployOss({
107
+ // ...other options
108
+ // ...other options
109
+ manifest: true,
110
+ })
60
111
  ```
61
112
 
62
- ## 说明
113
+ Customize manifest filename:
114
+ Customize manifest filename:
63
115
 
64
- - 当前版本仅支持 ESM(`import`),不再提供 CommonJS(`require`)入口。
65
- - `open` 默认 `true`,建议通过环境变量控制开关(例如 `DEPLOY_OSS=1` 时再上传)。
66
- - `debug` 默认关闭。开启后会在结束时额外输出每个关键步骤花了多久,方便排查慢点和卡点。
67
- - `fancy` 默认 `true`,TTY 终端下会显示实时动效进度(速度、预计剩余、并发、当前文件)。
68
- - `failOnError` 默认 `true`,上传有失败会抛错,适合 CI 场景保证发布质量。
69
- - `manifest` 默认关闭。开启后会在构建目录生成并上传 `oss-manifest.json`。
70
- - `manifest: true` 时默认文件名为 `oss-manifest.json`,也支持 `manifest: { fileName: 'meta/oss-manifest.json' }` 自定义路径。
71
- - `oss-manifest.json` 仅包含本次成功上传的文件,不包含汇总文件自身。
72
- - `oss-manifest.json` 内容示例:
116
+ ```ts
117
+ vitePluginDeployOss({
118
+ // ...other options
119
+ // ...other options
120
+ manifest: {
121
+ fileName: 'meta/oss-manifest.json',
122
+ },
123
+ })
124
+ ```
125
+
126
+ Manifest JSON example:
127
+ Manifest JSON example:
73
128
 
74
129
  ```json
75
130
  {
@@ -77,10 +132,45 @@ export default {
77
132
  "files": [
78
133
  {
79
134
  "file": "assets/index-abc123.js",
80
- "key": "H5/zz/test/assets/index-abc123.js",
81
- "url": "https://oss.eventnet.cn/H5/zz/test/assets/index-abc123.js",
135
+ "key": "H5/demo/prod/assets/index-abc123.js",
136
+ "url": "https://example.com/H5/demo/prod/assets/index-abc123.js",
82
137
  "md5": "d41d8cd98f00b204e9800998ecf8427e"
83
138
  }
84
139
  ]
85
140
  }
86
141
  ```
142
+
143
+ ## Debugging
144
+
145
+ ## Debugging
146
+
147
+ Enable `debug` to log the time taken for each key step during deployment, which helps locate bottlenecks:
148
+ Enable `debug` to log the time taken for each key step during deployment, which helps locate bottlenecks:
149
+
150
+ ```ts
151
+ vitePluginDeployOss({
152
+ // ...other options
153
+ // ...other options
154
+ debug: process.env.DEPLOY_OSS_DEBUG === '1',
155
+ })
156
+ ```
157
+
158
+ You can also run the playground command in the project:
159
+ You can also run the playground command in the project:
160
+
161
+ ```bash
162
+ pnpm run build:test:debug
163
+ ```
164
+
165
+ ## Notes
166
+
167
+ ## Notes
168
+
169
+ - It is highly recommended to use environment variables instead of hardcoding sensitive credentials.
170
+ - Keep `failOnError: true` for production builds to avoid completing the build/deployment pipeline when some files failed to upload.
171
+ - Make sure you do not need the local build directory before enabling `autoDelete`.
172
+ - The current version only supports ESM (`import` syntax). CommonJS (`require`) is not supported.
173
+ - It is highly recommended to use environment variables instead of hardcoding sensitive credentials.
174
+ - Keep `failOnError: true` for production builds to avoid completing the build/deployment pipeline when some files failed to upload.
175
+ - Make sure you do not need the local build directory before enabling `autoDelete`.
176
+ - The current version only supports ESM (`import` syntax). CommonJS (`require`) is not supported.
package/dist/index.js CHANGED
@@ -262,6 +262,9 @@ function vitePluginDeployOss(option) {
262
262
  const normalizedUploadDir = normalizePathSegments(uploadDir);
263
263
  const normalizedConfigBase = configBase ? ensureTrailingSlash(normalizeUrlLikeBase(configBase)) : void 0;
264
264
  const normalizedAlias = alias ? normalizeUrlLikeBase(alias) : void 0;
265
+ const manifestFileName = resolveManifestFileName(manifest);
266
+ const effectiveAutoDelete = manifestFileName ? false : autoDelete;
267
+ const effectiveSkip = manifestFileName ? [] : Array.isArray(skip) ? skip : [skip];
265
268
  let buildFailed = false;
266
269
  let upload = false;
267
270
  let outDir = normalizePath(resolve2("dist"));
@@ -304,7 +307,7 @@ function vitePluginDeployOss(option) {
304
307
  headers
305
308
  });
306
309
  if (result.res.status === 200) {
307
- if (autoDelete) {
310
+ if (effectiveAutoDelete) {
308
311
  try {
309
312
  await unlink(task.filePath);
310
313
  } catch (error) {
@@ -505,9 +508,8 @@ function vitePluginDeployOss(option) {
505
508
  if (!open || buildFailed) return;
506
509
  const validationErrors = validateOptions();
507
510
  if (validationErrors.length > 0) {
508
- console.log(`${chalk3.red("\u2717 \u914D\u7F6E\u9519\u8BEF:")}
511
+ throw new Error(`vite-plugin-deploy-oss \u914D\u7F6E\u9519\u8BEF:
509
512
  ${validationErrors.map((err) => ` - ${err}`).join("\n")}`);
510
- return;
511
513
  }
512
514
  upload = true;
513
515
  config.base = normalizedConfigBase || config.base;
@@ -525,12 +527,11 @@ ${validationErrors.map((err) => ` - ${err}`).join("\n")}`);
525
527
  const startTime = Date.now();
526
528
  const debugEntries = [];
527
529
  const client = new oss({ region, accessKeyId, accessKeySecret, secure, bucket, ...props });
528
- const manifestFileName = resolveManifestFileName(manifest);
529
530
  const collectFilesStartedAt = Date.now();
530
531
  const files = globSync("**/*", {
531
532
  cwd: outDir,
532
533
  nodir: true,
533
- ignore: Array.isArray(skip) ? skip : [skip]
534
+ ignore: effectiveSkip
534
535
  }).map((file) => normalizePath(file)).filter((file) => file !== manifestFileName);
535
536
  debugEntries.push({
536
537
  label: "\u626B\u63CF\u672C\u5730\u6587\u4EF6",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-deploy-oss",
3
- "version": "3.3.1",
3
+ "version": "3.4.1",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
@@ -29,10 +29,10 @@
29
29
  ],
30
30
  "author": "yulinzhao",
31
31
  "license": "MIT",
32
- "description": "将dist目录下的文件上传到阿里云oss",
32
+ "description": "Upload Vite build artifacts to Aliyun OSS.",
33
33
  "devDependencies": {
34
34
  "@types/ali-oss": "^6.23.3",
35
- "@types/node": "^22.19.13",
35
+ "@types/node": "^22.19.19",
36
36
  "prettier": "3.8.1",
37
37
  "tsup": "^8.5.1",
38
38
  "typescript": "^5.9.3"
@@ -45,10 +45,10 @@
45
45
  "ali-oss": "^6.23.0",
46
46
  "chalk": "^5.6.2",
47
47
  "cli-progress": "^3.12.0",
48
- "cli-truncate": "^5.2.0",
48
+ "cli-truncate": "^6.0.0",
49
49
  "glob": "^13.0.6",
50
50
  "log-symbols": "^7.0.1",
51
- "string-width": "^8.2.0"
51
+ "string-width": "^8.2.1"
52
52
  },
53
53
  "scripts": {
54
54
  "build": "tsup",
@@ -0,0 +1,2 @@
1
+ allowBuilds:
2
+ esbuild: false