@weapp-vite/volar 2.0.4 → 2.0.5

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
@@ -58,6 +58,9 @@ var JSON_LANG = "json";
58
58
  var JSON5_LANG = "json5";
59
59
  var PLUGIN_VERSION = 2.2;
60
60
  var TS_LANG = "ts";
61
+ var BACKSLASH_RE = /\\/g;
62
+ var NON_SPACE_RE = /\S/;
63
+ var WXS_MODULE_RE = /<wxs[\s\S]*?module\s*=\s*(?:"([^"]+)"|'([^']+)')[\s\S]*?\/?>/gi;
61
64
  var FULL_CAPABILITIES = {
62
65
  verification: true,
63
66
  completion: true,
@@ -84,11 +87,73 @@ try {
84
87
  } catch {
85
88
  hasSchematicsTypes = false;
86
89
  }
90
+ function parseVueSfc(content, filename = "component.vue") {
91
+ try {
92
+ const compilerSfc = require2("@vue/compiler-sfc");
93
+ return compilerSfc.parse(content, { filename });
94
+ } catch {
95
+ return void 0;
96
+ }
97
+ }
98
+ function collectWxsModuleNames(templateContent) {
99
+ if (!templateContent) {
100
+ return [];
101
+ }
102
+ const names = /* @__PURE__ */ new Set();
103
+ for (const match of templateContent.matchAll(WXS_MODULE_RE)) {
104
+ const name2 = match[1] ?? match[2];
105
+ if (name2) {
106
+ names.add(name2);
107
+ }
108
+ }
109
+ return [...names];
110
+ }
111
+ function createWxsModuleDeclarations(moduleNames) {
112
+ if (!moduleNames.length) {
113
+ return "";
114
+ }
115
+ return moduleNames.map((name2) => `const ${name2} = {} as Record<string, (...args: any[]) => any>`).join("\n");
116
+ }
117
+ function appendWxsDeclarations(code, moduleNames) {
118
+ const declarations = createWxsModuleDeclarations(moduleNames);
119
+ if (!declarations) {
120
+ return code;
121
+ }
122
+ return code ? `${code}
123
+
124
+ ${declarations}
125
+ ` : `${declarations}
126
+ `;
127
+ }
128
+ function createSyntheticScriptSetup(moduleNames) {
129
+ const content = createWxsModuleDeclarations(moduleNames);
130
+ if (!content) {
131
+ return void 0;
132
+ }
133
+ return {
134
+ type: "script",
135
+ content,
136
+ loc: {
137
+ source: `<script setup lang="ts">
138
+ ${content}
139
+ </script>`,
140
+ start: { column: 1, line: 1, offset: 0 },
141
+ end: { column: 1, line: 1, offset: 0 }
142
+ },
143
+ attrs: {
144
+ setup: true,
145
+ lang: "ts"
146
+ },
147
+ lang: "ts",
148
+ setup: true,
149
+ name: "scriptSetup"
150
+ };
151
+ }
87
152
  function normalizeFilename(filename) {
88
153
  if (!filename) {
89
154
  return "";
90
155
  }
91
- return filename.replace(/\\/g, "/");
156
+ return filename.replace(BACKSLASH_RE, "/");
92
157
  }
93
158
  function inferConfigType(filename) {
94
159
  const normalized = normalizeFilename(filename);
@@ -156,7 +221,7 @@ function injectSchemaIntoJsonObject(content, schemaId) {
156
221
  return content;
157
222
  }
158
223
  const afterLeft = content.slice(leftBraceIndex + 1);
159
- const firstNonSpace = afterLeft.match(/\S/);
224
+ const firstNonSpace = afterLeft.match(NON_SPACE_RE);
160
225
  const nextCharIndex = firstNonSpace ? leftBraceIndex + 1 + firstNonSpace.index : -1;
161
226
  const isEmptyObject = nextCharIndex >= 0 && content[nextCharIndex] === "}";
162
227
  const schemaLine = ` "$schema": "${schemaId}"`;
@@ -183,6 +248,27 @@ var plugin = (ctx) => {
183
248
  return {
184
249
  name,
185
250
  version: PLUGIN_VERSION,
251
+ order: -1,
252
+ parseSFC2(fileName, languageId, content) {
253
+ if (languageId !== "vue") {
254
+ return;
255
+ }
256
+ const parsed = parseVueSfc(content, fileName);
257
+ if (!parsed) {
258
+ return;
259
+ }
260
+ const descriptor = parsed.descriptor;
261
+ const wxsModuleNames = collectWxsModuleNames(descriptor.template?.content);
262
+ if (!wxsModuleNames.length) {
263
+ return parsed;
264
+ }
265
+ if (descriptor.scriptSetup) {
266
+ descriptor.scriptSetup.content = appendWxsDeclarations(descriptor.scriptSetup.content, wxsModuleNames);
267
+ } else {
268
+ descriptor.scriptSetup = createSyntheticScriptSetup(wxsModuleNames);
269
+ }
270
+ return parsed;
271
+ },
186
272
  getEmbeddedCodes(_, sfc) {
187
273
  const names = [];
188
274
  for (let i = 0; i < sfc.customBlocks.length; i++) {
package/dist/index.mjs CHANGED
@@ -24,6 +24,9 @@ var JSON_LANG = "json";
24
24
  var JSON5_LANG = "json5";
25
25
  var PLUGIN_VERSION = 2.2;
26
26
  var TS_LANG = "ts";
27
+ var BACKSLASH_RE = /\\/g;
28
+ var NON_SPACE_RE = /\S/;
29
+ var WXS_MODULE_RE = /<wxs[\s\S]*?module\s*=\s*(?:"([^"]+)"|'([^']+)')[\s\S]*?\/?>/gi;
27
30
  var FULL_CAPABILITIES = {
28
31
  verification: true,
29
32
  completion: true,
@@ -50,11 +53,73 @@ try {
50
53
  } catch {
51
54
  hasSchematicsTypes = false;
52
55
  }
56
+ function parseVueSfc(content, filename = "component.vue") {
57
+ try {
58
+ const compilerSfc = require2("@vue/compiler-sfc");
59
+ return compilerSfc.parse(content, { filename });
60
+ } catch {
61
+ return void 0;
62
+ }
63
+ }
64
+ function collectWxsModuleNames(templateContent) {
65
+ if (!templateContent) {
66
+ return [];
67
+ }
68
+ const names = /* @__PURE__ */ new Set();
69
+ for (const match of templateContent.matchAll(WXS_MODULE_RE)) {
70
+ const name2 = match[1] ?? match[2];
71
+ if (name2) {
72
+ names.add(name2);
73
+ }
74
+ }
75
+ return [...names];
76
+ }
77
+ function createWxsModuleDeclarations(moduleNames) {
78
+ if (!moduleNames.length) {
79
+ return "";
80
+ }
81
+ return moduleNames.map((name2) => `const ${name2} = {} as Record<string, (...args: any[]) => any>`).join("\n");
82
+ }
83
+ function appendWxsDeclarations(code, moduleNames) {
84
+ const declarations = createWxsModuleDeclarations(moduleNames);
85
+ if (!declarations) {
86
+ return code;
87
+ }
88
+ return code ? `${code}
89
+
90
+ ${declarations}
91
+ ` : `${declarations}
92
+ `;
93
+ }
94
+ function createSyntheticScriptSetup(moduleNames) {
95
+ const content = createWxsModuleDeclarations(moduleNames);
96
+ if (!content) {
97
+ return void 0;
98
+ }
99
+ return {
100
+ type: "script",
101
+ content,
102
+ loc: {
103
+ source: `<script setup lang="ts">
104
+ ${content}
105
+ </script>`,
106
+ start: { column: 1, line: 1, offset: 0 },
107
+ end: { column: 1, line: 1, offset: 0 }
108
+ },
109
+ attrs: {
110
+ setup: true,
111
+ lang: "ts"
112
+ },
113
+ lang: "ts",
114
+ setup: true,
115
+ name: "scriptSetup"
116
+ };
117
+ }
53
118
  function normalizeFilename(filename) {
54
119
  if (!filename) {
55
120
  return "";
56
121
  }
57
- return filename.replace(/\\/g, "/");
122
+ return filename.replace(BACKSLASH_RE, "/");
58
123
  }
59
124
  function inferConfigType(filename) {
60
125
  const normalized = normalizeFilename(filename);
@@ -122,7 +187,7 @@ function injectSchemaIntoJsonObject(content, schemaId) {
122
187
  return content;
123
188
  }
124
189
  const afterLeft = content.slice(leftBraceIndex + 1);
125
- const firstNonSpace = afterLeft.match(/\S/);
190
+ const firstNonSpace = afterLeft.match(NON_SPACE_RE);
126
191
  const nextCharIndex = firstNonSpace ? leftBraceIndex + 1 + firstNonSpace.index : -1;
127
192
  const isEmptyObject = nextCharIndex >= 0 && content[nextCharIndex] === "}";
128
193
  const schemaLine = ` "$schema": "${schemaId}"`;
@@ -149,6 +214,27 @@ var plugin = (ctx) => {
149
214
  return {
150
215
  name,
151
216
  version: PLUGIN_VERSION,
217
+ order: -1,
218
+ parseSFC2(fileName, languageId, content) {
219
+ if (languageId !== "vue") {
220
+ return;
221
+ }
222
+ const parsed = parseVueSfc(content, fileName);
223
+ if (!parsed) {
224
+ return;
225
+ }
226
+ const descriptor = parsed.descriptor;
227
+ const wxsModuleNames = collectWxsModuleNames(descriptor.template?.content);
228
+ if (!wxsModuleNames.length) {
229
+ return parsed;
230
+ }
231
+ if (descriptor.scriptSetup) {
232
+ descriptor.scriptSetup.content = appendWxsDeclarations(descriptor.scriptSetup.content, wxsModuleNames);
233
+ } else {
234
+ descriptor.scriptSetup = createSyntheticScriptSetup(wxsModuleNames);
235
+ }
236
+ return parsed;
237
+ },
152
238
  getEmbeddedCodes(_, sfc) {
153
239
  const names = [];
154
240
  for (let i = 0; i < sfc.customBlocks.length; i++) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weapp-vite/volar",
3
- "version": "2.0.4",
3
+ "version": "2.0.5",
4
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",
@@ -47,6 +47,7 @@
47
47
  "build": "tsup",
48
48
  "test": "vitest run",
49
49
  "test:dev": "vitest",
50
+ "typecheck": "tsc --noEmit",
50
51
  "release": "pnpm publish",
51
52
  "lint": "eslint .",
52
53
  "lint:fix": "eslint . --fix"