ms-vite-plugin 0.0.6 → 0.0.8

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.
@@ -0,0 +1 @@
1
+ export declare const buildAll: (isDev: boolean | undefined, workspacePath: string) => Promise<void>;
package/dist/build.js ADDED
@@ -0,0 +1,122 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.buildAll = void 0;
40
+ const _7zip_min_1 = require("7zip-min");
41
+ const fsExtra = __importStar(require("fs-extra"));
42
+ const path = __importStar(require("path"));
43
+ const vite_1 = require("vite");
44
+ const vite_plugin_bundle_obfuscator_1 = __importDefault(require("vite-plugin-bundle-obfuscator"));
45
+ /**
46
+ * 查找入口文件:main.js/main.ts 和所有以 _thread 结尾的 JS/TS 文件
47
+ * @param src 源目录路径
48
+ * @param baseDir 基础目录路径,用于计算相对路径
49
+ * @returns 返回入口文件的相对路径列表
50
+ */
51
+ async function findEntryFiles(src, baseDir) {
52
+ const result = [];
53
+ async function walk(dir) {
54
+ const entries = await fsExtra.readdir(dir, { withFileTypes: true });
55
+ for (const entry of entries) {
56
+ const fullPath = path.resolve(dir, entry.name);
57
+ if (entry.isDirectory()) {
58
+ await walk(fullPath);
59
+ }
60
+ else if (entry.isFile()) {
61
+ // 查找 main.js/main.ts 或 _thread.js/_thread.ts 文件
62
+ if (/^main\.(js|ts)$/.test(entry.name) ||
63
+ /_thread\.(js|ts)$/.test(entry.name)) {
64
+ result.push(path.relative(baseDir, fullPath));
65
+ }
66
+ }
67
+ }
68
+ }
69
+ await walk(src);
70
+ return result;
71
+ }
72
+ const buildAll = async (isDev = true, workspacePath) => {
73
+ const obfuscatorConfig = await fsExtra.readJSON(path.join(workspacePath, "obfuscator.json"));
74
+ const outputDir = path.join(workspacePath, "msbundle");
75
+ const baseDir = path.join(workspacePath, "scripts");
76
+ const entryFiles = await findEntryFiles(baseDir, baseDir);
77
+ await fsExtra.remove(outputDir);
78
+ for (const entry of entryFiles) {
79
+ await (0, vite_1.build)({
80
+ root: workspacePath,
81
+ build: {
82
+ lib: {
83
+ entry: {
84
+ [path.basename(entry, path.extname(entry))]: `./scripts/${entry}`,
85
+ },
86
+ formats: ["cjs"],
87
+ },
88
+ outDir: "msbundle/scripts",
89
+ emptyOutDir: false, // 不要每次清空输出目录
90
+ sourcemap: isDev,
91
+ },
92
+ plugins: isDev
93
+ ? []
94
+ : [
95
+ (0, vite_plugin_bundle_obfuscator_1.default)({
96
+ autoExcludeNodeModules: true,
97
+ threadPool: true,
98
+ options: obfuscatorConfig,
99
+ }),
100
+ ],
101
+ });
102
+ }
103
+ // 打包完成后执行
104
+ await fsExtra.copy(path.join(workspacePath, "res"), path.join(workspacePath, "msbundle", "res"));
105
+ await fsExtra.copy(path.join(workspacePath, "ui"), path.join(workspacePath, "msbundle", "ui"));
106
+ const outPath = path.join(workspacePath, "msbundle");
107
+ const distPath = path.join(workspacePath, "dist");
108
+ const src = path.join(workspacePath, "package.json");
109
+ const dest = path.join(outPath, "package.json");
110
+ const packageJson = await fsExtra.readJSON(src);
111
+ delete packageJson.scripts;
112
+ delete packageJson.devDependencies;
113
+ delete packageJson.dependencies;
114
+ await fsExtra.writeJSON(dest, packageJson, { spaces: 2 });
115
+ await fsExtra.emptyDir(distPath);
116
+ const zipPath = path.join(distPath, "ms.zip");
117
+ const msbundlePath = path.join(distPath, "ms.msbundle");
118
+ await (0, _7zip_min_1.pack)(outPath + "/*", zipPath);
119
+ await fsExtra.rename(zipPath, msbundlePath);
120
+ await fsExtra.remove(outPath);
121
+ };
122
+ exports.buildAll = buildAll;
package/dist/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
package/dist/cli.js ADDED
@@ -0,0 +1,101 @@
1
+ #!/usr/bin/env node
2
+ "use strict";
3
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
4
+ if (k2 === undefined) k2 = k;
5
+ var desc = Object.getOwnPropertyDescriptor(m, k);
6
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
7
+ desc = { enumerable: true, get: function() { return m[k]; } };
8
+ }
9
+ Object.defineProperty(o, k2, desc);
10
+ }) : (function(o, m, k, k2) {
11
+ if (k2 === undefined) k2 = k;
12
+ o[k2] = m[k];
13
+ }));
14
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
15
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
16
+ }) : function(o, v) {
17
+ o["default"] = v;
18
+ });
19
+ var __importStar = (this && this.__importStar) || (function () {
20
+ var ownKeys = function(o) {
21
+ ownKeys = Object.getOwnPropertyNames || function (o) {
22
+ var ar = [];
23
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
24
+ return ar;
25
+ };
26
+ return ownKeys(o);
27
+ };
28
+ return function (mod) {
29
+ if (mod && mod.__esModule) return mod;
30
+ var result = {};
31
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
32
+ __setModuleDefault(result, mod);
33
+ return result;
34
+ };
35
+ })();
36
+ Object.defineProperty(exports, "__esModule", { value: true });
37
+ const commander_1 = require("commander");
38
+ const path = __importStar(require("path"));
39
+ const build_1 = require("./build");
40
+ const fsExtra = __importStar(require("fs-extra"));
41
+ /**
42
+ * 检查工作目录是否为有效的 MagicScript 项目
43
+ * @param workspacePath 工作目录路径
44
+ * @returns 是否为有效项目
45
+ */
46
+ async function isValidMagicScriptProject(workspacePath) {
47
+ try {
48
+ const packageJsonPath = path.join(workspacePath, "package.json");
49
+ const scriptsPath = path.join(workspacePath, "scripts");
50
+ const obfuscatorPath = path.join(workspacePath, "obfuscator.json");
51
+ const [packageExists, scriptsExists, obfuscatorExists] = await Promise.all([
52
+ fsExtra.pathExists(packageJsonPath),
53
+ fsExtra.pathExists(scriptsPath),
54
+ fsExtra.pathExists(obfuscatorPath),
55
+ ]);
56
+ return packageExists && scriptsExists && obfuscatorExists;
57
+ }
58
+ catch {
59
+ return false;
60
+ }
61
+ }
62
+ /**
63
+ * 构建命令处理函数
64
+ * @param options 命令选项
65
+ */
66
+ async function buildCommand(options) {
67
+ try {
68
+ const workspacePath = options.path
69
+ ? path.resolve(options.path)
70
+ : process.cwd();
71
+ const isDev = options.dev || false;
72
+ console.log(`🔍 检查项目目录: ${workspacePath}`);
73
+ if (!(await isValidMagicScriptProject(workspacePath))) {
74
+ console.error("❌ 错误: 当前目录不是有效的 MagicScript 项目");
75
+ console.error("请确保目录包含以下文件:");
76
+ console.error(" - package.json");
77
+ console.error(" - scripts/ 目录");
78
+ console.error(" - obfuscator.json");
79
+ process.exit(1);
80
+ }
81
+ console.log(`🚀 开始构建项目 (${isDev ? "开发模式" : "生产模式"})...`);
82
+ await (0, build_1.buildAll)(isDev, workspacePath);
83
+ console.log("✅ 构建完成!");
84
+ console.log(`📦 输出文件: ${path.join(workspacePath, "dist", "ms.msbundle")}`);
85
+ }
86
+ catch (error) {
87
+ console.error("❌ 构建失败:", error instanceof Error ? error.message : error);
88
+ process.exit(1);
89
+ }
90
+ }
91
+ // 配置 CLI 程序
92
+ commander_1.program.name("ms-cli").description("MagicScript 构建工具").version("0.0.7");
93
+ // 构建命令
94
+ commander_1.program
95
+ .command("build")
96
+ .description("构建 MagicScript 项目")
97
+ .option("-d, --dev", "开发模式构建 (包含 source map)")
98
+ .option("-p, --path <path>", "指定项目路径")
99
+ .action(buildCommand);
100
+ // 解析命令行参数
101
+ commander_1.program.parse();
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "ms-vite-plugin",
3
- "version": "0.0.6",
4
- "type": "module",
3
+ "version": "0.0.8",
4
+ "type": "commonjs",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -10,27 +10,23 @@
10
10
  "dist",
