@weapp-vite/volar 2.0.0 → 2.0.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 +300 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.mjs +5 -1
- package/package.json +4 -3
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name2 in all)
|
|
10
|
+
__defProp(target, name2, { get: all[name2], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
default: () => index_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(index_exports);
|
|
36
|
+
var import_node_module = require("module");
|
|
37
|
+
var import_node_path = __toESM(require("path"));
|
|
38
|
+
var import_node_process = __toESM(require("process"));
|
|
39
|
+
|
|
40
|
+
// package.json
|
|
41
|
+
var name = "@weapp-vite/volar";
|
|
42
|
+
|
|
43
|
+
// src/schema.ts
|
|
44
|
+
var import_schematics = require("@weapp-core/schematics");
|
|
45
|
+
function getSchemaForType(type) {
|
|
46
|
+
const definition = import_schematics.JSON_SCHEMA_DEFINITIONS.find((d) => d.typeName === type);
|
|
47
|
+
if (!definition) {
|
|
48
|
+
return null;
|
|
49
|
+
}
|
|
50
|
+
return definition.schema;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// src/index.ts
|
|
54
|
+
var BLOCK_TYPE = "json";
|
|
55
|
+
var JS_LANG = "js";
|
|
56
|
+
var JSONC_LANG = "jsonc";
|
|
57
|
+
var JSON_LANG = "json";
|
|
58
|
+
var JSON5_LANG = "json5";
|
|
59
|
+
var PLUGIN_VERSION = 2.2;
|
|
60
|
+
var TS_LANG = "ts";
|
|
61
|
+
var FULL_CAPABILITIES = {
|
|
62
|
+
verification: true,
|
|
63
|
+
completion: true,
|
|
64
|
+
semantic: true,
|
|
65
|
+
navigation: true,
|
|
66
|
+
structure: true,
|
|
67
|
+
format: true
|
|
68
|
+
};
|
|
69
|
+
var VOID_CAPABILITIES = {
|
|
70
|
+
verification: false,
|
|
71
|
+
completion: false,
|
|
72
|
+
semantic: false,
|
|
73
|
+
navigation: false,
|
|
74
|
+
structure: false,
|
|
75
|
+
format: false
|
|
76
|
+
};
|
|
77
|
+
var require2 = (0, import_node_module.createRequire)(
|
|
78
|
+
typeof module !== "undefined" && module.filename ? module.filename : import_node_path.default.join(import_node_process.default.cwd(), "weapp-vite-volar.cjs")
|
|
79
|
+
);
|
|
80
|
+
var hasSchematicsTypes = false;
|
|
81
|
+
try {
|
|
82
|
+
require2.resolve("@weapp-core/schematics");
|
|
83
|
+
hasSchematicsTypes = true;
|
|
84
|
+
} catch {
|
|
85
|
+
hasSchematicsTypes = false;
|
|
86
|
+
}
|
|
87
|
+
function normalizeFilename(filename) {
|
|
88
|
+
if (!filename) {
|
|
89
|
+
return "";
|
|
90
|
+
}
|
|
91
|
+
return filename.replace(/\\/g, "/");
|
|
92
|
+
}
|
|
93
|
+
function inferConfigType(filename) {
|
|
94
|
+
const normalized = normalizeFilename(filename);
|
|
95
|
+
if (normalized.endsWith("/app.vue")) {
|
|
96
|
+
return "App";
|
|
97
|
+
}
|
|
98
|
+
if (normalized.includes("/plugin/")) {
|
|
99
|
+
return "Plugin";
|
|
100
|
+
}
|
|
101
|
+
if (normalized.includes("/components/")) {
|
|
102
|
+
return "Component";
|
|
103
|
+
}
|
|
104
|
+
if (normalized.includes("/theme/")) {
|
|
105
|
+
return "Theme";
|
|
106
|
+
}
|
|
107
|
+
if (normalized.includes("/sitemap")) {
|
|
108
|
+
return "Sitemap";
|
|
109
|
+
}
|
|
110
|
+
return "Page";
|
|
111
|
+
}
|
|
112
|
+
function normalizeLang(lang) {
|
|
113
|
+
if (!lang) {
|
|
114
|
+
return JSON_LANG;
|
|
115
|
+
}
|
|
116
|
+
const lower = lang.toLowerCase();
|
|
117
|
+
if (lower === "txt") {
|
|
118
|
+
return JSON_LANG;
|
|
119
|
+
}
|
|
120
|
+
return lower;
|
|
121
|
+
}
|
|
122
|
+
function findExportDefaultExpression(code, tsModule, lang) {
|
|
123
|
+
const scriptKind = lang === TS_LANG ? tsModule.ScriptKind.TS : tsModule.ScriptKind.JS;
|
|
124
|
+
const sourceFile = tsModule.createSourceFile(
|
|
125
|
+
`config.${lang}`,
|
|
126
|
+
code,
|
|
127
|
+
tsModule.ScriptTarget.Latest,
|
|
128
|
+
true,
|
|
129
|
+
scriptKind
|
|
130
|
+
);
|
|
131
|
+
for (const statement of sourceFile.statements) {
|
|
132
|
+
if (tsModule.isExportAssignment(statement)) {
|
|
133
|
+
const expressionStart = statement.expression.getStart(sourceFile);
|
|
134
|
+
const expressionEnd = statement.expression.getEnd();
|
|
135
|
+
const leading = code.slice(0, statement.getStart(sourceFile));
|
|
136
|
+
const expression = code.slice(expressionStart, expressionEnd);
|
|
137
|
+
const trailing = code.slice(statement.getEnd());
|
|
138
|
+
return {
|
|
139
|
+
expression,
|
|
140
|
+
expressionStart,
|
|
141
|
+
expressionEnd,
|
|
142
|
+
leading,
|
|
143
|
+
trailing
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return null;
|
|
148
|
+
}
|
|
149
|
+
function injectSchemaIntoJsonObject(content, schemaId) {
|
|
150
|
+
const trimmed = content.trim();
|
|
151
|
+
if (!trimmed.startsWith("{")) {
|
|
152
|
+
return content;
|
|
153
|
+
}
|
|
154
|
+
const leftBraceIndex = content.indexOf("{");
|
|
155
|
+
if (leftBraceIndex < 0) {
|
|
156
|
+
return content;
|
|
157
|
+
}
|
|
158
|
+
const afterLeft = content.slice(leftBraceIndex + 1);
|
|
159
|
+
const firstNonSpace = afterLeft.match(/\S/);
|
|
160
|
+
const nextCharIndex = firstNonSpace ? leftBraceIndex + 1 + firstNonSpace.index : -1;
|
|
161
|
+
const isEmptyObject = nextCharIndex >= 0 && content[nextCharIndex] === "}";
|
|
162
|
+
const schemaLine = ` "$schema": "${schemaId}"`;
|
|
163
|
+
const injected = isEmptyObject ? `{
|
|
164
|
+
${schemaLine}
|
|
165
|
+
}` : `{
|
|
166
|
+
${schemaLine},${content.slice(leftBraceIndex + 1)}`;
|
|
167
|
+
if (trimmed === content) {
|
|
168
|
+
return injected;
|
|
169
|
+
}
|
|
170
|
+
const leading = content.slice(0, content.indexOf(trimmed));
|
|
171
|
+
const trailing = content.slice(content.indexOf(trimmed) + trimmed.length);
|
|
172
|
+
return `${leading}${injected}${trailing}`;
|
|
173
|
+
}
|
|
174
|
+
var plugin = (ctx) => {
|
|
175
|
+
let tsModule = ctx?.modules?.typescript;
|
|
176
|
+
if (!tsModule) {
|
|
177
|
+
try {
|
|
178
|
+
tsModule = require2("typescript");
|
|
179
|
+
} catch {
|
|
180
|
+
tsModule = void 0;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return {
|
|
184
|
+
name,
|
|
185
|
+
version: PLUGIN_VERSION,
|
|
186
|
+
getEmbeddedCodes(_, sfc) {
|
|
187
|
+
const names = [];
|
|
188
|
+
for (let i = 0; i < sfc.customBlocks.length; i++) {
|
|
189
|
+
const block = sfc.customBlocks[i];
|
|
190
|
+
if (block.type === BLOCK_TYPE) {
|
|
191
|
+
const normalizedLang = normalizeLang(block.lang);
|
|
192
|
+
const isJsLike = normalizedLang === JS_LANG || normalizedLang === TS_LANG;
|
|
193
|
+
if (isJsLike) {
|
|
194
|
+
names.push({ id: `${BLOCK_TYPE}_${i}`, lang: TS_LANG });
|
|
195
|
+
continue;
|
|
196
|
+
}
|
|
197
|
+
const embeddedLang = normalizedLang === JSON_LANG || normalizedLang === JSONC_LANG || normalizedLang === JSON5_LANG ? JSONC_LANG : JSON_LANG;
|
|
198
|
+
names.push({ id: `${BLOCK_TYPE}_${i}`, lang: embeddedLang });
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return names;
|
|
202
|
+
},
|
|
203
|
+
resolveEmbeddedCode(fileName, sfc, embeddedCode) {
|
|
204
|
+
const match = embeddedCode.id.match(new RegExp(`^${BLOCK_TYPE}_(\\d+)$`));
|
|
205
|
+
if (!match) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const index = Number.parseInt(match[1]);
|
|
209
|
+
const block = sfc.customBlocks[index];
|
|
210
|
+
if (!block) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const normalizedLang = normalizeLang(block.lang);
|
|
214
|
+
const configType = inferConfigType(fileName);
|
|
215
|
+
if (!hasSchematicsTypes) {
|
|
216
|
+
embeddedCode.content.push([
|
|
217
|
+
block.content,
|
|
218
|
+
block.name,
|
|
219
|
+
0,
|
|
220
|
+
FULL_CAPABILITIES
|
|
221
|
+
]);
|
|
222
|
+
return;
|
|
223
|
+
}
|
|
224
|
+
const userWantsJs = normalizedLang === JS_LANG || normalizedLang === TS_LANG;
|
|
225
|
+
if (userWantsJs) {
|
|
226
|
+
const parsed = tsModule && findExportDefaultExpression(block.content, tsModule, normalizedLang);
|
|
227
|
+
if (parsed && hasSchematicsTypes) {
|
|
228
|
+
const typeImport = `import type { ${configType} as __WeappConfig } from '@weapp-core/schematics'
|
|
229
|
+
`;
|
|
230
|
+
const helper = "const __weapp_defineConfig = <T extends __WeappConfig>(config: T) => config\n\n";
|
|
231
|
+
embeddedCode.content.push([
|
|
232
|
+
`${typeImport}${helper}`,
|
|
233
|
+
void 0,
|
|
234
|
+
0,
|
|
235
|
+
VOID_CAPABILITIES
|
|
236
|
+
]);
|
|
237
|
+
if (parsed.leading) {
|
|
238
|
+
embeddedCode.content.push([
|
|
239
|
+
parsed.leading,
|
|
240
|
+
block.name,
|
|
241
|
+
0,
|
|
242
|
+
FULL_CAPABILITIES
|
|
243
|
+
]);
|
|
244
|
+
}
|
|
245
|
+
embeddedCode.content.push([
|
|
246
|
+
"export default __weapp_defineConfig(",
|
|
247
|
+
void 0,
|
|
248
|
+
parsed.expressionStart,
|
|
249
|
+
VOID_CAPABILITIES
|
|
250
|
+
]);
|
|
251
|
+
embeddedCode.content.push([
|
|
252
|
+
parsed.expression,
|
|
253
|
+
block.name,
|
|
254
|
+
parsed.expressionStart,
|
|
255
|
+
FULL_CAPABILITIES
|
|
256
|
+
]);
|
|
257
|
+
embeddedCode.content.push([
|
|
258
|
+
")",
|
|
259
|
+
void 0,
|
|
260
|
+
parsed.expressionEnd,
|
|
261
|
+
VOID_CAPABILITIES
|
|
262
|
+
]);
|
|
263
|
+
if (parsed.trailing) {
|
|
264
|
+
embeddedCode.content.push([
|
|
265
|
+
parsed.trailing,
|
|
266
|
+
block.name,
|
|
267
|
+
parsed.expressionEnd,
|
|
268
|
+
FULL_CAPABILITIES
|
|
269
|
+
]);
|
|
270
|
+
}
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
embeddedCode.content.push([
|
|
274
|
+
block.content,
|
|
275
|
+
block.name,
|
|
276
|
+
0,
|
|
277
|
+
FULL_CAPABILITIES
|
|
278
|
+
]);
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
const schema = getSchemaForType(configType);
|
|
282
|
+
if (schema && schema.$id && !block.content.includes("$schema")) {
|
|
283
|
+
embeddedCode.content.push([
|
|
284
|
+
injectSchemaIntoJsonObject(block.content, schema.$id),
|
|
285
|
+
block.name,
|
|
286
|
+
0,
|
|
287
|
+
FULL_CAPABILITIES
|
|
288
|
+
]);
|
|
289
|
+
return;
|
|
290
|
+
}
|
|
291
|
+
embeddedCode.content.push([
|
|
292
|
+
block.content,
|
|
293
|
+
block.name,
|
|
294
|
+
0,
|
|
295
|
+
FULL_CAPABILITIES
|
|
296
|
+
]);
|
|
297
|
+
}
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
var index_default = plugin;
|
package/dist/index.d.ts
ADDED
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
import { createRequire } from "module";
|
|
3
|
+
import path from "path";
|
|
4
|
+
import process from "process";
|
|
3
5
|
|
|
4
6
|
// package.json
|
|
5
7
|
var name = "@weapp-vite/volar";
|
|
@@ -38,7 +40,9 @@ var VOID_CAPABILITIES = {
|
|
|
38
40
|
structure: false,
|
|
39
41
|
format: false
|
|
40
42
|
};
|
|
41
|
-
var require2 = createRequire(
|
|
43
|
+
var require2 = createRequire(
|
|
44
|
+
typeof module !== "undefined" && module.filename ? module.filename : path.join(process.cwd(), "weapp-vite-volar.cjs")
|
|
45
|
+
);
|
|
42
46
|
var hasSchematicsTypes = false;
|
|
43
47
|
try {
|
|
44
48
|
require2.resolve("@weapp-core/schematics");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@weapp-vite/volar",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.2",
|
|
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",
|
|
@@ -26,10 +26,11 @@
|
|
|
26
26
|
"exports": {
|
|
27
27
|
".": {
|
|
28
28
|
"types": "./dist/index.d.mts",
|
|
29
|
-
"import": "./dist/index.mjs"
|
|
29
|
+
"import": "./dist/index.mjs",
|
|
30
|
+
"require": "./dist/index.cjs"
|
|
30
31
|
}
|
|
31
32
|
},
|
|
32
|
-
"main": "./dist/index.
|
|
33
|
+
"main": "./dist/index.cjs",
|
|
33
34
|
"module": "./dist/index.mjs",
|
|
34
35
|
"types": "./dist/index.d.mts",
|
|
35
36
|
"files": [
|