@weapp-vite/volar 0.0.2 → 0.1.0-alpha.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,58 @@
1
+ # @weapp-vite/volar
2
+
3
+ > 为 weapp-vite 提供 VSCode 智能提示和类型检查的 Volar 插件
4
+
5
+ ## 安装
6
+
7
+ 此包已作为 `weapp-vite` 的依赖自动安装,**无需单独安装**。
8
+
9
+ 如果你确实需要单独安装:
10
+
11
+ ```bash
12
+ npm install -D @weapp-vite/volar
13
+ # or
14
+ pnpm add -D @weapp-vite/volar
15
+ # or
16
+ yarn add -D @weapp-vite/volar
17
+ ```
18
+
19
+ ## 使用方法
20
+
21
+ **推荐方式:** 通过 weapp-vite 使用,插件会自动启用。
22
+
23
+ 详细使用文档请参考:[weapp-vite Volar 文档](../weapp-vite/docs/volar.md)
24
+
25
+ ## 功能特性
26
+
27
+ - ✅ **配置文件智能提示** - 为 `<config>` 代码块提供完整的类型检查和自动补全
28
+ - ✅ **JSON Schema 支持** - 支持 JSON Schema 验证和自动补全
29
+ - ✅ **TypeScript 类型检查** - 利用 TypeScript 类型系统确保配置正确性
30
+ - ✅ **自动推断配置类型** - 根据文件路径自动推断是 App/Page/Component 配置
31
+ - ✅ **多模式支持** - 支持 JSON 模式和 TypeScript 模式
32
+
33
+ ## 开发
34
+
35
+ ```bash
36
+ # 安装依赖
37
+ pnpm install
38
+
39
+ # 开发模式
40
+ pnpm dev
41
+
42
+ # 构建
43
+ pnpm build
44
+
45
+ # 测试
46
+ pnpm test
47
+ ```
48
+
49
+ ## 相关文档
50
+
51
+ - [weapp-vite 文档](../weapp-vite)
52
+ - [weapp-vite Volar 使用指南](../weapp-vite/docs/volar.md)
53
+ - [Vue 3 文档](https://vuejs.org/)
54
+ - [微信小程序官方文档](https://developers.weixin.qq.com/miniprogram/dev/framework/)
55
+
56
+ ## License
57
+
58
+ MIT
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// ../../node_modules/.pnpm/tsup@8.5.0_@swc+core@1.14.0_jiti@2.6.1_postcss@8.5.6_tsx@4.20.6_typescript@5.9.3_yaml@2.8.1/node_modules/tsup/assets/cjs_shims.js
2
- var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); 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; }// ../../node_modules/.pnpm/tsup@8.5.1_@swc+core@1.15.7_jiti@2.6.1_postcss@8.5.6_tsx@4.21.0_typescript@5.9.3_yaml@2.8.2/node_modules/tsup/assets/cjs_shims.js
2
+ var getImportMetaUrl = () => typeof document === "undefined" ? new URL(`file:${__filename}`).href : document.currentScript && document.currentScript.tagName.toUpperCase() === "SCRIPT" ? document.currentScript.src : new URL("main.js", document.baseURI).href;
3
3
  var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
4
4
 
5
5
  // src/index.ts
@@ -8,8 +8,19 @@ var _module = require('module');
8
8
  // package.json
9
9
  var name = "@weapp-vite/volar";
10
10
 
11
+ // src/schema.ts
12
+ var _schematics = require('@weapp-core/schematics');
13
+ function getSchemaForType(type) {
14
+ const definition = _schematics.JSON_SCHEMA_DEFINITIONS.find((d) => d.typeName === type);
15
+ if (!definition) {
16
+ return null;
17
+ }
18
+ return definition.schema;
19
+ }
20
+
11
21
  // src/index.ts
12
22
  var BLOCK_TYPE = "config";
23
+ var JS_LANG = "js";
13
24
  var JSON_LANG = "json";
