@vureact/compiler-core 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/lib/cli.d.cts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/lib/cli.d.ts ADDED
@@ -0,0 +1,2 @@
1
+
2
+ export { }
package/lib/cli.esm.js ADDED
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * @vureact/compiler-core v1.0.0
4
+ * (c) 2025-present Ruihong Zhong (Ryan John)
5
+ * @license MIT
6
+ */
7
+
8
+ import {
9
+ Helper,
10
+ VuReact,
11
+ bin,
12
+ calcElapsedTime,
13
+ normalizePath,
14
+ version
15
+ } from "./chunk-U72HWKIZ.esm.js";
16
+
17
+ // src/cli/index.ts
18
+ import { cac } from "cac";
19
+
20
+ // src/cli/action.ts
21
+ import chokidar from "chokidar";
22
+ import { existsSync } from "fs";
23
+ import kleur from "kleur";
24
+ import ora from "ora";
25
+ import path from "path";
26
+ import { pathToFileURL } from "url";
27
+ async function resolveAction(root, options) {
28
+ const projectRoot = root ? path.resolve(process.cwd(), root) : process.cwd();
29
+ const userConfig = await loadUserConfig(projectRoot);
30
+ const finalConfig = mergeConfig(projectRoot, options, userConfig);
31
+ const compiler = new VuReact(finalConfig);
32
+ await compiler.execute();
33
+ if (finalConfig.watch) {
34
+ setupWatcher(compiler, finalConfig);
35
+ console.info(
36
+ kleur.dim(`
37
+ ${(/* @__PURE__ */ new Date()).toLocaleTimeString()}`),
38
+ kleur.bold(kleur.magenta("[hrm]")),
39
+ kleur.gray(`Watching for file changes...
40
+ `)
41
+ );
42
+ }
43
+ }
44
+ async function loadUserConfig(root) {
45
+ const configPath = path.resolve(root, "vureact.config.js");
46
+ if (!existsSync(configPath)) return {};
47
+ try {
48
+ const configUrl = pathToFileURL(configPath).href;
49
+ const module = await import(configUrl);
50
+ return module.default || module;
51
+ } catch (err) {
52
+ console.warn(
53
+ kleur.yellow("\n\u26A0"),
54
+ `Load config failed at ${configPath}, using default options`,
55
+ err
56
+ );
57
+ return {};
58
+ }
59
+ }
60
+ function mergeConfig(projectRoot, options, userConfig) {
61
+ const merged = {
62
+ ...userConfig,
63
+ ...options,
64
+ root: projectRoot
65
+ };
66
+ if (options.exclude) {
67
+ merged.exclude = Array.isArray(options.exclude) ? options.exclude : [options.exclude];
68
+ } else if (userConfig.exclude) {
69
+ merged.exclude = userConfig.exclude;
70
+ }
71
+ merged.output = {
72
+ ...userConfig.output,
73
+ workspace: options.workspace ?? userConfig.output?.workspace,
74
+ outDir: options.outDir ?? userConfig.output?.outDir,
75
+ bootstrapVite: options.bootstrapVite ?? userConfig.output?.bootstrapVite
76
+ };
77
+ merged.format = {
78
+ enabled: options.format ?? userConfig.format?.enabled,
79
+ formatter: options.formatter ?? userConfig.format?.formatter
80
+ };
81
+ return merged;
82
+ }
83
+ function setupWatcher(compiler, config) {
84
+ const spinner = ora();
85
+ const cmpHelper = new Helper(config);
86
+ const watcher = chokidar.watch(cmpHelper.getInputPath(), {
87
+ ignored: cmpHelper.getExcludes(),
88
+ persistent: true,
89
+ ignoreInitial: true
90
+ // 初始扫描已由 compiler.execute 完成
91
+ });
92
+ watcher.on("all", async (event, filePath) => {
93
+ switch (event) {
94
+ case "add":
95
+ case "change":
96
+ await onRecompile(event, filePath);
97
+ break;
98
+ case "unlink":
99
+ case "unlinkDir":
100
+ await onRemoveFile(event, filePath);
101
+ break;
102
+ }
103
+ });
104
+ const processors = {
105
+ ".vue": (p) => compiler.processSFC(p),
106
+ ".js": (p) => compiler.processScript(p),
107
+ ".ts": (p) => compiler.processScript(p)
108
+ };
109
+ const onRecompile = async (event, filePath) => {
110
+ const ext = path.extname(filePath);
111
+ if (ext in processors) {
112
+ spinner.start("Recompiling...");
113
+ const startTime = performance.now();
114
+ const fn = processors[ext];
115
+ const unit = await fn(filePath);
116
+ cmpHelper.printCoreLogs();
117
+ cmpHelper.printCompileInfo(filePath, calcElapsedTime(startTime));
118
+ if (unit) {
119
+ await config.onChange?.(event, unit);
120
+ }
121
+ } else {
122
+ spinner.start("Updating assets...");
123
+ await compiler.processAsset(filePath);
124
+ cmpHelper.print(
125
+ kleur.blue("Copied Asset"),
126
+ kleur.dim(normalizePath(cmpHelper.relativePath(filePath)))
127
+ );
128
+ }
129
+ spinner.stop();
130
+ };
131
+ const onRemoveFile = async (type, filePath) => {
132
+ const ext = path.extname(filePath);
133
+ const removeFile = async (type2) => {
134
+ await compiler.removeOutputPath(filePath, type2);
135
+ cmpHelper.print(
136
+ kleur.yellow("Removed"),
137
+ kleur.dim(normalizePath(cmpHelper.relativePath(filePath)))
138
+ );
139
+ };
140
+ if (type === "unlink") {
141
+ if (ext === ".vue") {
142
+ await removeFile("sfc" /* SFC */);
143
+ return;
144
+ }
145
+ if (ext === ".js" || ext === ".ts") {
146
+ await removeFile("script" /* SCRIPT */);
147
+ return;
148
+ }
149
+ await removeFile("copied" /* ASSET */);
150
+ return;
151
+ }
152
+ if (type === "unlinkDir") {
153
+ await removeFile("sfc" /* SFC */);
154
+ await removeFile("script" /* SCRIPT */);
155
+ await removeFile("copied" /* ASSET */);
156
+ }
157
+ };
158
+ }
159
+
160
+ // src/cli/option.ts
161
+ function resolveOptions(command) {
162
+ return command.option("-i, --input <dir>", "Input directory (relative to root)").option("-o, --outDir <dir>", "Output directory name").option("--workspace <dir>", "The workspace directory for cache and output").option("--bootstrapVite", "Enable Vite to initialize a standard React project environment.").option("--exclude <pattern>", "Exclude files/directories (glob pattern)").option("--no-recursive", "Disable recursive search in subdirectories").option("--no-cache", "Disable cache", { default: void 0 }).option("--format", "Enable code formatting", { default: void 0 }).option("--formatter <type>", 'Choose formatter: "prettier" or "builtin"');
163
+ }
164
+
165
+ // src/cli/index.ts
166
+ var [programName] = Object.keys(bin);
167
+ var cli = cac(programName);
168
+ var buildCommand = cli.command("build [root]", "Compile Vue3 to React (one-time)");
169
+ resolveOptions(buildCommand).action((root, options) => {
170
+ resolveAction(root, { ...options, watch: false });
171
+ });
172
+ var watchCommand = cli.command("watch [root]", "Compile Vue3 to React and watch for changes");
173
+ resolveOptions(watchCommand).action((root, options) => {
174
+ resolveAction(root, { ...options, watch: true });
175
+ });
176
+ cli.help().version(version).parse();
package/lib/cli.js ADDED
@@ -0,0 +1,176 @@
1
+ #!/usr/bin/env node
2
+ "use strict"; function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }/**
3
+ * @vureact/compiler-core v1.0.0
4
+ * (c) 2025-present Ruihong Zhong (Ryan John)
5
+ * @license MIT
6
+ */
7
+
8
+
9
+
10
+
11
+
12
+
13
+
14
+
15
+ var _chunk5M5CTYQRjs = require('./chunk-5M5CTYQR.js');
16
+
17
+ // src/cli/index.ts
18
+ var _cac = require('cac');
19
+
20
+ // src/cli/action.ts
21
+ var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
22
+ var _fs = require('fs');
23
+ var _kleur = require('kleur'); var _kleur2 = _interopRequireDefault(_kleur);
24
+ var _ora = require('ora'); var _ora2 = _interopRequireDefault(_ora);
25
+ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
26
+ var _url = require('url');
27
+ async function resolveAction(root, options) {
28
+ const projectRoot = root ? _path2.default.resolve(process.cwd(), root) : process.cwd();
29
+ const userConfig = await loadUserConfig(projectRoot);
30
+ const finalConfig = mergeConfig(projectRoot, options, userConfig);
31
+ const compiler = new (0, _chunk5M5CTYQRjs.VuReact)(finalConfig);
32
+ await compiler.execute();
33
+ if (finalConfig.watch) {
34
+ setupWatcher(compiler, finalConfig);
35
+ console.info(
36
+ _kleur2.default.dim(`
37
+ ${(/* @__PURE__ */ new Date()).toLocaleTimeString()}`),
38
+ _kleur2.default.bold(_kleur2.default.magenta("[hrm]")),
39
+ _kleur2.default.gray(`Watching for file changes...
40
+ `)
41
+ );
42
+ }
43
+ }
44
+ async function loadUserConfig(root) {
45
+ const configPath = _path2.default.resolve(root, "vureact.config.js");
46
+ if (!_fs.existsSync.call(void 0, configPath)) return {};
47
+ try {
48
+ const configUrl = _url.pathToFileURL.call(void 0, configPath).href;
49
+ const module = await Promise.resolve().then(() => _interopRequireWildcard(require(configUrl)));
50
+ return module.default || module;
51
+ } catch (err) {
52
+ console.warn(
53
+ _kleur2.default.yellow("\n\u26A0"),
54
+ `Load config failed at ${configPath}, using default options`,
55
+ err
56
+ );
57
+ return {};
58
+ }
59
+ }
60
+ function mergeConfig(projectRoot, options, userConfig) {
61
+ const merged = {
62
+ ...userConfig,
63
+ ...options,
64
+ root: projectRoot
65
+ };
66
+ if (options.exclude) {
67
+ merged.exclude = Array.isArray(options.exclude) ? options.exclude : [options.exclude];
68
+ } else if (userConfig.exclude) {
69
+ merged.exclude = userConfig.exclude;
70
+ }
71
+ merged.output = {
72
+ ...userConfig.output,
73
+ workspace: _nullishCoalesce(options.workspace, () => ( _optionalChain([userConfig, 'access', _ => _.output, 'optionalAccess', _2 => _2.workspace]))),
74
+ outDir: _nullishCoalesce(options.outDir, () => ( _optionalChain([userConfig, 'access', _3 => _3.output, 'optionalAccess', _4 => _4.outDir]))),
75
+ bootstrapVite: _nullishCoalesce(options.bootstrapVite, () => ( _optionalChain([userConfig, 'access', _5 => _5.output, 'optionalAccess', _6 => _6.bootstrapVite])))
76
+ };
77
+ merged.format = {
78
+ enabled: _nullishCoalesce(options.format, () => ( _optionalChain([userConfig, 'access', _7 => _7.format, 'optionalAccess', _8 => _8.enabled]))),
79
+ formatter: _nullishCoalesce(options.formatter, () => ( _optionalChain([userConfig, 'access', _9 => _9.format, 'optionalAccess', _10 => _10.formatter])))
80
+ };
81
+ return merged;
82
+ }
83
+ function setupWatcher(compiler, config) {
84
+ const spinner = _ora2.default.call(void 0, );
85
+ const cmpHelper = new (0, _chunk5M5CTYQRjs.Helper)(config);
86
+ const watcher = _chokidar2.default.watch(cmpHelper.getInputPath(), {
87
+ ignored: cmpHelper.getExcludes(),
88
+ persistent: true,
89
+ ignoreInitial: true
90
+ // 初始扫描已由 compiler.execute 完成
91
+ });
92
+ watcher.on("all", async (event, filePath) => {
93
+ switch (event) {
94
+ case "add":
95
+ case "change":
96
+ await onRecompile(event, filePath);
97
+ break;
98
+ case "unlink":
99
+ case "unlinkDir":
100
+ await onRemoveFile(event, filePath);
101
+ break;
102
+ }
103
+ });
104
+ const processors = {
105
+ ".vue": (p) => compiler.processSFC(p),
106
+ ".js": (p) => compiler.processScript(p),
107
+ ".ts": (p) => compiler.processScript(p)
108
+ };
109
+ const onRecompile = async (event, filePath) => {
110
+ const ext = _path2.default.extname(filePath);
111
+ if (ext in processors) {
112
+ spinner.start("Recompiling...");
113
+ const startTime = performance.now();
114
+ const fn = processors[ext];
115
+ const unit = await fn(filePath);
116
+ cmpHelper.printCoreLogs();
117
+ cmpHelper.printCompileInfo(filePath, _chunk5M5CTYQRjs.calcElapsedTime.call(void 0, startTime));
118
+ if (unit) {
119
+ await _optionalChain([config, 'access', _11 => _11.onChange, 'optionalCall', _12 => _12(event, unit)]);
120
+ }
121
+ } else {
122
+ spinner.start("Updating assets...");
123
+ await compiler.processAsset(filePath);
124
+ cmpHelper.print(
125
+ _kleur2.default.blue("Copied Asset"),
126
+ _kleur2.default.dim(_chunk5M5CTYQRjs.normalizePath.call(void 0, cmpHelper.relativePath(filePath)))
127
+ );
128
+ }
129
+ spinner.stop();
130
+ };
131
+ const onRemoveFile = async (type, filePath) => {
132
+ const ext = _path2.default.extname(filePath);
133
+ const removeFile = async (type2) => {
134
+ await compiler.removeOutputPath(filePath, type2);
135
+ cmpHelper.print(
136
+ _kleur2.default.yellow("Removed"),
137
+ _kleur2.default.dim(_chunk5M5CTYQRjs.normalizePath.call(void 0, cmpHelper.relativePath(filePath)))
138
+ );
139
+ };
140
+ if (type === "unlink") {
141
+ if (ext === ".vue") {
142
+ await removeFile("sfc" /* SFC */);
143
+ return;
144
+ }
145
+ if (ext === ".js" || ext === ".ts") {
146
+ await removeFile("script" /* SCRIPT */);
147
+ return;
148
+ }
149
+ await removeFile("copied" /* ASSET */);
150
+ return;
151
+ }
152
+ if (type === "unlinkDir") {
153
+ await removeFile("sfc" /* SFC */);
154
+ await removeFile("script" /* SCRIPT */);
155
+ await removeFile("copied" /* ASSET */);
156
+ }
157
+ };
158
+ }
159
+
160
+ // src/cli/option.ts
161
+ function resolveOptions(command) {
162
+ return command.option("-i, --input <dir>", "Input directory (relative to root)").option("-o, --outDir <dir>", "Output directory name").option("--workspace <dir>", "The workspace directory for cache and output").option("--bootstrapVite", "Enable Vite to initialize a standard React project environment.").option("--exclude <pattern>", "Exclude files/directories (glob pattern)").option("--no-recursive", "Disable recursive search in subdirectories").option("--no-cache", "Disable cache", { default: void 0 }).option("--format", "Enable code formatting", { default: void 0 }).option("--formatter <type>", 'Choose formatter: "prettier" or "builtin"');
163
+ }
164
+
165
+ // src/cli/index.ts
166
+ var [programName] = Object.keys(_chunk5M5CTYQRjs.bin);
167
+ var cli = _cac.cac.call(void 0, programName);
168
+ var buildCommand = cli.command("build [root]", "Compile Vue3 to React (one-time)");
169
+ resolveOptions(buildCommand).action((root, options) => {
170
+ resolveAction(root, { ...options, watch: false });
171
+ });
172
+ var watchCommand = cli.command("watch [root]", "Compile Vue3 to React and watch for changes");
173
+ resolveOptions(watchCommand).action((root, options) => {
174
+ resolveAction(root, { ...options, watch: true });
175
+ });
176
+ cli.help().version(_chunk5M5CTYQRjs.version).parse();