@wevu/compiler 6.12.4 → 6.13.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/dist/index.d.mts +2 -0
- package/dist/index.mjs +122 -62
- package/package.json +4 -4
package/dist/index.d.mts
CHANGED
|
@@ -38,6 +38,7 @@ declare const WE_VU_RUNTIME_APIS: {
|
|
|
38
38
|
readonly createApp: "createApp";
|
|
39
39
|
readonly createWevuComponent: "createWevuComponent";
|
|
40
40
|
readonly createWevuScopedSlotComponent: "createWevuScopedSlotComponent";
|
|
41
|
+
readonly defineAppSetup: "defineAppSetup";
|
|
41
42
|
readonly defineComponent: "defineComponent";
|
|
42
43
|
readonly setWevuDefaults: "setWevuDefaults";
|
|
43
44
|
};
|
|
@@ -559,6 +560,7 @@ declare function stripJsonMacroCallsFromCode(code: string, filename: string): st
|
|
|
559
560
|
*/
|
|
560
561
|
declare function extractJsonMacroFromScriptSetup(content: string, filename: string, lang?: string, options?: {
|
|
561
562
|
merge?: (target: Record<string, any>, source: Record<string, any>) => Record<string, any> | void;
|
|
563
|
+
preambleContent?: string;
|
|
562
564
|
}): Promise<{
|
|
563
565
|
stripped: string;
|
|
564
566
|
config?: Record<string, any>;
|
package/dist/index.mjs
CHANGED
|
@@ -3,19 +3,20 @@ import path from "pathe";
|
|
|
3
3
|
import { NodeTypes, baseParse } from "@vue/compiler-core";
|
|
4
4
|
import { createHash } from "node:crypto";
|
|
5
5
|
import * as t from "@weapp-vite/ast/babelTypes";
|
|
6
|
-
import fs from "fs-extra";
|
|
7
6
|
import MagicString from "magic-string";
|
|
8
7
|
import { recursive } from "merge";
|
|
9
8
|
import { bundleRequire } from "rolldown-require";
|
|
10
|
-
import {
|
|
11
|
-
import
|
|
9
|
+
import { access, mkdir, readFile as readFile$1, rm, stat, writeFile } from "node:fs/promises";
|
|
10
|
+
import { BABEL_TS_MODULE_PARSER_OPTIONS, generate, parse as parse$3, parseJsLike, traverse } from "@weapp-vite/ast/babel";
|
|
11
|
+
import fs from "node:fs";
|
|
12
12
|
import os from "node:os";
|
|
13
13
|
import process from "node:process";
|
|
14
14
|
import { collectFeatureFlagsFromCode, collectJsxImportedComponentsAndDefaultExportFromBabelAst, collectJsxTemplateTagsFromBabelExpression, getRenderPropertyFromComponentOptions, parseJsLikeWithEngine, resolveRenderExpressionFromComponentOptions, toStaticObjectKey, unwrapTypeScriptExpression } from "@weapp-vite/ast";
|
|
15
15
|
import { LRUCache } from "lru-cache";
|
|
16
16
|
import { compileScript, parse } from "vue/compiler-sfc";
|
|
17
17
|
import { fileURLToPath } from "node:url";
|
|
18
|
-
import { parse as parse$1 } from "
|
|
18
|
+
import { parse as parse$1 } from "@vue/compiler-dom";
|
|
19
|
+
import { parse as parse$2 } from "comment-json";
|
|
19
20
|
import vm from "node:vm";
|
|
20
21
|
//#region src/auto-import-components/builtin.auto.ts
|
|
21
22
|
const components = [
|
|
@@ -145,6 +146,7 @@ const WE_VU_RUNTIME_APIS = {
|
|
|
145
146
|
createApp: "createApp",
|
|
146
147
|
createWevuComponent: "createWevuComponent",
|
|
147
148
|
createWevuScopedSlotComponent: "createWevuScopedSlotComponent",
|
|
149
|
+
defineAppSetup: "defineAppSetup",
|
|
148
150
|
defineComponent: "defineComponent",
|
|
149
151
|
setWevuDefaults: "setWevuDefaults"
|
|
150
152
|
};
|
|
@@ -296,6 +298,34 @@ function collectKeptStatementPaths(programPath, macroStatements) {
|
|
|
296
298
|
};
|
|
297
299
|
}
|
|
298
300
|
//#endregion
|
|
301
|
+
//#region src/utils/fs.ts
|
|
302
|
+
/**
|
|
303
|
+
* 创建目录,行为等价于 ensureDir。
|
|
304
|
+
*/
|
|
305
|
+
async function ensureDir(path) {
|
|
306
|
+
await mkdir(path, { recursive: true });
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* 删除文件或目录,行为等价于 remove。
|
|
310
|
+
*/
|
|
311
|
+
async function remove(path) {
|
|
312
|
+
await rm(path, {
|
|
313
|
+
recursive: true,
|
|
314
|
+
force: true
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
/**
|
|
318
|
+
* 判断路径是否存在。
|
|
319
|
+
*/
|
|
320
|
+
async function pathExists$1(path) {
|
|
321
|
+
try {
|
|
322
|
+
await access(path);
|
|
323
|
+
return true;
|
|
324
|
+
} catch {
|
|
325
|
+
return false;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
//#endregion
|
|
299
329
|
//#region src/plugins/vue/transform/tempDirLock.ts
|
|
300
330
|
const locks = /* @__PURE__ */ new Map();
|
|
301
331
|
async function withTempDirLock(tempDir, fn) {
|
|
@@ -386,7 +416,7 @@ function getWevuConfigCacheRoot() {
|
|
|
386
416
|
if (env) return path.resolve(env);
|
|
387
417
|
const cwd = process.cwd();
|
|
388
418
|
const projectCacheDir = path.join(cwd, ".wevu-config");
|
|
389
|
-
if (fs
|
|
419
|
+
if (fs.existsSync(projectCacheDir)) return projectCacheDir;
|
|
390
420
|
return path.join(os.tmpdir(), "weapp-vite", "wevu-config");
|
|
391
421
|
}
|
|
392
422
|
function resolveWevuConfigTempDir(fromDir) {
|
|
@@ -411,8 +441,8 @@ function resolveMacroAlias(macroName) {
|
|
|
411
441
|
return macroName === "defineAppJson" ? "__weapp_defineAppJson" : macroName === "definePageJson" ? "__weapp_definePageJson" : macroName === "defineComponentJson" ? "__weapp_defineComponentJson" : macroName === "defineSitemapJson" ? "__weapp_defineSitemapJson" : "__weapp_defineThemeJson";
|
|
412
442
|
}
|
|
413
443
|
async function evaluateScriptSetupJsonMacro(params) {
|
|
414
|
-
const { originalContent, filename, lang, macroName, macroStatements, bodyPaths, keptStatementPaths, options } = params;
|
|
415
|
-
const ms = new MagicString(originalContent);
|
|
444
|
+
const { preambleContent, originalContent, filename, lang, macroName, macroStatements, bodyPaths, keptStatementPaths, options } = params;
|
|
445
|
+
const ms = new MagicString(preambleContent ? `${preambleContent}\n${originalContent}` : originalContent);
|
|
416
446
|
const dir = path.dirname(filename);
|
|
417
447
|
const tempRoot = resolveWevuConfigTempDir(dir);
|
|
418
448
|
const basename = path.basename(filename, path.extname(filename));
|
|
@@ -448,9 +478,9 @@ const __weapp_defineThemeJson = (config) => (__weapp_json_macro_values.push(conf
|
|
|
448
478
|
`.trimStart() + ms.toString() + `\nexport default __weapp_json_macro_values\n`;
|
|
449
479
|
const extension = resolveScriptSetupExtension$1(lang);
|
|
450
480
|
return await withTempDirLock(tempRoot, async () => {
|
|
451
|
-
await
|
|
481
|
+
await ensureDir(tempDir);
|
|
452
482
|
const tempFile = path.join(tempDir, `${basename}.json-macro.${unique}.${extension}`);
|
|
453
|
-
await
|
|
483
|
+
await writeFile(tempFile, evalSource, "utf8");
|
|
454
484
|
try {
|
|
455
485
|
const { mod, dependencies } = await bundleRequire({
|
|
456
486
|
filepath: tempFile,
|
|
@@ -478,7 +508,7 @@ const __weapp_defineThemeJson = (config) => (__weapp_json_macro_values.push(conf
|
|
|
478
508
|
};
|
|
479
509
|
} finally {
|
|
480
510
|
try {
|
|
481
|
-
await
|
|
511
|
+
await remove(tempDir);
|
|
482
512
|
} catch {}
|
|
483
513
|
}
|
|
484
514
|
});
|
|
@@ -494,7 +524,7 @@ const JSON_MACROS = new Set([
|
|
|
494
524
|
]);
|
|
495
525
|
function parseScriptSetupAst(content, filename) {
|
|
496
526
|
try {
|
|
497
|
-
return parse$
|
|
527
|
+
return parse$3(content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
498
528
|
} catch (error) {
|
|
499
529
|
const message = error instanceof Error ? error.message : String(error);
|
|
500
530
|
throw new Error(`解析 ${filename} 中的 <script setup> 失败:${message}`);
|
|
@@ -558,7 +588,7 @@ function stripScriptSetupMacroStatements(content, ast, filename) {
|
|
|
558
588
|
function stripJsonMacroCallsFromCode(code, filename) {
|
|
559
589
|
let ast;
|
|
560
590
|
try {
|
|
561
|
-
ast = parse$
|
|
591
|
+
ast = parse$3(code, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
562
592
|
} catch (error) {
|
|
563
593
|
try {
|
|
564
594
|
ast = parseJsLike(code);
|
|
@@ -581,6 +611,26 @@ function stripJsonMacroCallsFromCode(code, filename) {
|
|
|
581
611
|
}
|
|
582
612
|
//#endregion
|
|
583
613
|
//#region src/plugins/vue/transform/jsonMacros/index.ts
|
|
614
|
+
async function evaluateJsonMacroConfig(content, filename, lang, options) {
|
|
615
|
+
const ast = parseScriptSetupAst(options?.preambleContent ? `${options.preambleContent}\n${content}` : content, filename);
|
|
616
|
+
const { macroNames, macroStatements } = collectMacroCallPaths(ast, filename);
|
|
617
|
+
const macroName = assertSingleMacro(macroNames, filename);
|
|
618
|
+
if (!macroName || !macroStatements.length) return;
|
|
619
|
+
const programPath = findProgramPath(ast);
|
|
620
|
+
if (!programPath) return;
|
|
621
|
+
const { bodyPaths, keptStatementPaths } = collectKeptStatementPaths(programPath, macroStatements);
|
|
622
|
+
return await evaluateScriptSetupJsonMacro({
|
|
623
|
+
preambleContent: options?.preambleContent,
|
|
624
|
+
originalContent: content,
|
|
625
|
+
filename,
|
|
626
|
+
lang,
|
|
627
|
+
macroName,
|
|
628
|
+
macroStatements,
|
|
629
|
+
bodyPaths,
|
|
630
|
+
keptStatementPaths,
|
|
631
|
+
options
|
|
632
|
+
});
|
|
633
|
+
}
|
|
584
634
|
/**
|
|
585
635
|
* 从 `<script setup>` 中提取 JSON 宏配置并返回剥离后的代码。
|
|
586
636
|
*/
|
|
@@ -602,25 +652,6 @@ async function extractJsonMacroFromScriptSetup(content, filename, lang, options)
|
|
|
602
652
|
macroHash
|
|
603
653
|
};
|
|
604
654
|
}
|
|
605
|
-
async function evaluateJsonMacroConfig(content, filename, lang, options) {
|
|
606
|
-
const ast = parseScriptSetupAst(content, filename);
|
|
607
|
-
const { macroNames, macroStatements } = collectMacroCallPaths(ast, filename);
|
|
608
|
-
const macroName = assertSingleMacro(macroNames, filename);
|
|
609
|
-
if (!macroName || !macroStatements.length) return;
|
|
610
|
-
const programPath = findProgramPath(ast);
|
|
611
|
-
if (!programPath) return;
|
|
612
|
-
const { bodyPaths, keptStatementPaths } = collectKeptStatementPaths(programPath, macroStatements);
|
|
613
|
-
return await evaluateScriptSetupJsonMacro({
|
|
614
|
-
originalContent: content,
|
|
615
|
-
filename,
|
|
616
|
-
lang,
|
|
617
|
-
macroName,
|
|
618
|
-
macroStatements,
|
|
619
|
-
bodyPaths,
|
|
620
|
-
keptStatementPaths,
|
|
621
|
-
options
|
|
622
|
-
});
|
|
623
|
-
}
|
|
624
655
|
//#endregion
|
|
625
656
|
//#region src/plugins/vue/transform/jsonMerge.ts
|
|
626
657
|
function toPlainRecord(value) {
|
|
@@ -1900,6 +1931,18 @@ function createImportVisitors(program, state) {
|
|
|
1900
1931
|
}
|
|
1901
1932
|
} };
|
|
1902
1933
|
}
|
|
1934
|
+
//#endregion
|
|
1935
|
+
//#region src/plugins/vue/transform/transformScript/macros/appSetup.ts
|
|
1936
|
+
/**
|
|
1937
|
+
* 为 defineAppSetup 宏注入 wevu 运行时导入,允许在 <script setup> 中免导入调用。
|
|
1938
|
+
*/
|
|
1939
|
+
function createAppSetupVisitors(program, state) {
|
|
1940
|
+
return { CallExpression(path) {
|
|
1941
|
+
if (!t.isIdentifier(path.node.callee, { name: WE_VU_RUNTIME_APIS.defineAppSetup })) return;
|
|
1942
|
+
ensureRuntimeImport(program, WE_VU_RUNTIME_APIS.defineAppSetup);
|
|
1943
|
+
state.transformed = true;
|
|
1944
|
+
} };
|
|
1945
|
+
}
|
|
1903
1946
|
function isPlainRecord(value) {
|
|
1904
1947
|
if (!value || typeof value !== "object" || Array.isArray(value)) return false;
|
|
1905
1948
|
const proto = Object.getPrototypeOf(value);
|
|
@@ -2127,15 +2170,18 @@ function createStripTypesVisitors(state) {
|
|
|
2127
2170
|
}
|
|
2128
2171
|
//#endregion
|
|
2129
2172
|
//#region src/plugins/vue/transform/transformScript/macros/index.ts
|
|
2130
|
-
function createMacroVisitors(state) {
|
|
2173
|
+
function createMacroVisitors(program, state) {
|
|
2174
|
+
const appSetupVisitors = createAppSetupVisitors(program, state);
|
|
2131
2175
|
const setupExposeVisitors = createSetupExposeVisitors(state);
|
|
2132
2176
|
const stripTypesVisitors = createStripTypesVisitors(state);
|
|
2133
2177
|
const pageMetaVisitors = createPageMetaVisitors(state);
|
|
2134
2178
|
return {
|
|
2179
|
+
...appSetupVisitors,
|
|
2135
2180
|
...setupExposeVisitors,
|
|
2136
2181
|
...stripTypesVisitors,
|
|
2137
2182
|
...pageMetaVisitors,
|
|
2138
2183
|
CallExpression(path) {
|
|
2184
|
+
appSetupVisitors.CallExpression?.(path);
|
|
2139
2185
|
setupExposeVisitors.CallExpression?.(path);
|
|
2140
2186
|
stripTypesVisitors.CallExpression?.(path);
|
|
2141
2187
|
if (!path.removed) pageMetaVisitors.CallExpression?.(path);
|
|
@@ -2451,7 +2497,7 @@ function parseBabelExpression(exp) {
|
|
|
2451
2497
|
const cached = babelExpressionCache.get(exp);
|
|
2452
2498
|
if (cached !== void 0) return cached === false ? null : cached;
|
|
2453
2499
|
try {
|
|
2454
|
-
const stmt = parse$
|
|
2500
|
+
const stmt = parse$3(`(${exp})`, {
|
|
2455
2501
|
sourceType: "module",
|
|
2456
2502
|
plugins: ["typescript"]
|
|
2457
2503
|
}).program.body[0];
|
|
@@ -2469,7 +2515,7 @@ function parseBabelExpression(exp) {
|
|
|
2469
2515
|
}
|
|
2470
2516
|
function parseBabelExpressionFile(exp) {
|
|
2471
2517
|
try {
|
|
2472
|
-
const ast = parse$
|
|
2518
|
+
const ast = parse$3(`(${exp})`, {
|
|
2473
2519
|
sourceType: "module",
|
|
2474
2520
|
plugins: ["typescript"]
|
|
2475
2521
|
});
|
|
@@ -2877,7 +2923,7 @@ function rewriteDefaultExport(ast, state, options, enabledPageFeatures, serializ
|
|
|
2877
2923
|
* 转换 Vue SFC 脚本:处理宏、导入、默认导出与 wevu 相关注入。
|
|
2878
2924
|
*/
|
|
2879
2925
|
function transformScript(source, options) {
|
|
2880
|
-
const ast = parse$
|
|
2926
|
+
const ast = parse$3(source, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
2881
2927
|
const warn = resolveWarnHandler(options?.warn);
|
|
2882
2928
|
const state = {
|
|
2883
2929
|
transformed: false,
|
|
@@ -2890,7 +2936,7 @@ function transformScript(source, options) {
|
|
|
2890
2936
|
const parsedWevuDefaults = serializedWevuDefaults ? JSON.parse(serializedWevuDefaults) : void 0;
|
|
2891
2937
|
traverse(ast, vueSfcTransformPlugin().visitor);
|
|
2892
2938
|
traverse(ast, {
|
|
2893
|
-
...createMacroVisitors(state),
|
|
2939
|
+
...createMacroVisitors(ast.program, state),
|
|
2894
2940
|
...createImportVisitors(ast.program, state),
|
|
2895
2941
|
...createCollectVisitors(state)
|
|
2896
2942
|
});
|
|
@@ -2979,7 +3025,7 @@ const TRAILING_EMPTY_CONCAT_RE = /\s*\+\s*''\s*$/g;
|
|
|
2979
3025
|
function normalizeWxmlExpression(exp) {
|
|
2980
3026
|
if (!exp.includes("`") && !exp.includes("??") && !exp.includes("?.")) return exp;
|
|
2981
3027
|
try {
|
|
2982
|
-
const ast = parse$
|
|
3028
|
+
const ast = parse$3(`(${exp})`, {
|
|
2983
3029
|
sourceType: "module",
|
|
2984
3030
|
plugins: ["typescript"]
|
|
2985
3031
|
});
|
|
@@ -3117,7 +3163,7 @@ function removeRenderOptionFromObjectExpression(node) {
|
|
|
3117
3163
|
function stripRenderOptionFromScript(source, filename, warn) {
|
|
3118
3164
|
let ast;
|
|
3119
3165
|
try {
|
|
3120
|
-
ast = parse$
|
|
3166
|
+
ast = parse$3(source, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
3121
3167
|
} catch {
|
|
3122
3168
|
return source;
|
|
3123
3169
|
}
|
|
@@ -3753,7 +3799,7 @@ function createJsxCompileContext(options) {
|
|
|
3753
3799
|
* 单次解析同时编译模板和收集自动组件上下文,避免重复 babelParse 和 traverse。
|
|
3754
3800
|
*/
|
|
3755
3801
|
function compileJsxTemplateAndCollectComponents(source, filename, options) {
|
|
3756
|
-
const ast = parse$
|
|
3802
|
+
const ast = parse$3(source, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
3757
3803
|
const context = createJsxCompileContext(options);
|
|
3758
3804
|
const { renderExpression, autoComponentContext } = analyzeJsxAst(ast, context);
|
|
3759
3805
|
let template;
|
|
@@ -3877,7 +3923,7 @@ const pathExistsCache = new LRUCache({ max: 4096 });
|
|
|
3877
3923
|
*/
|
|
3878
3924
|
async function isInvalidate(id) {
|
|
3879
3925
|
const cached = mtimeCache.get(id);
|
|
3880
|
-
const stats = await
|
|
3926
|
+
const stats = await stat(id);
|
|
3881
3927
|
const mtimeMs = typeof stats?.mtimeMs === "number" ? stats.mtimeMs : NaN;
|
|
3882
3928
|
const size = typeof stats?.size === "number" ? stats.size : NaN;
|
|
3883
3929
|
if (!Number.isFinite(mtimeMs)) return true;
|
|
@@ -3902,7 +3948,7 @@ async function readFile(id, options) {
|
|
|
3902
3948
|
if (!checkMtime) {
|
|
3903
3949
|
const cached = loadCache.get(id);
|
|
3904
3950
|
if (cached !== void 0) return cached;
|
|
3905
|
-
const content = normalizeLineEndings(await
|
|
3951
|
+
const content = normalizeLineEndings(await readFile$1(id, encoding));
|
|
3906
3952
|
loadCache.set(id, content);
|
|
3907
3953
|
return content;
|
|
3908
3954
|
}
|
|
@@ -3910,7 +3956,7 @@ async function readFile(id, options) {
|
|
|
3910
3956
|
const cached = loadCache.get(id);
|
|
3911
3957
|
if (cached !== void 0) return cached;
|
|
3912
3958
|
}
|
|
3913
|
-
const content = normalizeLineEndings(await
|
|
3959
|
+
const content = normalizeLineEndings(await readFile$1(id, encoding));
|
|
3914
3960
|
loadCache.set(id, content);
|
|
3915
3961
|
return content;
|
|
3916
3962
|
}
|
|
@@ -3921,7 +3967,7 @@ async function pathExists(id, options) {
|
|
|
3921
3967
|
const ttlMs = options?.ttlMs;
|
|
3922
3968
|
const cached = pathExistsCache.get(id);
|
|
3923
3969
|
if (cached !== void 0) return cached;
|
|
3924
|
-
const exists = await
|
|
3970
|
+
const exists = await pathExists$1(id);
|
|
3925
3971
|
if (typeof ttlMs === "number" && Number.isFinite(ttlMs) && ttlMs > 0) pathExistsCache.set(id, exists, { ttl: ttlMs });
|
|
3926
3972
|
else pathExistsCache.set(id, exists);
|
|
3927
3973
|
return exists;
|
|
@@ -5969,7 +6015,7 @@ function parseSlotPropsExpression(exp, context) {
|
|
|
5969
6015
|
const trimmed = exp.trim();
|
|
5970
6016
|
if (!trimmed) return {};
|
|
5971
6017
|
try {
|
|
5972
|
-
const stmt = parse$
|
|
6018
|
+
const stmt = parse$3(`(${trimmed}) => {}`, {
|
|
5973
6019
|
sourceType: "module",
|
|
5974
6020
|
plugins: ["typescript"]
|
|
5975
6021
|
}).program.body[0];
|
|
@@ -6588,7 +6634,7 @@ function compileVueTemplateToWxml(template, filename, options) {
|
|
|
6588
6634
|
const wxsExtension = options?.wxsExtension;
|
|
6589
6635
|
const scopedSlotsRequireProps = options?.scopedSlotsRequireProps ?? options?.scopedSlotsCompiler !== "augmented";
|
|
6590
6636
|
try {
|
|
6591
|
-
const ast =
|
|
6637
|
+
const ast = parse$1(template, {
|
|
6592
6638
|
isVoidTag: (tag) => HTML_VOID_TAGS.has(tag),
|
|
6593
6639
|
onError: (error) => {
|
|
6594
6640
|
warnings.push(`模板解析失败:${error.message}`);
|
|
@@ -6683,10 +6729,9 @@ async function evaluateJsLikeConfig(source, filename, lang) {
|
|
|
6683
6729
|
const unique = `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
6684
6730
|
const tempDir = path.join(tempRoot, `${basename}.config.${unique}`);
|
|
6685
6731
|
return await withTempDirLock(tempRoot, async () => {
|
|
6686
|
-
await
|
|
6732
|
+
await ensureDir(tempDir);
|
|
6687
6733
|
const tempFile = path.join(tempDir, `${basename}.${unique}.${extension}`);
|
|
6688
|
-
|
|
6689
|
-
await fs.writeFile(tempFile, rewritten, "utf8");
|
|
6734
|
+
await writeFile(tempFile, rewriteJsLikeImportsForTempDir(source, dir, tempDir), "utf8");
|
|
6690
6735
|
try {
|
|
6691
6736
|
const { mod } = await bundleRequire({
|
|
6692
6737
|
filepath: tempFile,
|
|
@@ -6699,7 +6744,7 @@ async function evaluateJsLikeConfig(source, filename, lang) {
|
|
|
6699
6744
|
throw new Error("配置块必须导出对象或返回对象的函数。");
|
|
6700
6745
|
} finally {
|
|
6701
6746
|
try {
|
|
6702
|
-
await
|
|
6747
|
+
await remove(tempDir);
|
|
6703
6748
|
} catch {}
|
|
6704
6749
|
}
|
|
6705
6750
|
});
|
|
@@ -6715,7 +6760,7 @@ async function compileConfigBlocks(blocks, filename, options) {
|
|
|
6715
6760
|
const lang = normalizeConfigLang(block.lang);
|
|
6716
6761
|
try {
|
|
6717
6762
|
if (isJsonLikeLang(lang)) {
|
|
6718
|
-
const parsed = parse$
|
|
6763
|
+
const parsed = parse$2(block.content, void 0, true);
|
|
6719
6764
|
if (options?.merge) {
|
|
6720
6765
|
const merged = options.merge(accumulator, parsed);
|
|
6721
6766
|
if (merged && typeof merged === "object" && !Array.isArray(merged)) accumulator = merged;
|
|
@@ -6849,7 +6894,7 @@ const SERIALIZABLE_NATIVE_FUNCTIONS = new Map([
|
|
|
6849
6894
|
]);
|
|
6850
6895
|
function isParsableFunctionExpressionSource(source) {
|
|
6851
6896
|
try {
|
|
6852
|
-
parse$
|
|
6897
|
+
parse$3(`(${source})`, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
6853
6898
|
return true;
|
|
6854
6899
|
} catch {
|
|
6855
6900
|
return false;
|
|
@@ -6857,7 +6902,7 @@ function isParsableFunctionExpressionSource(source) {
|
|
|
6857
6902
|
}
|
|
6858
6903
|
function tryConvertObjectMethodSourceToFunctionExpression(source) {
|
|
6859
6904
|
try {
|
|
6860
|
-
const statement = parse$
|
|
6905
|
+
const statement = parse$3(`({ ${source} })`, BABEL_TS_MODULE_PARSER_OPTIONS).program.body[0];
|
|
6861
6906
|
if (!statement || !t.isExpressionStatement(statement) || !t.isObjectExpression(statement.expression)) return null;
|
|
6862
6907
|
const firstProperty = statement.expression.properties[0];
|
|
6863
6908
|
if (!firstProperty) return null;
|
|
@@ -6876,13 +6921,13 @@ function normalizeFunctionSourceToExpression(source) {
|
|
|
6876
6921
|
throw new Error("defineOptions 的参数中包含无法序列化的函数值。");
|
|
6877
6922
|
}
|
|
6878
6923
|
function parseExpressionFromSource(source) {
|
|
6879
|
-
const statement = parse$
|
|
6924
|
+
const statement = parse$3(`(${source})`, BABEL_TS_MODULE_PARSER_OPTIONS).program.body[0];
|
|
6880
6925
|
if (!statement || !t.isExpressionStatement(statement)) throw new Error("defineOptions 的函数源码解析失败。");
|
|
6881
6926
|
return statement.expression;
|
|
6882
6927
|
}
|
|
6883
6928
|
function rewriteFunctionSourceWithScopeValues(source, scopeValues) {
|
|
6884
6929
|
if (!Object.keys(scopeValues).length) return source;
|
|
6885
|
-
const wrappedAst = parse$
|
|
6930
|
+
const wrappedAst = parse$3(`(${source})`, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
6886
6931
|
traverse(wrappedAst, { Identifier(path) {
|
|
6887
6932
|
if (!path.isReferencedIdentifier()) return;
|
|
6888
6933
|
if (path.scope.hasBinding(path.node.name)) return;
|
|
@@ -6939,7 +6984,7 @@ function serializeStaticValueToExpression(value, seen = /* @__PURE__ */ new Weak
|
|
|
6939
6984
|
//#endregion
|
|
6940
6985
|
//#region src/plugins/vue/transform/defineOptions/inline.ts
|
|
6941
6986
|
function collectDefineOptionsStatements(content, filename) {
|
|
6942
|
-
const ast = parse$
|
|
6987
|
+
const ast = parse$3(content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
6943
6988
|
const statements = [];
|
|
6944
6989
|
traverse(ast, { CallExpression(path) {
|
|
6945
6990
|
const callee = path.node.callee;
|
|
@@ -6958,7 +7003,7 @@ function evaluateStaticExpression(source) {
|
|
|
6958
7003
|
return vm.runInNewContext(`(${source})`, {});
|
|
6959
7004
|
}
|
|
6960
7005
|
function collectStaticTopLevelScopeValues(content) {
|
|
6961
|
-
const ast = parse$
|
|
7006
|
+
const ast = parse$3(content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
6962
7007
|
const scopeValues = {};
|
|
6963
7008
|
for (const statement of ast.program.body) {
|
|
6964
7009
|
if (!t.isVariableDeclaration(statement) || statement.kind !== "const") continue;
|
|
@@ -7045,9 +7090,9 @@ const __weapp_defineOptions = (value) => (__weapp_define_options_values.push(val
|
|
|
7045
7090
|
export const __weapp_define_options = __weapp_define_options_values
|
|
7046
7091
|
`;
|
|
7047
7092
|
return await withTempDirLock(tempRoot, async () => {
|
|
7048
|
-
await
|
|
7093
|
+
await ensureDir(tempDir);
|
|
7049
7094
|
const tempFile = path.join(tempDir, `${basename}.define-options.${unique}.${extension}`);
|
|
7050
|
-
await
|
|
7095
|
+
await writeFile(tempFile, evalSource, "utf8");
|
|
7051
7096
|
try {
|
|
7052
7097
|
const { mod, dependencies } = await bundleRequire({
|
|
7053
7098
|
filepath: tempFile,
|
|
@@ -7061,7 +7106,7 @@ export const __weapp_define_options = __weapp_define_options_values
|
|
|
7061
7106
|
};
|
|
7062
7107
|
} finally {
|
|
7063
7108
|
try {
|
|
7064
|
-
await
|
|
7109
|
+
await remove(tempDir);
|
|
7065
7110
|
} catch {}
|
|
7066
7111
|
}
|
|
7067
7112
|
});
|
|
@@ -7122,10 +7167,19 @@ async function inlineScriptSetupDefineOptionsArgs(content, filename, lang) {
|
|
|
7122
7167
|
const SETUP_CALL_RE = /\bsetup\s*\(/;
|
|
7123
7168
|
const DEFINE_OPTIONS_CALL_RE = /\bdefineOptions\s*\(/;
|
|
7124
7169
|
const APP_VUE_FILE_RE = /[\\/]app\.vue$/;
|
|
7170
|
+
function resolveSfcScriptLangLabel(lang) {
|
|
7171
|
+
return typeof lang === "string" && lang.length > 0 ? lang : "(default)";
|
|
7172
|
+
}
|
|
7173
|
+
function assertMatchingSfcScriptLang(descriptor, filename) {
|
|
7174
|
+
const scriptLang = descriptor.script?.lang;
|
|
7175
|
+
const scriptSetupLang = descriptor.scriptSetup?.lang;
|
|
7176
|
+
if (!descriptor.script || !descriptor.scriptSetup || scriptLang === scriptSetupLang) return;
|
|
7177
|
+
throw new Error(`解析 ${filename} 失败:同一个 SFC 中 <script> 与 <script setup> 的 lang 必须一致,当前分别为 ${resolveSfcScriptLangLabel(scriptLang)} 与 ${resolveSfcScriptLangLabel(scriptSetupLang)}`);
|
|
7178
|
+
}
|
|
7125
7179
|
function extractDefineOptionsHash(content) {
|
|
7126
7180
|
let ast;
|
|
7127
7181
|
try {
|
|
7128
|
-
ast = parse$
|
|
7182
|
+
ast = parse$3(content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
7129
7183
|
} catch {
|
|
7130
7184
|
return;
|
|
7131
7185
|
}
|
|
@@ -7154,6 +7208,7 @@ async function parseVueFile(source, filename, options) {
|
|
|
7154
7208
|
const error = errors[0];
|
|
7155
7209
|
throw new Error(`解析 ${filename} 失败:${error.message}`);
|
|
7156
7210
|
}
|
|
7211
|
+
assertMatchingSfcScriptLang(descriptor, filename);
|
|
7157
7212
|
let resolvedDescriptor = descriptor;
|
|
7158
7213
|
let sfcSrcDeps;
|
|
7159
7214
|
if (options?.sfcSrc) {
|
|
@@ -7178,7 +7233,10 @@ async function parseVueFile(source, filename, options) {
|
|
|
7178
7233
|
});
|
|
7179
7234
|
const scriptSetup = resolvedDescriptor.scriptSetup;
|
|
7180
7235
|
if (scriptSetup?.content) {
|
|
7181
|
-
const extracted = await extractJsonMacroFromScriptSetup(scriptSetup.content, filename, scriptSetup.lang, {
|
|
7236
|
+
const extracted = await extractJsonMacroFromScriptSetup(scriptSetup.content, filename, scriptSetup.lang, {
|
|
7237
|
+
merge: (target, source) => mergeJson(target, source, "macro"),
|
|
7238
|
+
preambleContent: resolvedDescriptor.script?.content
|
|
7239
|
+
});
|
|
7182
7240
|
if (extracted.stripped !== scriptSetup.content) if (scriptSetup.src) descriptorForCompile = {
|
|
7183
7241
|
...descriptorForCompile,
|
|
7184
7242
|
scriptSetup: {
|
|
@@ -7201,6 +7259,7 @@ async function parseVueFile(source, filename, options) {
|
|
|
7201
7259
|
const error = nextErrors[0];
|
|
7202
7260
|
throw new Error(`解析 ${filename} 失败:${error.message}`);
|
|
7203
7261
|
}
|
|
7262
|
+
assertMatchingSfcScriptLang(nextDescriptor, filename);
|
|
7204
7263
|
if (options?.sfcSrc) {
|
|
7205
7264
|
const resolvedNext = await resolveSfcBlockSrc(nextDescriptor, filename, options.sfcSrc);
|
|
7206
7265
|
descriptorForCompile = resolvedNext.descriptor;
|
|
@@ -7240,6 +7299,7 @@ async function parseVueFile(source, filename, options) {
|
|
|
7240
7299
|
const error = nextErrors[0];
|
|
7241
7300
|
throw new Error(`解析 ${filename} 失败:${error.message}`);
|
|
7242
7301
|
}
|
|
7302
|
+
assertMatchingSfcScriptLang(nextDescriptor, filename);
|
|
7243
7303
|
if (options?.sfcSrc) {
|
|
7244
7304
|
const resolvedNext = await resolveSfcBlockSrc(nextDescriptor, filename, options.sfcSrc);
|
|
7245
7305
|
descriptorForCompile = resolvedNext.descriptor;
|
|
@@ -7284,7 +7344,7 @@ async function compileScriptPhase(descriptor, descriptorForCompile, filename, op
|
|
|
7284
7344
|
if (autoUsingComponents && descriptor.scriptSetup && descriptor.template) {
|
|
7285
7345
|
const templateComponentNames = collectTemplateComponentNames(descriptor.template.content, filename, autoUsingComponents.warn ?? options?.warn);
|
|
7286
7346
|
if (templateComponentNames.size) try {
|
|
7287
|
-
const setupAst = parse$
|
|
7347
|
+
const setupAst = parse$3(descriptorForCompile.scriptSetup.content, BABEL_TS_MODULE_PARSER_OPTIONS);
|
|
7288
7348
|
const pending = [];
|
|
7289
7349
|
traverse(setupAst, { ImportDeclaration(path) {
|
|
7290
7350
|
if (path.node.importKind === "type") return;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wevu/compiler",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "6.
|
|
4
|
+
"version": "6.13.0",
|
|
5
5
|
"description": "wevu 编译器基础包,面向小程序模板的编译与转换",
|
|
6
6
|
"author": "ice breaker <1324318532@qq.com>",
|
|
7
7
|
"license": "MIT",
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
11
|
"url": "git+https://github.com/weapp-vite/weapp-vite.git",
|
|
12
|
-
"directory": "packages/wevu-compiler"
|
|
12
|
+
"directory": "packages-runtime/wevu-compiler"
|
|
13
13
|
},
|
|
14
14
|
"bugs": {
|
|
15
15
|
"url": "https://github.com/weapp-vite/weapp-vite/issues"
|
|
@@ -42,15 +42,15 @@
|
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@vue/compiler-core": "^3.5.31",
|
|
45
|
+
"@vue/compiler-dom": "^3.5.31",
|
|
45
46
|
"comment-json": "^4.6.2",
|
|
46
|
-
"fs-extra": "^11.3.4",
|
|
47
47
|
"lru-cache": "^11.2.7",
|
|
48
48
|
"magic-string": "^0.30.21",
|
|
49
49
|
"merge": "^2.1.1",
|
|
50
50
|
"pathe": "^2.0.3",
|
|
51
51
|
"vue": "^3.5.31",
|
|
52
52
|
"@weapp-core/shared": "3.0.2",
|
|
53
|
-
"@weapp-vite/ast": "6.
|
|
53
|
+
"@weapp-vite/ast": "6.13.0",
|
|
54
54
|
"rolldown-require": "2.0.12"
|
|
55
55
|
},
|
|
56
56
|
"publishConfig": {
|