11
11
  "bin"
12
12
  ],
13
- "main": "./dist/MSPlugin.umd.cjs",
14
- "module": "./dist/MSPlugin.js",
15
- "types": "./dist/index.d.ts",
16
- "exports": {
17
- "types": "./dist/index.d.ts",
18
- "import": "./dist/MSPlugin.js",
19
- "require": "./dist/MSPlugin.umd.cjs"
13
+ "bin": {
14
+ "ms-cli": "./dist/cli.js",
15
+ "ms": "./dist/cli.js"
20
16
  },
21
17
  "scripts": {
22
- "build": "tsc && vite build",
18
+ "build": "tsc",
23
19
  "prepublishOnly": "npm run build"
24
20
  },
25
21
  "dependencies": {
26
- "@types/fs-extra": "^11.0.4",
27
22
  "7zip-min": "^2.1.0",
28
- "fs-extra": "^11.3.0"
23
+ "@types/fs-extra": "^11.0.4",
24
+ "commander": "^14.0.0",
25
+ "fs-extra": "^11.3.0",
26
+ "vite-plugin-bundle-obfuscator": "^1.8.0"
29
27
  },
30
28
  "devDependencies": {
31
29
  "@types/node": "^24.0.14",
32
- "typescript": "~5.8.3",
33
- "vite": "^7.0.0",
34
- "vite-plugin-dts": "^4.5.4"
30
+ "typescript": "~5.8.3"
35
31
  }
36
32
  }
