@meituan-nocode/vite-plugin-nocode-compiler 0.4.0 → 0.4.1-beta.2

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/dist/index.cjs CHANGED
@@ -34,8 +34,8 @@ __export(index_exports, {
34
34
  default: () => index_default
35
35
  });
36
36
  module.exports = __toCommonJS(index_exports);
37
- var import_nocode_compiler_core = require("@meituan-nocode/nocode-compiler-core");
38
- var fs = __toESM(require("fs"), 1);
37
+ var import_nocode_compiler_core2 = require("@meituan-nocode/nocode-compiler-core");
38
+ var fs2 = __toESM(require("fs"), 1);
39
39
 
40
40
  // src/utils/index.ts
41
41
  var import_path = require("path");
@@ -179,11 +179,12 @@ function readJsonBody(req) {
179
179
  }
180
180
 
181
181
  // package.json
182
- var version = "0.4.0";
182
+ var version = "0.4.1-beta.2";
183
183
 
184
184
  // src/design-mode/types.ts
185
185
  var SANDBOX_SCRIPT_PATH = "/sandbox-script.js";
186
186
  var VIRTUAL_CODE_API_PATH = "/api/virtual-code";
187
+ var SWAP_NODE_API_PATH = "/api/virtual-code/swap";
187
188
  var DEFAULT_DESIGN_MODE_OPTIONS = {
188
189
  enableVirtualCode: true,
189
190
  virtualCodeApiPath: VIRTUAL_CODE_API_PATH,
@@ -194,6 +195,8 @@ var DEFAULT_DESIGN_MODE_OPTIONS = {
194
195
 
195
196
  // src/design-mode/virtual-code.ts
196
197
  var path = __toESM(require("path"), 1);
198
+ var fs = __toESM(require("fs"), 1);
199
+ var import_nocode_compiler_core = require("@meituan-nocode/nocode-compiler-core");
197
200
  var virtualCodeMap = /* @__PURE__ */ new Map();
198
201
  function matchesDesignModePattern(id, options) {
199
202
  const exclude = options.exclude || [];
@@ -221,20 +224,96 @@ function loadVirtualCode(id, options) {
221
224
  const relativePath = extractRelativePath(id);
222
225
  if (!relativePath) return void 0;
223
226
  const code = virtualCodeMap.get(relativePath) || virtualCodeMap.get("/" + relativePath);
224
- if (code && options.verbose) {
225
- console.log(`[DesignMode] Using virtual code for: ${relativePath}`);
227
+ if (code) {
228
+ console.log(`[DesignMode] load hook: using virtual code for "${relativePath}" (${code.length} chars)`);
229
+ } else if (virtualCodeMap.size > 0) {
230
+ console.log(`[DesignMode] load hook: NO virtual code for "${relativePath}", map keys: [${Array.from(virtualCodeMap.keys()).join(", ")}]`);
226
231
  }
227
232
  return code;
228
233
  }
234
+ function createSwapNodeMiddleware(server, options, projectRoot) {
235
+ var _a;
236
+ const swapApiPath = SWAP_NODE_API_PATH;
237
+ const verbose = (_a = options.verbose) != null ? _a : false;
238
+ const compiler = new import_nocode_compiler_core.DesignModeCompiler({ rootPath: projectRoot });
239
+ return (req, res, next) => {
240
+ var _a2;
241
+ if (!((_a2 = req.url) == null ? void 0 : _a2.startsWith(swapApiPath))) {
242
+ return next();
243
+ }
244
+ if (req.method === "OPTIONS") {
245
+ res.setHeader("Access-Control-Allow-Origin", "*");
246
+ res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
247
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type");
248
+ res.statusCode = 204;
249
+ res.end();
250
+ return;
251
+ }
252
+ res.setHeader("Content-Type", "application/json");
253
+ res.setHeader("Access-Control-Allow-Origin", "*");
254
+ if (req.method !== "POST") {
255
+ res.statusCode = 405;
256
+ res.end(JSON.stringify({ error: "Method not allowed" }));
257
+ return;
258
+ }
259
+ let body = "";
260
+ req.on("data", (chunk) => body += chunk.toString());
261
+ req.on("end", () => {
262
+ try {
263
+ const { filePath, nodeAId, nodeBId } = JSON.parse(body);
264
+ if (!filePath || !nodeAId || !nodeBId) {
265
+ res.statusCode = 400;
266
+ res.end(JSON.stringify({ error: "filePath, nodeAId, and nodeBId are required" }));
267
+ return;
268
+ }
269
+ const fullPath = path.resolve(projectRoot, filePath);
270
+ if (!fs.existsSync(fullPath)) {
271
+ res.statusCode = 404;
272
+ res.end(JSON.stringify({ error: `File not found: ${filePath}` }));
273
+ return;
274
+ }
275
+ const cleanCode = fs.readFileSync(fullPath, "utf-8");
276
+ const result = compiler.swapNodes(cleanCode, fullPath, nodeAId, nodeBId);
277
+ if (!result) {
278
+ res.statusCode = 400;
279
+ res.end(JSON.stringify({ error: "Swap failed: nodes not found or not siblings" }));
280
+ return;
281
+ }
282
+ virtualCodeMap.set(filePath, result.dndCode);
283
+ console.log(`[DnD] virtualCodeMap key: "${filePath}", dndCode length: ${result.dndCode.length}`);
284
+ fs.writeFileSync(fullPath, result.cleanCode, "utf-8");
285
+ triggerHmrUpdate(server, fullPath, true);
286
+ if (verbose) {
287
+ console.log(`[DnD] Swap completed for: ${filePath}, ${nodeAId} \u2194 ${nodeBId}`);
288
+ }
289
+ res.end(
290
+ JSON.stringify({
291
+ success: true,
292
+ cleanCode: result.cleanCode,
293
+ dndCode: result.dndCode,
294
+ newSelectedId: result.newSelectedId
295
+ })
296
+ );
297
+ } catch (error) {
298
+ console.error("[DnD] Swap error:", error);
299
+ res.statusCode = 500;
300
+ res.end(JSON.stringify({ error: String(error) }));
301
+ }
302
+ });
303
+ };
304
+ }
229
305
  function createVirtualCodeMiddleware(server, options, projectRoot) {
230
306
  var _a;
231
307
  const apiPath = options.virtualCodeApiPath || VIRTUAL_CODE_API_PATH;
232
308
  const verbose = (_a = options.verbose) != null ? _a : false;
233
309
  return (req, res, next) => {
234
- var _a2;
310
+ var _a2, _b;
235
311
  if (!((_a2 = req.url) == null ? void 0 : _a2.startsWith(apiPath))) {
236
312
  return next();
237
313
  }
314
+ if ((_b = req.url) == null ? void 0 : _b.startsWith(SWAP_NODE_API_PATH)) {
315
+ return next();
316
+ }
238
317
  if (req.method === "OPTIONS") {
239
318
  res.setHeader("Access-Control-Allow-Origin", "*");
240
319
  res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
@@ -430,15 +509,15 @@ function componentCompiler(options = {}) {
430
509
  ...DEFAULT_DESIGN_MODE_OPTIONS,
431
510
  ...options.designModeOptions
432
511
  };
433
- const monitorService = new import_nocode_compiler_core.MonitorService({
512
+ const monitorService = new import_nocode_compiler_core2.MonitorService({
434
513
  pluginName: "vite-plugin-nocode-compiler",
435
514
  framework: "vite",
436
515
  pluginVersion: version
437
516
  });
438
- monitorService.reportMetrics(import_nocode_compiler_core.MetricType.PLUGIN_USE, 1);
439
- const vueCompiler = new import_nocode_compiler_core.VueCompiler(options);
440
- const jsxCompiler = new import_nocode_compiler_core.JSXCompiler(options);
441
- const designModeCompiler = new import_nocode_compiler_core.DesignModeCompiler(options);
517
+ monitorService.reportMetrics(import_nocode_compiler_core2.MetricType.PLUGIN_USE, 1);
518
+ const vueCompiler = new import_nocode_compiler_core2.VueCompiler(options);
519
+ const jsxCompiler = new import_nocode_compiler_core2.JSXCompiler(options);
520
+ const designModeCompiler = new import_nocode_compiler_core2.DesignModeCompiler(options);
442
521
  let server;
443
522
  let pkgRegistry;
444
523
  let projectRoot = "";
@@ -456,13 +535,13 @@ function componentCompiler(options = {}) {
456
535
  var _a;
457
536
  if (command === "serve" && designModeOptions.enableVirtualCode) {
458
537
  const root = userConfig.root || process.cwd();
459
- if (!(0, import_nocode_compiler_core.detectShadcnProject)(root)) {
538
+ if (!(0, import_nocode_compiler_core2.detectShadcnProject)(root)) {
460
539
  return;
461
540
  }
462
- console.log(`[DesignMode] Shadcn project detected, adding ${import_nocode_compiler_core.SHADCN_NPM_DEPS.length} deps to optimizeDeps.include`);
541
+ console.log(`[DesignMode] Shadcn project detected, adding ${import_nocode_compiler_core2.SHADCN_NPM_DEPS.length} deps to optimizeDeps.include`);
463
542
  return {
464
543
  optimizeDeps: {
465
- include: [...Array.isArray((_a = userConfig.optimizeDeps) == null ? void 0 : _a.include) ? userConfig.optimizeDeps.include : [], ...import_nocode_compiler_core.SHADCN_NPM_DEPS]
544
+ include: [...Array.isArray((_a = userConfig.optimizeDeps) == null ? void 0 : _a.include) ? userConfig.optimizeDeps.include : [], ...import_nocode_compiler_core2.SHADCN_NPM_DEPS]
466
545
  }
467
546
  };
468
547
  }
@@ -475,9 +554,9 @@ function componentCompiler(options = {}) {
475
554
  */
476
555
  configResolved(config) {
477
556
  ensurePluginFirst(config, (error) => monitorService.reportError(error, { scene: "ensurePluginFirst" }));
478
- pkgRegistry = new import_nocode_compiler_core.PackageVersionRegistry(config.root);
557
+ pkgRegistry = new import_nocode_compiler_core2.PackageVersionRegistry(config.root);
479
558
  projectRoot = config.root;
480
- (0, import_nocode_compiler_core.preloadDependencyVersions)(pkgRegistry);
559
+ (0, import_nocode_compiler_core2.preloadDependencyVersions)(pkgRegistry);
481
560
  },
482
561
  /**
483
562
  * 配置开发服务器,添加 Override 中间件和设计模式中间件
@@ -497,6 +576,7 @@ function componentCompiler(options = {}) {
497
576
  server.middlewares.use(createOverrideMiddleware(server));
498
577
  server.middlewares.use(createSandboxScriptMiddleware(designModeOptions));
499
578
  if (designModeOptions.enableVirtualCode) {
579
+ server.middlewares.use(createSwapNodeMiddleware(server, designModeOptions, projectRoot));
500
580
  server.middlewares.use(createVirtualCodeMiddleware(server, designModeOptions, projectRoot));
501
581
  console.log(`[DesignMode] Virtual code API enabled at ${designModeOptions.virtualCodeApiPath}`);
502
582
  }
@@ -508,8 +588,8 @@ function componentCompiler(options = {}) {
508
588
  * 兼容 Vite 2.x - 6.x(transformIndexHtml 从 Vite 2 起就支持简单函数形式)
509
589
  */
510
590
  transformIndexHtml(html) {
511
- const metadata = (0, import_nocode_compiler_core.generateBodyMetadata)(pkgRegistry, version);
512
- html = (0, import_nocode_compiler_core.injectBodyMetadata)(html, metadata);
591
+ const metadata = (0, import_nocode_compiler_core2.generateBodyMetadata)(pkgRegistry, version);
592
+ html = (0, import_nocode_compiler_core2.injectBodyMetadata)(html, metadata);
513
593
  html = injectSandboxScript(html);
514
594
  return html;
515
595
  },
@@ -538,7 +618,7 @@ function componentCompiler(options = {}) {
538
618
  code = result.code;
539
619
  }
540
620
  }
541
- const { useJSXCompiler, useVueCompiler } = (0, import_nocode_compiler_core.detectCompileScenario)({
621
+ const { useJSXCompiler, useVueCompiler } = (0, import_nocode_compiler_core2.detectCompileScenario)({
542
622
  filePath,
543
623
  query
544
624
  });
@@ -546,7 +626,7 @@ function componentCompiler(options = {}) {
546
626
  let diskCode;
547
627
  if (hasVirtualCode(id)) {
548
628
  try {
549
- diskCode = fs.readFileSync(filePath, "utf-8");
629
+ diskCode = fs2.readFileSync(filePath, "utf-8");
550
630
  } catch {
551
631
  }
552
632
  }
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // src/index.ts
2
- import { DesignModeCompiler, detectCompileScenario, detectShadcnProject, generateBodyMetadata, injectBodyMetadata, JSXCompiler, MetricType, MonitorService, PackageVersionRegistry, preloadDependencyVersions, SHADCN_NPM_DEPS, VueCompiler } from "@meituan-nocode/nocode-compiler-core";
3
- import * as fs from "fs";
2
+ import { DesignModeCompiler as DesignModeCompiler2, detectCompileScenario, detectShadcnProject, generateBodyMetadata, injectBodyMetadata, JSXCompiler, MetricType, MonitorService, PackageVersionRegistry, preloadDependencyVersions, SHADCN_NPM_DEPS, VueCompiler } from "@meituan-nocode/nocode-compiler-core";
3
+ import * as fs2 from "fs";
4
4
 
5
5
  // src/utils/index.ts
6
6
  import { normalize, dirname, isAbsolute, join } from "path";
@@ -144,11 +144,12 @@ function readJsonBody(req) {
144
144
  }
145
145
 
146
146
  // package.json
147
- var version = "0.4.0";
147
+ var version = "0.4.1-beta.2";
148
148
 
149
149
  // src/design-mode/types.ts
150
150
  var SANDBOX_SCRIPT_PATH = "/sandbox-script.js";
151
151
  var VIRTUAL_CODE_API_PATH = "/api/virtual-code";
152
+ var SWAP_NODE_API_PATH = "/api/virtual-code/swap";
152
153
  var DEFAULT_DESIGN_MODE_OPTIONS = {
153
154
  enableVirtualCode: true,
154
155
  virtualCodeApiPath: VIRTUAL_CODE_API_PATH,
@@ -159,6 +160,8 @@ var DEFAULT_DESIGN_MODE_OPTIONS = {
159
160
 
160
161
  // src/design-mode/virtual-code.ts
161
162
  import * as path from "path";
163
+ import * as fs from "fs";
164
+ import { DesignModeCompiler } from "@meituan-nocode/nocode-compiler-core";
162
165
  var virtualCodeMap = /* @__PURE__ */ new Map();
163
166
  function matchesDesignModePattern(id, options) {
164
167
  const exclude = options.exclude || [];
@@ -186,20 +189,96 @@ function loadVirtualCode(id, options) {
186
189
  const relativePath = extractRelativePath(id);
187
190
  if (!relativePath) return void 0;
188
191
  const code = virtualCodeMap.get(relativePath) || virtualCodeMap.get("/" + relativePath);
189
- if (code && options.verbose) {
190
- console.log(`[DesignMode] Using virtual code for: ${relativePath}`);
192
+ if (code) {
193
+ console.log(`[DesignMode] load hook: using virtual code for "${relativePath}" (${code.length} chars)`);
194
+ } else if (virtualCodeMap.size > 0) {
195
+ console.log(`[DesignMode] load hook: NO virtual code for "${relativePath}", map keys: [${Array.from(virtualCodeMap.keys()).join(", ")}]`);
191
196
  }
192
197
  return code;
193
198
  }
199
+ function createSwapNodeMiddleware(server, options, projectRoot) {
200
+ var _a;
201
+ const swapApiPath = SWAP_NODE_API_PATH;
202
+ const verbose = (_a = options.verbose) != null ? _a : false;
203
+ const compiler = new DesignModeCompiler({ rootPath: projectRoot });
204
+ return (req, res, next) => {
205
+ var _a2;
206
+ if (!((_a2 = req.url) == null ? void 0 : _a2.startsWith(swapApiPath))) {
207
+ return next();
208
+ }
209
+ if (req.method === "OPTIONS") {
210
+ res.setHeader("Access-Control-Allow-Origin", "*");
211
+ res.setHeader("Access-Control-Allow-Methods", "POST, OPTIONS");
212
+ res.setHeader("Access-Control-Allow-Headers", "Content-Type");
213
+ res.statusCode = 204;
214
+ res.end();
215
+ return;
216
+ }
217
+ res.setHeader("Content-Type", "application/json");
218
+ res.setHeader("Access-Control-Allow-Origin", "*");
219
+ if (req.method !== "POST") {
220
+ res.statusCode = 405;
221
+ res.end(JSON.stringify({ error: "Method not allowed" }));
222
+ return;
223
+ }
224
+ let body = "";
225
+ req.on("data", (chunk) => body += chunk.toString());
226
+ req.on("end", () => {
227
+ try {
228
+ const { filePath, nodeAId, nodeBId } = JSON.parse(body);
229
+ if (!filePath || !nodeAId || !nodeBId) {
230
+ res.statusCode = 400;
231
+ res.end(JSON.stringify({ error: "filePath, nodeAId, and nodeBId are required" }));
232
+ return;
233
+ }
234
+ const fullPath = path.resolve(projectRoot, filePath);
235
+ if (!fs.existsSync(fullPath)) {
236
+ res.statusCode = 404;
237
+ res.end(JSON.stringify({ error: `File not found: ${filePath}` }));
238
+ return;
239
+ }
240
+ const cleanCode = fs.readFileSync(fullPath, "utf-8");
241
+ const result = compiler.swapNodes(cleanCode, fullPath, nodeAId, nodeBId);
242
+ if (!result) {
243
+ res.statusCode = 400;
244
+ res.end(JSON.stringify({ error: "Swap failed: nodes not found or not siblings" }));
245
+ return;
246
+ }
247
+ virtualCodeMap.set(filePath, result.dndCode);
248
+ console.log(`[DnD] virtualCodeMap key: "${filePath}", dndCode length: ${result.dndCode.length}`);
249
+ fs.writeFileSync(fullPath, result.cleanCode, "utf-8");
250
+ triggerHmrUpdate(server, fullPath, true);
251
+ if (verbose) {
252
+ console.log(`[DnD] Swap completed for: ${filePath}, ${nodeAId} \u2194 ${nodeBId}`);
253
+ }
254
+ res.end(
255
+ JSON.stringify({
256
+ success: true,
257
+ cleanCode: result.cleanCode,
258
+ dndCode: result.dndCode,
259
+ newSelectedId: result.newSelectedId
260
+ })
261
+ );
262
+ } catch (error) {
263
+ console.error("[DnD] Swap error:", error);
264
+ res.statusCode = 500;
265
+ res.end(JSON.stringify({ error: String(error) }));
266
+ }
267
+ });
268
+ };
269
+ }
194
270
  function createVirtualCodeMiddleware(server, options, projectRoot) {
195
271
  var _a;
196
272
  const apiPath = options.virtualCodeApiPath || VIRTUAL_CODE_API_PATH;
197
273
  const verbose = (_a = options.verbose) != null ? _a : false;
198
274
  return (req, res, next) => {
199
- var _a2;
275
+ var _a2, _b;
200
276
  if (!((_a2 = req.url) == null ? void 0 : _a2.startsWith(apiPath))) {
201
277
  return next();
202
278
  }
279
+ if ((_b = req.url) == null ? void 0 : _b.startsWith(SWAP_NODE_API_PATH)) {
280
+ return next();
281
+ }
203
282
  if (req.method === "OPTIONS") {
204
283
  res.setHeader("Access-Control-Allow-Origin", "*");
205
284
  res.setHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, OPTIONS");
@@ -403,7 +482,7 @@ function componentCompiler(options = {}) {
403
482
  monitorService.reportMetrics(MetricType.PLUGIN_USE, 1);
404
483
  const vueCompiler = new VueCompiler(options);
405
484
  const jsxCompiler = new JSXCompiler(options);
406
- const designModeCompiler = new DesignModeCompiler(options);
485
+ const designModeCompiler = new DesignModeCompiler2(options);
407
486
  let server;
408
487
  let pkgRegistry;
409
488
  let projectRoot = "";
@@ -462,6 +541,7 @@ function componentCompiler(options = {}) {
462
541
  server.middlewares.use(createOverrideMiddleware(server));
463
542
  server.middlewares.use(createSandboxScriptMiddleware(designModeOptions));
464
543
  if (designModeOptions.enableVirtualCode) {
544
+ server.middlewares.use(createSwapNodeMiddleware(server, designModeOptions, projectRoot));
465
545
  server.middlewares.use(createVirtualCodeMiddleware(server, designModeOptions, projectRoot));
466
546
  console.log(`[DesignMode] Virtual code API enabled at ${designModeOptions.virtualCodeApiPath}`);
467
547
  }
@@ -511,7 +591,7 @@ function componentCompiler(options = {}) {
511
591
  let diskCode;
512
592
  if (hasVirtualCode(id)) {
513
593
  try {
514
- diskCode = fs.readFileSync(filePath, "utf-8");
594
+ diskCode = fs2.readFileSync(filePath, "utf-8");
515
595
  } catch {
516
596
  }
517
597
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meituan-nocode/vite-plugin-nocode-compiler",
3
- "version": "0.4.0",
3
+ "version": "0.4.1-beta.2",
4
4
  "description": "Vite plugin for nocode compiler",
5
5
  "type": "module",
6
6
  "exports": {
@@ -17,7 +17,7 @@
17
17
  "dist"
18
18
  ],
19
19
  "dependencies": {
20
- "@meituan-nocode/nocode-compiler-core": "0.2.9-beta.8",
20
+ "@meituan-nocode/nocode-compiler-core": "0.2.9-beta.9",
21
21
  "@meituan-nocode/nocode-design-mode-sandbox-script": "0.1.0-beta.4"
22
22
  },
23
23
  "devDependencies": {