vite-plugin-deploy-oss 3.3.1 → 3.4.0

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,10 +1,6 @@
1
1
  # vite-plugin-deploy-oss
2
2
 
3
- dist 目录上传到 OSS
4
-
5
- ## 介绍
6
-
7
- `vite-plugin-deploy-oss` 是一个 Vite 插件,它可以将你的 dist 目录上传到 OSS 上。
3
+ Vite 打包后的文件上传到阿里云 OSS
8
4
 
9
5
  ## 安装
10
6
 
@@ -12,64 +8,92 @@
12
8
  pnpm add vite-plugin-deploy-oss -D
13
9
  ```
14
10
 
15
- ## 调试模式
11
+ ## 快速开始
16
12
 
17
- ```bash
18
- pnpm run build:test:debug
19
- ```
20
-
21
- 这会进入带调试信息的上传模式,构建结束后额外输出每个关键步骤花了多久。
22
-
23
- ## 使用
13
+ 推荐用环境变量控制上传,避免本地随手打包时误上传。
24
14
 
25
15
  ```ts
26
16
  // vite.config.ts
17
+ import { defineConfig } from 'vite'
27
18
  import vitePluginDeployOss from 'vite-plugin-deploy-oss'
28
19
 
29
- // ...existing code...
30
- export default {
31
- // ...existing code...
20
+ export default defineConfig({
32
21
  plugins: [
33
- // 在最后一个插件中使用
34
22
  vitePluginDeployOss({
35
- // 建议按环境变量开关上传,避免本地/CI误上传
36
23
  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
24
 
52
- // 生成并上传 OSS 汇总文件
53
- manifest: true,
25
+ accessKeyId: process.env.OSS_ACCESS_KEY_ID || '',
26
+ accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET || '',
27
+ bucket: process.env.OSS_BUCKET || '',
28
+ region: process.env.OSS_REGION || '',
54
29
 
55
- // 修改打包后的资源路径
56
- configBase: `https://oss.eventnet.cn/H5/zz/test/`,
30
+ uploadDir: 'H5/demo/prod',
31
+ configBase: 'https://example.com/H5/demo/prod/',
32
+
33
+ manifest: true,
34
+ failOnError: true,
57
35
  }),
58
36
  ],
59
- }
37
+ })
38
+ ```
39
+
40
+ 发布时执行:
41
+
42
+ ```bash
43
+ DEPLOY_OSS=1 pnpm build
44
+ ```
45
+
46
+ ## 常用配置
47
+
48
+ | 配置 | 默认值 | 说明 |
49
+ | ----------------- | ----------------- | ---------------------------------- |
50
+ | `open` | `true` | 是否开启上传。建议用环境变量控制。 |
51
+ | `accessKeyId` | - | OSS 访问密钥。 |
52
+ | `accessKeySecret` | - | OSS 访问密钥。 |
53
+ | `bucket` | - | OSS bucket 名称。 |
54
+ | `region` | - | OSS 区域,例如 `oss-cn-beijing`。 |
55
+ | `uploadDir` | - | 文件上传到 OSS 的目标目录。 |
56
+ | `configBase` | - | 同步修改 Vite 的资源访问路径。 |
57
+ | `skip` | `'**/index.html'` | 不上传的文件规则。 |
58
+ | `overwrite` | `true` | 是否允许覆盖远端同名文件。 |
59
+ | `autoDelete` | `false` | 上传成功后是否删除本地构建文件。 |
60
+ | `manifest` | `false` | 是否生成并上传文件清单。 |
61
+ | `failOnError` | `true` | 上传失败时是否中断构建。 |
62
+ | `debug` | `false` | 是否输出耗时信息。 |
63
+ | `fancy` | `true` | 是否显示更友好的终端进度。 |
64
+
65
+ ## 重要行为
66
+
67
+ - `open: true` 时,如果缺少 `accessKeyId`、`accessKeySecret`、`bucket`、`region` 或 `uploadDir`,会直接中断构建。
68
+ - `manifest` 开启后,会自动上传全部文件,并自动保留本地构建文件。
69
+ - `manifest` 开启后,`skip` 会按空数组处理,`autoDelete` 会按 `false` 处理。
70
+ - `oss-manifest.json` 只记录本次成功上传的文件,不包含清单文件自身。
71
+ - `configBase` 会影响 Vite 打包后的资源路径,也会影响清单里的访问地址。
72
+ - `alias` 只影响清单里生成的访问地址,不会改变实际上传路径。
73
+
74
+ ## Manifest
75
+
76
+ 开启:
77
+
78
+ ```ts
79
+ vitePluginDeployOss({
80
+ // ...其他配置
81
+ manifest: true,
82
+ })
60
83
  ```
61
84
 
62
- ## 说明
85
+ 自定义文件名:
63
86
 
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` 内容示例:
87
+ ```ts
88
+ vitePluginDeployOss({
89
+ // ...其他配置
90
+ manifest: {
91
+ fileName: 'meta/oss-manifest.json',
92
+ },
93
+ })
94
+ ```
95
+
96
+ 清单内容示例:
73
97
 
74
98
  ```json
75
99
  {
@@ -77,10 +101,34 @@ export default {
77
101
  "files": [
78
102
  {
79
103
  "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",
104
+ "key": "H5/demo/prod/assets/index-abc123.js",
105
+ "url": "https://example.com/H5/demo/prod/assets/index-abc123.js",
82
106
  "md5": "d41d8cd98f00b204e9800998ecf8427e"
83
107
  }
84
108
  ]
85
109
  }
86
110
  ```
111
+
112
+ ## 调试
113
+
114
+ 开启 `debug` 后,构建结束会额外输出关键步骤耗时,方便判断慢在哪里。
115
+
116
+ ```ts
117
+ vitePluginDeployOss({
118
+ // ...其他配置
119
+ debug: process.env.DEPLOY_OSS_DEBUG === '1',
120
+ })
121
+ ```
122
+
123
+ 也可以使用项目内的 playground 命令:
124
+
125
+ ```bash
126
+ pnpm run build:test:debug
127
+ ```
128
+
129
+ ## 注意事项
130
+
131
+ - 建议不要在配置里直接写真实密钥,优先使用环境变量。
132
+ - 建议生产发布时保持 `failOnError: true`,避免部分文件没上传却继续完成流程。
133
+ - 如果开启 `autoDelete`,请确认不需要保留本地构建文件。
134
+ - 当前版本仅支持 ESM,也就是 `import` 用法,不提供 `require` 入口。
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.0",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
6
6
  "type": "module",
@@ -32,7 +32,7 @@
32
32
  "description": "将dist目录下的文件上传到阿里云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