vite-plugin-spire.officejs-brook 1.0.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 ADDED
@@ -0,0 +1,44 @@
1
+ # vite-plugin-spire.officejs
2
+
3
+ 一个作用于`spire-officejs`的服务与打包插件。
4
+
5
+ # 安装
6
+
7
+ ```bash
8
+ npm install vite-plugin-spire.officejs
9
+ ```
10
+
11
+ # 使用方式
12
+
13
+ ```js
14
+ // vite.config.js
15
+ import vue from '@vitejs/plugin-vue'
16
+ import { defineConfig } from 'vite'
17
+ import officejsPlugin from 'vite-plugin-spire.officejs'
18
+
19
+ // 默认参数
20
+ export default defineConfig({
21
+ plugins: [
22
+ vue(),
23
+ officejsPlugin()
24
+ ],
25
+ })
26
+
27
+ // 带参数
28
+ export default defineConfig({
29
+ plugins: [
30
+ vue(),
31
+ officejsPlugin({
32
+ name: 'vite-plugin-spire.officejs',
33
+ port: 8000
34
+ })
35
+ ],
36
+ })
37
+ ```
38
+
39
+
40
+ # 参数
41
+ | 属性 | 说明 | 类型 | 默认 |
42
+ | ---- | -------- | -------- | ------------------------------ |
43
+ | port | 服务端口 | `number` | 8000 |
44
+ | name | 服务名称 | `string` | `'vite-plugin-spire.officejs'` |
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ declare const plugin: (...args: any[]) => any;
2
+ export default plugin;
package/lib/index.mjs ADDED
@@ -0,0 +1,112 @@
1
+ import { fileURLToPath as c } from "node:url";
2
+ import { dirname as n } from "node:path";
3
+ import l from "path";
4
+ import f from "./utils/initserver.mjs";
5
+ import m from "./utils/bundle.mjs";
6
+ import { bundleCoAuthServer as u, developCoAuthServer as b } from "./utils/coAuthServer.mjs";
7
+ n(c(import.meta.url));
8
+ function x(t = {}) {
9
+ const r = [
10
+ {
11
+ package: "spire.officejs-web-word",
12
+ path: "/spireword",
13
+ route: "/service/spireword",
14
+ name: "spireword",
15
+ build: "/service/spireword"
16
+ },
17
+ {
18
+ package: "spire.officejs-web-excel",
19
+ path: "/spireexcel",
20
+ route: "/service/spireexcel",
21
+ name: "spireexcel",
22
+ build: "/service/spireexcel"
23
+ },
24
+ {
25
+ package: "spire.officejs-web-powerpoint",
26
+ path: "/spirepowerpoint",
27
+ route: "/service/spirepowerpoint",
28
+ name: "spirepowerpoint",
29
+ build: "/service/spirepowerpoint"
30
+ },
31
+ {
32
+ package: "spire.officejs-web-pdf",
33
+ path: "/spireword",
34
+ route: "/service/spireword",
35
+ name: "pdf",
36
+ build: "/service/spireword"
37
+ },
38
+ {
39
+ package: "spire.officejs-web-common",
40
+ path: "/lib",
41
+ route: "/service/spirecommon",
42
+ name: "spirecommon",
43
+ build: "/service/spirecommon"
44
+ },
45
+ {
46
+ package: "spire.officejs-web-editors",
47
+ path: "/lib",
48
+ route: "/editors",
49
+ name: "editors",
50
+ build: "/editors"
51
+ },
52
+ {
53
+ package: "spire.officejs-web-externs",
54
+ path: "/lib",
55
+ route: "/externs",
56
+ name: "externs",
57
+ build: "/externs"
58
+ },
59
+ {
60
+ package: "spire.officejs-web-fontsweb",
61
+ path: "/lib",
62
+ route: "/fontsweb",
63
+ name: "fontsweb",
64
+ build: "/fontsweb"
65
+ }
66
+ ], o = [
67
+ {
68
+ package: "spire.officejs-service-win-brook",
69
+ path: "/lib",
70
+ name: "service-windows",
71
+ platform: "win32",
72
+ build: "/"
73
+ },
74
+ {
75
+ package: "spire.officejs-service-linux-brook",
76
+ path: "/lib",
77
+ name: "service-linux",
78
+ platform: "linux",
79
+ build: "/"
80
+ },
81
+ {
82
+ package: "spire.officejs-service-arm-brook",
83
+ path: "/lib",
84
+ name: "service-arm",
85
+ platform: "arm",
86
+ build: "/"
87
+ }
88
+ ], {
89
+ port: a = 7e3,
90
+ name: p = "vite-plugin-spire.officejs"
91
+ } = t;
92
+ let s = null, i = "";
93
+ return {
94
+ name: p,
95
+ configResolved(e) {
96
+ s = e, i = e.root, l.resolve(i, e.build.outDir);
97
+ },
98
+ // 开发环境
99
+ // 创建一个子进程来提供officeJS的静态资源
100
+ configureServer(e) {
101
+ f(e, r, a, p), b(e, i, o, s);
102
+ },
103
+ // 生产环境
104
+ // 将officeJS的静态资源拷贝到输出目录中
105
+ async writeBundle(e, d) {
106
+ await m(e, i, r), await u(e, i, o);
107
+ }
108
+ };
109
+ }
110
+ export {
111
+ x as default
112
+ };
@@ -0,0 +1,12 @@
1
+ interface interface_options {
2
+ dir: string;
3
+ }
4
+ interface interface_mapping {
5
+ package: string;
6
+ path: string;
7
+ route: string;
8
+ name: string;
9
+ build: string;
10
+ }
11
+ declare const _default: (options: interface_options, projectRoot: string, mappings: Array<interface_mapping>) => Promise<void>;
12
+ export default _default;
@@ -0,0 +1,25 @@
1
+ import r from "path";
2
+ import { copyFile as h } from "./copyFile.mjs";
3
+ const k = [
4
+ "README.md",
5
+ "package.json",
6
+ "index.js",
7
+ "index.d.ts",
8
+ "**/SpireCloudEditor.module.js"
9
+ ], y = async (i, s, t) => {
10
+ const l = i.dir, o = "spire.officejs/web";
11
+ try {
12
+ const n = (await import("module").then((e) => e.createRequire))(s + "/node_modules/");
13
+ for (let e = 0; e < t.length; e++) {
14
+ let a = t[e].package, d = t[e].path, u = o + t[e].build;
15
+ const p = n.resolve(`${a}/package.json`), g = r.dirname(p) + d, f = r.join(l, u);
16
+ console.log(`copying ${a} ......`), await h(g, f, k);
17
+ }
18
+ console.log(`✅ successfully copied to: ${o}`);
19
+ } catch (c) {
20
+ throw c;
21
+ }
22
+ };
23
+ export {
24
+ y as default
25
+ };
@@ -0,0 +1,12 @@
1
+ interface interface_mapping {
2
+ package: string;
3
+ path: string;
4
+ name: string;
5
+ platform: string;
6
+ build: string;
7
+ }
8
+ export declare const developCoAuthServer: (viteServer: any, root: string, serviceMapping: Array<interface_mapping>, options: any) => void;
9
+ export declare const bundleCoAuthServer: (options: {
10
+ dir: string;
11
+ }, projectRoot: string, mappings: Array<interface_mapping>) => Promise<void>;
12
+ export {};
@@ -0,0 +1,12 @@
1
+ interface interface_mapping {
2
+ package: string;
3
+ path: string;
4
+ name: string;
5
+ platform: string;
6
+ build: string;
7
+ }
8
+ export declare const developCoAuthServer: (viteServer: any, root: string, serviceMapping: Array<interface_mapping>, options: any) => void;
9
+ export declare const bundleCoAuthServer: (options: {
10
+ dir: string;
11
+ }, projectRoot: string, mappings: Array<interface_mapping>) => Promise<void>;
12
+ export {};
@@ -0,0 +1,87 @@
1
+ import { fileURLToPath as y } from "node:url";
2
+ import { dirname as d, resolve as s, join as O } from "node:path";
3
+ import { spawn as b } from "node:child_process";
4
+ import { copyFile as _, copySingleFile as D } from "./copyFile.mjs";
5
+ import { chmodSync as C } from "node:fs";
6
+ const I = y(import.meta.url);
7
+ d(I);
8
+ const w = "spire.officejs/service", P = "spire.officejs-server-config.json", A = w + "/OnlineServer/config", M = A + "/default.json", W = "win32";
9
+ let c = null, i, F, S, $, u, m;
10
+ const G = async (n, a, r) => {
11
+ var o, p;
12
+ const e = x(a);
13
+ try {
14
+ try {
15
+ const t = await import.meta.resolve(
16
+ `${e.package}/package.json`
17
+ );
18
+ i = d(t);
19
+ } catch {
20
+ i = s(process.cwd(), "node_modules", e.package);
21
+ }
22
+ i.startsWith("file://") || (i.startsWith("/") || i.match(/^[A-Za-z]:/)) && (i = "file://" + (i.startsWith("/") ? "" : "/") + i);
23
+ const f = y(i), h = s(n, P), l = R();
24
+ console.log("platform", l), l != "win32" ? (u = "OfficeServer/sources", $ = "officeserver-linux") : ($ = "OnlineServer", u = "officeserver-win.exe"), F = f + e.path, S = s(F, u), m = s(S, u);
25
+ const g = await T(
26
+ r,
27
+ n,
28
+ e
29
+ );
30
+ if (l != "win32")
31
+ try {
32
+ C(m, 493);
33
+ } catch (t) {
34
+ console.error(t);
35
+ }
36
+ c = b(m, [], {
37
+ stdio: "pipe",
38
+ detached: !1,
39
+ cwd: S,
40
+ env: {
41
+ ...process.env,
42
+ NODE_CONFIG_DIR: g
43
+ }
44
+ }), console.log(`Officeserver started with PID: ${c.pid}`), (o = c.stdout) == null || o.on("data", (t) => {
45
+ console.log(`[officeserver] ${t.toString().trim()}`);
46
+ }), (p = c.stderr) == null || p.on("data", (t) => {
47
+ console.error(`[${e.package}] ${t.toString().trim()}`);
48
+ }), c.on("close", (t) => {
49
+ console.log(`${e.package} process exited with code ${t}`), c = null;
50
+ }), c.on("error", (t) => {
51
+ console.error("Failed to start ${mapping.package}:", t), c = null;
52
+ });
53
+ } catch (f) {
54
+ console.error(
55
+ `${e.package} executable not found at ${m}:`,
56
+ f
57
+ );
58
+ }
59
+ }, J = () => {
60
+ c && (c.kill("SIGTERM"), c = null);
61
+ }, x = (n) => {
62
+ const a = R();
63
+ let r = n.filter((e) => e.platform == a)[0];
64
+ return r || (r = n.filter((e) => e.platform == W)[0]), r;
65
+ }, R = () => process && process.platform, T = async (n, a, r) => {
66
+ const e = s(a, P), o = r.package, p = r.path, l = (await import("module").then((k) => k.createRequire))(a + "/node_modules/").resolve(`${o}/package.json`), g = d(l) + p, t = s(g, "OnlineServer/config"), v = s(t, "default.json");
67
+ return await D(e, v), t;
68
+ }, H = (n, a, r, e) => {
69
+ var o;
70
+ G(a, r, e), (o = n.httpServer) == null || o.on("close", () => {
71
+ J();
72
+ });
73
+ }, K = async (n, a, r) => {
74
+ const e = x(r);
75
+ try {
76
+ const o = n.dir, p = s(o, w), h = (await import("module").then((N) => N.createRequire))(a + "/node_modules/"), l = s(a, P), g = s(o, M);
77
+ let t = e.package, v = e.path, k = w + e.build;
78
+ const q = h.resolve(`${t}/package.json`), E = d(q) + v, j = O(o, k);
79
+ console.log(`copying ${t} ......`), await _(E, j, ["logs"]), await D(l, g), console.log(`✅ successfully copied to: ${p}`);
80
+ } catch (o) {
81
+ throw new Error(o);
82
+ }
83
+ };
84
+ export {
85
+ K as bundleCoAuthServer,
86
+ H as developCoAuthServer
87
+ };
@@ -0,0 +1,7 @@
1
+ /**
2
+ * 复制单个文件
3
+ * @param src 源文件路径
4
+ * @param dest 目标文件路径
5
+ */
6
+ export declare const copySingleFile: (src: string, dest: string) => Promise<void>;
7
+ export declare const copyFile: (src: string, dest: string, excludes: Array<string>) => Promise<void>;
@@ -0,0 +1,17 @@
1
+ import { readdir as m, mkdir as u, cp as l } from "fs/promises";
2
+ import { join as c, relative as d, dirname as y } from "path";
3
+ const g = async (e, i) => {
4
+ const r = y(i);
5
+ await u(r, { recursive: !0 }), await l(e, i, { recursive: !0 });
6
+ }, w = async (e, i, r) => {
7
+ const f = await m(e, { withFileTypes: !0 });
8
+ await u(i, { recursive: !0 });
9
+ for (const o of f) {
10
+ const s = c(e, o.name), n = c(i, o.name), a = d(e, s);
11
+ r.some((t) => typeof t == "string" ? a.includes(t) || o.name === t : t instanceof RegExp ? t.test(a) : !1) || (o.isDirectory() ? await w(s, n, r) : await l(s, n, { recursive: !0 }));
12
+ }
13
+ };
14
+ export {
15
+ w as copyFile,
16
+ g as copySingleFile
17
+ };
@@ -0,0 +1,8 @@
1
+ interface mapping_inter {
2
+ package: string;
3
+ path: string;
4
+ route: string;
5
+ name: string;
6
+ }
7
+ declare const _default: (viteServer: any, mappings: Array<mapping_inter>, port: Number, name: String) => void;
8
+ export default _default;
@@ -0,0 +1,60 @@
1
+ import { createServer as S } from "node:http";
2
+ import { fileURLToPath as j } from "node:url";
3
+ import { dirname as x, resolve as y, join as P } from "node:path";
4
+ import { access as k } from "node:fs/promises";
5
+ import { statSync as T, createReadStream as C } from "node:fs";
6
+ const A = (f, i, c, g) => {
7
+ var p;
8
+ let r;
9
+ r = S(async (a, t) => {
10
+ var d;
11
+ const l = new URL(a.url, `http://${a.headers.host}`).pathname;
12
+ let o = {};
13
+ if (l.includes("Spire.Pdf.Base") ? o = i.find((e) => e.name == "pdf") : o = i.find((e) => l.startsWith(e.route)), !o) {
14
+ t.writeHead(404, { "Content-Type": "text/plain" }), t.end("Resource not found");
15
+ return;
16
+ }
17
+ t.setHeader("Access-Control-Allow-Origin", "*");
18
+ try {
19
+ let e;
20
+ try {
21
+ const s = await import.meta.resolve(`${o.package}/package.json`);
22
+ e = x(s);
23
+ } catch {
24
+ e = y(process.cwd(), "node_modules", o.package);
25
+ }
26
+ e.startsWith("file://") || (e.startsWith("/") || e.match(/^[A-Za-z]:/)) && (e = "file://" + (e.startsWith("/") ? "" : "/") + e);
27
+ const u = j(e), v = l.replace(o.route, "") || "/index.html", n = P(u, o.path, v);
28
+ await k(n);
29
+ const w = T(n), h = (d = n.split(".").pop()) == null ? void 0 : d.toLowerCase(), $ = {
30
+ html: "text/html",
31
+ js: "application/javascript",
32
+ css: "text/css",
33
+ json: "application/json",
34
+ png: "image/png",
35
+ jpg: "image/jpeg",
36
+ svg: "image/svg+xml",
37
+ wasm: "application/wasm"
38
+ };
39
+ t.writeHead(200, {
40
+ "Content-Type": h && $[h] || "application/octet-stream",
41
+ "Content-Length": w.size
42
+ });
43
+ const m = C(n);
44
+ m.pipe(t), m.on("error", (s) => {
45
+ console.error(`Stream error for ${n}:`, s), t.writeHead(500), t.end("Internal server error");
46
+ });
47
+ } catch (e) {
48
+ e.code === "ENOENT" ? (t.writeHead(404, { "Content-Type": "text/plain" }), t.end(`File not found: ${a.url}`)) : (console.error(`Error serving ${a.url}:`, e), t.writeHead(500), t.end("Internal server error"));
49
+ }
50
+ }), r.listen(c, () => {
51
+ console.log(`📦 [${g}] The Spire.OfficeJS static resource service has been started.`), console.log(` - Service address: http://localhost:${c}`), i.forEach((a) => {
52
+ console.log(` - Mapping ${a.package}${a.path} -> http://localhost:${c}${a.route}`);
53
+ });
54
+ }), (p = f.httpServer) == null || p.on("close", () => {
55
+ r == null || r.close();
56
+ });
57
+ };
58
+ export {
59
+ A as default
60
+ };
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "vite-plugin-spire.officejs-brook",
3
+ "version": "1.0.0",
4
+ "description": "",
5
+ "main": "./lib/index.mjs",
6
+ "files": [
7
+ "lib"
8
+ ],
9
+ "keywords": [
10
+ "vite",
11
+ "spire.officejs"
12
+ ],
13
+ "author": "",
14
+ "license": "MIT",
15
+ "type": "module",
16
+ "types": "./lib/index.d.ts"
17
+ }