14
25
  var TS_LANG = "ts";
15
26
  var PLUGIN_VERSION = 2.2;
@@ -41,10 +52,14 @@ function normalizeLang(lang) {
41
52
  if (!lang) {
42
53
  return JSON_LANG;
43
54
  }
44
- if (lang === "txt") {
55
+ const lower = lang.toLowerCase();
56
+ if (lower === "txt") {
45
57
  return JSON_LANG;
46
58
  }
47
- return lang;
59
+ if (lower === "jsonc") {
60
+ return JSON_LANG;
61
+ }
62
+ return lower;
48
63
  }
49
64
  function normalizeFilename(filename) {
50
65
  if (!filename) {
@@ -71,7 +86,42 @@ function inferConfigType(filename) {
71
86
  }
72
87
  return "Page";
73
88
  }
74
- var plugin = () => {
89
+ function findExportDefaultExpression(code, tsModule, lang) {
90
+ const scriptKind = lang === TS_LANG ? tsModule.ScriptKind.TS : tsModule.ScriptKind.JS;
91
+ const sourceFile = tsModule.createSourceFile(
92
+ `config.${lang}`,
93
+ code,
94
+ tsModule.ScriptTarget.Latest,
95
+ true,
96
+ scriptKind
97
+ );
98
+ for (const statement of sourceFile.statements) {
99
+ if (tsModule.isExportAssignment(statement)) {
100
+ const expressionStart = statement.expression.getStart(sourceFile);
101
+ const expressionEnd = statement.expression.getEnd();
102
+ const leading = code.slice(0, statement.getStart(sourceFile));
103
+ const expression = code.slice(expressionStart, expressionEnd);
104
+ const trailing = code.slice(statement.getEnd());
105
+ return {
106
+ expression,
107
+ expressionStart,
108
+ expressionEnd,
109
+ leading,
110
+ trailing
111
+ };
112
+ }
113
+ }
114
+ return null;
115
+ }
116
+ var plugin = (ctx) => {
117
+ let tsModule = _optionalChain([ctx, 'optionalAccess', _2 => _2.modules, 'optionalAccess', _3 => _3.typescript]);
118
+ if (!tsModule) {
119
+ try {
120
+ tsModule = require2("typescript");
121
+ } catch (e2) {
122
+ tsModule = void 0;
123
+ }
124
+ }
75
125
  return {
76
126
  name,
77
127
  version: PLUGIN_VERSION,
@@ -81,7 +131,8 @@ var plugin = () => {
81
131
  const block = sfc.customBlocks[i];
82
132
  if (block.type === BLOCK_TYPE) {
83
133
  const normalizedLang = normalizeLang(block.lang);
84
- const lang = hasSchematicsTypes ? TS_LANG : normalizedLang;
134
+ const isJsLike = normalizedLang === JS_LANG || normalizedLang === TS_LANG;
135
+ const lang = isJsLike ? TS_LANG : normalizedLang;
85
136
  names.push({ id: `${BLOCK_TYPE}_${i}`, lang });
86
137
  }
87
138
  }
@@ -97,6 +148,8 @@ var plugin = () => {
97
148
  if (!block) {
98
149
  return;
99
150
  }
151
+ const normalizedLang = normalizeLang(block.lang);
152
+ const configType = inferConfigType(fileName);
100
153
  if (!hasSchematicsTypes) {
101
154
  embeddedCode.content.push([
102
155
  block.content,
@@ -106,7 +159,89 @@ var plugin = () => {
106
159
  ]);
107
160
  return;
108
161
  }
109
- const configType = inferConfigType(fileName);
162
+ const userWantsJson = normalizedLang === "json" || normalizedLang === "jsonc" || block.content.includes("$schema");
163
+ const userWantsJs = normalizedLang === "js" || normalizedLang === "ts";
164
+ if (userWantsJs) {
165
+ const parsed = tsModule && findExportDefaultExpression(block.content, tsModule, normalizedLang);
166
+ if (parsed && hasSchematicsTypes) {
167
+ const typeImport = `import type { ${configType} as __WeappConfig } from '@weapp-core/schematics'
168
+ `;
169
+ const helper = "const __weapp_defineConfig = <T extends __WeappConfig>(config: T) => config\n\n";
170
+ embeddedCode.content.push([
171
+ `${typeImport}${helper}`,
172
+ void 0,
173
+ 0,
174
+ VOID_CAPABILITIES
175
+ ]);
176
+ if (parsed.leading) {
177
+ embeddedCode.content.push([
178
+ parsed.leading,
179
+ block.name,
180
+ 0,
181
+ FULL_CAPABILITIES
182
+ ]);
183
+ }
184
+ embeddedCode.content.push([
185
+ "export default __weapp_defineConfig(",
186
+ void 0,
187
+ parsed.expressionStart,
188
+ VOID_CAPABILITIES
189
+ ]);
190
+ embeddedCode.content.push([
191
+ parsed.expression,
192
+ block.name,
193
+ parsed.expressionStart,
194
+ FULL_CAPABILITIES
195
+ ]);
196
+ embeddedCode.content.push([
197
+ ")",
198
+ void 0,
199
+ parsed.expressionEnd,
200
+ VOID_CAPABILITIES
201
+ ]);
202
+ if (parsed.trailing) {
203
+ embeddedCode.content.push([
204
+ parsed.trailing,
205
+ block.name,
206
+ parsed.expressionEnd,
207
+ FULL_CAPABILITIES
208
+ ]);
209
+ }
210
+ return;
211
+ }
212
+ embeddedCode.content.push([
213
+ block.content,
214
+ block.name,
215
+ 0,
216
+ FULL_CAPABILITIES
217
+ ]);
218
+ return;
219
+ }
220
+ if (userWantsJson) {
221
+ const schema = getSchemaForType(configType);
222
+ if (schema && schema.$id && !block.content.includes("$schema")) {
223
+ const schemaComment = ` "$schema": "${schema.$id}",
224
+ `;
225
+ const content = block.content.trim();
226
+ const hasOpeningBrace = content.startsWith("{");
227
+ const injected = hasOpeningBrace ? `${content.slice(0, 1)}
228
+ ${schemaComment}${content.slice(1)}` : block.content;
229
+ embeddedCode.content.push([
230
+ injected,
231
+ block.name,
232
+ 0,
233
+ FULL_CAPABILITIES
234
+ ]);
235
+ } else {
236
+ embeddedCode.content.push([
237
+ block.content,
238
+ block.name,
239
+ 0,
240
+ FULL_CAPABILITIES
241
+ ]);
242
+ }
243
+ return;
244
+ }
110
245
  const prefix = `import type { ${configType} as __WeappConfig } from '@weapp-core/schematics'
111
246
 
112
247
  export default `;
package/dist/index.mjs CHANGED
@@ -4,8 +4,19 @@ import { createRequire } from "module";
4
4
  // package.json
5
5
  var name = "@weapp-vite/volar";
6
6
 
7
+ // src/schema.ts
8
+ import { JSON_SCHEMA_DEFINITIONS } from "@weapp-core/schematics";
9
+ function getSchemaForType(type) {
10
+ const definition = JSON_SCHEMA_DEFINITIONS.find((d) => d.typeName === type);
11
+ if (!definition) {
12
+ return null;
13
+ }
14
+ return definition.schema;
15
+ }
16
+
7
17
  // src/index.ts
8
18
  var BLOCK_TYPE = "config";
19
+ var JS_LANG = "js";
9
20
  var JSON_LANG = "json";
10
21
  var TS_LANG = "ts";
11
22
  var PLUGIN_VERSION = 2.2;
@@ -37,10 +48,14 @@ function normalizeLang(lang) {
37
48
  if (!lang) {
38
49
  return JSON_LANG;
39
50
  }
40
- if (lang === "txt") {
51
+ const lower = lang.toLowerCase();
52
+ if (lower === "txt") {
41
53
  return JSON_LANG;
42
54
  }
43
- return lang;
55
+ if (lower === "jsonc") {
56
+ return JSON_LANG;
57
+ }
58
+ return lower;
44
59
  }
45
60
  function normalizeFilename(filename) {
46
61
  if (!filename) {
@@ -67,7 +82,42 @@ function inferConfigType(filename) {
67
82
  }
68
83
  return "Page";
69
84
  }
70
- var plugin = () => {
85
+ function findExportDefaultExpression(code, tsModule, lang) {
86
+ const scriptKind = lang === TS_LANG ? tsModule.ScriptKind.TS : tsModule.ScriptKind.JS;
87
+ const sourceFile = tsModule.createSourceFile(
88
+ `config.${lang}`,
89
+ code,
90
+ tsModule.ScriptTarget.Latest,
91
+ true,
92
+ scriptKind
93
+ );
94
+ for (const statement of sourceFile.statements) {
95
+ if (tsModule.isExportAssignment(statement)) {
96
+ const expressionStart = statement.expression.getStart(sourceFile);
97
+ const expressionEnd = statement.expression.getEnd();
98
+ const leading = code.slice(0, statement.getStart(sourceFile));
99
+ const expression = code.slice(expressionStart, expressionEnd);
100
+ const trailing = code.slice(statement.getEnd());
101
+ return {
102
+ expression,
103
+ expressionStart,
104
+ expressionEnd,
105
+ leading,
106
+ trailing
107
+ };
108
+ }
109
+ }
110
+ return null;
111
+ }
112
+ var plugin = (ctx) => {
113
+ let tsModule = ctx?.modules?.typescript;
114
+ if (!tsModule) {
115
+ try {
116
+ tsModule = require2("typescript");
117
+ } catch {
118
+ tsModule = void 0;
119
+ }
120
+ }
71
121
  return {
72
122
  name,
73
123
  version: PLUGIN_VERSION,
@@ -77,7 +127,8 @@ var plugin = () => {
77
127
  const block = sfc.customBlocks[i];
78
128
  if (block.type === BLOCK_TYPE) {
79
129
  const normalizedLang = normalizeLang(block.lang);
80
- const lang = hasSchematicsTypes ? TS_LANG : normalizedLang;
130
+ const isJsLike = normalizedLang === JS_LANG || normalizedLang === TS_LANG;
131
+ const lang = isJsLike ? TS_LANG : normalizedLang;
81
132
  names.push({ id: `${BLOCK_TYPE}_${i}`, lang });
82
133
  }
83
134
  }
@@ -93,6 +144,8 @@ var plugin = () => {
93
144
  if (!block) {
94
145
  return;
95
146
  }
147
+ const normalizedLang = normalizeLang(block.lang);
148
+ const configType = inferConfigType(fileName);
96
149
  if (!hasSchematicsTypes) {
97
150
  embeddedCode.content.push([
98
151
  block.content,
@@ -102,7 +155,89 @@ var plugin = () => {
102
155
  ]);
103
156
  return;
104
157
  }
105
- const configType = inferConfigType(fileName);
158
+ const userWantsJson = normalizedLang === "json" || normalizedLang === "jsonc" || block.content.includes("$schema");
159
+ const userWantsJs = normalizedLang === "js" || normalizedLang === "ts";
160
+ if (userWantsJs) {
161
+ const parsed = tsModule && findExportDefaultExpression(block.content, tsModule, normalizedLang);
162
+ if (parsed && hasSchematicsTypes) {
163
+ const typeImport = `import type { ${configType} as __WeappConfig } from '@weapp-core/schematics'
164
+ `;
165
+ const helper = "const __weapp_defineConfig = <T extends __WeappConfig>(config: T) => config\n\n";
166
+ embeddedCode.content.push([
167
+ `${typeImport}${helper}`,
168
+ void 0,
169
+ 0,
170
+ VOID_CAPABILITIES
171
+ ]);
172
+ if (parsed.leading) {
173
+ embeddedCode.content.push([
174
+ parsed.leading,
175
+ block.name,
176
+ 0,
177
+ FULL_CAPABILITIES
178
+ ]);
179
+ }
180
+ embeddedCode.content.push([
181
+ "export default __weapp_defineConfig(",
182
+ void 0,
183
+ parsed.expressionStart,
184
+ VOID_CAPABILITIES
185
+ ]);
186
+ embeddedCode.content.push([
187
+ parsed.expression,
188
+ block.name,
189
+ parsed.expressionStart,
190
+ FULL_CAPABILITIES
191
+ ]);
192
+ embeddedCode.content.push([
193
+ ")",
194
+ void 0,
195
+ parsed.expressionEnd,
196
+ VOID_CAPABILITIES
197
+ ]);
198
+ if (parsed.trailing) {
199
+ embeddedCode.content.push([
200
+ parsed.trailing,
201
+ block.name,
202
+ parsed.expressionEnd,
203
+ FULL_CAPABILITIES
204
+ ]);
205
+ }
206
+ return;
207
+ }
208
+ embeddedCode.content.push([
209
+ block.content,
210
+ block.name,
211
+ 0,
212
+ FULL_CAPABILITIES
213
+ ]);
214
+ return;
215
+ }
216
+ if (userWantsJson) {
217
+ const schema = getSchemaForType(configType);
218
+ if (schema && schema.$id && !block.content.includes("$schema")) {
219
+ const schemaComment = ` "$schema": "${schema.$id}",
220
+ `;
221
+ const content = block.content.trim();
222
+ const hasOpeningBrace = content.startsWith("{");
223
+ const injected = hasOpeningBrace ? `${content.slice(0, 1)}
224
+ ${schemaComment}${content.slice(1)}` : block.content;
225
+ embeddedCode.content.push([
226
+ injected,
227
+ block.name,
228
+ 0,
229
+ FULL_CAPABILITIES
230
+ ]);
231
+ } else {
232
+ embeddedCode.content.push([
233
+ block.content,
234
+ block.name,
235
+ 0,
236
+ FULL_CAPABILITIES
237
+ ]);
238
+ }
239
+ return;
240
+ }
106
241
  const prefix = `import type { ${configType} as __WeappConfig } from '@weapp-core/schematics'
107
242
 
108
243
  export default `;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@weapp-vite/volar",
3
- "version": "0.0.2",
4
- "description": "tsup(esbuild) build package template",
3
+ "version": "0.1.0-alpha.0",
4
+ "description": "Volar plugin for weapp-vite - Provides IntelliSense and type checking for WeChat mini-program config blocks",
5
5
  "author": "ice breaker <1324318532@qq.com>",
6
6
  "license": "MIT",
7
7
  "repository": {
@@ -12,7 +12,16 @@
12
12
  "bugs": {
13
13
  "url": "https://github.com/weapp-vite/weapp-vite/issues"
14
14
  },
15
- "keywords": [],
15
+ "keywords": [
16
+ "volar",
17
+ "vue",
18
+ "wechat",
19
+ "miniprogram",
20
+ "weapp",
21
+ "intellisense",
22
+ "typescript",
23
+ "json-schema"
24
+ ],
16
25
  "sideEffects": false,
17
26
  "exports": {
18
27
  ".": {
@@ -31,7 +40,7 @@
31
40
  "dist"
32
41
  ],
33
42
  "dependencies": {
34
- "@weapp-core/schematics": "4.0.0"
43
+ "@weapp-core/schematics": "4.0.1-alpha.0"
35
44
  },
36
45
  "scripts": {
37
46
  "dev": "tsup --watch --sourcemap",