@weapp-vite/web 1.3.15 → 1.3.17
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 +2 -2
- package/dist/_virtual/_rolldown/runtime.mjs +18 -0
- package/dist/compiler/wxml/attributes.mjs +93 -0
- package/dist/compiler/wxml/attributes.mjs.map +1 -0
- package/dist/compiler/wxml/compile.mjs +158 -0
- package/dist/compiler/wxml/compile.mjs.map +1 -0
- package/dist/compiler/wxml/dependency.mjs +31 -0
- package/dist/compiler/wxml/dependency.mjs.map +1 -0
- package/dist/compiler/wxml/interpolation.mjs +142 -0
- package/dist/compiler/wxml/interpolation.mjs.map +1 -0
- package/dist/compiler/wxml/navigation.mjs +85 -0
- package/dist/compiler/wxml/navigation.mjs.map +1 -0
- package/dist/compiler/wxml/parser.mjs +41 -0
- package/dist/compiler/wxml/parser.mjs.map +1 -0
- package/dist/compiler/wxml/renderer.mjs +114 -0
- package/dist/compiler/wxml/renderer.mjs.map +1 -0
- package/dist/compiler/wxml/specialNodes.mjs +85 -0
- package/dist/compiler/wxml/specialNodes.mjs.map +1 -0
- package/dist/compiler/wxs.mjs +83 -0
- package/dist/compiler/wxs.mjs.map +1 -0
- package/dist/css/wxss.d.mts +25 -0
- package/dist/css/wxss.mjs +17 -0
- package/dist/css/wxss.mjs.map +1 -0
- package/dist/index.d.mts +18 -2
- package/dist/index.mjs +18 -3
- package/dist/plugin/constants.mjs +43 -0
- package/dist/plugin/constants.mjs.map +1 -0
- package/dist/plugin/entry.mjs +29 -0
- package/dist/plugin/entry.mjs.map +1 -0
- package/dist/plugin/files.mjs +58 -0
- package/dist/plugin/files.mjs.map +1 -0
- package/dist/plugin/index.d.mts +33 -0
- package/dist/plugin/index.mjs +188 -0
- package/dist/plugin/index.mjs.map +1 -0
- package/dist/plugin/navigation.mjs +20 -0
- package/dist/plugin/navigation.mjs.map +1 -0
- package/dist/plugin/path.mjs +85 -0
- package/dist/plugin/path.mjs.map +1 -0
- package/dist/plugin/register.mjs +83 -0
- package/dist/plugin/register.mjs.map +1 -0
- package/dist/plugin/scan.mjs +195 -0
- package/dist/plugin/scan.mjs.map +1 -0
- package/dist/plugin/scanConfig.mjs +45 -0
- package/dist/plugin/scanConfig.mjs.map +1 -0
- package/dist/plugin/state.mjs +22 -0
- package/dist/plugin/state.mjs.map +1 -0
- package/dist/plugin/types.d.mts +46 -0
- package/dist/plugin.d.mts +3 -2
- package/dist/plugin.mjs +3 -2
- package/dist/runtime/button/helpers.mjs +92 -0
- package/dist/runtime/button/helpers.mjs.map +1 -0
- package/dist/runtime/button/index.d.mts +9 -0
- package/dist/runtime/button/index.mjs +198 -0
- package/dist/runtime/button/index.mjs.map +1 -0
- package/dist/runtime/button/style.mjs +168 -0
- package/dist/runtime/button/style.mjs.map +1 -0
- package/dist/runtime/component/behavior.mjs +98 -0
- package/dist/runtime/component/behavior.mjs.map +1 -0
- package/dist/runtime/component/constants.mjs +13 -0
- package/dist/runtime/component/constants.mjs.map +1 -0
- package/dist/runtime/component/dom.mjs +32 -0
- package/dist/runtime/component/dom.mjs.map +1 -0
- package/dist/runtime/component/element.mjs +208 -0
- package/dist/runtime/component/element.mjs.map +1 -0
- package/dist/runtime/component/events.mjs +49 -0
- package/dist/runtime/component/events.mjs.map +1 -0
- package/dist/runtime/component/index.d.mts +7 -0
- package/dist/runtime/component/index.mjs +44 -0
- package/dist/runtime/component/index.mjs.map +1 -0
- package/dist/runtime/component/state.mjs +63 -0
- package/dist/runtime/component/state.mjs.map +1 -0
- package/dist/runtime/component/types.d.mts +49 -0
- package/dist/runtime/component/utils.mjs +39 -0
- package/dist/runtime/component/utils.mjs.map +1 -0
- package/dist/runtime/execution.d.mts +7 -0
- package/dist/runtime/execution.mjs +37 -0
- package/dist/runtime/execution.mjs.map +1 -0
- package/dist/runtime/index.d.mts +16 -2
- package/dist/runtime/index.mjs +16 -2
- package/dist/runtime/legacyTemplate/dom.mjs +76 -0
- package/dist/runtime/legacyTemplate/dom.mjs.map +1 -0
- package/dist/runtime/legacyTemplate/expression.mjs +113 -0
- package/dist/runtime/legacyTemplate/expression.mjs.map +1 -0
- package/dist/runtime/legacyTemplate/index.d.mts +9 -0
- package/dist/runtime/legacyTemplate/index.mjs +132 -0
- package/dist/runtime/legacyTemplate/index.mjs.map +1 -0
- package/dist/runtime/legacyTemplate/types.d.mts +6 -0
- package/dist/runtime/navigationBar/index.d.mts +10 -0
- package/dist/runtime/navigationBar/index.mjs +162 -0
- package/dist/runtime/navigationBar/index.mjs.map +1 -0
- package/dist/runtime/navigationBar/style.mjs +108 -0
- package/dist/runtime/navigationBar/style.mjs.map +1 -0
- package/dist/runtime/polyfill/ad.mjs +150 -0
- package/dist/runtime/polyfill/ad.mjs.map +1 -0
- package/dist/runtime/polyfill/appState.mjs +31 -0
- package/dist/runtime/polyfill/appState.mjs.map +1 -0
- package/dist/runtime/polyfill/async.mjs +31 -0
- package/dist/runtime/polyfill/async.mjs.map +1 -0
- package/dist/runtime/polyfill/auth.mjs +120 -0
- package/dist/runtime/polyfill/auth.mjs.map +1 -0
- package/dist/runtime/polyfill/authApi.mjs +101 -0
- package/dist/runtime/polyfill/authApi.mjs.map +1 -0
- package/dist/runtime/polyfill/background.mjs +31 -0
- package/dist/runtime/polyfill/background.mjs.map +1 -0
- package/dist/runtime/polyfill/canvasContext.mjs +115 -0
- package/dist/runtime/polyfill/canvasContext.mjs.map +1 -0
- package/dist/runtime/polyfill/capability.mjs +20 -0
- package/dist/runtime/polyfill/capability.mjs.map +1 -0
- package/dist/runtime/polyfill/cloud.mjs +39 -0
- package/dist/runtime/polyfill/cloud.mjs.map +1 -0
- package/dist/runtime/polyfill/device.mjs +44 -0
- package/dist/runtime/polyfill/device.mjs.map +1 -0
- package/dist/runtime/polyfill/deviceApi.mjs +31 -0
- package/dist/runtime/polyfill/deviceApi.mjs.map +1 -0
- package/dist/runtime/polyfill/deviceAuthSystemApi.d.mts +102 -0
- package/dist/runtime/polyfill/deviceAuthSystemApi.mjs +144 -0
- package/dist/runtime/polyfill/deviceAuthSystemApi.mjs.map +1 -0
- package/dist/runtime/polyfill/filePicker.mjs +158 -0
- package/dist/runtime/polyfill/filePicker.mjs.map +1 -0
- package/dist/runtime/polyfill/fileSystemManager.mjs +45 -0
- package/dist/runtime/polyfill/fileSystemManager.mjs.map +1 -0
- package/dist/runtime/polyfill/files.mjs +118 -0
- package/dist/runtime/polyfill/files.mjs.map +1 -0
- package/dist/runtime/polyfill/index.d.mts +40 -0
- package/dist/runtime/polyfill/index.mjs +110 -0
- package/dist/runtime/polyfill/index.mjs.map +1 -0
- package/dist/runtime/polyfill/interaction.mjs +38 -0
- package/dist/runtime/polyfill/interaction.mjs.map +1 -0
- package/dist/runtime/polyfill/interactionApi.mjs +54 -0
- package/dist/runtime/polyfill/interactionApi.mjs.map +1 -0
- package/dist/runtime/polyfill/location.mjs +106 -0
- package/dist/runtime/polyfill/location.mjs.map +1 -0
- package/dist/runtime/polyfill/locationApi.mjs +120 -0
- package/dist/runtime/polyfill/locationApi.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaActions.mjs +52 -0
- package/dist/runtime/polyfill/mediaActions.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/file.mjs +63 -0
- package/dist/runtime/polyfill/mediaApi/file.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/info.mjs +59 -0
- package/dist/runtime/polyfill/mediaApi/info.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/picker.mjs +82 -0
- package/dist/runtime/polyfill/mediaApi/picker.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/preview.mjs +39 -0
- package/dist/runtime/polyfill/mediaApi/preview.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaApi/process.mjs +46 -0
- package/dist/runtime/polyfill/mediaApi/process.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaInfo.mjs +136 -0
- package/dist/runtime/polyfill/mediaInfo.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaPicker.mjs +213 -0
- package/dist/runtime/polyfill/mediaPicker.mjs.map +1 -0
- package/dist/runtime/polyfill/mediaProcess.mjs +65 -0
- package/dist/runtime/polyfill/mediaProcess.mjs.map +1 -0
- package/dist/runtime/polyfill/menuApi.mjs +70 -0
- package/dist/runtime/polyfill/menuApi.mjs.map +1 -0
- package/dist/runtime/polyfill/navigationBarRuntime.mjs +66 -0
- package/dist/runtime/polyfill/navigationBarRuntime.mjs.map +1 -0
- package/dist/runtime/polyfill/network/request.mjs +162 -0
- package/dist/runtime/polyfill/network/request.mjs.map +1 -0
- package/dist/runtime/polyfill/network/requestBridge.mjs +49 -0
- package/dist/runtime/polyfill/network/requestBridge.mjs.map +1 -0
- package/dist/runtime/polyfill/network/status.mjs +55 -0
- package/dist/runtime/polyfill/network/status.mjs.map +1 -0
- package/dist/runtime/polyfill/platformApi.mjs +33 -0
- package/dist/runtime/polyfill/platformApi.mjs.map +1 -0
- package/dist/runtime/polyfill/platformRuntime.mjs +120 -0
- package/dist/runtime/polyfill/platformRuntime.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/dom.mjs +36 -0
- package/dist/runtime/polyfill/routeRuntime/dom.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/lifecycle.mjs +83 -0
- package/dist/runtime/polyfill/routeRuntime/lifecycle.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/options.d.mts +33 -0
- package/dist/runtime/polyfill/routeRuntime/options.mjs +76 -0
- package/dist/runtime/polyfill/routeRuntime/options.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime/url.mjs +35 -0
- package/dist/runtime/polyfill/routeRuntime/url.mjs.map +1 -0
- package/dist/runtime/polyfill/routeRuntime.d.mts +43 -0
- package/dist/runtime/polyfill/routeRuntime.mjs +202 -0
- package/dist/runtime/polyfill/routeRuntime.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeCapabilityApi.mjs +26 -0
- package/dist/runtime/polyfill/runtimeCapabilityApi.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeDataApi.d.mts +79 -0
- package/dist/runtime/polyfill/runtimeDataApi.mjs +134 -0
- package/dist/runtime/polyfill/runtimeDataApi.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeInfra.mjs +40 -0
- package/dist/runtime/polyfill/runtimeInfra.mjs.map +1 -0
- package/dist/runtime/polyfill/runtimeOps.mjs +62 -0
- package/dist/runtime/polyfill/runtimeOps.mjs.map +1 -0
- package/dist/runtime/polyfill/selectorQuery.mjs +204 -0
- package/dist/runtime/polyfill/selectorQuery.mjs.map +1 -0
- package/dist/runtime/polyfill/storage.mjs +103 -0
- package/dist/runtime/polyfill/storage.mjs.map +1 -0
- package/dist/runtime/polyfill/storageAsync.mjs +62 -0
- package/dist/runtime/polyfill/storageAsync.mjs.map +1 -0
- package/dist/runtime/polyfill/subscribe.mjs +30 -0
- package/dist/runtime/polyfill/subscribe.mjs.map +1 -0
- package/dist/runtime/polyfill/system.mjs +111 -0
- package/dist/runtime/polyfill/system.mjs.map +1 -0
- package/dist/runtime/polyfill/systemApi.mjs +65 -0
- package/dist/runtime/polyfill/systemApi.mjs.map +1 -0
- package/dist/runtime/polyfill/types/base.d.mts +12 -0
- package/dist/runtime/polyfill/types/common.d.mts +126 -0
- package/dist/runtime/polyfill/types/locationRuntime.d.mts +151 -0
- package/dist/runtime/polyfill/types/mediaAuth.d.mts +152 -0
- package/dist/runtime/polyfill/types/platformRuntime.d.mts +141 -0
- package/dist/runtime/polyfill/types/systemAuth.d.mts +122 -0
- package/dist/runtime/polyfill/ui.mjs +135 -0
- package/dist/runtime/polyfill/ui.mjs.map +1 -0
- package/dist/runtime/polyfill/uiFeedback.mjs +35 -0
- package/dist/runtime/polyfill/uiFeedback.mjs.map +1 -0
- package/dist/runtime/polyfill/uiMediaApi.d.mts +172 -0
- package/dist/runtime/polyfill/uiMediaApi.mjs +162 -0
- package/dist/runtime/polyfill/uiMediaApi.mjs.map +1 -0
- package/dist/runtime/polyfill/videoContext.mjs +56 -0
- package/dist/runtime/polyfill/videoContext.mjs.map +1 -0
- package/dist/runtime/polyfill/vkSession.mjs +58 -0
- package/dist/runtime/polyfill/vkSession.mjs.map +1 -0
- package/dist/runtime/polyfill/windowResize.mjs +39 -0
- package/dist/runtime/polyfill/windowResize.mjs.map +1 -0
- package/dist/runtime/polyfill/worker.mjs +77 -0
- package/dist/runtime/polyfill/worker.mjs.map +1 -0
- package/dist/runtime/polyfill.d.mts +5 -0
- package/dist/runtime/renderContext.d.mts +21 -0
- package/dist/runtime/renderContext.mjs +147 -0
- package/dist/runtime/renderContext.mjs.map +1 -0
- package/dist/runtime/rpx.d.mts +9 -0
- package/dist/runtime/rpx.mjs +27 -0
- package/dist/runtime/rpx.mjs.map +1 -0
- package/dist/runtime/style.d.mts +6 -0
- package/dist/runtime/style.mjs +37 -0
- package/dist/runtime/style.mjs.map +1 -0
- package/dist/runtime/template.d.mts +10 -0
- package/dist/runtime/warning.d.mts +10 -0
- package/dist/runtime/warning.mjs +43 -0
- package/dist/runtime/warning.mjs.map +1 -0
- package/dist/{slugify-B4l45KNs.mjs → shared/slugify.mjs} +3 -1
- package/dist/shared/slugify.mjs.map +1 -0
- package/dist/shared/wxml.mjs +83 -0
- package/dist/shared/wxml.mjs.map +1 -0
- package/package.json +2 -2
- package/dist/index-w9xxIH-l.d.mts +0 -1229
- package/dist/plugin-BARxSPO8.mjs +0 -1567
- package/dist/plugin-DP2iPVmw.d.mts +0 -96
- package/dist/runtime-D6j2_x_N.mjs +0 -6251
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { SCRIPT_EXTS, STYLE_EXTS, TEMPLATE_EXTS } from "./constants.mjs";
|
|
2
|
+
import { extname } from "pathe";
|
|
3
|
+
import { fs } from "@weapp-core/shared/fs";
|
|
4
|
+
import { bundleRequire } from "rolldown-require";
|
|
5
|
+
|
|
6
|
+
//#region src/plugin/files.ts
|
|
7
|
+
function isRecord(value) {
|
|
8
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
9
|
+
}
|
|
10
|
+
async function readJsonFile(pathname) {
|
|
11
|
+
const candidates = [pathname];
|
|
12
|
+
if (pathname.endsWith(".json")) candidates.push(`${pathname}.ts`, `${pathname}.js`);
|
|
13
|
+
for (const candidate of candidates) {
|
|
14
|
+
if (!await fs.pathExists(candidate)) continue;
|
|
15
|
+
if (candidate.endsWith(".json")) {
|
|
16
|
+
const json = await fs.readJson(candidate).catch(() => void 0);
|
|
17
|
+
if (!isRecord(json)) return;
|
|
18
|
+
return json;
|
|
19
|
+
}
|
|
20
|
+
const { mod } = await bundleRequire({
|
|
21
|
+
filepath: candidate,
|
|
22
|
+
preserveTemporaryFile: true
|
|
23
|
+
});
|
|
24
|
+
const resolved = typeof mod.default === "function" ? await mod.default() : mod.default;
|
|
25
|
+
if (!isRecord(resolved)) return;
|
|
26
|
+
return resolved;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
async function resolveJsonPath(basePath) {
|
|
30
|
+
const candidates = [basePath];
|
|
31
|
+
if (basePath.endsWith(".json")) candidates.push(`${basePath}.ts`, `${basePath}.js`);
|
|
32
|
+
for (const candidate of candidates) if (await fs.pathExists(candidate)) return candidate;
|
|
33
|
+
}
|
|
34
|
+
async function resolveScriptFile(basePath) {
|
|
35
|
+
if (extname(basePath) && await fs.pathExists(basePath)) return basePath;
|
|
36
|
+
for (const candidateExt of SCRIPT_EXTS) {
|
|
37
|
+
const candidate = `${basePath}${candidateExt}`;
|
|
38
|
+
if (await fs.pathExists(candidate)) return candidate;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
async function resolveStyleFile(scriptPath) {
|
|
42
|
+
const base = scriptPath.replace(new RegExp(`${extname(scriptPath)}$`), "");
|
|
43
|
+
for (const ext of STYLE_EXTS) {
|
|
44
|
+
const candidate = `${base}${ext}`;
|
|
45
|
+
if (await fs.pathExists(candidate)) return candidate;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
async function resolveTemplateFile(scriptPath) {
|
|
49
|
+
const base = scriptPath.replace(new RegExp(`${extname(scriptPath)}$`), "");
|
|
50
|
+
for (const ext of TEMPLATE_EXTS) {
|
|
51
|
+
const candidate = `${base}${ext}`;
|
|
52
|
+
if (await fs.pathExists(candidate)) return candidate;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
//#endregion
|
|
57
|
+
export { isRecord, readJsonFile, resolveJsonPath, resolveScriptFile, resolveStyleFile, resolveTemplateFile };
|
|
58
|
+
//# sourceMappingURL=files.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"files.mjs","names":[],"sources":["../../src/plugin/files.ts"],"sourcesContent":["import { fs } from '@weapp-core/shared/fs'\nimport { extname } from 'pathe'\nimport { bundleRequire } from 'rolldown-require'\n\nimport { SCRIPT_EXTS, STYLE_EXTS, TEMPLATE_EXTS } from './constants'\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value)\n}\n\nexport async function readJsonFile(pathname: string) {\n const candidates = [pathname]\n if (pathname.endsWith('.json')) {\n candidates.push(`${pathname}.ts`, `${pathname}.js`)\n }\n\n for (const candidate of candidates) {\n if (!(await fs.pathExists(candidate))) {\n continue\n }\n if (candidate.endsWith('.json')) {\n const json = await fs.readJson(candidate).catch(() => undefined)\n if (!isRecord(json)) {\n return undefined\n }\n return json\n }\n const { mod } = await bundleRequire({\n filepath: candidate,\n preserveTemporaryFile: true,\n })\n const resolved = typeof mod.default === 'function'\n ? await mod.default()\n : mod.default\n if (!isRecord(resolved)) {\n return undefined\n }\n return resolved\n }\n\n return undefined\n}\n\nexport async function resolveJsonPath(basePath: string) {\n const candidates = [basePath]\n if (basePath.endsWith('.json')) {\n candidates.push(`${basePath}.ts`, `${basePath}.js`)\n }\n for (const candidate of candidates) {\n if (await fs.pathExists(candidate)) {\n return candidate\n }\n }\n return undefined\n}\n\nexport async function resolveScriptFile(basePath: string) {\n const ext = extname(basePath)\n if (ext && (await fs.pathExists(basePath))) {\n return basePath\n }\n for (const candidateExt of SCRIPT_EXTS) {\n const candidate = `${basePath}${candidateExt}`\n if (await fs.pathExists(candidate)) {\n return candidate\n }\n }\n return undefined\n}\n\nexport async function resolveStyleFile(scriptPath: string) {\n const base = scriptPath.replace(new RegExp(`${extname(scriptPath)}$`), '')\n for (const ext of STYLE_EXTS) {\n const candidate = `${base}${ext}`\n if (await fs.pathExists(candidate)) {\n return candidate\n }\n }\n return undefined\n}\n\nexport async function resolveTemplateFile(scriptPath: string) {\n const base = scriptPath.replace(new RegExp(`${extname(scriptPath)}$`), '')\n for (const ext of TEMPLATE_EXTS) {\n const candidate = `${base}${ext}`\n if (await fs.pathExists(candidate)) {\n return candidate\n }\n }\n return undefined\n}\n"],"mappings":";;;;;;AAMA,SAAgB,SAAS,OAAkD;AACzE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM;;AAG7E,eAAsB,aAAa,UAAkB;CACnD,MAAM,aAAa,CAAC,SAAS;AAC7B,KAAI,SAAS,SAAS,QAAQ,CAC5B,YAAW,KAAK,GAAG,SAAS,MAAM,GAAG,SAAS,KAAK;AAGrD,MAAK,MAAM,aAAa,YAAY;AAClC,MAAI,CAAE,MAAM,GAAG,WAAW,UAAU,CAClC;AAEF,MAAI,UAAU,SAAS,QAAQ,EAAE;GAC/B,MAAM,OAAO,MAAM,GAAG,SAAS,UAAU,CAAC,YAAY,OAAU;AAChE,OAAI,CAAC,SAAS,KAAK,CACjB;AAEF,UAAO;;EAET,MAAM,EAAE,QAAQ,MAAM,cAAc;GAClC,UAAU;GACV,uBAAuB;GACxB,CAAC;EACF,MAAM,WAAW,OAAO,IAAI,YAAY,aACpC,MAAM,IAAI,SAAS,GACnB,IAAI;AACR,MAAI,CAAC,SAAS,SAAS,CACrB;AAEF,SAAO;;;AAMX,eAAsB,gBAAgB,UAAkB;CACtD,MAAM,aAAa,CAAC,SAAS;AAC7B,KAAI,SAAS,SAAS,QAAQ,CAC5B,YAAW,KAAK,GAAG,SAAS,MAAM,GAAG,SAAS,KAAK;AAErD,MAAK,MAAM,aAAa,WACtB,KAAI,MAAM,GAAG,WAAW,UAAU,CAChC,QAAO;;AAMb,eAAsB,kBAAkB,UAAkB;AAExD,KADY,QAAQ,SAAS,IACjB,MAAM,GAAG,WAAW,SAAS,CACvC,QAAO;AAET,MAAK,MAAM,gBAAgB,aAAa;EACtC,MAAM,YAAY,GAAG,WAAW;AAChC,MAAI,MAAM,GAAG,WAAW,UAAU,CAChC,QAAO;;;AAMb,eAAsB,iBAAiB,YAAoB;CACzD,MAAM,OAAO,WAAW,QAAQ,IAAI,OAAO,GAAG,QAAQ,WAAW,CAAC,GAAG,EAAE,GAAG;AAC1E,MAAK,MAAM,OAAO,YAAY;EAC5B,MAAM,YAAY,GAAG,OAAO;AAC5B,MAAI,MAAM,GAAG,WAAW,UAAU,CAChC,QAAO;;;AAMb,eAAsB,oBAAoB,YAAoB;CAC5D,MAAM,OAAO,WAAW,QAAQ,IAAI,OAAO,GAAG,QAAQ,WAAW,CAAC,GAAG,EAAE,GAAG;AAC1E,MAAK,MAAM,OAAO,eAAe;EAC/B,MAAM,YAAY,GAAG,OAAO;AAC5B,MAAI,MAAM,GAAG,WAAW,UAAU,CAChC,QAAO"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { WeappWebPluginOptions } from "./types.mjs";
|
|
2
|
+
import { SourceMap } from "magic-string";
|
|
3
|
+
|
|
4
|
+
//#region src/plugin/index.d.ts
|
|
5
|
+
interface WebPluginContext {
|
|
6
|
+
warn?: (message: string) => void;
|
|
7
|
+
addWatchFile?: (id: string) => void;
|
|
8
|
+
}
|
|
9
|
+
type WebTransformResult = {
|
|
10
|
+
code: string;
|
|
11
|
+
map: SourceMap | null;
|
|
12
|
+
} | null;
|
|
13
|
+
interface WebResolvedConfig {
|
|
14
|
+
root: string;
|
|
15
|
+
command: string;
|
|
16
|
+
}
|
|
17
|
+
interface WebHmrContext {
|
|
18
|
+
file: string;
|
|
19
|
+
}
|
|
20
|
+
interface WeappWebVitePlugin {
|
|
21
|
+
name: string;
|
|
22
|
+
enforce?: 'pre' | 'post';
|
|
23
|
+
configResolved?: (this: WebPluginContext, config: WebResolvedConfig) => void | Promise<void>;
|
|
24
|
+
buildStart?: (this: WebPluginContext) => void | Promise<void>;
|
|
25
|
+
resolveId?: (id: string) => string | null | Promise<string | null>;
|
|
26
|
+
load?: (id: string) => string | null | Promise<string | null>;
|
|
27
|
+
handleHotUpdate?: (this: WebPluginContext, ctx: WebHmrContext) => void | Promise<void>;
|
|
28
|
+
transform?: (this: WebPluginContext, code: string, id: string) => WebTransformResult | Promise<WebTransformResult>;
|
|
29
|
+
}
|
|
30
|
+
declare function weappWebPlugin(options?: WeappWebPluginOptions): WeappWebVitePlugin;
|
|
31
|
+
//#endregion
|
|
32
|
+
export { weappWebPlugin };
|
|
33
|
+
//# sourceMappingURL=index.d.mts.map
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import { transformWxssToCss } from "../css/wxss.mjs";
|
|
2
|
+
import { compileWxml } from "../compiler/wxml/compile.mjs";
|
|
3
|
+
import { transformWxsToEsm } from "../compiler/wxs.mjs";
|
|
4
|
+
import { ENTRY_ID, SCRIPT_EXTS, STYLE_EXTS, TEMPLATE_EXTS, TRANSFORM_STYLE_EXTS, WXS_EXTS } from "./constants.mjs";
|
|
5
|
+
import { cleanUrl, isHtmlEntry, isInsideDir, normalizePath, resolveTemplatePathSync, resolveWxsPathSync, toRelativeImport } from "./path.mjs";
|
|
6
|
+
import { generateEntryModule } from "./entry.mjs";
|
|
7
|
+
import { transformScriptModule } from "./register.mjs";
|
|
8
|
+
import { scanProject } from "./scan.mjs";
|
|
9
|
+
import { createEmptyScanState } from "./state.mjs";
|
|
10
|
+
import process from "node:process";
|
|
11
|
+
import { extname, resolve } from "pathe";
|
|
12
|
+
|
|
13
|
+
//#region src/plugin/index.ts
|
|
14
|
+
function isTemplateFile(id) {
|
|
15
|
+
const lower = id.toLowerCase();
|
|
16
|
+
return TEMPLATE_EXTS.some((ext) => lower.endsWith(ext));
|
|
17
|
+
}
|
|
18
|
+
function isWxsFile(id) {
|
|
19
|
+
const lower = id.toLowerCase();
|
|
20
|
+
return WXS_EXTS.some((ext) => lower.endsWith(ext));
|
|
21
|
+
}
|
|
22
|
+
function hasWxsQuery(id) {
|
|
23
|
+
return id.includes("?wxs") || id.includes("&wxs");
|
|
24
|
+
}
|
|
25
|
+
function createInlineStyleModule(css) {
|
|
26
|
+
return [
|
|
27
|
+
`const __weapp_injected_styles__ = new Map()`,
|
|
28
|
+
`function __weapp_createStyleId__(css) {`,
|
|
29
|
+
` let hash = 2166136261`,
|
|
30
|
+
` for (let i = 0; i < css.length; i++) {`,
|
|
31
|
+
` hash ^= css.charCodeAt(i)`,
|
|
32
|
+
` hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)`,
|
|
33
|
+
` }`,
|
|
34
|
+
` return 'weapp-web-style-' + (hash >>> 0).toString(36)`,
|
|
35
|
+
`}`,
|
|
36
|
+
`function __weapp_removeStyle__(id) {`,
|
|
37
|
+
` if (typeof document === 'undefined') {`,
|
|
38
|
+
` return`,
|
|
39
|
+
` }`,
|
|
40
|
+
` const style = __weapp_injected_styles__.get(id)`,
|
|
41
|
+
` if (style) {`,
|
|
42
|
+
` style.remove()`,
|
|
43
|
+
` __weapp_injected_styles__.delete(id)`,
|
|
44
|
+
` }`,
|
|
45
|
+
`}`,
|
|
46
|
+
`function injectStyle(css, id) {`,
|
|
47
|
+
` if (typeof document === 'undefined') {`,
|
|
48
|
+
` return () => {}`,
|
|
49
|
+
` }`,
|
|
50
|
+
` const styleId = id ?? __weapp_createStyleId__(css)`,
|
|
51
|
+
` const existing = __weapp_injected_styles__.get(styleId)`,
|
|
52
|
+
` if (existing) {`,
|
|
53
|
+
` if (existing.textContent !== css) {`,
|
|
54
|
+
` existing.textContent = css`,
|
|
55
|
+
` }`,
|
|
56
|
+
` return () => __weapp_removeStyle__(styleId)`,
|
|
57
|
+
` }`,
|
|
58
|
+
` const style = document.createElement('style')`,
|
|
59
|
+
` style.id = styleId`,
|
|
60
|
+
` style.textContent = css`,
|
|
61
|
+
` document.head.append(style)`,
|
|
62
|
+
` __weapp_injected_styles__.set(styleId, style)`,
|
|
63
|
+
` return () => __weapp_removeStyle__(styleId)`,
|
|
64
|
+
`}`,
|
|
65
|
+
`const css = ${JSON.stringify(css)}`,
|
|
66
|
+
`export default css`,
|
|
67
|
+
`export function useStyle(id) {`,
|
|
68
|
+
` return injectStyle(css, id)`,
|
|
69
|
+
`}`
|
|
70
|
+
].join("\n");
|
|
71
|
+
}
|
|
72
|
+
function weappWebPlugin(options = {}) {
|
|
73
|
+
let root = process.cwd();
|
|
74
|
+
let srcRoot = resolve(root, options.srcDir ?? "src");
|
|
75
|
+
let enableHmr = false;
|
|
76
|
+
const state = createEmptyScanState();
|
|
77
|
+
const wxssOptions = options.wxss;
|
|
78
|
+
const resolveTemplatePath = (raw, importer) => resolveTemplatePathSync(raw, importer, srcRoot);
|
|
79
|
+
const resolveWxsPath = (raw, importer) => resolveWxsPathSync(raw, importer, srcRoot);
|
|
80
|
+
return {
|
|
81
|
+
name: "@weapp-vite/web",
|
|
82
|
+
enforce: "pre",
|
|
83
|
+
async configResolved(config) {
|
|
84
|
+
root = config.root;
|
|
85
|
+
srcRoot = resolve(root, options.srcDir ?? "src");
|
|
86
|
+
enableHmr = config.command === "serve";
|
|
87
|
+
await scanProject({
|
|
88
|
+
srcRoot,
|
|
89
|
+
warn: this.warn?.bind(this),
|
|
90
|
+
state
|
|
91
|
+
});
|
|
92
|
+
},
|
|
93
|
+
async buildStart() {
|
|
94
|
+
await scanProject({
|
|
95
|
+
srcRoot,
|
|
96
|
+
warn: this.warn?.bind(this),
|
|
97
|
+
state
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
resolveId(id) {
|
|
101
|
+
if (id === "/@weapp-vite/web/entry" || id === "@weapp-vite/web/entry") return ENTRY_ID;
|
|
102
|
+
return null;
|
|
103
|
+
},
|
|
104
|
+
load(id) {
|
|
105
|
+
if (id === "\0@weapp-vite/web/entry") return generateEntryModule(state.scanResult, root, wxssOptions, options);
|
|
106
|
+
return null;
|
|
107
|
+
},
|
|
108
|
+
async handleHotUpdate(ctx) {
|
|
109
|
+
const clean = cleanUrl(ctx.file);
|
|
110
|
+
if (clean.endsWith(".json") || isTemplateFile(clean) || isWxsFile(clean) || clean.endsWith(".wxss") || SCRIPT_EXTS.includes(extname(clean))) await scanProject({
|
|
111
|
+
srcRoot,
|
|
112
|
+
warn: this.warn?.bind(this),
|
|
113
|
+
state
|
|
114
|
+
});
|
|
115
|
+
},
|
|
116
|
+
transform(code, id) {
|
|
117
|
+
const clean = cleanUrl(id);
|
|
118
|
+
if (isTemplateFile(clean)) {
|
|
119
|
+
if (isHtmlEntry(clean, root)) return null;
|
|
120
|
+
const normalizedId = normalizePath(clean);
|
|
121
|
+
if (state.templatePathSet.size > 0) {
|
|
122
|
+
if (!isInsideDir(clean, srcRoot)) return null;
|
|
123
|
+
if (!state.templatePathSet.has(normalizedId)) return null;
|
|
124
|
+
}
|
|
125
|
+
const navigationConfig = state.pageNavigationMap.get(normalizedId);
|
|
126
|
+
const componentTags = state.templateComponentMap.get(normalizedId);
|
|
127
|
+
const { code: compiled, dependencies, warnings } = compileWxml({
|
|
128
|
+
id: clean,
|
|
129
|
+
source: code,
|
|
130
|
+
resolveTemplatePath,
|
|
131
|
+
resolveWxsPath,
|
|
132
|
+
navigationBar: navigationConfig ? { config: navigationConfig } : void 0,
|
|
133
|
+
componentTags
|
|
134
|
+
});
|
|
135
|
+
const addWatchFile = this.addWatchFile;
|
|
136
|
+
if (dependencies.length > 0 && addWatchFile) for (const dep of dependencies) addWatchFile.call(this, dep);
|
|
137
|
+
const warn = this.warn;
|
|
138
|
+
if (warnings?.length && warn) for (const warning of warnings) warn.call(this, warning);
|
|
139
|
+
return {
|
|
140
|
+
code: compiled,
|
|
141
|
+
map: null
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
if (isWxsFile(clean) || hasWxsQuery(id)) {
|
|
145
|
+
const { code: compiled, dependencies, warnings } = transformWxsToEsm(code, clean, {
|
|
146
|
+
resolvePath: resolveWxsPath,
|
|
147
|
+
toImportPath: (resolved, importer) => normalizePath(toRelativeImport(importer, resolved))
|
|
148
|
+
});
|
|
149
|
+
const addWatchFile = this.addWatchFile;
|
|
150
|
+
if (dependencies.length > 0 && addWatchFile) for (const dep of dependencies) addWatchFile.call(this, dep);
|
|
151
|
+
const warn = this.warn;
|
|
152
|
+
if (warnings?.length && warn) for (const warning of warnings) warn.call(this, warning);
|
|
153
|
+
return {
|
|
154
|
+
code: compiled,
|
|
155
|
+
map: null
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (TRANSFORM_STYLE_EXTS.some((ext) => clean.endsWith(ext))) {
|
|
159
|
+
const { css } = transformWxssToCss(code, wxssOptions);
|
|
160
|
+
return {
|
|
161
|
+
code: createInlineStyleModule(css),
|
|
162
|
+
map: null
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
if (STYLE_EXTS.some((ext) => clean.endsWith(ext)) && !clean.endsWith(".wxss")) {
|
|
166
|
+
const { css } = transformWxssToCss(code, wxssOptions);
|
|
167
|
+
return {
|
|
168
|
+
code: css,
|
|
169
|
+
map: null
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
if (!SCRIPT_EXTS.some((ext) => clean.endsWith(ext))) return null;
|
|
173
|
+
if (clean.includes("node_modules")) return null;
|
|
174
|
+
const meta = state.moduleMeta.get(normalizePath(clean));
|
|
175
|
+
if (!meta) return null;
|
|
176
|
+
return transformScriptModule({
|
|
177
|
+
code,
|
|
178
|
+
cleanId: clean,
|
|
179
|
+
meta,
|
|
180
|
+
enableHmr
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
//#endregion
|
|
187
|
+
export { weappWebPlugin };
|
|
188
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../src/plugin/index.ts"],"sourcesContent":["import type { SourceMap } from 'magic-string'\nimport type { WeappWebPluginOptions } from './types'\nimport process from 'node:process'\n\nimport { extname, resolve } from 'pathe'\nimport { compileWxml } from '../compiler/wxml'\nimport { transformWxsToEsm } from '../compiler/wxs'\nimport { transformWxssToCss } from '../css/wxss'\nimport { ENTRY_ID, SCRIPT_EXTS, STYLE_EXTS, TEMPLATE_EXTS, TRANSFORM_STYLE_EXTS, WXS_EXTS } from './constants'\nimport { generateEntryModule } from './entry'\nimport { cleanUrl, isHtmlEntry, isInsideDir, normalizePath, resolveTemplatePathSync, resolveWxsPathSync, toRelativeImport } from './path'\nimport { transformScriptModule } from './register'\nimport { scanProject } from './scan'\nimport { createEmptyScanState } from './state'\n\ninterface WebPluginContext {\n warn?: (message: string) => void\n addWatchFile?: (id: string) => void\n}\n\ntype WebTransformResult = { code: string, map: SourceMap | null } | null\n\ninterface WebResolvedConfig {\n root: string\n command: string\n}\n\ninterface WebHmrContext {\n file: string\n}\n\ninterface WeappWebVitePlugin {\n name: string\n enforce?: 'pre' | 'post'\n configResolved?: (this: WebPluginContext, config: WebResolvedConfig) => void | Promise<void>\n buildStart?: (this: WebPluginContext) => void | Promise<void>\n resolveId?: (id: string) => string | null | Promise<string | null>\n load?: (id: string) => string | null | Promise<string | null>\n handleHotUpdate?: (this: WebPluginContext, ctx: WebHmrContext) => void | Promise<void>\n transform?: (\n this: WebPluginContext,\n code: string,\n id: string,\n ) => WebTransformResult | Promise<WebTransformResult>\n}\n\nfunction isTemplateFile(id: string) {\n const lower = id.toLowerCase()\n return TEMPLATE_EXTS.some(ext => lower.endsWith(ext))\n}\n\nfunction isWxsFile(id: string) {\n const lower = id.toLowerCase()\n return WXS_EXTS.some(ext => lower.endsWith(ext))\n}\n\nfunction hasWxsQuery(id: string) {\n return id.includes('?wxs') || id.includes('&wxs')\n}\n\nfunction createInlineStyleModule(css: string) {\n const serialized = JSON.stringify(css)\n return [\n `const __weapp_injected_styles__ = new Map()`,\n `function __weapp_createStyleId__(css) {`,\n ` let hash = 2166136261`,\n ` for (let i = 0; i < css.length; i++) {`,\n ` hash ^= css.charCodeAt(i)`,\n ` hash += (hash << 1) + (hash << 4) + (hash << 7) + (hash << 8) + (hash << 24)`,\n ` }`,\n ` return 'weapp-web-style-' + (hash >>> 0).toString(36)`,\n `}`,\n `function __weapp_removeStyle__(id) {`,\n ` if (typeof document === 'undefined') {`,\n ` return`,\n ` }`,\n ` const style = __weapp_injected_styles__.get(id)`,\n ` if (style) {`,\n ` style.remove()`,\n ` __weapp_injected_styles__.delete(id)`,\n ` }`,\n `}`,\n `function injectStyle(css, id) {`,\n ` if (typeof document === 'undefined') {`,\n ` return () => {}`,\n ` }`,\n ` const styleId = id ?? __weapp_createStyleId__(css)`,\n ` const existing = __weapp_injected_styles__.get(styleId)`,\n ` if (existing) {`,\n ` if (existing.textContent !== css) {`,\n ` existing.textContent = css`,\n ` }`,\n ` return () => __weapp_removeStyle__(styleId)`,\n ` }`,\n ` const style = document.createElement('style')`,\n ` style.id = styleId`,\n ` style.textContent = css`,\n ` document.head.append(style)`,\n ` __weapp_injected_styles__.set(styleId, style)`,\n ` return () => __weapp_removeStyle__(styleId)`,\n `}`,\n `const css = ${serialized}`,\n `export default css`,\n `export function useStyle(id) {`,\n ` return injectStyle(css, id)`,\n `}`,\n ].join('\\n')\n}\n\nexport function weappWebPlugin(options: WeappWebPluginOptions = {}): WeappWebVitePlugin {\n let root = process.cwd()\n let srcRoot = resolve(root, options.srcDir ?? 'src')\n let enableHmr = false\n\n const state = createEmptyScanState()\n const wxssOptions = options.wxss\n\n const resolveTemplatePath = (raw: string, importer: string) => resolveTemplatePathSync(raw, importer, srcRoot)\n const resolveWxsPath = (raw: string, importer: string) => resolveWxsPathSync(raw, importer, srcRoot)\n\n return {\n name: '@weapp-vite/web',\n enforce: 'pre',\n async configResolved(this: WebPluginContext, config: WebResolvedConfig) {\n root = config.root\n srcRoot = resolve(root, options.srcDir ?? 'src')\n enableHmr = config.command === 'serve'\n await scanProject({ srcRoot, warn: this.warn?.bind(this), state })\n },\n async buildStart(this: WebPluginContext) {\n await scanProject({ srcRoot, warn: this.warn?.bind(this), state })\n },\n resolveId(id: string) {\n if (id === '/@weapp-vite/web/entry' || id === '@weapp-vite/web/entry') {\n return ENTRY_ID\n }\n return null\n },\n load(id: string) {\n if (id === ENTRY_ID) {\n return generateEntryModule(state.scanResult, root, wxssOptions, options)\n }\n return null\n },\n async handleHotUpdate(this: WebPluginContext, ctx: WebHmrContext) {\n const clean = cleanUrl(ctx.file)\n if (clean.endsWith('.json') || isTemplateFile(clean) || isWxsFile(clean) || clean.endsWith('.wxss') || SCRIPT_EXTS.includes(extname(clean))) {\n await scanProject({ srcRoot, warn: this.warn?.bind(this), state })\n }\n },\n transform(this: WebPluginContext, code: string, id: string) {\n const clean = cleanUrl(id)\n\n if (isTemplateFile(clean)) {\n if (isHtmlEntry(clean, root)) {\n return null\n }\n const normalizedId = normalizePath(clean)\n if (state.templatePathSet.size > 0) {\n if (!isInsideDir(clean, srcRoot)) {\n return null\n }\n if (!state.templatePathSet.has(normalizedId)) {\n return null\n }\n }\n const navigationConfig = state.pageNavigationMap.get(normalizedId)\n const componentTags = state.templateComponentMap.get(normalizedId)\n const { code: compiled, dependencies, warnings } = compileWxml({\n id: clean,\n source: code,\n resolveTemplatePath,\n resolveWxsPath,\n navigationBar: navigationConfig ? { config: navigationConfig } : undefined,\n componentTags,\n })\n const addWatchFile = this.addWatchFile\n if (dependencies.length > 0 && addWatchFile) {\n for (const dep of dependencies) {\n addWatchFile.call(this, dep)\n }\n }\n const warn = this.warn\n if (warnings?.length && warn) {\n for (const warning of warnings) {\n warn.call(this, warning)\n }\n }\n return { code: compiled, map: null }\n }\n\n if (isWxsFile(clean) || hasWxsQuery(id)) {\n const { code: compiled, dependencies, warnings } = transformWxsToEsm(code, clean, {\n resolvePath: resolveWxsPath,\n toImportPath: (resolved, importer) => normalizePath(toRelativeImport(importer, resolved)),\n })\n const addWatchFile = this.addWatchFile\n if (dependencies.length > 0 && addWatchFile) {\n for (const dep of dependencies) {\n addWatchFile.call(this, dep)\n }\n }\n const warn = this.warn\n if (warnings?.length && warn) {\n for (const warning of warnings) {\n warn.call(this, warning)\n }\n }\n return { code: compiled, map: null }\n }\n\n if (TRANSFORM_STYLE_EXTS.some(ext => clean.endsWith(ext))) {\n const { css } = transformWxssToCss(code, wxssOptions)\n return {\n code: createInlineStyleModule(css),\n map: null,\n }\n }\n\n if (STYLE_EXTS.some(ext => clean.endsWith(ext)) && !clean.endsWith('.wxss')) {\n const { css } = transformWxssToCss(code, wxssOptions)\n return { code: css, map: null }\n }\n\n if (!SCRIPT_EXTS.some(ext => clean.endsWith(ext))) {\n return null\n }\n if (clean.includes('node_modules')) {\n return null\n }\n\n const meta = state.moduleMeta.get(normalizePath(clean))\n if (!meta) {\n return null\n }\n\n return transformScriptModule({\n code,\n cleanId: clean,\n meta,\n enableHmr,\n })\n },\n }\n}\n\nexport type { WeappWebPluginOptions } from './types'\n"],"mappings":";;;;;;;;;;;;;AA8CA,SAAS,eAAe,IAAY;CAClC,MAAM,QAAQ,GAAG,aAAa;AAC9B,QAAO,cAAc,MAAK,QAAO,MAAM,SAAS,IAAI,CAAC;;AAGvD,SAAS,UAAU,IAAY;CAC7B,MAAM,QAAQ,GAAG,aAAa;AAC9B,QAAO,SAAS,MAAK,QAAO,MAAM,SAAS,IAAI,CAAC;;AAGlD,SAAS,YAAY,IAAY;AAC/B,QAAO,GAAG,SAAS,OAAO,IAAI,GAAG,SAAS,OAAO;;AAGnD,SAAS,wBAAwB,KAAa;AAE5C,QAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA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,SACpB,MAAK,KAAK,MAAM,QAAQ;AAG5B,WAAO;KAAE,MAAM;KAAU,KAAK;KAAM;;AAGtC,OAAI,qBAAqB,MAAK,QAAO,MAAM,SAAS,IAAI,CAAC,EAAE;IACzD,MAAM,EAAE,QAAQ,mBAAmB,MAAM,YAAY;AACrD,WAAO;KACL,MAAM,wBAAwB,IAAI;KAClC,KAAK;KACN;;AAGH,OAAI,WAAW,MAAK,QAAO,MAAM,SAAS,IAAI,CAAC,IAAI,CAAC,MAAM,SAAS,QAAQ,EAAE;IAC3E,MAAM,EAAE,QAAQ,mBAAmB,MAAM,YAAY;AACrD,WAAO;KAAE,MAAM;KAAK,KAAK;KAAM;;AAGjC,OAAI,CAAC,YAAY,MAAK,QAAO,MAAM,SAAS,IAAI,CAAC,CAC/C,QAAO;AAET,OAAI,MAAM,SAAS,eAAe,CAChC,QAAO;GAGT,MAAM,OAAO,MAAM,WAAW,IAAI,cAAc,MAAM,CAAC;AACvD,OAAI,CAAC,KACH,QAAO;AAGT,UAAO,sBAAsB;IAC3B;IACA,SAAS;IACT;IACA;IACD,CAAC;;EAEL"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/plugin/navigation.ts
|
|
2
|
+
function pickNavigationConfig(source) {
|
|
3
|
+
const config = {};
|
|
4
|
+
if (!source) return config;
|
|
5
|
+
if (typeof source.navigationBarTitleText === "string") config.title = source.navigationBarTitleText;
|
|
6
|
+
if (typeof source.navigationBarBackgroundColor === "string") config.backgroundColor = source.navigationBarBackgroundColor;
|
|
7
|
+
if (typeof source.navigationBarTextStyle === "string") config.textStyle = source.navigationBarTextStyle;
|
|
8
|
+
if (typeof source.navigationStyle === "string") config.navigationStyle = source.navigationStyle;
|
|
9
|
+
return config;
|
|
10
|
+
}
|
|
11
|
+
function mergeNavigationConfig(base, overrides) {
|
|
12
|
+
return {
|
|
13
|
+
...base,
|
|
14
|
+
...overrides
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
//#endregion
|
|
19
|
+
export { mergeNavigationConfig, pickNavigationConfig };
|
|
20
|
+
//# sourceMappingURL=navigation.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"navigation.mjs","names":[],"sources":["../../src/plugin/navigation.ts"],"sourcesContent":["import type { NavigationBarConfig } from '../compiler/wxml'\n\nexport function pickNavigationConfig(source: Record<string, unknown> | undefined): NavigationBarConfig {\n const config: NavigationBarConfig = {}\n if (!source) {\n return config\n }\n if (typeof source.navigationBarTitleText === 'string') {\n config.title = source.navigationBarTitleText\n }\n if (typeof source.navigationBarBackgroundColor === 'string') {\n config.backgroundColor = source.navigationBarBackgroundColor\n }\n if (typeof source.navigationBarTextStyle === 'string') {\n config.textStyle = source.navigationBarTextStyle\n }\n if (typeof source.navigationStyle === 'string') {\n config.navigationStyle = source.navigationStyle\n }\n return config\n}\n\nexport function mergeNavigationConfig(base: NavigationBarConfig, overrides: NavigationBarConfig) {\n return {\n ...base,\n ...overrides,\n }\n}\n"],"mappings":";AAEA,SAAgB,qBAAqB,QAAkE;CACrG,MAAM,SAA8B,EAAE;AACtC,KAAI,CAAC,OACH,QAAO;AAET,KAAI,OAAO,OAAO,2BAA2B,SAC3C,QAAO,QAAQ,OAAO;AAExB,KAAI,OAAO,OAAO,iCAAiC,SACjD,QAAO,kBAAkB,OAAO;AAElC,KAAI,OAAO,OAAO,2BAA2B,SAC3C,QAAO,YAAY,OAAO;AAE5B,KAAI,OAAO,OAAO,oBAAoB,SACpC,QAAO,kBAAkB,OAAO;AAElC,QAAO;;AAGT,SAAgB,sBAAsB,MAA2B,WAAgC;AAC/F,QAAO;EACL,GAAG;EACH,GAAG;EACJ"}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { TEMPLATE_EXTS, WXS_RESOLVE_EXTS } from "./constants.mjs";
|
|
2
|
+
import { dirname, extname, normalize, posix, relative, resolve } from "pathe";
|
|
3
|
+
import { existsSync } from "node:fs";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
//#region src/plugin/path.ts
|
|
7
|
+
function cleanUrl(url) {
|
|
8
|
+
const queryIndex = url.indexOf("?");
|
|
9
|
+
if (queryIndex >= 0) return url.slice(0, queryIndex);
|
|
10
|
+
return url;
|
|
11
|
+
}
|
|
12
|
+
function normalizePath(p) {
|
|
13
|
+
return posix.normalize(p.split("\\").join("/"));
|
|
14
|
+
}
|
|
15
|
+
function isInsideDir(filePath, dir) {
|
|
16
|
+
const rel = relative(dir, filePath);
|
|
17
|
+
return rel === "" || !rel.startsWith("..") && !posix.isAbsolute(rel);
|
|
18
|
+
}
|
|
19
|
+
function isHtmlEntry(filePath, root) {
|
|
20
|
+
if (!filePath.toLowerCase().endsWith(".html")) return false;
|
|
21
|
+
return normalizePath(filePath) === normalizePath(resolve(root, "index.html"));
|
|
22
|
+
}
|
|
23
|
+
function toPosixId(id) {
|
|
24
|
+
return normalize(id).split("\\").join("/");
|
|
25
|
+
}
|
|
26
|
+
function toRelativeImport(from, target) {
|
|
27
|
+
const rel = relative(dirname(from), target);
|
|
28
|
+
if (!rel || rel.startsWith(".")) return normalizePath(rel || `./${posix.basename(target)}`);
|
|
29
|
+
return `./${normalizePath(rel)}`;
|
|
30
|
+
}
|
|
31
|
+
function appendInlineQuery(id) {
|
|
32
|
+
if (id.includes("?")) {
|
|
33
|
+
if (id.includes("?inline") || id.includes("&inline")) return id;
|
|
34
|
+
return `${id}&inline`;
|
|
35
|
+
}
|
|
36
|
+
return `${id}?inline`;
|
|
37
|
+
}
|
|
38
|
+
function relativeModuleId(root, absPath) {
|
|
39
|
+
return `/${normalizePath(relative(root, absPath))}`;
|
|
40
|
+
}
|
|
41
|
+
function toViteFsImport(absPath) {
|
|
42
|
+
const normalized = normalizePath(absPath);
|
|
43
|
+
return normalized.startsWith("/") ? `/@fs${normalized}` : `/@fs/${normalized}`;
|
|
44
|
+
}
|
|
45
|
+
function resolveRuntimePolyfillPath() {
|
|
46
|
+
const currentDir = dirname(fileURLToPath(import.meta.url));
|
|
47
|
+
const candidates = [
|
|
48
|
+
resolve(currentDir, "../runtime/index.mjs"),
|
|
49
|
+
resolve(currentDir, "./runtime/index.mjs"),
|
|
50
|
+
resolve(currentDir, "../runtime/polyfill.ts"),
|
|
51
|
+
resolve(currentDir, "../runtime/index.ts")
|
|
52
|
+
];
|
|
53
|
+
for (const candidate of candidates) if (existsSync(candidate)) return candidate;
|
|
54
|
+
throw new Error("[@weapp-vite/web] Failed to resolve runtime polyfill path.");
|
|
55
|
+
}
|
|
56
|
+
function resolveImportBase(raw, importer, srcRoot) {
|
|
57
|
+
if (!raw) return;
|
|
58
|
+
if (raw.startsWith(".")) return resolve(dirname(importer), raw);
|
|
59
|
+
if (raw.startsWith("/")) return resolve(srcRoot, raw.slice(1));
|
|
60
|
+
return resolve(srcRoot, raw);
|
|
61
|
+
}
|
|
62
|
+
function resolveFileWithExtensionsSync(basePath, extensions) {
|
|
63
|
+
if (extname(basePath) && existsSync(basePath)) return basePath;
|
|
64
|
+
for (const ext of extensions) {
|
|
65
|
+
const candidate = `${basePath}${ext}`;
|
|
66
|
+
if (existsSync(candidate)) return candidate;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
function resolveTemplatePathSync(raw, importer, srcRoot) {
|
|
70
|
+
const base = resolveImportBase(raw, importer, srcRoot);
|
|
71
|
+
if (!base) return;
|
|
72
|
+
return resolveFileWithExtensionsSync(base, TEMPLATE_EXTS);
|
|
73
|
+
}
|
|
74
|
+
function resolveWxsPathSync(raw, importer, srcRoot) {
|
|
75
|
+
if (!raw) return;
|
|
76
|
+
let base;
|
|
77
|
+
if (raw.startsWith(".")) base = resolve(dirname(importer), raw);
|
|
78
|
+
else if (raw.startsWith("/")) base = resolve(srcRoot, raw.slice(1));
|
|
79
|
+
else return;
|
|
80
|
+
return resolveFileWithExtensionsSync(base, WXS_RESOLVE_EXTS);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
//#endregion
|
|
84
|
+
export { appendInlineQuery, cleanUrl, isHtmlEntry, isInsideDir, normalizePath, relativeModuleId, resolveRuntimePolyfillPath, resolveTemplatePathSync, resolveWxsPathSync, toPosixId, toRelativeImport, toViteFsImport };
|
|
85
|
+
//# sourceMappingURL=path.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"path.mjs","names":[],"sources":["../../src/plugin/path.ts"],"sourcesContent":["import { existsSync } from 'node:fs'\nimport { fileURLToPath } from 'node:url'\nimport { dirname, extname, normalize, posix, relative, resolve } from 'pathe'\n\nimport { TEMPLATE_EXTS, WXS_RESOLVE_EXTS } from './constants'\n\nexport function cleanUrl(url: string) {\n const queryIndex = url.indexOf('?')\n if (queryIndex >= 0) {\n return url.slice(0, queryIndex)\n }\n return url\n}\n\nexport function normalizePath(p: string) {\n return posix.normalize(p.split('\\\\').join('/'))\n}\n\nexport function isInsideDir(filePath: string, dir: string) {\n const rel = relative(dir, filePath)\n return rel === '' || (!rel.startsWith('..') && !posix.isAbsolute(rel))\n}\n\nexport function isHtmlEntry(filePath: string, root: string) {\n if (!filePath.toLowerCase().endsWith('.html')) {\n return false\n }\n return normalizePath(filePath) === normalizePath(resolve(root, 'index.html'))\n}\n\nexport function toPosixId(id: string) {\n return normalize(id).split('\\\\').join('/')\n}\n\nexport function toRelativeImport(from: string, target: string) {\n const fromDir = dirname(from)\n const rel = relative(fromDir, target)\n if (!rel || rel.startsWith('.')) {\n return normalizePath(rel || `./${posix.basename(target)}`)\n }\n return `./${normalizePath(rel)}`\n}\n\nexport function appendInlineQuery(id: string) {\n if (id.includes('?')) {\n if (id.includes('?inline') || id.includes('&inline')) {\n return id\n }\n return `${id}&inline`\n }\n return `${id}?inline`\n}\n\nexport function relativeModuleId(root: string, absPath: string) {\n const rel = relative(root, absPath)\n return `/${normalizePath(rel)}`\n}\n\nexport function toViteFsImport(absPath: string) {\n const normalized = normalizePath(absPath)\n return normalized.startsWith('/')\n ? `/@fs${normalized}`\n : `/@fs/${normalized}`\n}\n\nexport function resolveRuntimePolyfillPath() {\n const currentDir = dirname(fileURLToPath(import.meta.url))\n const candidates = [\n resolve(currentDir, '../runtime/index.mjs'),\n resolve(currentDir, './runtime/index.mjs'),\n resolve(currentDir, '../runtime/polyfill.ts'),\n resolve(currentDir, '../runtime/index.ts'),\n ]\n\n for (const candidate of candidates) {\n if (existsSync(candidate)) {\n return candidate\n }\n }\n\n throw new Error('[@weapp-vite/web] Failed to resolve runtime polyfill path.')\n}\n\nexport function resolveImportBase(raw: string, importer: string, srcRoot: string) {\n if (!raw) {\n return undefined\n }\n if (raw.startsWith('.')) {\n return resolve(dirname(importer), raw)\n }\n if (raw.startsWith('/')) {\n return resolve(srcRoot, raw.slice(1))\n }\n return resolve(srcRoot, raw)\n}\n\nexport function resolveFileWithExtensionsSync(basePath: string, extensions: string[]) {\n if (extname(basePath) && existsSync(basePath)) {\n return basePath\n }\n for (const ext of extensions) {\n const candidate = `${basePath}${ext}`\n if (existsSync(candidate)) {\n return candidate\n }\n }\n return undefined\n}\n\nexport function resolveTemplatePathSync(raw: string, importer: string, srcRoot: string) {\n const base = resolveImportBase(raw, importer, srcRoot)\n if (!base) {\n return undefined\n }\n return resolveFileWithExtensionsSync(base, TEMPLATE_EXTS)\n}\n\nexport function resolveWxsPathSync(raw: string, importer: string, srcRoot: string) {\n if (!raw) {\n return undefined\n }\n let base: string | undefined\n if (raw.startsWith('.')) {\n base = resolve(dirname(importer), raw)\n }\n else if (raw.startsWith('/')) {\n base = resolve(srcRoot, raw.slice(1))\n }\n else {\n return undefined\n }\n return resolveFileWithExtensionsSync(base, WXS_RESOLVE_EXTS)\n}\n"],"mappings":";;;;;;AAMA,SAAgB,SAAS,KAAa;CACpC,MAAM,aAAa,IAAI,QAAQ,IAAI;AACnC,KAAI,cAAc,EAChB,QAAO,IAAI,MAAM,GAAG,WAAW;AAEjC,QAAO;;AAGT,SAAgB,cAAc,GAAW;AACvC,QAAO,MAAM,UAAU,EAAE,MAA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BAAsB;EAC3C;AAED,MAAK,MAAM,aAAa,WACtB,KAAI,WAAW,UAAU,CACvB,QAAO;AAIX,OAAM,IAAI,MAAM,6DAA6D;;AAG/E,SAAgB,kBAAkB,KAAa,UAAkB,SAAiB;AAChF,KAAI,CAAC,IACH;AAEF,KAAI,IAAI,WAAW,IAAI,CACrB,QAAO,QAAQ,QAAQ,SAAS,EAAE,IAAI;AAExC,KAAI,IAAI,WAAW,IAAI,CACrB,QAAO,QAAQ,SAAS,IAAI,MAAM,EAAE,CAAC;AAEvC,QAAO,QAAQ,SAAS,IAAI;;AAG9B,SAAgB,8BAA8B,UAAkB,YAAsB;AACpF,KAAI,QAAQ,SAAS,IAAI,WAAW,SAAS,CAC3C,QAAO;AAET,MAAK,MAAM,OAAO,YAAY;EAC5B,MAAM,YAAY,GAAG,WAAW;AAChC,MAAI,WAAW,UAAU,CACvB,QAAO;;;AAMb,SAAgB,wBAAwB,KAAa,UAAkB,SAAiB;CACtF,MAAM,OAAO,kBAAkB,KAAK,UAAU,QAAQ;AACtD,KAAI,CAAC,KACH;AAEF,QAAO,8BAA8B,MAAM,cAAc;;AAG3D,SAAgB,mBAAmB,KAAa,UAAkB,SAAiB;AACjF,KAAI,CAAC,IACH;CAEF,IAAI;AACJ,KAAI,IAAI,WAAW,IAAI,CACrB,QAAO,QAAQ,QAAQ,SAAS,EAAE,IAAI;UAE/B,IAAI,WAAW,IAAI,CAC1B,QAAO,QAAQ,SAAS,IAAI,MAAM,EAAE,CAAC;KAGrC;AAEF,QAAO,8BAA8B,MAAM,iBAAiB"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { appendInlineQuery, resolveRuntimePolyfillPath, toRelativeImport, toViteFsImport } from "./path.mjs";
|
|
2
|
+
import { parse } from "@babel/parser";
|
|
3
|
+
import _babelTraverse from "@babel/traverse";
|
|
4
|
+
import * as t from "@babel/types";
|
|
5
|
+
import MagicString from "magic-string";
|
|
6
|
+
|
|
7
|
+
//#region src/plugin/register.ts
|
|
8
|
+
const traverseCandidate = (() => {
|
|
9
|
+
const mod = _babelTraverse;
|
|
10
|
+
if (typeof mod === "function") return mod;
|
|
11
|
+
if (mod?.default && typeof mod.default === "function") return mod.default;
|
|
12
|
+
if (mod?.traverse && typeof mod.traverse === "function") return mod.traverse;
|
|
13
|
+
})();
|
|
14
|
+
if (typeof traverseCandidate !== "function") throw new TypeError("[@weapp-vite/web] Failed to resolve @babel/traverse export.");
|
|
15
|
+
const traverse = traverseCandidate;
|
|
16
|
+
function mapRegisterIdentifier(kind) {
|
|
17
|
+
if (kind === "page") return "Page";
|
|
18
|
+
if (kind === "component") return "Component";
|
|
19
|
+
if (kind === "app") return "App";
|
|
20
|
+
return "";
|
|
21
|
+
}
|
|
22
|
+
function getRegisterName(kind) {
|
|
23
|
+
if (kind === "page") return "registerPage";
|
|
24
|
+
if (kind === "component") return "registerComponent";
|
|
25
|
+
if (kind === "app") return "registerApp";
|
|
26
|
+
}
|
|
27
|
+
function overwriteCall(path, meta, registerName, templateIdent, styleIdent, s) {
|
|
28
|
+
const node = path.node;
|
|
29
|
+
const callee = node.callee;
|
|
30
|
+
if (!t.isIdentifier(callee)) return;
|
|
31
|
+
const insertPosition = node.end - 1;
|
|
32
|
+
const metaParts = [`id: ${JSON.stringify(meta.id)}`];
|
|
33
|
+
if (templateIdent) metaParts.push(`template: ${templateIdent}`);
|
|
34
|
+
if (styleIdent) metaParts.push(`style: ${styleIdent}`);
|
|
35
|
+
const metaCode = `{ ${metaParts.join(", ")} }`;
|
|
36
|
+
s.overwrite(callee.start, callee.end, registerName);
|
|
37
|
+
s.appendLeft(insertPosition, `, ${metaCode}`);
|
|
38
|
+
}
|
|
39
|
+
function transformScriptModule({ code, cleanId, meta, enableHmr }) {
|
|
40
|
+
const registerName = getRegisterName(meta.kind);
|
|
41
|
+
if (!registerName) return null;
|
|
42
|
+
let ast;
|
|
43
|
+
try {
|
|
44
|
+
ast = parse(code, {
|
|
45
|
+
sourceType: "module",
|
|
46
|
+
plugins: ["typescript", "jsx"],
|
|
47
|
+
errorRecovery: true,
|
|
48
|
+
ranges: true
|
|
49
|
+
});
|
|
50
|
+
} catch {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
const s = new MagicString(code);
|
|
54
|
+
let transformed = false;
|
|
55
|
+
const imports = [];
|
|
56
|
+
const runtimePolyfillId = toViteFsImport(resolveRuntimePolyfillPath());
|
|
57
|
+
const templateIdent = meta.templatePath ? "__weapp_template__" : void 0;
|
|
58
|
+
const styleIdent = meta.stylePath ? "__weapp_style__" : void 0;
|
|
59
|
+
if (meta.templatePath && templateIdent) imports.push(`import ${templateIdent} from '${toRelativeImport(cleanId, meta.templatePath)}'`);
|
|
60
|
+
if (meta.stylePath && styleIdent) imports.push(`import ${styleIdent} from '${appendInlineQuery(toRelativeImport(cleanId, meta.stylePath))}'`);
|
|
61
|
+
const registerImports = /* @__PURE__ */ new Set();
|
|
62
|
+
traverse(ast, { CallExpression(path) {
|
|
63
|
+
if (!t.isIdentifier(path.node.callee)) return;
|
|
64
|
+
if (path.node.callee.name === mapRegisterIdentifier(meta.kind)) {
|
|
65
|
+
registerImports.add(registerName);
|
|
66
|
+
overwriteCall(path, meta, registerName, templateIdent, styleIdent, s);
|
|
67
|
+
transformed = true;
|
|
68
|
+
}
|
|
69
|
+
} });
|
|
70
|
+
if (!transformed) return null;
|
|
71
|
+
if (registerImports.size > 0) imports.unshift(`import { ${Array.from(registerImports).join(", ")} } from '${runtimePolyfillId}'`);
|
|
72
|
+
const prefix = `${imports.join("\n")}\n`;
|
|
73
|
+
s.prepend(prefix);
|
|
74
|
+
if (enableHmr) s.append(`\nif (import.meta.hot) { import.meta.hot.accept() }\n`);
|
|
75
|
+
return {
|
|
76
|
+
code: s.toString(),
|
|
77
|
+
map: s.generateMap({ hires: true })
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//#endregion
|
|
82
|
+
export { transformScriptModule };
|
|
83
|
+
//# sourceMappingURL=register.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"register.mjs","names":[],"sources":["../../src/plugin/register.ts"],"sourcesContent":["import type { NodePath } from '@babel/traverse'\nimport type { CallExpression } from '@babel/types'\nimport type { SourceMap } from 'magic-string'\n\nimport type { ModuleMeta } from './types'\nimport { parse } from '@babel/parser'\nimport _babelTraverse from '@babel/traverse'\nimport * as t from '@babel/types'\n\nimport MagicString from 'magic-string'\nimport { appendInlineQuery, resolveRuntimePolyfillPath, toRelativeImport, toViteFsImport } from './path'\n\ntype TraverseFunction = typeof _babelTraverse extends (...args: any[]) => any\n ? typeof _babelTraverse\n : typeof _babelTraverse extends { default: infer D }\n ? D\n : typeof _babelTraverse\n\nconst traverseCandidate: any = (() => {\n const mod: any = _babelTraverse\n if (typeof mod === 'function') {\n return mod\n }\n if (mod?.default && typeof mod.default === 'function') {\n return mod.default\n }\n if (mod?.traverse && typeof mod.traverse === 'function') {\n return mod.traverse\n }\n return undefined\n})()\n\nif (typeof traverseCandidate !== 'function') {\n throw new TypeError('[@weapp-vite/web] Failed to resolve @babel/traverse export.')\n}\n\nconst traverse: TraverseFunction = traverseCandidate\n\nfunction mapRegisterIdentifier(kind: ModuleMeta['kind']) {\n if (kind === 'page') {\n return 'Page'\n }\n if (kind === 'component') {\n return 'Component'\n }\n if (kind === 'app') {\n return 'App'\n }\n return ''\n}\n\nfunction getRegisterName(kind: ModuleMeta['kind']) {\n if (kind === 'page') {\n return 'registerPage'\n }\n if (kind === 'component') {\n return 'registerComponent'\n }\n if (kind === 'app') {\n return 'registerApp'\n }\n return undefined\n}\n\nfunction overwriteCall(\n path: NodePath<CallExpression>,\n meta: ModuleMeta,\n registerName: string,\n templateIdent: string | undefined,\n styleIdent: string | undefined,\n s: MagicString,\n) {\n const node = path.node\n const callee = node.callee\n if (!t.isIdentifier(callee)) {\n return\n }\n const end = node.end!\n const insertPosition = end - 1\n const metaParts: string[] = [`id: ${JSON.stringify(meta.id)}`]\n if (templateIdent) {\n metaParts.push(`template: ${templateIdent}`)\n }\n if (styleIdent) {\n metaParts.push(`style: ${styleIdent}`)\n }\n const metaCode = `{ ${metaParts.join(', ')} }`\n s.overwrite(callee.start!, callee.end!, registerName)\n s.appendLeft(insertPosition, `, ${metaCode}`)\n}\n\ninterface TransformScriptModuleOptions {\n code: string\n cleanId: string\n meta: ModuleMeta\n enableHmr: boolean\n}\n\nexport function transformScriptModule({\n code,\n cleanId,\n meta,\n enableHmr,\n}: TransformScriptModuleOptions): null | { code: string, map: SourceMap } {\n const registerName = getRegisterName(meta.kind)\n if (!registerName) {\n return null\n }\n\n let ast: ReturnType<typeof parse> | undefined\n try {\n ast = parse(code, {\n sourceType: 'module',\n plugins: ['typescript', 'jsx'],\n errorRecovery: true,\n ranges: true,\n })\n }\n catch {\n return null\n }\n\n const s = new MagicString(code)\n let transformed = false\n\n const imports: string[] = []\n const runtimePolyfillId = toViteFsImport(resolveRuntimePolyfillPath())\n const templateIdent = meta.templatePath ? '__weapp_template__' : undefined\n const styleIdent = meta.stylePath ? '__weapp_style__' : undefined\n\n if (meta.templatePath && templateIdent) {\n imports.push(`import ${templateIdent} from '${toRelativeImport(cleanId, meta.templatePath)}'`)\n }\n\n if (meta.stylePath && styleIdent) {\n imports.push(`import ${styleIdent} from '${appendInlineQuery(toRelativeImport(cleanId, meta.stylePath))}'`)\n }\n\n const registerImports = new Set<string>()\n\n traverse(ast, {\n CallExpression(path: NodePath<CallExpression>) {\n if (!t.isIdentifier(path.node.callee)) {\n return\n }\n const name = path.node.callee.name\n if (name === mapRegisterIdentifier(meta.kind)) {\n registerImports.add(registerName)\n overwriteCall(path, meta, registerName, templateIdent, styleIdent, s)\n transformed = true\n }\n },\n })\n\n if (!transformed) {\n return null\n }\n\n if (registerImports.size > 0) {\n imports.unshift(`import { ${Array.from(registerImports).join(', ')} } from '${runtimePolyfillId}'`)\n }\n\n const prefix = `${imports.join('\\n')}\\n`\n s.prepend(prefix)\n\n if (enableHmr) {\n s.append(`\\nif (import.meta.hot) { import.meta.hot.accept() }\\n`)\n }\n\n return {\n code: s.toString(),\n map: s.generateMap({\n hires: true,\n }),\n }\n}\n"],"mappings":";;;;;;;AAkBA,MAAM,2BAAgC;CACpC,MAAM,MAAW;AACjB,KAAI,OAAO,QAAQ,WACjB,QAAO;AAET,KAAI,KAAK,WAAW,OAAO,IAAI,YAAY,WACzC,QAAO,IAAI;AAEb,KAAI,KAAK,YAAY,OAAO,IAAI,aAAa,WAC3C,QAAO,IAAI;IAGX;AAEJ,IAAI,OAAO,sBAAsB,WAC/B,OAAM,IAAI,UAAU,8DAA8D;AAGpF,MAAM,WAA6B;AAEnC,SAAS,sBAAsB,MAA0B;AACvD,KAAI,SAAS,OACX,QAAO;AAET,KAAI,SAAS,YACX,QAAO;AAET,KAAI,SAAS,MACX,QAAO;AAET,QAAO;;AAGT,SAAS,gBAAgB,MAA0B;AACjD,KAAI,SAAS,OACX,QAAO;AAET,KAAI,SAAS,YACX,QAAO;AAET,KAAI,SAAS,MACX,QAAO;;AAKX,SAAS,cACP,MACA,MACA,cACA,eACA,YACA,GACA;CACA,MAAM,OAAO,KAAK;CAClB,MAAM,SAAS,KAAK;AACpB,KAAI,CAAC,EAAE,aAAa,OAAO,CACzB;CAGF,MAAM,iBADM,KAAK,MACY;CAC7B,MAAM,YAAsB,CAAC,OAAO,KAAK,UAAU,KAAK,GAAG,GAAG;AAC9D,KAAI,cACF,WAAU,KAAK,aAAa,gBAAgB;AAE9C,KAAI,WACF,WAAU,KAAK,UAAU,aAAa;CAExC,MAAM,WAAW,KAAK,UAAU,KAAK,KAAK,CAAC;AAC3C,GAAE,UAAU,OAAO,OAAQ,OAAO,KAAM,aAAa;AACrD,GAAE,WAAW,gBAAgB,KAAK,WAAW;;AAU/C,SAAgB,sBAAsB,EACpC,MACA,SACA,MACA,aACwE;CACxE,MAAM,eAAe,gBAAgB,KAAK,KAAK;AAC/C,KAAI,CAAC,aACH,QAAO;CAGT,IAAI;AACJ,KA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