vite-plugin-deploy-oss 0.0.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 ADDED
@@ -0,0 +1,38 @@
1
+ # vite-plugin-deploy-oss
2
+
3
+ 将 dist 目录上传到 OSS
4
+
5
+ ## 介绍
6
+
7
+ `vite-plugin-deploy-oss` 是一个 Vite 插件,它可以将你的 dist 目录上传到 OSS 上。
8
+
9
+ ## 安装
10
+
11
+ ```bash
12
+ pnpm add vite-plugin-deploy-oss -D
13
+ ```
14
+
15
+ ## 使用
16
+
17
+ ```ts
18
+ // vite.config.ts
19
+ import vitePluginDeployOss from 'vite-plugin-deploy-oss'
20
+
21
+ // ...existing code...
22
+ export default {
23
+ // ...existing code...
24
+ plugins: [
25
+ // 在最后一个插件中使用
26
+ vitePluginDeployOss({
27
+ accessKeyId: '***',
28
+ accessKeySecret: '***',
29
+ bucket: '***',
30
+ region: '***',
31
+ uploadDir: `H5/zz/test`,
32
+ skip: ['**/index.html'],
33
+ // 修改打包后的资源路径
34
+ configBase: `https://oss.eventnet.cn/H5/zz/test/`,
35
+ }),
36
+ ],
37
+ }
38
+ ```
@@ -0,0 +1,19 @@
1
+ import oss from 'ali-oss';
2
+ import { Plugin } from 'vite';
3
+
4
+ type vitePluginDeployOssOption = oss.Options & {
5
+ configBase?: string;
6
+ accessKeyId: string;
7
+ accessKeySecret: string;
8
+ region?: string;
9
+ secure?: boolean;
10
+ bucket?: string;
11
+ overwrite?: boolean;
12
+ uploadDir: string;
13
+ alias?: string;
14
+ autoDelete?: boolean;
15
+ skip?: string | string[];
16
+ };
17
+ declare function vitePluginDeployOss(option: vitePluginDeployOssOption): Plugin;
18
+
19
+ export { vitePluginDeployOss as default, type vitePluginDeployOssOption };
@@ -0,0 +1,19 @@
1
+ import oss from 'ali-oss';
2
+ import { Plugin } from 'vite';
3
+
4
+ type vitePluginDeployOssOption = oss.Options & {
5
+ configBase?: string;
6
+ accessKeyId: string;
7
+ accessKeySecret: string;
8
+ region?: string;
9
+ secure?: boolean;
10
+ bucket?: string;
11
+ overwrite?: boolean;
12
+ uploadDir: string;
13
+ alias?: string;
14
+ autoDelete?: boolean;
15
+ skip?: string | string[];
16
+ };
17
+ declare function vitePluginDeployOss(option: vitePluginDeployOssOption): Plugin;
18
+
19
+ export { vitePluginDeployOss as default, type vitePluginDeployOssOption };
package/dist/index.js ADDED
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __export = (target, all) => {
9
+ for (var name in all)
10
+ __defProp(target, name, { get: all[name], enumerable: true });
11
+ };
12
+ var __copyProps = (to, from, except, desc) => {
13
+ if (from && typeof from === "object" || typeof from === "function") {
14
+ for (let key of __getOwnPropNames(from))
15
+ if (!__hasOwnProp.call(to, key) && key !== except)
16
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
17
+ }
18
+ return to;
19
+ };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
28
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
+
30
+ // src/index.ts
31
+ var src_exports = {};
32
+ __export(src_exports, {
33
+ default: () => vitePluginDeployOss
34
+ });
35
+ module.exports = __toCommonJS(src_exports);
36
+ var import_ali_oss = __toESM(require("ali-oss"));
37
+ var import_chalk = __toESM(require("chalk"));
38
+ var import_delete_empty = __toESM(require("delete-empty"));
39
+ var import_glob = require("glob");
40
+ var import_node_fs = require("fs");
41
+ var import_node_path = require("path");
42
+ var import_vite = require("vite");
43
+ function vitePluginDeployOss(option) {
44
+ const {
45
+ accessKeyId,
46
+ accessKeySecret,
47
+ region,
48
+ bucket,
49
+ configBase,
50
+ skip = "**/index.html",
51
+ uploadDir = "/",
52
+ overwrite = false,
53
+ secure = true,
54
+ autoDelete = false,
55
+ alias,
56
+ ...props
57
+ } = option || {};
58
+ let upload = false;
59
+ let outDir = "";
60
+ return {
61
+ name: "vite-plugin-deploy-oss",
62
+ apply: "build",
63
+ enforce: "post",
64
+ async config(config) {
65
+ if (!accessKeyId || !accessKeySecret || !bucket || !region) {
66
+ console.log(`:: ${import_chalk.default.red("\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570")}`);
67
+ return;
68
+ }
69
+ upload = true;
70
+ config.base = configBase || config.base;
71
+ outDir = config.build?.outDir || "dist";
72
+ return config;
73
+ },
74
+ closeBundle: {
75
+ sequential: true,
76
+ order: "post",
77
+ async handler() {
78
+ if (!upload) return;
79
+ console.log(`:: ${import_chalk.default.blue("\u5F00\u59CB\u4E0A\u4F20\u6587\u4EF6")} =>
80
+ `);
81
+ const client = new import_ali_oss.default({ region, accessKeyId, accessKeySecret, secure, bucket, ...props });
82
+ const files = (0, import_glob.globSync)(outDir + "/**/*", {
83
+ nodir: true,
84
+ ignore: Array.isArray(skip) ? skip : [skip]
85
+ });
86
+ for (const file of files) {
87
+ const filePath = (0, import_vite.normalizePath)(file);
88
+ const name = filePath.replace("dist", `${uploadDir}`);
89
+ try {
90
+ const result = await client.put(name, filePath, {
91
+ timeout: 6e5,
92
+ headers: {
93
+ "x-oss-storage-class": "Standard",
94
+ "x-oss-object-acl": "default",
95
+ "Cache-Control": "no-cache",
96
+ "x-oss-forbid-overwrite": overwrite ? "true" : "false"
97
+ }
98
+ });
99
+ if (result.res.status === 200) {
100
+ console.log(`\u4E0A\u4F20\u6210\u529F => ${import_chalk.default.green(alias ? alias + name : result.url)}`);
101
+ if (autoDelete) (0, import_node_fs.unlinkSync)(filePath);
102
+ }
103
+ } catch (error) {
104
+ console.log(`${import_chalk.default.red("\u4E0A\u4F20\u5931\u8D25")} => ${error}`);
105
+ }
106
+ }
107
+ (0, import_delete_empty.default)((0, import_node_path.resolve)(outDir));
108
+ console.log(`
109
+ :: ${import_chalk.default.blue("\u4E0A\u4F20\u5B8C\u6210")}
110
+ `);
111
+ }
112
+ }
113
+ };
114
+ }
package/dist/index.mjs ADDED
@@ -0,0 +1,83 @@
1
+ // src/index.ts
2
+ import oss from "ali-oss";
3
+ import chalk from "chalk";
4
+ import deleteEmpty from "delete-empty";
5
+ import { globSync } from "glob";
6
+ import { unlinkSync } from "node:fs";
7
+ import { resolve } from "node:path";
8
+ import { normalizePath } from "vite";
9
+ function vitePluginDeployOss(option) {
10
+ const {
11
+ accessKeyId,
12
+ accessKeySecret,
13
+ region,
14
+ bucket,
15
+ configBase,
16
+ skip = "**/index.html",
17
+ uploadDir = "/",
18
+ overwrite = false,
19
+ secure = true,
20
+ autoDelete = false,
21
+ alias,
22
+ ...props
23
+ } = option || {};
24
+ let upload = false;
25
+ let outDir = "";
26
+ return {
27
+ name: "vite-plugin-deploy-oss",
28
+ apply: "build",
29
+ enforce: "post",
30
+ async config(config) {
31
+ if (!accessKeyId || !accessKeySecret || !bucket || !region) {
32
+ console.log(`:: ${chalk.red("\u7F3A\u5C11\u5FC5\u8981\u53C2\u6570")}`);
33
+ return;
34
+ }
35
+ upload = true;
36
+ config.base = configBase || config.base;
37
+ outDir = config.build?.outDir || "dist";
38
+ return config;
39
+ },
40
+ closeBundle: {
41
+ sequential: true,
42
+ order: "post",
43
+ async handler() {
44
+ if (!upload) return;
45
+ console.log(`:: ${chalk.blue("\u5F00\u59CB\u4E0A\u4F20\u6587\u4EF6")} =>
46
+ `);
47
+ const client = new oss({ region, accessKeyId, accessKeySecret, secure, bucket, ...props });
48
+ const files = globSync(outDir + "/**/*", {
49
+ nodir: true,
50
+ ignore: Array.isArray(skip) ? skip : [skip]
51
+ });
52
+ for (const file of files) {
53
+ const filePath = normalizePath(file);
54
+ const name = filePath.replace("dist", `${uploadDir}`);
55
+ try {
56
+ const result = await client.put(name, filePath, {
57
+ timeout: 6e5,
58
+ headers: {
59
+ "x-oss-storage-class": "Standard",
60
+ "x-oss-object-acl": "default",
61
+ "Cache-Control": "no-cache",
62
+ "x-oss-forbid-overwrite": overwrite ? "true" : "false"
63
+ }
64
+ });
65
+ if (result.res.status === 200) {
66
+ console.log(`\u4E0A\u4F20\u6210\u529F => ${chalk.green(alias ? alias + name : result.url)}`);
67
+ if (autoDelete) unlinkSync(filePath);
68
+ }
69
+ } catch (error) {
70
+ console.log(`${chalk.red("\u4E0A\u4F20\u5931\u8D25")} => ${error}`);
71
+ }
72
+ }
73
+ deleteEmpty(resolve(outDir));
74
+ console.log(`
75
+ :: ${chalk.blue("\u4E0A\u4F20\u5B8C\u6210")}
76
+ `);
77
+ }
78
+ }
79
+ };
80
+ }
81
+ export {
82
+ vitePluginDeployOss as default
83
+ };
package/package.json ADDED
@@ -0,0 +1,39 @@
1
+ {
2
+ "name": "vite-plugin-deploy-oss",
3
+ "version": "0.0.1",
4
+ "main": "./dist/index.js",
5
+ "module": "./dist/index.mjs",
6
+ "types": "./dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.mjs",
10
+ "require": "./dist/index.js"
11
+ }
12
+ },
13
+ "keywords": [
14
+ "vite-plugin"
15
+ ],
16
+ "author": "yulinzhao",
17
+ "license": "MIT",
18
+ "description": "将dist目录下的文件上传到阿里云oss",
19
+ "devDependencies": {
20
+ "@types/node": "^22.10.1",
21
+ "tsup": "^8.3.5",
22
+ "typescript": "^5.7.2"
23
+ },
24
+ "peerDependencies": {
25
+ "vite": "^6.0.3"
26
+ },
27
+ "dependencies": {
28
+ "@types/ali-oss": "^6.16.11",
29
+ "@types/delete-empty": "^3.0.5",
30
+ "ali-oss": "^6.22.0",
31
+ "chalk": "^5.3.0",
32
+ "delete-empty": "^3.0.0",
33
+ "glob": "^11.0.0"
34
+ },
35
+ "scripts": {
36
+ "build": "tsup",
37
+ "pack": "pnpm run build && pnpm pack"
38
+ }
39
+ }