package/dist/MSPlugin.js DELETED
@@ -1,40 +0,0 @@
1
- import i from "fs-extra";
2
- import s from "path";
3
- import * as l from "7zip-min";
4
- async function d(r, e) {
5
- const t = [];
6
- async function a(c) {
7
- const o = await i.readdir(c, { withFileTypes: !0 });
8
- for (const n of o) {
9
- const p = s.resolve(c, n.name);
10
- n.isDirectory() ? await a(p) : n.isFile() && /\.(js|ts)$/.test(n.name) && t.push(s.relative(e, p));
11
- }
12
- }
13
- return await a(r), t;
14
- }
15
- function w() {
16
- const r = s.join(process.cwd(), "scripts");
17
- return {
18
- name: "ms-main-plugin",
19
- async config(e) {
20
- const t = await d(r, r);
21
- e.build || (e.build = {}), e.build.lib = {
22
- entry: [
23
- "./scripts/main.js",
24
- ...t.map((a) => `./scripts/${a}`)
25
- ],
26
- formats: ["cjs"]
27
- }, e.build.outDir = "msbundle/scripts";
28
- },
29
- async closeBundle() {
30
- await i.copy("./res", s.join("msbundle", "res")), await i.copy("./ui", s.join("msbundle", "ui"));
31
- const e = s.join(process.cwd(), "msbundle"), t = s.join(process.cwd(), "dist"), a = s.join(process.cwd(), "package.json"), c = s.join(e, "package.json"), o = await i.readJSON(a);
32
- delete o.scripts, delete o.devDependencies, delete o.dependencies, await i.writeJSON(c, o, { spaces: 2 }), await i.emptyDir(t);
33
- const n = s.join(t, "ms.zip"), p = s.join(t, "ms.msbundle");
34
- await l.pack(e + "/*", n), await i.rename(n, p), await i.rm(e, { recursive: !0 });
35
- }
36
- };
37
- }
38
- export {
39
- w as MSMainPlugin
40
- };
@@ -1 +0,0 @@
1
- (function(o,n){typeof exports=="object"&&typeof module<"u"?n(exports,require("fs-extra"),require("path"),require("7zip-min")):typeof define=="function"&&define.amd?define(["exports","fs-extra","path","7zip-min"],n):(o=typeof globalThis<"u"?globalThis:o||self,n(o.MSPlugin={},o.fsExtra,o.path,o._7z))})(this,function(o,n,s,l){"use strict";function p(t){const e=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(t){for(const i in t)if(i!=="default"){const c=Object.getOwnPropertyDescriptor(t,i);Object.defineProperty(e,i,c.get?c:{enumerable:!0,get:()=>t[i]})}}return e.default=t,Object.freeze(e)}const m=p(l);async function f(t,e){const i=[];async function c(u){const a=await n.readdir(u,{withFileTypes:!0});for(const r of a){const d=s.resolve(u,r.name);r.isDirectory()?await c(d):r.isFile()&&/\.(js|ts)$/.test(r.name)&&i.push(s.relative(e,d))}}return await c(t),i}function j(){const t=s.join(process.cwd(),"scripts");return{name:"ms-main-plugin",async config(e){const i=await f(t,t);e.build||(e.build={}),e.build.lib={entry:["./scripts/main.js",...i.map(c=>`./scripts/${c}`)],formats:["cjs"]},e.build.outDir="msbundle/scripts"},async closeBundle(){await n.copy("./res",s.join("msbundle","res")),await n.copy("./ui",s.join("msbundle","ui"));const e=s.join(process.cwd(),"msbundle"),i=s.join(process.cwd(),"dist"),c=s.join(process.cwd(),"package.json"),u=s.join(e,"package.json"),a=await n.readJSON(c);delete a.scripts,delete a.devDependencies,delete a.dependencies,await n.writeJSON(u,a,{spaces:2}),await n.emptyDir(i);const r=s.join(i,"ms.zip"),d=s.join(i,"ms.msbundle");await m.pack(e+"/*",r),await n.rename(r,d),await n.rm(e,{recursive:!0})}}}o.MSMainPlugin=j,Object.defineProperty(o,Symbol.toStringTag,{value:"Module"})});
package/dist/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- import { Plugin } from 'vite';
2
- export declare function MSMainPlugin(): Plugin;