resuxjs 0.2.43 → 0.3.1
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 +993 -666
- package/dist/cli.js +1990 -355
- package/dist/cli.js.map +1 -1
- package/dist/compiler/adapter.d.ts +17 -0
- package/dist/compiler/adapter.js +106 -0
- package/dist/compiler/adapter.js.map +1 -0
- package/dist/compiler/dev-warnings.d.ts +11 -0
- package/dist/compiler/dev-warnings.js +360 -0
- package/dist/compiler/dev-warnings.js.map +1 -0
- package/dist/compiler/directives.d.ts +13 -0
- package/dist/compiler/directives.js +167 -0
- package/dist/compiler/directives.js.map +1 -0
- package/dist/compiler/implementation.d.ts +129 -0
- package/dist/compiler/implementation.js +3745 -0
- package/dist/compiler/implementation.js.map +1 -0
- package/dist/compiler/index.d.ts +9 -103
- package/dist/compiler/index.js +74 -1971
- package/dist/compiler/index.js.map +1 -1
- package/dist/core/config.d.ts +8 -0
- package/dist/core/config.js +21 -0
- package/dist/core/config.js.map +1 -0
- package/dist/core/hooks.d.ts +133 -0
- package/dist/core/hooks.js +54 -0
- package/dist/core/hooks.js.map +1 -0
- package/dist/core/index.d.ts +4 -0
- package/dist/core/index.js +5 -0
- package/dist/core/index.js.map +1 -0
- package/dist/core/module-container.d.ts +100 -0
- package/dist/core/module-container.js +171 -0
- package/dist/core/module-container.js.map +1 -0
- package/dist/core/resux.d.ts +18 -0
- package/dist/core/resux.js +18 -0
- package/dist/core/resux.js.map +1 -0
- package/dist/create.d.ts +1 -0
- package/dist/create.js +878 -22
- package/dist/create.js.map +1 -1
- package/dist/deploy/cloudflare.js +17 -3
- package/dist/deploy/cloudflare.js.map +1 -1
- package/dist/deploy/common.js +4 -3
- package/dist/deploy/common.js.map +1 -1
- package/dist/fonts/index.d.ts +16 -0
- package/dist/fonts/index.js +154 -0
- package/dist/fonts/index.js.map +1 -0
- package/dist/globals.d.ts +27 -2
- package/dist/halal-core/ai/classifyProject.d.ts +4 -0
- package/dist/halal-core/ai/classifyProject.js +104 -0
- package/dist/halal-core/ai/classifyProject.js.map +1 -0
- package/dist/halal-core/ai/createProjectSummary.d.ts +39 -0
- package/dist/halal-core/ai/createProjectSummary.js +42 -0
- package/dist/halal-core/ai/createProjectSummary.js.map +1 -0
- package/dist/halal-core/ai/halalClassifierPrompt.d.ts +1 -0
- package/dist/halal-core/ai/halalClassifierPrompt.js +20 -0
- package/dist/halal-core/ai/halalClassifierPrompt.js.map +1 -0
- package/dist/halal-core/ai/redactSensitiveData.d.ts +1 -0
- package/dist/halal-core/ai/redactSensitiveData.js +139 -0
- package/dist/halal-core/ai/redactSensitiveData.js.map +1 -0
- package/dist/halal-core/ai/validateAiResponse.d.ts +2 -0
- package/dist/halal-core/ai/validateAiResponse.js +97 -0
- package/dist/halal-core/ai/validateAiResponse.js.map +1 -0
- package/dist/halal-core/cli/check.d.ts +1 -0
- package/dist/halal-core/cli/check.js +36 -0
- package/dist/halal-core/cli/check.js.map +1 -0
- package/dist/halal-core/cli/explain.d.ts +1 -0
- package/dist/halal-core/cli/explain.js +35 -0
- package/dist/halal-core/cli/explain.js.map +1 -0
- package/dist/halal-core/cli/report.d.ts +1 -0
- package/dist/halal-core/cli/report.js +15 -0
- package/dist/halal-core/cli/report.js.map +1 -0
- package/dist/halal-core/cli/submitReview.d.ts +1 -0
- package/dist/halal-core/cli/submitReview.js +12 -0
- package/dist/halal-core/cli/submitReview.js.map +1 -0
- package/dist/halal-core/cli/verifyReview.d.ts +1 -0
- package/dist/halal-core/cli/verifyReview.js +19 -0
- package/dist/halal-core/cli/verifyReview.js.map +1 -0
- package/dist/halal-core/config.d.ts +40 -0
- package/dist/halal-core/config.js +128 -0
- package/dist/halal-core/config.js.map +1 -0
- package/dist/halal-core/crypto/integrity.d.ts +9 -0
- package/dist/halal-core/crypto/integrity.js +101 -0
- package/dist/halal-core/crypto/integrity.js.map +1 -0
- package/dist/halal-core/enforce.d.ts +5 -0
- package/dist/halal-core/enforce.js +119 -0
- package/dist/halal-core/enforce.js.map +1 -0
- package/dist/halal-core/index.d.ts +14 -0
- package/dist/halal-core/index.js +17 -0
- package/dist/halal-core/index.js.map +1 -0
- package/dist/halal-core/lifecycle.d.ts +1 -0
- package/dist/halal-core/lifecycle.js +15 -0
- package/dist/halal-core/lifecycle.js.map +1 -0
- package/dist/halal-core/policy.d.ts +2 -0
- package/dist/halal-core/policy.js +5 -0
- package/dist/halal-core/policy.js.map +1 -0
- package/dist/halal-core/report/formatBrowserOverlay.d.ts +2 -0
- package/dist/halal-core/report/formatBrowserOverlay.js +41 -0
- package/dist/halal-core/report/formatBrowserOverlay.js.map +1 -0
- package/dist/halal-core/report/formatTerminalReport.d.ts +2 -0
- package/dist/halal-core/report/formatTerminalReport.js +39 -0
- package/dist/halal-core/report/formatTerminalReport.js.map +1 -0
- package/dist/halal-core/report/generateHalalReport.d.ts +7 -0
- package/dist/halal-core/report/generateHalalReport.js +46 -0
- package/dist/halal-core/report/generateHalalReport.js.map +1 -0
- package/dist/halal-core/report/signReport.d.ts +3 -0
- package/dist/halal-core/report/signReport.js +17 -0
- package/dist/halal-core/report/signReport.js.map +1 -0
- package/dist/halal-core/report/verifyReport.d.ts +11 -0
- package/dist/halal-core/report/verifyReport.js +45 -0
- package/dist/halal-core/report/verifyReport.js.map +1 -0
- package/dist/halal-core/review/createReviewRequest.d.ts +2 -0
- package/dist/halal-core/review/createReviewRequest.js +21 -0
- package/dist/halal-core/review/createReviewRequest.js.map +1 -0
- package/dist/halal-core/review/reviewPolicy.d.ts +3 -0
- package/dist/halal-core/review/reviewPolicy.js +21 -0
- package/dist/halal-core/review/reviewPolicy.js.map +1 -0
- package/dist/halal-core/review/signedReviewFile.d.ts +13 -0
- package/dist/halal-core/review/signedReviewFile.js +28 -0
- package/dist/halal-core/review/signedReviewFile.js.map +1 -0
- package/dist/halal-core/review/verifyReviewApproval.d.ts +10 -0
- package/dist/halal-core/review/verifyReviewApproval.js +68 -0
- package/dist/halal-core/review/verifyReviewApproval.js.map +1 -0
- package/dist/halal-core/rules/adultContentRules.d.ts +2 -0
- package/dist/halal-core/rules/adultContentRules.js +26 -0
- package/dist/halal-core/rules/adultContentRules.js.map +1 -0
- package/dist/halal-core/rules/alcoholDrugVapeRules.d.ts +2 -0
- package/dist/halal-core/rules/alcoholDrugVapeRules.js +25 -0
- package/dist/halal-core/rules/alcoholDrugVapeRules.js.map +1 -0
- package/dist/halal-core/rules/childSafetyRules.d.ts +2 -0
- package/dist/halal-core/rules/childSafetyRules.js +22 -0
- package/dist/halal-core/rules/childSafetyRules.js.map +1 -0
- package/dist/halal-core/rules/coreCategories.d.ts +7 -0
- package/dist/halal-core/rules/coreCategories.js +112 -0
- package/dist/halal-core/rules/coreCategories.js.map +1 -0
- package/dist/halal-core/rules/cyberAbuseRules.d.ts +2 -0
- package/dist/halal-core/rules/cyberAbuseRules.js +30 -0
- package/dist/halal-core/rules/cyberAbuseRules.js.map +1 -0
- package/dist/halal-core/rules/defaultBlockedRules.d.ts +20 -0
- package/dist/halal-core/rules/defaultBlockedRules.js +216 -0
- package/dist/halal-core/rules/defaultBlockedRules.js.map +1 -0
- package/dist/halal-core/rules/dependencyRules.d.ts +9 -0
- package/dist/halal-core/rules/dependencyRules.js +59 -0
- package/dist/halal-core/rules/dependencyRules.js.map +1 -0
- package/dist/halal-core/rules/endpointRules.d.ts +6 -0
- package/dist/halal-core/rules/endpointRules.js +26 -0
- package/dist/halal-core/rules/endpointRules.js.map +1 -0
- package/dist/halal-core/rules/environmentalHarmRules.d.ts +2 -0
- package/dist/halal-core/rules/environmentalHarmRules.js +20 -0
- package/dist/halal-core/rules/environmentalHarmRules.js.map +1 -0
- package/dist/halal-core/rules/financeRibaRules.d.ts +2 -0
- package/dist/halal-core/rules/financeRibaRules.js +23 -0
- package/dist/halal-core/rules/financeRibaRules.js.map +1 -0
- package/dist/halal-core/rules/gamblingRules.d.ts +2 -0
- package/dist/halal-core/rules/gamblingRules.js +25 -0
- package/dist/halal-core/rules/gamblingRules.js.map +1 -0
- package/dist/halal-core/rules/generalHarmRules.d.ts +2 -0
- package/dist/halal-core/rules/generalHarmRules.js +18 -0
- package/dist/halal-core/rules/generalHarmRules.js.map +1 -0
- package/dist/halal-core/rules/healthHarmRules.d.ts +2 -0
- package/dist/halal-core/rules/healthHarmRules.js +22 -0
- package/dist/halal-core/rules/healthHarmRules.js.map +1 -0
- package/dist/halal-core/rules/keywordRules.d.ts +10 -0
- package/dist/halal-core/rules/keywordRules.js +41 -0
- package/dist/halal-core/rules/keywordRules.js.map +1 -0
- package/dist/halal-core/rules/privacyAbuseRules.d.ts +2 -0
- package/dist/halal-core/rules/privacyAbuseRules.js +24 -0
- package/dist/halal-core/rules/privacyAbuseRules.js.map +1 -0
- package/dist/halal-core/rules/scamFraudRules.d.ts +2 -0
- package/dist/halal-core/rules/scamFraudRules.js +32 -0
- package/dist/halal-core/rules/scamFraudRules.js.map +1 -0
- package/dist/halal-core/rules/selfHarmRules.d.ts +2 -0
- package/dist/halal-core/rules/selfHarmRules.js +20 -0
- package/dist/halal-core/rules/selfHarmRules.js.map +1 -0
- package/dist/halal-core/rules/violenceExtremismRules.d.ts +2 -0
- package/dist/halal-core/rules/violenceExtremismRules.js +33 -0
- package/dist/halal-core/rules/violenceExtremismRules.js.map +1 -0
- package/dist/halal-core/scanner/scanApiRoutes.d.ts +1 -0
- package/dist/halal-core/scanner/scanApiRoutes.js +40 -0
- package/dist/halal-core/scanner/scanApiRoutes.js.map +1 -0
- package/dist/halal-core/scanner/scanComponents.d.ts +1 -0
- package/dist/halal-core/scanner/scanComponents.js +17 -0
- package/dist/halal-core/scanner/scanComponents.js.map +1 -0
- package/dist/halal-core/scanner/scanContent.d.ts +5 -0
- package/dist/halal-core/scanner/scanContent.js +302 -0
- package/dist/halal-core/scanner/scanContent.js.map +1 -0
- package/dist/halal-core/scanner/scanDependencies.d.ts +1 -0
- package/dist/halal-core/scanner/scanDependencies.js +19 -0
- package/dist/halal-core/scanner/scanDependencies.js.map +1 -0
- package/dist/halal-core/scanner/scanDomains.d.ts +4 -0
- package/dist/halal-core/scanner/scanDomains.js +14 -0
- package/dist/halal-core/scanner/scanDomains.js.map +1 -0
- package/dist/halal-core/scanner/scanEnvNames.d.ts +1 -0
- package/dist/halal-core/scanner/scanEnvNames.js +30 -0
- package/dist/halal-core/scanner/scanEnvNames.js.map +1 -0
- package/dist/halal-core/scanner/scanMetadata.d.ts +6 -0
- package/dist/halal-core/scanner/scanMetadata.js +33 -0
- package/dist/halal-core/scanner/scanMetadata.js.map +1 -0
- package/dist/halal-core/scanner/scanPages.d.ts +1 -0
- package/dist/halal-core/scanner/scanPages.js +19 -0
- package/dist/halal-core/scanner/scanPages.js.map +1 -0
- package/dist/halal-core/scanner/scanProject.d.ts +13 -0
- package/dist/halal-core/scanner/scanProject.js +62 -0
- package/dist/halal-core/scanner/scanProject.js.map +1 -0
- package/dist/halal-core/scanner/scanRoutes.d.ts +1 -0
- package/dist/halal-core/scanner/scanRoutes.js +43 -0
- package/dist/halal-core/scanner/scanRoutes.js.map +1 -0
- package/dist/halal-core/scanner/scanTranslations.d.ts +1 -0
- package/dist/halal-core/scanner/scanTranslations.js +32 -0
- package/dist/halal-core/scanner/scanTranslations.js.map +1 -0
- package/dist/halal-core/status.d.ts +14 -0
- package/dist/halal-core/status.js +2 -0
- package/dist/halal-core/status.js.map +1 -0
- package/dist/halal-core/tamper/verifyCoreIntegrity.d.ts +1 -0
- package/dist/halal-core/tamper/verifyCoreIntegrity.js +28 -0
- package/dist/halal-core/tamper/verifyCoreIntegrity.js.map +1 -0
- package/dist/halal-core/tamper/verifyLifecycleHooks.d.ts +1 -0
- package/dist/halal-core/tamper/verifyLifecycleHooks.js +14 -0
- package/dist/halal-core/tamper/verifyLifecycleHooks.js.map +1 -0
- package/dist/halal-core/tamper/verifyReportSignature.d.ts +5 -0
- package/dist/halal-core/tamper/verifyReportSignature.js +18 -0
- package/dist/halal-core/tamper/verifyReportSignature.js.map +1 -0
- package/dist/i18n/index.d.ts +45 -0
- package/dist/i18n/index.js +284 -0
- package/dist/i18n/index.js.map +1 -0
- package/dist/i18n/shared.d.ts +35 -0
- package/dist/i18n/shared.js +270 -0
- package/dist/i18n/shared.js.map +1 -0
- package/dist/icons/index.d.ts +159 -0
- package/dist/icons/index.js +286 -0
- package/dist/icons/index.js.map +1 -0
- package/dist/kit/index.d.ts +29 -0
- package/dist/kit/index.js +72 -0
- package/dist/kit/index.js.map +1 -0
- package/dist/nitro-server/index.d.ts +7 -0
- package/dist/nitro-server/index.js +4 -0
- package/dist/nitro-server/index.js.map +1 -0
- package/dist/reactivity/computed.js +9 -4
- package/dist/reactivity/computed.js.map +1 -1
- package/dist/reactivity/effect.d.ts +3 -1
- package/dist/reactivity/effect.js +56 -6
- package/dist/reactivity/effect.js.map +1 -1
- package/dist/reactivity/reactive.js +64 -15
- package/dist/reactivity/reactive.js.map +1 -1
- package/dist/reactivity/ref.d.ts +1 -0
- package/dist/reactivity/ref.js +13 -6
- package/dist/reactivity/ref.js.map +1 -1
- package/dist/reactivity/scheduler.js +36 -4
- package/dist/reactivity/scheduler.js.map +1 -1
- package/dist/reactivity/watch.js +27 -5
- package/dist/reactivity/watch.js.map +1 -1
- package/dist/runtime/index.d.ts +226 -3
- package/dist/runtime/index.js +13344 -6395
- package/dist/runtime/index.js.map +1 -1
- package/dist/ui/index.d.ts +1828 -0
- package/dist/ui/index.js +938 -0
- package/dist/ui/index.js.map +1 -0
- package/dist/vite-builder/index.d.ts +6 -0
- package/dist/vite-builder/index.js +4 -0
- package/dist/vite-builder/index.js.map +1 -0
- package/package.json +150 -105
- package/templates/default/.env.example +1 -0
- package/templates/default/README.md +18 -14
- package/templates/default/_gitignore +8 -5
- package/templates/default/assets/css/main.css +4 -0
- package/templates/default/nitro.config.ts +9 -5
- package/templates/default/pages/index.vue +102 -87
- package/templates/default/public/favicon.svg +4 -0
- package/templates/default/types/app.d.ts +3 -0
- package/templates/default/.resux-nitro/handler.ts +0 -21
package/dist/compiler/index.js
CHANGED
|
@@ -1,1941 +1,99 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { promises as fsPromises } from "node:fs";
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { syncBuiltinESMExports } from "node:module";
|
|
3
4
|
import path from "node:path";
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
location;
|
|
14
|
-
constructor(message, location) {
|
|
15
|
-
super(location ? `${message} (${location.file}:${location.line}:${location.column})` : message);
|
|
16
|
-
this.name = "ResuxCompileError";
|
|
17
|
-
this.location = location;
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
const RESUMABLE_HANDLER_ALLOWED_GLOBALS = new Set([
|
|
21
|
-
"Math",
|
|
22
|
-
"Number",
|
|
23
|
-
"String",
|
|
24
|
-
"Boolean",
|
|
25
|
-
"Array",
|
|
26
|
-
"Object",
|
|
27
|
-
"Date",
|
|
28
|
-
"JSON",
|
|
29
|
-
"Intl",
|
|
30
|
-
"Promise",
|
|
31
|
-
"console",
|
|
32
|
-
"undefined",
|
|
33
|
-
"globalThis",
|
|
34
|
-
"performance",
|
|
35
|
-
"fetch",
|
|
36
|
-
"Headers",
|
|
37
|
-
"Request",
|
|
38
|
-
"Response",
|
|
39
|
-
"URL",
|
|
40
|
-
"URLSearchParams",
|
|
41
|
-
"AbortController",
|
|
42
|
-
"setTimeout",
|
|
43
|
-
"clearTimeout",
|
|
44
|
-
"setInterval",
|
|
45
|
-
"clearInterval",
|
|
46
|
-
"queueMicrotask",
|
|
47
|
-
"ref",
|
|
48
|
-
"reactive",
|
|
49
|
-
"computed",
|
|
50
|
-
"watch",
|
|
51
|
-
"watchEffect",
|
|
52
|
-
"readonly",
|
|
53
|
-
"toRef",
|
|
54
|
-
"toRefs",
|
|
55
|
-
"unref",
|
|
56
|
-
"isRef",
|
|
57
|
-
"isReactive",
|
|
58
|
-
"isReadonly",
|
|
59
|
-
"nextTick",
|
|
60
|
-
"useState",
|
|
61
|
-
"useAsyncData",
|
|
62
|
-
"useRoute",
|
|
63
|
-
"useRouter",
|
|
64
|
-
"useHead",
|
|
65
|
-
"useSeoMeta",
|
|
66
|
-
"useRuntimeConfig",
|
|
67
|
-
"useResuxApp",
|
|
68
|
-
"useResuxImage",
|
|
69
|
-
"apiURL",
|
|
70
|
-
"useFetch",
|
|
71
|
-
"$fetch",
|
|
72
|
-
"onMounted",
|
|
73
|
-
"definePageMeta",
|
|
74
|
-
"defineProps"
|
|
75
|
-
]);
|
|
76
|
-
export async function buildProject(appRoot, outDir = path.join(appRoot, ".resux"), options = {}) {
|
|
77
|
-
const absoluteRoot = path.resolve(appRoot);
|
|
78
|
-
const absoluteOut = path.resolve(outDir);
|
|
79
|
-
const buildOptions = {
|
|
80
|
-
vite: options.vite ?? "build",
|
|
81
|
-
server: options.server ?? (options.vite === "dev" ? "modules" : "bundle")
|
|
82
|
-
};
|
|
83
|
-
await cleanGeneratedOutput(absoluteOut);
|
|
84
|
-
await mkdir(path.join(absoluteOut, "server"), { recursive: true });
|
|
85
|
-
await mkdir(path.join(absoluteOut, "server", "resux-plugins"), { recursive: true });
|
|
86
|
-
await mkdir(path.join(absoluteOut, "server", "resux-middleware"), { recursive: true });
|
|
87
|
-
await mkdir(path.join(absoluteOut, "server", "request-middleware"), { recursive: true });
|
|
88
|
-
await mkdir(path.join(absoluteOut, "server", "handlers"), { recursive: true });
|
|
89
|
-
await mkdir(path.join(absoluteOut, "server", "config-modules"), { recursive: true });
|
|
90
|
-
await mkdir(path.join(absoluteOut, "client", "handlers"), { recursive: true });
|
|
91
|
-
await mkdir(path.join(absoluteOut, "client", "vue-islands"), { recursive: true });
|
|
92
|
-
await mkdir(path.join(absoluteOut, "client", "plugins"), { recursive: true });
|
|
93
|
-
await mkdir(path.join(absoluteOut, "client", "middleware"), { recursive: true });
|
|
94
|
-
await mkdir(path.join(absoluteOut, "vite-client", "handlers"), { recursive: true });
|
|
95
|
-
await mkdir(path.join(absoluteOut, "vite-client", "vue-islands"), { recursive: true });
|
|
96
|
-
await mkdir(path.join(absoluteOut, "vite-client", "plugins"), { recursive: true });
|
|
97
|
-
await mkdir(path.join(absoluteOut, "vite-client", "middleware"), { recursive: true });
|
|
98
|
-
const runtimeConfig = await readRuntimeConfig(absoluteRoot, absoluteOut);
|
|
99
|
-
const routeRules = createRouteRules(runtimeConfig);
|
|
100
|
-
const componentFiles = await discoverAppVueFiles(absoluteRoot, "components");
|
|
101
|
-
const pageFiles = await discoverAppVueFiles(absoluteRoot, "pages");
|
|
102
|
-
const layoutFiles = await discoverAppVueFiles(absoluteRoot, "layouts");
|
|
103
|
-
const appFile = await optionalFile(path.join(absoluteRoot, "app.vue"))
|
|
104
|
-
?? await optionalFile(path.join(absoluteRoot, "app", "app.vue"));
|
|
105
|
-
const errorFile = await optionalFile(path.join(absoluteRoot, "error.vue"))
|
|
106
|
-
?? await optionalFile(path.join(absoluteRoot, "app", "error.vue"));
|
|
107
|
-
const pluginFiles = await discoverSupportTsFiles(absoluteRoot, "plugins");
|
|
108
|
-
const middlewareFiles = await discoverSupportTsFiles(absoluteRoot, "middleware");
|
|
109
|
-
const serverMiddlewareFiles = await discoverSupportTsFiles(absoluteRoot, path.join("server", "middleware"));
|
|
110
|
-
const serverHandlerFiles = [
|
|
111
|
-
...await discoverServerFiles(path.join(absoluteRoot, "server", "api"), "/api"),
|
|
112
|
-
...await discoverServerFiles(path.join(absoluteRoot, "server", "routes"), "")
|
|
113
|
-
];
|
|
114
|
-
const vueIslands = await discoverVueIslands(absoluteRoot);
|
|
115
|
-
const allFiles = [...componentFiles, ...layoutFiles, ...pageFiles, ...(appFile ? [appFile] : []), ...(errorFile ? [errorFile] : [])];
|
|
116
|
-
const idByFile = new Map();
|
|
117
|
-
const compiledByFile = new Map();
|
|
118
|
-
let index = 0;
|
|
119
|
-
for (const file of allFiles) {
|
|
120
|
-
idByFile.set(file, `m${index++}`);
|
|
121
|
-
}
|
|
122
|
-
for (const file of allFiles) {
|
|
123
|
-
const component = await compileVueFile(file, {
|
|
124
|
-
id: idByFile.get(file),
|
|
125
|
-
name: inferComponentName(absoluteRoot, file)
|
|
126
|
-
});
|
|
127
|
-
compiledByFile.set(file, component);
|
|
128
|
-
await writeFile(path.join(absoluteOut, "server", `${component.id}.mjs`), component.serverSource, "utf8");
|
|
129
|
-
await writeFile(path.join(absoluteOut, "vite-client", "handlers", `${component.id}.mjs`), component.clientSource, "utf8");
|
|
130
|
-
}
|
|
131
|
-
const routes = createRouteManifest(absoluteRoot, pageFiles, idByFile, compiledByFile);
|
|
132
|
-
const components = allFiles.map((file) => compiledByFile.get(file));
|
|
133
|
-
const layouts = layoutFiles.map((file) => compiledByFile.get(file));
|
|
134
|
-
const plugins = pluginFiles.map((file, pluginIndex) => compilePluginFile(file, pluginIndex));
|
|
135
|
-
const middleware = middlewareFiles.map((file, middlewareIndex) => compileMiddlewareFile(file, middlewareIndex));
|
|
136
|
-
const serverMiddleware = serverMiddlewareFiles.map((file, middlewareIndex) => compileServerMiddlewareFile(file, middlewareIndex));
|
|
137
|
-
const serverHandlers = serverHandlerFiles.map((record, handlerIndex) => compileServerHandlerFile(record.file, handlerIndex, record.path));
|
|
138
|
-
const app = appFile ? compiledByFile.get(appFile) : undefined;
|
|
139
|
-
const error = errorFile ? compiledByFile.get(errorFile) : undefined;
|
|
140
|
-
for (const plugin of plugins) {
|
|
141
|
-
if (plugin.mode !== "client") {
|
|
142
|
-
await writeFile(path.join(absoluteOut, "server", "resux-plugins", `${plugin.id}.mjs`), plugin.serverSource, "utf8");
|
|
143
|
-
}
|
|
144
|
-
if (plugin.mode !== "server") {
|
|
145
|
-
await writeFile(path.join(absoluteOut, "vite-client", "plugins", `${plugin.id}.mjs`), plugin.clientSource, "utf8");
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
for (const entry of middleware) {
|
|
149
|
-
if (entry.mode !== "client") {
|
|
150
|
-
await writeFile(path.join(absoluteOut, "server", "resux-middleware", `${entry.id}.mjs`), entry.serverSource, "utf8");
|
|
151
|
-
}
|
|
152
|
-
if (entry.mode !== "server") {
|
|
153
|
-
await writeFile(path.join(absoluteOut, "vite-client", "middleware", `${entry.id}.mjs`), entry.clientSource, "utf8");
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
for (const entry of serverMiddleware) {
|
|
157
|
-
await writeFile(path.join(absoluteOut, "server", "request-middleware", `${entry.id}.mjs`), entry.source, "utf8");
|
|
158
|
-
}
|
|
159
|
-
for (const handler of serverHandlers) {
|
|
160
|
-
await writeFile(path.join(absoluteOut, "server", "handlers", `${handler.id}.mjs`), handler.source, "utf8");
|
|
161
|
-
}
|
|
162
|
-
for (const island of vueIslands) {
|
|
163
|
-
const islandEntryDir = path.join(absoluteOut, "vite-client", "vue-islands");
|
|
164
|
-
await writeFile(path.join(islandEntryDir, `${island.name}.mjs`), createVueIslandClientSource(island, islandEntryDir), "utf8");
|
|
165
|
-
}
|
|
166
|
-
if (buildOptions.vite === "dev") {
|
|
167
|
-
await writeViteClientRuntime(absoluteOut);
|
|
168
|
-
}
|
|
169
|
-
else {
|
|
170
|
-
await buildClientAssets(absoluteRoot, absoluteOut, components, plugins, middleware, vueIslands);
|
|
171
|
-
}
|
|
172
|
-
await writeFile(path.join(absoluteOut, "server", "manifest.mjs"), createServerManifestSource(routes, components, layouts, plugins, middleware, serverMiddleware, serverHandlers, vueIslands, app, error, runtimeConfig, buildOptions), "utf8");
|
|
173
|
-
if (buildOptions.server === "bundle") {
|
|
174
|
-
await buildServerBundle(absoluteRoot, absoluteOut);
|
|
175
|
-
}
|
|
176
|
-
await writeFile(path.join(absoluteOut, "manifest.json"), JSON.stringify({
|
|
177
|
-
routes,
|
|
178
|
-
components: components.map(({ id, name, file, handlers, styles, styleScopeId, meta }) => ({ id, name, file, handlers, styles, styleScopeId, meta })),
|
|
179
|
-
layouts: layouts.map(({ id, name, file, styles, styleScopeId }) => ({ id, name, file, styles, styleScopeId })),
|
|
180
|
-
plugins: plugins.map(({ id, file, mode }) => ({ id, file, mode })),
|
|
181
|
-
middleware: middleware.map(({ id, name, file, global, mode }) => ({ id, name, file, global, mode })),
|
|
182
|
-
serverMiddleware: serverMiddleware.map(({ id, file }) => ({ id, file })),
|
|
183
|
-
serverHandlers: serverHandlers.map(({ id, path: routePath, file, params }) => ({ id, path: routePath, file, params })),
|
|
184
|
-
vueIslands,
|
|
185
|
-
app: app?.id,
|
|
186
|
-
error: error?.id,
|
|
187
|
-
runtimeConfig,
|
|
188
|
-
routeRules,
|
|
189
|
-
features: buildOptions
|
|
190
|
-
}, null, 2), "utf8");
|
|
191
|
-
return {
|
|
192
|
-
appRoot: absoluteRoot,
|
|
193
|
-
outDir: absoluteOut,
|
|
194
|
-
routes,
|
|
195
|
-
components,
|
|
196
|
-
layouts,
|
|
197
|
-
plugins,
|
|
198
|
-
middleware,
|
|
199
|
-
serverMiddleware,
|
|
200
|
-
serverHandlers,
|
|
201
|
-
vueIslands,
|
|
202
|
-
routeRules,
|
|
203
|
-
app,
|
|
204
|
-
error
|
|
205
|
-
};
|
|
206
|
-
}
|
|
207
|
-
async function cleanGeneratedOutput(outDir) {
|
|
208
|
-
await mkdir(outDir, { recursive: true });
|
|
209
|
-
await rm(path.join(outDir, "server"), { recursive: true, force: true });
|
|
210
|
-
await rm(path.join(outDir, "server-bundle"), { recursive: true, force: true });
|
|
211
|
-
await rm(path.join(outDir, "client"), { recursive: true, force: true });
|
|
212
|
-
await rm(path.join(outDir, "vite-client"), { recursive: true, force: true });
|
|
213
|
-
await rm(path.join(outDir, "manifest.json"), { force: true });
|
|
214
|
-
}
|
|
215
|
-
async function buildServerBundle(root, outDir) {
|
|
216
|
-
try {
|
|
217
|
-
await viteBuild({
|
|
218
|
-
root,
|
|
219
|
-
configFile: false,
|
|
220
|
-
publicDir: false,
|
|
221
|
-
appType: "custom",
|
|
222
|
-
clearScreen: false,
|
|
223
|
-
logLevel: "warn",
|
|
224
|
-
build: {
|
|
225
|
-
ssr: path.join(outDir, "server", "manifest.mjs"),
|
|
226
|
-
outDir: path.join(outDir, "server-bundle"),
|
|
227
|
-
emptyOutDir: true,
|
|
228
|
-
minify: "oxc",
|
|
229
|
-
sourcemap: false,
|
|
230
|
-
manifest: false,
|
|
231
|
-
rollupOptions: {
|
|
232
|
-
external: ["resuxjs/runtime"],
|
|
233
|
-
output: {
|
|
234
|
-
format: "es",
|
|
235
|
-
entryFileNames: "index.mjs",
|
|
236
|
-
chunkFileNames: "chunks/[name]-[hash].mjs"
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
}
|
|
240
|
-
});
|
|
241
|
-
}
|
|
242
|
-
catch (error) {
|
|
243
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
244
|
-
throw new Error(`Vite server build failed. Ensure vite is installed. ${message}`);
|
|
245
|
-
}
|
|
246
|
-
}
|
|
247
|
-
async function buildClientAssets(root, outDir, components, plugins, middleware, vueIslands) {
|
|
248
|
-
const inputRoot = path.join(outDir, "vite-client");
|
|
249
|
-
const runtimeInput = await writeViteClientRuntime(outDir);
|
|
250
|
-
const input = {
|
|
251
|
-
"runtime-client": runtimeInput
|
|
252
|
-
};
|
|
253
|
-
for (const component of components) {
|
|
254
|
-
input[`handlers/${component.id}`] = path.join(inputRoot, "handlers", `${component.id}.mjs`);
|
|
255
|
-
}
|
|
256
|
-
for (const plugin of plugins.filter((entry) => entry.mode !== "server")) {
|
|
257
|
-
input[`plugins/${plugin.id}`] = path.join(inputRoot, "plugins", `${plugin.id}.mjs`);
|
|
258
|
-
}
|
|
259
|
-
for (const entry of middleware.filter((candidate) => candidate.mode !== "server")) {
|
|
260
|
-
input[`middleware/${entry.id}`] = path.join(inputRoot, "middleware", `${entry.id}.mjs`);
|
|
261
|
-
}
|
|
262
|
-
for (const island of vueIslands) {
|
|
263
|
-
input[`vue-islands/${island.name}`] = path.join(inputRoot, "vue-islands", `${island.name}.mjs`);
|
|
5
|
+
import * as implementation from "./implementation.js";
|
|
6
|
+
import { normalizeResuxSfcSource, rethrowWithResuxDirectiveBrand, } from "./directives.js";
|
|
7
|
+
import { emitResuxDevWarnings } from "./dev-warnings.js";
|
|
8
|
+
export * from "./implementation.js";
|
|
9
|
+
export { normalizeResuxDirectiveSyntax, normalizeResuxSfcSource } from "./directives.js";
|
|
10
|
+
export function compileVueSource(source, options) {
|
|
11
|
+
const usesResuxDirectives = source.includes("rx-");
|
|
12
|
+
if (options.dev === true) {
|
|
13
|
+
emitResuxDevWarnings(source, options.file);
|
|
264
14
|
}
|
|
265
15
|
try {
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
appType: "custom",
|
|
271
|
-
clearScreen: false,
|
|
272
|
-
logLevel: "warn",
|
|
273
|
-
plugins: [vuePlugin()],
|
|
274
|
-
resolve: {
|
|
275
|
-
alias: {
|
|
276
|
-
vue: require.resolve("vue")
|
|
277
|
-
}
|
|
278
|
-
},
|
|
279
|
-
build: {
|
|
280
|
-
outDir: path.join(outDir, "client"),
|
|
281
|
-
emptyOutDir: false,
|
|
282
|
-
minify: "oxc",
|
|
283
|
-
sourcemap: false,
|
|
284
|
-
manifest: false,
|
|
285
|
-
rollupOptions: {
|
|
286
|
-
input,
|
|
287
|
-
preserveEntrySignatures: "strict",
|
|
288
|
-
external: (id) => id === "/__resux/runtime-client.mjs",
|
|
289
|
-
output: {
|
|
290
|
-
format: "es",
|
|
291
|
-
entryFileNames: "[name].mjs",
|
|
292
|
-
chunkFileNames: "chunks/[name]-[hash].mjs",
|
|
293
|
-
assetFileNames: "assets/[name]-[hash][extname]"
|
|
294
|
-
}
|
|
295
|
-
}
|
|
296
|
-
}
|
|
16
|
+
return implementation.compileVueSource(normalizeResuxSfcSource(source, options.file), {
|
|
17
|
+
file: options.file,
|
|
18
|
+
id: options.id,
|
|
19
|
+
name: options.name,
|
|
297
20
|
});
|
|
298
21
|
}
|
|
299
22
|
catch (error) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
await rm(inputRoot, { recursive: true, force: true });
|
|
23
|
+
if (usesResuxDirectives) {
|
|
24
|
+
return rethrowWithResuxDirectiveBrand(error);
|
|
25
|
+
}
|
|
26
|
+
throw error;
|
|
305
27
|
}
|
|
306
28
|
}
|
|
307
|
-
async function writeViteClientRuntime(outDir) {
|
|
308
|
-
const runtimeInput = path.join(outDir, "vite-client", "runtime-client.mjs");
|
|
309
|
-
await writeFile(runtimeInput, getClientRuntimeSource(), "utf8");
|
|
310
|
-
return runtimeInput;
|
|
311
|
-
}
|
|
312
29
|
export async function compileVueFile(file, options) {
|
|
313
30
|
const source = await readFile(file, "utf8");
|
|
314
31
|
return compileVueSource(source, {
|
|
315
32
|
file,
|
|
316
33
|
id: options.id,
|
|
317
|
-
name: options.name ?? pascalCase(path.basename(file, ".vue"))
|
|
34
|
+
name: options.name ?? pascalCase(path.basename(file, ".vue")),
|
|
35
|
+
dev: options.dev,
|
|
318
36
|
});
|
|
319
37
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
const analysis = analyzeScript(script, options.file, template.inlineHandlers);
|
|
330
|
-
const compiledStyles = compileSfcStyles(descriptor.styles, {
|
|
331
|
-
id: options.id,
|
|
332
|
-
file: options.file
|
|
333
|
-
});
|
|
334
|
-
validateTemplateHandlers(template.handlers, analysis, options.file, source);
|
|
335
|
-
const serverSource = createComponentModuleSource({
|
|
336
|
-
id: options.id,
|
|
337
|
-
name: options.name,
|
|
338
|
-
file: options.file,
|
|
339
|
-
template: template.nodes,
|
|
340
|
-
analysis,
|
|
341
|
-
styles: compiledStyles.styles,
|
|
342
|
-
styleScopeId: compiledStyles.styleScopeId,
|
|
343
|
-
client: false
|
|
344
|
-
});
|
|
345
|
-
const clientSource = createComponentModuleSource({
|
|
346
|
-
id: options.id,
|
|
347
|
-
name: options.name,
|
|
348
|
-
file: options.file,
|
|
349
|
-
template: template.nodes,
|
|
350
|
-
analysis,
|
|
351
|
-
styles: compiledStyles.styles,
|
|
352
|
-
styleScopeId: compiledStyles.styleScopeId,
|
|
353
|
-
client: true
|
|
354
|
-
});
|
|
355
|
-
return {
|
|
356
|
-
id: options.id,
|
|
357
|
-
name: options.name,
|
|
358
|
-
file: options.file,
|
|
359
|
-
serverSource,
|
|
360
|
-
clientSource,
|
|
361
|
-
template: template.nodes,
|
|
362
|
-
handlers: [...new Set(template.handlers.map((event) => event.handler))],
|
|
363
|
-
styles: compiledStyles.styles,
|
|
364
|
-
styleScopeId: compiledStyles.styleScopeId,
|
|
365
|
-
meta: analysis.pageMeta
|
|
366
|
-
};
|
|
367
|
-
}
|
|
368
|
-
function compileSfcStyles(styleBlocks, options) {
|
|
369
|
-
const scopeId = `rx-s-${options.id}`;
|
|
370
|
-
const styles = [];
|
|
371
|
-
let hasScopedStyles = false;
|
|
372
|
-
for (let index = 0; index < styleBlocks.length; index++) {
|
|
373
|
-
const style = styleBlocks[index];
|
|
374
|
-
const lang = style.lang ?? (typeof style.attrs.lang === "string" ? style.attrs.lang : undefined);
|
|
375
|
-
if (lang && lang !== "css") {
|
|
376
|
-
throw new ResuxCompileError(`Resux <style> only supports plain CSS in resumable components. Unsupported lang "${lang}".`, styleBlockLocation(options.file, style));
|
|
377
|
-
}
|
|
378
|
-
if (style.module) {
|
|
379
|
-
throw new ResuxCompileError("Resux <style module> is not supported in resumable components yet.", styleBlockLocation(options.file, style));
|
|
380
|
-
}
|
|
381
|
-
if (style.src) {
|
|
382
|
-
throw new ResuxCompileError("Resux <style src> is not supported in resumable components yet.", styleBlockLocation(options.file, style));
|
|
383
|
-
}
|
|
384
|
-
const result = compileStyle({
|
|
385
|
-
id: scopeId,
|
|
386
|
-
filename: options.file,
|
|
387
|
-
source: style.content,
|
|
388
|
-
scoped: Boolean(style.scoped),
|
|
389
|
-
isProd: true
|
|
390
|
-
});
|
|
391
|
-
if (result.errors.length > 0) {
|
|
392
|
-
const error = result.errors[0];
|
|
393
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
394
|
-
throw new ResuxCompileError(message, styleBlockLocation(options.file, style));
|
|
395
|
-
}
|
|
396
|
-
const css = result.code.trim();
|
|
397
|
-
if (!css) {
|
|
398
|
-
continue;
|
|
399
|
-
}
|
|
400
|
-
hasScopedStyles ||= Boolean(style.scoped);
|
|
401
|
-
styles.push({
|
|
402
|
-
id: `${options.id}-${index}`,
|
|
403
|
-
css,
|
|
404
|
-
scoped: Boolean(style.scoped)
|
|
405
|
-
});
|
|
406
|
-
}
|
|
407
|
-
return {
|
|
408
|
-
styles,
|
|
409
|
-
styleScopeId: hasScopedStyles ? `data-v-${scopeId}` : undefined
|
|
410
|
-
};
|
|
411
|
-
}
|
|
412
|
-
function styleBlockLocation(file, style) {
|
|
413
|
-
return {
|
|
414
|
-
file,
|
|
415
|
-
line: style.loc.start.line,
|
|
416
|
-
column: style.loc.start.column
|
|
417
|
-
};
|
|
418
|
-
}
|
|
419
|
-
export function createRouteManifest(root, files, idByFile, compiledByFile) {
|
|
420
|
-
return files
|
|
421
|
-
.map((file, index) => {
|
|
422
|
-
const normalizedRoot = normalizePath(path.resolve(root));
|
|
423
|
-
const normalizedFile = normalizePath(path.resolve(file));
|
|
424
|
-
const appPagesRoot = `${normalizedRoot}/app/pages/`;
|
|
425
|
-
const pagesRoot = `${normalizedRoot}/pages/`;
|
|
426
|
-
const relative = normalizedFile.startsWith(appPagesRoot)
|
|
427
|
-
? normalizedFile.slice(appPagesRoot.length)
|
|
428
|
-
: normalizedFile.startsWith(pagesRoot)
|
|
429
|
-
? normalizedFile.slice(pagesRoot.length)
|
|
430
|
-
: normalizePath(path.relative(path.join(root, "pages"), file));
|
|
431
|
-
const withoutExtension = relative.replace(/\.vue$/, "");
|
|
432
|
-
const parts = withoutExtension.split("/");
|
|
433
|
-
const params = [];
|
|
434
|
-
const routeParts = parts
|
|
435
|
-
.filter((part, partIndex) => !(part === "index" && partIndex === parts.length - 1))
|
|
436
|
-
.map((part) => {
|
|
437
|
-
const catchAllMatch = /^\[\.\.\.([A-Za-z0-9_]+)\]$/.exec(part);
|
|
438
|
-
if (catchAllMatch) {
|
|
439
|
-
params.push(catchAllMatch[1]);
|
|
440
|
-
return `:${catchAllMatch[1]}*`;
|
|
441
|
-
}
|
|
442
|
-
const match = /^\[([A-Za-z0-9_]+)\]$/.exec(part);
|
|
443
|
-
if (match) {
|
|
444
|
-
params.push(match[1]);
|
|
445
|
-
return `:${match[1]}`;
|
|
446
|
-
}
|
|
447
|
-
return part;
|
|
448
|
-
});
|
|
449
|
-
const routePath = `/${routeParts.join("/")}`.replace(/\/$/, "") || "/";
|
|
450
|
-
return {
|
|
451
|
-
id: `r${index}`,
|
|
452
|
-
path: routePath,
|
|
453
|
-
file,
|
|
454
|
-
params,
|
|
455
|
-
componentId: idByFile?.get(file) ?? `m${index}`,
|
|
456
|
-
meta: compiledByFile?.get(file)?.meta
|
|
457
|
-
};
|
|
458
|
-
})
|
|
459
|
-
.sort((a, b) => routeScore(b.path) - routeScore(a.path) || a.path.localeCompare(b.path));
|
|
460
|
-
}
|
|
461
|
-
function compileTemplate(template, file, templateRefBindings = new Set()) {
|
|
462
|
-
const ast = parseTemplate(template, { comments: false });
|
|
463
|
-
const state = {
|
|
464
|
-
file,
|
|
465
|
-
templateRefBindings,
|
|
466
|
-
bindingIndex: 0,
|
|
467
|
-
inlineHandlerIndex: 0,
|
|
468
|
-
handlers: [],
|
|
469
|
-
inlineHandlers: []
|
|
470
|
-
};
|
|
471
|
-
return {
|
|
472
|
-
nodes: compileTemplateChildren(ast.children, state),
|
|
473
|
-
handlers: state.handlers,
|
|
474
|
-
inlineHandlers: state.inlineHandlers
|
|
475
|
-
};
|
|
476
|
-
}
|
|
477
|
-
function compileTemplateChildren(children, state) {
|
|
478
|
-
const nodes = [];
|
|
479
|
-
let conditionalChainExpressions = null;
|
|
480
|
-
for (const child of children) {
|
|
481
|
-
if (child.type === NodeTypes.ELEMENT) {
|
|
482
|
-
const conditionalDirective = resolveConditionalDirective(child, state);
|
|
483
|
-
if (!conditionalDirective) {
|
|
484
|
-
conditionalChainExpressions = null;
|
|
485
|
-
const compiledNode = compileTemplateNode(child, state);
|
|
486
|
-
if (compiledNode) {
|
|
487
|
-
nodes.push(compiledNode);
|
|
488
|
-
}
|
|
489
|
-
continue;
|
|
490
|
-
}
|
|
491
|
-
if (conditionalDirective.kind === "if") {
|
|
492
|
-
const expression = transformTemplateExpression(conditionalDirective.expression, state.templateRefBindings, state.file, conditionalDirective.directive);
|
|
493
|
-
conditionalChainExpressions = [expression];
|
|
494
|
-
const compiledNode = compileTemplateNode(child, state, { ifExpressionOverride: expression });
|
|
495
|
-
if (compiledNode) {
|
|
496
|
-
nodes.push(compiledNode);
|
|
497
|
-
}
|
|
498
|
-
continue;
|
|
499
|
-
}
|
|
500
|
-
if (!conditionalChainExpressions || conditionalChainExpressions.length === 0) {
|
|
501
|
-
throw new ResuxCompileError(`v-${conditionalDirective.kind} must follow a sibling with v-if or v-else-if.`, locationFromVueNode(state.file, conditionalDirective.directive));
|
|
502
|
-
}
|
|
503
|
-
if (conditionalDirective.kind === "else-if") {
|
|
504
|
-
const branchExpression = transformTemplateExpression(conditionalDirective.expression, state.templateRefBindings, state.file, conditionalDirective.directive);
|
|
505
|
-
const expression = `${buildConditionalChainGuard(conditionalChainExpressions)} && (${branchExpression})`;
|
|
506
|
-
conditionalChainExpressions.push(branchExpression);
|
|
507
|
-
const compiledNode = compileTemplateNode(child, state, { ifExpressionOverride: expression });
|
|
508
|
-
if (compiledNode) {
|
|
509
|
-
nodes.push(compiledNode);
|
|
510
|
-
}
|
|
511
|
-
continue;
|
|
512
|
-
}
|
|
513
|
-
const expression = buildConditionalChainGuard(conditionalChainExpressions);
|
|
514
|
-
conditionalChainExpressions = null;
|
|
515
|
-
const compiledNode = compileTemplateNode(child, state, { ifExpressionOverride: expression });
|
|
516
|
-
if (compiledNode) {
|
|
517
|
-
nodes.push(compiledNode);
|
|
518
|
-
}
|
|
519
|
-
continue;
|
|
520
|
-
}
|
|
521
|
-
if (child.type !== NodeTypes.TEXT || child.content.trim().length > 0) {
|
|
522
|
-
conditionalChainExpressions = null;
|
|
523
|
-
}
|
|
524
|
-
const compiledNode = compileTemplateNode(child, state);
|
|
525
|
-
if (compiledNode) {
|
|
526
|
-
nodes.push(compiledNode);
|
|
527
|
-
}
|
|
528
|
-
}
|
|
529
|
-
return nodes;
|
|
530
|
-
}
|
|
531
|
-
function buildConditionalChainGuard(expressions) {
|
|
532
|
-
return expressions.map((expression) => `!(${expression})`).join(" && ");
|
|
533
|
-
}
|
|
534
|
-
function resolveConditionalDirective(node, state) {
|
|
535
|
-
let conditional = null;
|
|
536
|
-
for (const prop of node.props) {
|
|
537
|
-
if (prop.type !== NodeTypes.DIRECTIVE) {
|
|
538
|
-
continue;
|
|
539
|
-
}
|
|
540
|
-
if (prop.name !== "if" && prop.name !== "else-if" && prop.name !== "else") {
|
|
541
|
-
continue;
|
|
542
|
-
}
|
|
543
|
-
if (conditional) {
|
|
544
|
-
throw new ResuxCompileError("Only one conditional directive is allowed per element.", locationFromVueNode(state.file, prop));
|
|
545
|
-
}
|
|
546
|
-
const expression = expressionContent(prop.exp).trim();
|
|
547
|
-
if ((prop.name === "if" || prop.name === "else-if") && expression.length === 0) {
|
|
548
|
-
throw new ResuxCompileError(`v-${prop.name} needs an expression.`, locationFromVueNode(state.file, prop));
|
|
549
|
-
}
|
|
550
|
-
if (prop.name === "else" && expression.length > 0) {
|
|
551
|
-
throw new ResuxCompileError("v-else does not accept an expression.", locationFromVueNode(state.file, prop));
|
|
552
|
-
}
|
|
553
|
-
conditional = {
|
|
554
|
-
kind: prop.name,
|
|
555
|
-
directive: prop,
|
|
556
|
-
expression: expression.length > 0 ? expression : undefined,
|
|
557
|
-
};
|
|
558
|
-
}
|
|
559
|
-
return conditional;
|
|
560
|
-
}
|
|
561
|
-
function expressionContent(node) {
|
|
562
|
-
if (node && typeof node === "object" && "content" in node && typeof node.content === "string") {
|
|
563
|
-
return node.content;
|
|
564
|
-
}
|
|
565
|
-
return "";
|
|
566
|
-
}
|
|
567
|
-
function compileTemplateNode(node, state, options = {}) {
|
|
568
|
-
if (node.type === NodeTypes.TEXT) {
|
|
569
|
-
return {
|
|
570
|
-
type: "text",
|
|
571
|
-
value: node.content
|
|
572
|
-
};
|
|
573
|
-
}
|
|
574
|
-
if (node.type === NodeTypes.INTERPOLATION) {
|
|
575
|
-
return {
|
|
576
|
-
type: "interpolation",
|
|
577
|
-
expression: transformTemplateExpression(expressionContent(node.content), state.templateRefBindings, state.file, node),
|
|
578
|
-
bindingId: nextBindingId(state)
|
|
579
|
-
};
|
|
580
|
-
}
|
|
581
|
-
if (node.type !== NodeTypes.ELEMENT) {
|
|
582
|
-
return null;
|
|
583
|
-
}
|
|
584
|
-
const attrs = [];
|
|
585
|
-
const events = [];
|
|
586
|
-
const element = {
|
|
587
|
-
type: "element",
|
|
588
|
-
tag: node.tag,
|
|
589
|
-
attrs,
|
|
590
|
-
events,
|
|
591
|
-
children: compileTemplateChildren(node.children, state)
|
|
592
|
-
};
|
|
593
|
-
for (const prop of node.props) {
|
|
594
|
-
if (prop.type === NodeTypes.ATTRIBUTE) {
|
|
595
|
-
attrs.push({
|
|
596
|
-
kind: "static",
|
|
597
|
-
name: prop.name,
|
|
598
|
-
value: prop.value?.content ?? ""
|
|
599
|
-
});
|
|
600
|
-
continue;
|
|
38
|
+
let buildQueue = Promise.resolve();
|
|
39
|
+
const emittedBuildDevWarnings = new Set();
|
|
40
|
+
export function buildProject(appRoot, outDir, options) {
|
|
41
|
+
return enqueueBuild(async () => {
|
|
42
|
+
const emittedDevWarnings = options?.vite === "dev"
|
|
43
|
+
? emittedBuildDevWarnings
|
|
44
|
+
: undefined;
|
|
45
|
+
try {
|
|
46
|
+
return await withResuxDirectiveFileReads(() => implementation.buildProject(appRoot, outDir, options), emittedDevWarnings);
|
|
601
47
|
}
|
|
602
|
-
|
|
603
|
-
|
|
48
|
+
catch (error) {
|
|
49
|
+
return rethrowWithResuxDirectiveBrand(error);
|
|
604
50
|
}
|
|
605
|
-
if (prop.name === "bind") {
|
|
606
|
-
const arg = expressionContent(prop.arg);
|
|
607
|
-
const expression = expressionContent(prop.exp);
|
|
608
|
-
if (!arg || !expression) {
|
|
609
|
-
throw new ResuxCompileError("Dynamic bindings need an argument and expression.", locationFromVueNode(state.file, prop));
|
|
610
|
-
}
|
|
611
|
-
attrs.push({
|
|
612
|
-
kind: "dynamic",
|
|
613
|
-
name: arg,
|
|
614
|
-
value: transformTemplateExpression(expression, state.templateRefBindings, state.file, prop),
|
|
615
|
-
bindingId: nextBindingId(state)
|
|
616
|
-
});
|
|
617
|
-
continue;
|
|
618
|
-
}
|
|
619
|
-
if (prop.name === "on") {
|
|
620
|
-
const arg = expressionContent(prop.arg);
|
|
621
|
-
const expression = expressionContent(prop.exp);
|
|
622
|
-
const modifiers = normalizeEventModifiers(prop.modifiers ?? [], state.file, prop);
|
|
623
|
-
if (!arg || !expression) {
|
|
624
|
-
throw new ResuxCompileError("Events need an argument and expression.", locationFromVueNode(state.file, prop));
|
|
625
|
-
}
|
|
626
|
-
const handler = /^[A-Za-z_$][\w$]*$/.test(expression)
|
|
627
|
-
? expression
|
|
628
|
-
: createInlineEventHandler(expression, state);
|
|
629
|
-
const event = {
|
|
630
|
-
name: arg,
|
|
631
|
-
handler,
|
|
632
|
-
modifiers
|
|
633
|
-
};
|
|
634
|
-
events.push(event);
|
|
635
|
-
state.handlers.push(event);
|
|
636
|
-
continue;
|
|
637
|
-
}
|
|
638
|
-
if (prop.name === "if") {
|
|
639
|
-
if (options.ifExpressionOverride) {
|
|
640
|
-
continue;
|
|
641
|
-
}
|
|
642
|
-
const expression = expressionContent(prop.exp);
|
|
643
|
-
if (!expression) {
|
|
644
|
-
throw new ResuxCompileError("v-if needs an expression.", locationFromVueNode(state.file, prop));
|
|
645
|
-
}
|
|
646
|
-
element.if = {
|
|
647
|
-
expression: transformTemplateExpression(expression, state.templateRefBindings, state.file, prop),
|
|
648
|
-
blockId: nextBindingId(state)
|
|
649
|
-
};
|
|
650
|
-
continue;
|
|
651
|
-
}
|
|
652
|
-
if (prop.name === "else-if" || prop.name === "else") {
|
|
653
|
-
if (options.ifExpressionOverride) {
|
|
654
|
-
continue;
|
|
655
|
-
}
|
|
656
|
-
throw new ResuxCompileError(`v-${prop.name} must follow a sibling with v-if or v-else-if.`, locationFromVueNode(state.file, prop));
|
|
657
|
-
}
|
|
658
|
-
if (prop.name === "for") {
|
|
659
|
-
const expression = expressionContent(prop.exp);
|
|
660
|
-
if (!expression) {
|
|
661
|
-
throw new ResuxCompileError("v-for needs an expression.", locationFromVueNode(state.file, prop));
|
|
662
|
-
}
|
|
663
|
-
element.for = parseForExpression(expression, state.file, prop, state);
|
|
664
|
-
element.for.source = transformTemplateExpression(element.for.source, state.templateRefBindings, state.file, prop);
|
|
665
|
-
continue;
|
|
666
|
-
}
|
|
667
|
-
if (prop.name === "show") {
|
|
668
|
-
const expression = expressionContent(prop.exp);
|
|
669
|
-
if (!expression) {
|
|
670
|
-
throw new ResuxCompileError("v-show needs an expression.", locationFromVueNode(state.file, prop));
|
|
671
|
-
}
|
|
672
|
-
attrs.push({
|
|
673
|
-
kind: "dynamic",
|
|
674
|
-
name: "hidden",
|
|
675
|
-
value: `!(${transformTemplateExpression(expression, state.templateRefBindings, state.file, prop)})`,
|
|
676
|
-
bindingId: nextBindingId(state)
|
|
677
|
-
});
|
|
678
|
-
continue;
|
|
679
|
-
}
|
|
680
|
-
if (prop.name === "text") {
|
|
681
|
-
const expression = expressionContent(prop.exp);
|
|
682
|
-
if (!expression) {
|
|
683
|
-
throw new ResuxCompileError("v-text needs an expression.", locationFromVueNode(state.file, prop));
|
|
684
|
-
}
|
|
685
|
-
element.children = [{
|
|
686
|
-
type: "interpolation",
|
|
687
|
-
expression: transformTemplateExpression(expression, state.templateRefBindings, state.file, prop),
|
|
688
|
-
bindingId: nextBindingId(state)
|
|
689
|
-
}];
|
|
690
|
-
continue;
|
|
691
|
-
}
|
|
692
|
-
if (prop.name === "html") {
|
|
693
|
-
const expression = expressionContent(prop.exp);
|
|
694
|
-
if (!expression) {
|
|
695
|
-
throw new ResuxCompileError("v-html needs an expression.", locationFromVueNode(state.file, prop));
|
|
696
|
-
}
|
|
697
|
-
element.html = {
|
|
698
|
-
expression: transformTemplateExpression(expression, state.templateRefBindings, state.file, prop),
|
|
699
|
-
bindingId: nextBindingId(state)
|
|
700
|
-
};
|
|
701
|
-
element.children = [];
|
|
702
|
-
continue;
|
|
703
|
-
}
|
|
704
|
-
if (prop.name === "model") {
|
|
705
|
-
const expression = expressionContent(prop.exp);
|
|
706
|
-
if (!expression) {
|
|
707
|
-
throw new ResuxCompileError("v-model needs an expression.", locationFromVueNode(state.file, prop));
|
|
708
|
-
}
|
|
709
|
-
if (!isAssignableExpression(expression)) {
|
|
710
|
-
throw new ResuxCompileError("v-model needs an assignable expression like \"message.value\".", locationFromVueNode(state.file, prop));
|
|
711
|
-
}
|
|
712
|
-
const model = createModelBinding(node.tag, node.props, expression, state);
|
|
713
|
-
attrs.push({
|
|
714
|
-
kind: "dynamic",
|
|
715
|
-
name: model.attribute,
|
|
716
|
-
value: transformTemplateExpression(model.value, state.templateRefBindings, state.file, prop),
|
|
717
|
-
bindingId: nextBindingId(state)
|
|
718
|
-
});
|
|
719
|
-
const event = {
|
|
720
|
-
name: model.event,
|
|
721
|
-
handler: model.handler,
|
|
722
|
-
modifiers: []
|
|
723
|
-
};
|
|
724
|
-
events.push(event);
|
|
725
|
-
state.handlers.push(event);
|
|
726
|
-
continue;
|
|
727
|
-
}
|
|
728
|
-
throw new ResuxCompileError(`v-${prop.name} is not supported in the v1 compiler.`, locationFromVueNode(state.file, prop));
|
|
729
|
-
}
|
|
730
|
-
if (options.ifExpressionOverride) {
|
|
731
|
-
element.if = {
|
|
732
|
-
expression: options.ifExpressionOverride,
|
|
733
|
-
blockId: nextBindingId(state),
|
|
734
|
-
};
|
|
735
|
-
}
|
|
736
|
-
return element;
|
|
737
|
-
}
|
|
738
|
-
function createInlineEventHandler(expression, state) {
|
|
739
|
-
const name = nextInlineHandlerName(state);
|
|
740
|
-
state.inlineHandlers.push({
|
|
741
|
-
name,
|
|
742
|
-
source: `function ${name}($event) {\n${expression}\n}`
|
|
743
51
|
});
|
|
744
|
-
return name;
|
|
745
52
|
}
|
|
746
|
-
function
|
|
747
|
-
const
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
const
|
|
753
|
-
const
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
53
|
+
function enqueueBuild(task) {
|
|
54
|
+
const run = buildQueue.then(task, task);
|
|
55
|
+
buildQueue = run.then(() => undefined, () => undefined);
|
|
56
|
+
return run;
|
|
57
|
+
}
|
|
58
|
+
async function withResuxDirectiveFileReads(task, emittedDevWarnings) {
|
|
59
|
+
const mutableFs = fsPromises;
|
|
60
|
+
const originalReadFile = mutableFs.readFile;
|
|
61
|
+
mutableFs.readFile = (async (...args) => {
|
|
62
|
+
const result = await originalReadFile(...args);
|
|
63
|
+
const file = args[0];
|
|
64
|
+
const filename = typeof file === "string"
|
|
65
|
+
? file
|
|
66
|
+
: file instanceof URL
|
|
67
|
+
? file.pathname
|
|
68
|
+
: Buffer.isBuffer(file)
|
|
69
|
+
? file.toString()
|
|
70
|
+
: String(file);
|
|
71
|
+
if (!filename.toLowerCase().endsWith(".vue")) {
|
|
72
|
+
return result;
|
|
73
|
+
}
|
|
74
|
+
if (typeof result === "string") {
|
|
75
|
+
if (emittedDevWarnings) {
|
|
76
|
+
emitResuxDevWarnings(result, filename, emittedDevWarnings);
|
|
77
|
+
}
|
|
78
|
+
return normalizeResuxSfcSource(result, filename);
|
|
79
|
+
}
|
|
80
|
+
if (Buffer.isBuffer(result)) {
|
|
81
|
+
const source = result.toString("utf8");
|
|
82
|
+
if (emittedDevWarnings) {
|
|
83
|
+
emitResuxDevWarnings(source, filename, emittedDevWarnings);
|
|
84
|
+
}
|
|
85
|
+
return Buffer.from(normalizeResuxSfcSource(source, filename), "utf8");
|
|
86
|
+
}
|
|
87
|
+
return result;
|
|
759
88
|
});
|
|
760
|
-
|
|
761
|
-
attribute: isCheckbox ? "checked" : "value",
|
|
762
|
-
value: expression,
|
|
763
|
-
event: isCheckbox || normalizedTag === "select" ? "change" : "input",
|
|
764
|
-
handler
|
|
765
|
-
};
|
|
766
|
-
}
|
|
767
|
-
function staticAttributeValue(props, name) {
|
|
768
|
-
const attr = props.find((prop) => prop.type === NodeTypes.ATTRIBUTE && prop.name === name);
|
|
769
|
-
return attr?.value?.content;
|
|
770
|
-
}
|
|
771
|
-
function transformTemplateExpression(expression, templateRefBindings, file, node) {
|
|
772
|
-
if (templateRefBindings.size === 0) {
|
|
773
|
-
return expression;
|
|
774
|
-
}
|
|
775
|
-
const wrapped = `(${expression})`;
|
|
776
|
-
const sourceFile = ts.createSourceFile(`${file}?template-expression`, wrapped, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
777
|
-
if ((sourceFile.parseDiagnostics ?? []).length > 0) {
|
|
778
|
-
throw new ResuxCompileError("Template expression is not valid JavaScript.", locationFromVueNode(file, node));
|
|
779
|
-
}
|
|
780
|
-
const insertions = [];
|
|
781
|
-
const visit = (current) => {
|
|
782
|
-
if (ts.isIdentifier(current) && templateRefBindings.has(current.text) && shouldAutoUnwrapTemplateIdentifier(current)) {
|
|
783
|
-
const position = current.end - 1;
|
|
784
|
-
if (position >= 0 && position <= expression.length) {
|
|
785
|
-
insertions.push(position);
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
|
-
ts.forEachChild(current, visit);
|
|
789
|
-
};
|
|
790
|
-
visit(sourceFile);
|
|
791
|
-
if (insertions.length === 0) {
|
|
792
|
-
return expression;
|
|
793
|
-
}
|
|
794
|
-
const uniqueInsertions = [...new Set(insertions)].sort((a, b) => b - a);
|
|
795
|
-
let transformed = expression;
|
|
796
|
-
for (const position of uniqueInsertions) {
|
|
797
|
-
transformed = `${transformed.slice(0, position)}.value${transformed.slice(position)}`;
|
|
798
|
-
}
|
|
799
|
-
return transformed;
|
|
800
|
-
}
|
|
801
|
-
function shouldAutoUnwrapTemplateIdentifier(node) {
|
|
802
|
-
const parent = node.parent;
|
|
803
|
-
if (!parent) {
|
|
804
|
-
return true;
|
|
805
|
-
}
|
|
806
|
-
if (ts.isPropertyAccessExpression(parent)) {
|
|
807
|
-
if (parent.expression === node && parent.name.text === "value") {
|
|
808
|
-
return false;
|
|
809
|
-
}
|
|
810
|
-
return parent.name !== node;
|
|
811
|
-
}
|
|
812
|
-
if (ts.isPropertyAssignment(parent) && parent.name === node) {
|
|
813
|
-
return false;
|
|
814
|
-
}
|
|
815
|
-
if (ts.isShorthandPropertyAssignment(parent) || ts.isBindingElement(parent)) {
|
|
816
|
-
return false;
|
|
817
|
-
}
|
|
818
|
-
return true;
|
|
819
|
-
}
|
|
820
|
-
function isAssignableExpression(expression) {
|
|
821
|
-
return /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*|\[[^\]]+\])*$/.test(expression.trim());
|
|
822
|
-
}
|
|
823
|
-
function normalizeEventModifiers(modifiers, file, node) {
|
|
824
|
-
const modifierNames = modifiers.map((modifier) => String(modifier.content));
|
|
825
|
-
const supported = new Set([
|
|
826
|
-
"prevent",
|
|
827
|
-
"stop",
|
|
828
|
-
"self",
|
|
829
|
-
"once",
|
|
830
|
-
"capture",
|
|
831
|
-
"passive",
|
|
832
|
-
"ctrl",
|
|
833
|
-
"shift",
|
|
834
|
-
"alt",
|
|
835
|
-
"meta",
|
|
836
|
-
"exact",
|
|
837
|
-
"left",
|
|
838
|
-
"middle",
|
|
839
|
-
"right",
|
|
840
|
-
"enter",
|
|
841
|
-
"tab",
|
|
842
|
-
"delete",
|
|
843
|
-
"esc",
|
|
844
|
-
"escape",
|
|
845
|
-
"space",
|
|
846
|
-
"up",
|
|
847
|
-
"down",
|
|
848
|
-
"left",
|
|
849
|
-
"right"
|
|
850
|
-
]);
|
|
851
|
-
const unsupported = modifierNames.find((modifier) => !supported.has(modifier));
|
|
852
|
-
if (unsupported) {
|
|
853
|
-
throw new ResuxCompileError(`Event modifier ".${unsupported}" is not supported in the resumability runtime yet.`, locationFromVueNode(file, node));
|
|
854
|
-
}
|
|
855
|
-
return modifierNames;
|
|
856
|
-
}
|
|
857
|
-
function parseForExpression(expression, file, node, state) {
|
|
858
|
-
const match = /^\s*(?:\(([^)]+)\)|([A-Za-z_$][\w$]*))\s+(?:in|of)\s+(.+)\s*$/.exec(expression);
|
|
859
|
-
if (!match) {
|
|
860
|
-
throw new ResuxCompileError("v-for must look like \"item in items\" or \"(item, index) in items\".", locationFromVueNode(file, node));
|
|
861
|
-
}
|
|
862
|
-
const aliases = (match[1] ?? match[2]).split(",").map((part) => part.trim()).filter(Boolean);
|
|
863
|
-
if (!aliases[0] || aliases.length > 2) {
|
|
864
|
-
throw new ResuxCompileError("v-for supports only value and optional index aliases.", locationFromVueNode(file, node));
|
|
865
|
-
}
|
|
866
|
-
return {
|
|
867
|
-
value: aliases[0],
|
|
868
|
-
index: aliases[1],
|
|
869
|
-
source: match[3],
|
|
870
|
-
blockId: nextBindingId(state)
|
|
871
|
-
};
|
|
872
|
-
}
|
|
873
|
-
function analyzeScript(script, file, inlineHandlers = []) {
|
|
874
|
-
const sourceFile = ts.createSourceFile(file, script, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
875
|
-
const imports = [];
|
|
876
|
-
const body = [];
|
|
877
|
-
const bindings = new Set();
|
|
878
|
-
const handlerNames = new Set();
|
|
879
|
-
const resumableBindings = new Set();
|
|
880
|
-
const templateRefBindings = inferTemplateRefBindings(script, file);
|
|
881
|
-
const handlers = new Map();
|
|
882
|
-
let pageMeta;
|
|
883
|
-
for (const statement of sourceFile.statements) {
|
|
884
|
-
const text = statement.getFullText(sourceFile);
|
|
885
|
-
if (ts.isImportDeclaration(statement) || ts.isImportEqualsDeclaration(statement)) {
|
|
886
|
-
imports.push(text);
|
|
887
|
-
continue;
|
|
888
|
-
}
|
|
889
|
-
if (isDefinePageMetaStatement(statement)) {
|
|
890
|
-
pageMeta = readPageMeta(statement, sourceFile, file);
|
|
891
|
-
continue;
|
|
892
|
-
}
|
|
893
|
-
body.push(text);
|
|
894
|
-
if (ts.isFunctionDeclaration(statement) && statement.name) {
|
|
895
|
-
bindings.add(statement.name.text);
|
|
896
|
-
handlerNames.add(statement.name.text);
|
|
897
|
-
handlers.set(statement.name.text, statement);
|
|
898
|
-
continue;
|
|
899
|
-
}
|
|
900
|
-
if (ts.isVariableStatement(statement)) {
|
|
901
|
-
for (const declaration of statement.declarationList.declarations) {
|
|
902
|
-
for (const name of collectBindingNames(declaration.name)) {
|
|
903
|
-
bindings.add(name);
|
|
904
|
-
}
|
|
905
|
-
if (ts.isIdentifier(declaration.name)) {
|
|
906
|
-
if (isHandlerLikeInitializer(declaration.initializer)) {
|
|
907
|
-
handlerNames.add(declaration.name.text);
|
|
908
|
-
handlers.set(declaration.name.text, declaration);
|
|
909
|
-
}
|
|
910
|
-
if (isResumableInitializer(declaration.initializer)) {
|
|
911
|
-
resumableBindings.add(declaration.name.text);
|
|
912
|
-
}
|
|
913
|
-
}
|
|
914
|
-
}
|
|
915
|
-
}
|
|
916
|
-
}
|
|
917
|
-
if (inlineHandlers.length > 0) {
|
|
918
|
-
const inlineSource = inlineHandlers.map((handler) => handler.source).join("\n");
|
|
919
|
-
const inlineSourceFile = ts.createSourceFile(`${file}?inline-handlers`, inlineSource, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
920
|
-
body.push(inlineSource);
|
|
921
|
-
for (const statement of inlineSourceFile.statements) {
|
|
922
|
-
if (ts.isFunctionDeclaration(statement) && statement.name) {
|
|
923
|
-
bindings.add(statement.name.text);
|
|
924
|
-
handlerNames.add(statement.name.text);
|
|
925
|
-
handlers.set(statement.name.text, statement);
|
|
926
|
-
}
|
|
927
|
-
}
|
|
928
|
-
}
|
|
929
|
-
return {
|
|
930
|
-
imports: imports.join("\n"),
|
|
931
|
-
setupBody: body.join("\n"),
|
|
932
|
-
bindings: [...bindings],
|
|
933
|
-
handlerNames,
|
|
934
|
-
resumableBindings,
|
|
935
|
-
templateRefBindings,
|
|
936
|
-
handlers,
|
|
937
|
-
sourceFile,
|
|
938
|
-
pageMeta
|
|
939
|
-
};
|
|
940
|
-
}
|
|
941
|
-
function inferTemplateRefBindings(script, file) {
|
|
942
|
-
const sourceFile = ts.createSourceFile(file, script, ts.ScriptTarget.Latest, true, ts.ScriptKind.TSX);
|
|
943
|
-
const bindings = new Set();
|
|
944
|
-
for (const statement of sourceFile.statements) {
|
|
945
|
-
if (!ts.isVariableStatement(statement)) {
|
|
946
|
-
continue;
|
|
947
|
-
}
|
|
948
|
-
for (const declaration of statement.declarationList.declarations) {
|
|
949
|
-
const initializer = unwrapAwaitExpression(declaration.initializer);
|
|
950
|
-
if (!initializer || !ts.isCallExpression(initializer)) {
|
|
951
|
-
continue;
|
|
952
|
-
}
|
|
953
|
-
const callee = initializer.expression;
|
|
954
|
-
const calleeName = ts.isIdentifier(callee)
|
|
955
|
-
? callee.text
|
|
956
|
-
: ts.isPropertyAccessExpression(callee) && ts.isIdentifier(callee.name)
|
|
957
|
-
? callee.name.text
|
|
958
|
-
: "";
|
|
959
|
-
if (ts.isIdentifier(declaration.name) && isTemplateRefFactory(calleeName)) {
|
|
960
|
-
bindings.add(declaration.name.text);
|
|
961
|
-
continue;
|
|
962
|
-
}
|
|
963
|
-
if (ts.isObjectBindingPattern(declaration.name) && isObjectTemplateRefFactory(calleeName)) {
|
|
964
|
-
for (const element of declaration.name.elements) {
|
|
965
|
-
if (!ts.isBindingElement(element) || !ts.isIdentifier(element.name)) {
|
|
966
|
-
continue;
|
|
967
|
-
}
|
|
968
|
-
const propertyName = element.propertyName && ts.isIdentifier(element.propertyName)
|
|
969
|
-
? element.propertyName.text
|
|
970
|
-
: element.name.text;
|
|
971
|
-
if (calleeName === "toRefs" || ["data", "pending", "error", "value"].includes(propertyName)) {
|
|
972
|
-
bindings.add(element.name.text);
|
|
973
|
-
}
|
|
974
|
-
}
|
|
975
|
-
}
|
|
976
|
-
}
|
|
977
|
-
}
|
|
978
|
-
return bindings;
|
|
979
|
-
}
|
|
980
|
-
function unwrapAwaitExpression(node) {
|
|
981
|
-
return node && ts.isAwaitExpression(node) ? node.expression : node;
|
|
982
|
-
}
|
|
983
|
-
function isTemplateRefFactory(name) {
|
|
984
|
-
return ["ref", "computed", "toRef", "useState", "useAsyncData", "useFetch", "useResuxImage"].includes(name);
|
|
985
|
-
}
|
|
986
|
-
function isAsyncDataFactory(name) {
|
|
987
|
-
return ["useAsyncData", "useFetch"].includes(name);
|
|
988
|
-
}
|
|
989
|
-
function isObjectTemplateRefFactory(name) {
|
|
990
|
-
return isAsyncDataFactory(name) || name === "toRefs";
|
|
991
|
-
}
|
|
992
|
-
function isDefinePageMetaStatement(statement) {
|
|
993
|
-
return ts.isExpressionStatement(statement)
|
|
994
|
-
&& ts.isCallExpression(statement.expression)
|
|
995
|
-
&& ts.isIdentifier(statement.expression.expression)
|
|
996
|
-
&& statement.expression.expression.text === "definePageMeta";
|
|
997
|
-
}
|
|
998
|
-
function readPageMeta(statement, sourceFile, file) {
|
|
999
|
-
if (!isDefinePageMetaStatement(statement)) {
|
|
1000
|
-
return {};
|
|
1001
|
-
}
|
|
1002
|
-
const call = statement.expression;
|
|
1003
|
-
const firstArg = call.arguments[0];
|
|
1004
|
-
if (!firstArg || !ts.isObjectLiteralExpression(firstArg)) {
|
|
1005
|
-
throw new ResuxCompileError("definePageMeta only supports an object literal in Resux.", locationFromTsNode(file, sourceFile, statement));
|
|
1006
|
-
}
|
|
1007
|
-
return readObjectLiteral(firstArg, sourceFile, file);
|
|
1008
|
-
}
|
|
1009
|
-
function readObjectLiteral(node, sourceFile, file) {
|
|
1010
|
-
const output = {};
|
|
1011
|
-
for (const property of node.properties) {
|
|
1012
|
-
if (!ts.isPropertyAssignment(property)) {
|
|
1013
|
-
throw new ResuxCompileError("Only plain object properties are supported here.", locationFromTsNode(file, sourceFile, property));
|
|
1014
|
-
}
|
|
1015
|
-
const name = readPropertyName(property.name, sourceFile, file);
|
|
1016
|
-
output[name] = readLiteralValue(property.initializer, sourceFile, file);
|
|
1017
|
-
}
|
|
1018
|
-
return output;
|
|
1019
|
-
}
|
|
1020
|
-
function readLiteralValue(node, sourceFile, file) {
|
|
1021
|
-
if (ts.isStringLiteralLike(node)) {
|
|
1022
|
-
return node.text;
|
|
1023
|
-
}
|
|
1024
|
-
if (node.kind === ts.SyntaxKind.TrueKeyword) {
|
|
1025
|
-
return true;
|
|
1026
|
-
}
|
|
1027
|
-
if (node.kind === ts.SyntaxKind.FalseKeyword) {
|
|
1028
|
-
return false;
|
|
1029
|
-
}
|
|
1030
|
-
if (ts.isNumericLiteral(node)) {
|
|
1031
|
-
return Number(node.text);
|
|
1032
|
-
}
|
|
1033
|
-
if (ts.isArrayLiteralExpression(node)) {
|
|
1034
|
-
return node.elements.map((element) => readLiteralValue(element, sourceFile, file));
|
|
1035
|
-
}
|
|
1036
|
-
if (ts.isObjectLiteralExpression(node)) {
|
|
1037
|
-
return readObjectLiteral(node, sourceFile, file);
|
|
1038
|
-
}
|
|
1039
|
-
throw new ResuxCompileError("Only JSON-like literals are supported here.", locationFromTsNode(file, sourceFile, node));
|
|
1040
|
-
}
|
|
1041
|
-
function readPropertyName(name, sourceFile, file) {
|
|
1042
|
-
if (ts.isIdentifier(name) || ts.isStringLiteralLike(name) || ts.isNumericLiteral(name)) {
|
|
1043
|
-
return name.text;
|
|
1044
|
-
}
|
|
1045
|
-
throw new ResuxCompileError("Only simple property names are supported here.", locationFromTsNode(file, sourceFile, name));
|
|
1046
|
-
}
|
|
1047
|
-
function validateTemplateHandlers(events, analysis, file, fullSource) {
|
|
1048
|
-
for (const event of events) {
|
|
1049
|
-
if (!analysis.handlerNames.has(event.handler)) {
|
|
1050
|
-
throw new ResuxCompileError(`Handler "${event.handler}" must be a top-level function or arrow function.`, locationFromOffset(file, fullSource, 0));
|
|
1051
|
-
}
|
|
1052
|
-
const handlerNode = analysis.handlers.get(event.handler);
|
|
1053
|
-
if (!handlerNode) {
|
|
1054
|
-
continue;
|
|
1055
|
-
}
|
|
1056
|
-
const unsupportedCapture = findUnsupportedCapture(handlerNode, event.handler, analysis);
|
|
1057
|
-
if (unsupportedCapture) {
|
|
1058
|
-
throw new ResuxCompileError(`Handler "${event.handler}" captures "${unsupportedCapture}", which is not resumable in Resux.`, locationFromTsNode(file, analysis.sourceFile, handlerNode));
|
|
1059
|
-
}
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
function findUnsupportedCapture(node, handlerName, analysis) {
|
|
1063
|
-
const locals = new Set([handlerName, "event"]);
|
|
1064
|
-
const allowedTopLevel = new Set([...analysis.resumableBindings]);
|
|
1065
|
-
function registerPattern(name) {
|
|
1066
|
-
for (const binding of collectBindingNames(name)) {
|
|
1067
|
-
locals.add(binding);
|
|
1068
|
-
}
|
|
1069
|
-
}
|
|
1070
|
-
if (ts.isFunctionDeclaration(node)) {
|
|
1071
|
-
for (const parameter of node.parameters) {
|
|
1072
|
-
registerPattern(parameter.name);
|
|
1073
|
-
}
|
|
1074
|
-
}
|
|
1075
|
-
if (ts.isVariableDeclaration(node) && node.initializer && (ts.isArrowFunction(node.initializer) || ts.isFunctionExpression(node.initializer))) {
|
|
1076
|
-
for (const parameter of node.initializer.parameters) {
|
|
1077
|
-
registerPattern(parameter.name);
|
|
1078
|
-
}
|
|
1079
|
-
}
|
|
1080
|
-
let unsupported = null;
|
|
1081
|
-
function visit(current) {
|
|
1082
|
-
if (unsupported) {
|
|
1083
|
-
return;
|
|
1084
|
-
}
|
|
1085
|
-
if (ts.isVariableDeclaration(current)) {
|
|
1086
|
-
registerPattern(current.name);
|
|
1087
|
-
}
|
|
1088
|
-
if (ts.isIdentifier(current) && isValueIdentifier(current)) {
|
|
1089
|
-
const name = current.text;
|
|
1090
|
-
if (!locals.has(name) && !RESUMABLE_HANDLER_ALLOWED_GLOBALS.has(name) && !allowedTopLevel.has(name)) {
|
|
1091
|
-
unsupported = name;
|
|
1092
|
-
return;
|
|
1093
|
-
}
|
|
1094
|
-
}
|
|
1095
|
-
ts.forEachChild(current, visit);
|
|
1096
|
-
}
|
|
1097
|
-
ts.forEachChild(node, visit);
|
|
1098
|
-
return unsupported;
|
|
1099
|
-
}
|
|
1100
|
-
function isValueIdentifier(node) {
|
|
1101
|
-
const parent = node.parent;
|
|
1102
|
-
if (!parent) {
|
|
1103
|
-
return true;
|
|
1104
|
-
}
|
|
1105
|
-
if (ts.isPropertyAccessExpression(parent) && parent.name === node) {
|
|
1106
|
-
return false;
|
|
1107
|
-
}
|
|
1108
|
-
if (ts.isPropertyAssignment(parent) && parent.name === node) {
|
|
1109
|
-
return false;
|
|
1110
|
-
}
|
|
1111
|
-
if ((ts.isVariableDeclaration(parent) || ts.isFunctionDeclaration(parent) || ts.isParameter(parent)) && parent.name === node) {
|
|
1112
|
-
return false;
|
|
1113
|
-
}
|
|
1114
|
-
if (ts.isBindingElement(parent) && parent.name === node) {
|
|
1115
|
-
return false;
|
|
1116
|
-
}
|
|
1117
|
-
return true;
|
|
1118
|
-
}
|
|
1119
|
-
function isHandlerLikeInitializer(node) {
|
|
1120
|
-
return Boolean(node && (ts.isArrowFunction(node) || ts.isFunctionExpression(node)));
|
|
1121
|
-
}
|
|
1122
|
-
function isResumableInitializer(node) {
|
|
1123
|
-
if (!node) {
|
|
1124
|
-
return false;
|
|
1125
|
-
}
|
|
1126
|
-
if (ts.isCallExpression(node) && ts.isIdentifier(node.expression)) {
|
|
1127
|
-
return [
|
|
1128
|
-
"ref",
|
|
1129
|
-
"reactive",
|
|
1130
|
-
"computed",
|
|
1131
|
-
"readonly",
|
|
1132
|
-
"toRef",
|
|
1133
|
-
"toRefs",
|
|
1134
|
-
"watch",
|
|
1135
|
-
"watchEffect",
|
|
1136
|
-
"useState",
|
|
1137
|
-
"useAsyncData",
|
|
1138
|
-
"useRoute",
|
|
1139
|
-
"useRouter",
|
|
1140
|
-
"useRuntimeConfig",
|
|
1141
|
-
"useResuxApp",
|
|
1142
|
-
"useResuxImage",
|
|
1143
|
-
"defineProps"
|
|
1144
|
-
].includes(node.expression.text);
|
|
1145
|
-
}
|
|
1146
|
-
if (ts.isAwaitExpression(node) && ts.isCallExpression(node.expression) && ts.isIdentifier(node.expression.expression)) {
|
|
1147
|
-
return ["useAsyncData", "useFetch"].includes(node.expression.expression.text);
|
|
1148
|
-
}
|
|
1149
|
-
return false;
|
|
1150
|
-
}
|
|
1151
|
-
function collectBindingNames(name) {
|
|
1152
|
-
if (ts.isIdentifier(name)) {
|
|
1153
|
-
return [name.text];
|
|
1154
|
-
}
|
|
1155
|
-
const names = [];
|
|
1156
|
-
for (const element of name.elements) {
|
|
1157
|
-
if (ts.isBindingElement(element)) {
|
|
1158
|
-
names.push(...collectBindingNames(element.name));
|
|
1159
|
-
}
|
|
1160
|
-
}
|
|
1161
|
-
return names;
|
|
1162
|
-
}
|
|
1163
|
-
function createComponentModuleSource(options) {
|
|
1164
|
-
const importPath = options.client ? "/__resux/runtime-client.mjs" : "resuxjs/runtime";
|
|
1165
|
-
const factory = options.client ? "createClientComponent" : "defineComponent";
|
|
1166
|
-
const source = [
|
|
1167
|
-
`import { ${factory} } from ${JSON.stringify(importPath)};`,
|
|
1168
|
-
options.analysis.imports,
|
|
1169
|
-
`const __template = ${JSON.stringify(options.template, null, 2)};`,
|
|
1170
|
-
`async function __rx_setup(__ctx) {`,
|
|
1171
|
-
`const { ref, reactive, computed, watch, watchEffect, readonly, toRef, toRefs, unref, isRef, isReactive, isReadonly, nextTick, useState, useAsyncData, useRoute, useRouter, useHead, useSeoMeta, useRuntimeConfig, useResuxApp, useResuxImage, apiURL, useFetch, $fetch, onMounted, definePageMeta, defineProps } = __ctx;`,
|
|
1172
|
-
options.analysis.setupBody,
|
|
1173
|
-
`return { ${options.analysis.bindings.join(", ")} };`,
|
|
1174
|
-
`}`,
|
|
1175
|
-
`export default ${factory}({`,
|
|
1176
|
-
`id: ${JSON.stringify(options.id)},`,
|
|
1177
|
-
`name: ${JSON.stringify(options.name)},`,
|
|
1178
|
-
`file: ${JSON.stringify(normalizePath(options.file))},`,
|
|
1179
|
-
`script: __rx_setup,`,
|
|
1180
|
-
`template: __template,`,
|
|
1181
|
-
`handlers: ${JSON.stringify([...options.analysis.handlerNames])},`,
|
|
1182
|
-
`styles: ${JSON.stringify(options.styles)},`,
|
|
1183
|
-
`styleScopeId: ${JSON.stringify(options.styleScopeId)},`,
|
|
1184
|
-
`meta: ${JSON.stringify(options.analysis.pageMeta ?? {})}`,
|
|
1185
|
-
`});`
|
|
1186
|
-
].join("\n");
|
|
1187
|
-
return ts.transpileModule(source, {
|
|
1188
|
-
compilerOptions: {
|
|
1189
|
-
target: ts.ScriptTarget.ES2022,
|
|
1190
|
-
module: ts.ModuleKind.ES2022,
|
|
1191
|
-
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Remove
|
|
1192
|
-
}
|
|
1193
|
-
}).outputText;
|
|
1194
|
-
}
|
|
1195
|
-
function createVueIslandClientSource(island, entryDir) {
|
|
1196
|
-
const componentImport = relativeImportPath(entryDir, island.file);
|
|
1197
|
-
return [
|
|
1198
|
-
`import { createApp } from "vue";`,
|
|
1199
|
-
`import Component from ${JSON.stringify(componentImport)};`,
|
|
1200
|
-
`export function mount(el, props = {}) {`,
|
|
1201
|
-
` const app = createApp(Component, props);`,
|
|
1202
|
-
` app.mount(el);`,
|
|
1203
|
-
` return app;`,
|
|
1204
|
-
`}`,
|
|
1205
|
-
`export default { mount };`
|
|
1206
|
-
].join("\n");
|
|
1207
|
-
}
|
|
1208
|
-
function relativeImportPath(fromDir, file) {
|
|
1209
|
-
const relative = normalizePath(path.relative(fromDir, file));
|
|
1210
|
-
return relative.startsWith(".") ? relative : `./${relative}`;
|
|
1211
|
-
}
|
|
1212
|
-
function createServerManifestSource(routes, components, layouts, plugins, middleware, serverMiddleware, serverHandlers, vueIslands, app, error, runtimeConfig = {}, buildOptions = {}) {
|
|
1213
|
-
const importSuffix = buildOptions.vite === "dev" ? `?t=${Date.now()}` : "";
|
|
1214
|
-
const serverPlugins = plugins.filter((entry) => entry.mode !== "client");
|
|
1215
|
-
const clientPlugins = plugins.filter((entry) => entry.mode !== "server");
|
|
1216
|
-
const serverRouteMiddleware = middleware.filter((entry) => entry.mode !== "client");
|
|
1217
|
-
const clientRouteMiddleware = middleware.filter((entry) => entry.mode !== "server");
|
|
1218
|
-
const imports = components.map((component) => `import ${component.id} from "./${component.id}.mjs${importSuffix}";`).join("\n");
|
|
1219
|
-
const pluginImports = serverPlugins.map((plugin) => `import ${plugin.id} from "./resux-plugins/${plugin.id}.mjs${importSuffix}";`).join("\n");
|
|
1220
|
-
const middlewareImports = serverRouteMiddleware.map((entry) => `import ${entry.id} from "./resux-middleware/${entry.id}.mjs${importSuffix}";`).join("\n");
|
|
1221
|
-
const serverMiddlewareImports = serverMiddleware.map((entry) => `import ${entry.id} from "./request-middleware/${entry.id}.mjs${importSuffix}";`).join("\n");
|
|
1222
|
-
const handlerImports = serverHandlers.map((handler) => `import ${handler.id} from "./handlers/${handler.id}.mjs${importSuffix}";`).join("\n");
|
|
1223
|
-
const componentEntries = components.map((component) => `${JSON.stringify(component.name)}: ${component.id}`).join(",\n");
|
|
1224
|
-
const layoutEntries = layouts.map((layout) => `${JSON.stringify(layoutNameFromFile(layout.file))}: ${layout.id}`).join(",\n");
|
|
1225
|
-
const moduleEntries = components
|
|
1226
|
-
.map((component) => `${JSON.stringify(component.id)}: ${JSON.stringify(`/__resux/handlers/${component.id}.mjs`)}`)
|
|
1227
|
-
.join(",\n");
|
|
1228
|
-
const vueIslandEntries = vueIslands
|
|
1229
|
-
.map((island) => `${JSON.stringify(island.name)}: ${JSON.stringify(`/__resux/vue-islands/${island.name}.mjs`)}`)
|
|
1230
|
-
.join(",\n");
|
|
1231
|
-
const pluginEntries = serverPlugins
|
|
1232
|
-
.map((plugin) => plugin.id)
|
|
1233
|
-
.join(",\n");
|
|
1234
|
-
const clientPluginEntries = clientPlugins
|
|
1235
|
-
.map((plugin) => `{
|
|
1236
|
-
id: ${JSON.stringify(plugin.id)},
|
|
1237
|
-
file: ${JSON.stringify(normalizePath(plugin.file))},
|
|
1238
|
-
mode: ${JSON.stringify(plugin.mode)},
|
|
1239
|
-
src: ${JSON.stringify(`/__resux/plugins/${plugin.id}.mjs`)}
|
|
1240
|
-
}`)
|
|
1241
|
-
.join(",\n");
|
|
1242
|
-
const middlewareEntries = serverRouteMiddleware
|
|
1243
|
-
.map((entry) => `{
|
|
1244
|
-
id: ${JSON.stringify(entry.id)},
|
|
1245
|
-
name: ${JSON.stringify(entry.name)},
|
|
1246
|
-
file: ${JSON.stringify(normalizePath(entry.file))},
|
|
1247
|
-
global: ${JSON.stringify(entry.global)},
|
|
1248
|
-
mode: ${JSON.stringify(entry.mode)},
|
|
1249
|
-
handler: ${entry.id}
|
|
1250
|
-
}`)
|
|
1251
|
-
.join(",\n");
|
|
1252
|
-
const clientMiddlewareEntries = clientRouteMiddleware
|
|
1253
|
-
.map((entry) => `{
|
|
1254
|
-
id: ${JSON.stringify(entry.id)},
|
|
1255
|
-
name: ${JSON.stringify(entry.name)},
|
|
1256
|
-
file: ${JSON.stringify(normalizePath(entry.file))},
|
|
1257
|
-
global: ${JSON.stringify(entry.global)},
|
|
1258
|
-
mode: ${JSON.stringify(entry.mode)},
|
|
1259
|
-
src: ${JSON.stringify(`/__resux/middleware/${entry.id}.mjs`)}
|
|
1260
|
-
}`)
|
|
1261
|
-
.join(",\n");
|
|
1262
|
-
const serverMiddlewareEntries = serverMiddleware
|
|
1263
|
-
.map((entry) => `{
|
|
1264
|
-
id: ${JSON.stringify(entry.id)},
|
|
1265
|
-
file: ${JSON.stringify(normalizePath(entry.file))},
|
|
1266
|
-
handler: ${entry.id}
|
|
1267
|
-
}`)
|
|
1268
|
-
.join(",\n");
|
|
1269
|
-
const serverHandlerEntries = serverHandlers
|
|
1270
|
-
.map((handler) => `{
|
|
1271
|
-
id: ${JSON.stringify(handler.id)},
|
|
1272
|
-
path: ${JSON.stringify(handler.path)},
|
|
1273
|
-
file: ${JSON.stringify(normalizePath(handler.file))},
|
|
1274
|
-
params: ${JSON.stringify(handler.params)},
|
|
1275
|
-
handler: ${handler.id},
|
|
1276
|
-
match: createMatcher(${JSON.stringify(handler.path)})
|
|
1277
|
-
}`)
|
|
1278
|
-
.join(",\n");
|
|
1279
|
-
const routeEntries = routes
|
|
1280
|
-
.map((route) => {
|
|
1281
|
-
const component = components.find((candidate) => candidate.id === route.componentId);
|
|
1282
|
-
if (!component) {
|
|
1283
|
-
throw new Error(`Missing compiled component for ${route.file}.`);
|
|
1284
|
-
}
|
|
1285
|
-
return `{
|
|
1286
|
-
id: ${JSON.stringify(route.id)},
|
|
1287
|
-
path: ${JSON.stringify(route.path)},
|
|
1288
|
-
file: ${JSON.stringify(normalizePath(route.file))},
|
|
1289
|
-
params: ${JSON.stringify(route.params)},
|
|
1290
|
-
meta: ${JSON.stringify(route.meta ?? {})},
|
|
1291
|
-
component: ${route.componentId},
|
|
1292
|
-
match: createMatcher(${JSON.stringify(route.path)})
|
|
1293
|
-
}`;
|
|
1294
|
-
})
|
|
1295
|
-
.join(",\n");
|
|
1296
|
-
const appHead = createAppHead(runtimeConfig);
|
|
1297
|
-
const appRuntimeConfig = createRuntimeConfig(runtimeConfig);
|
|
1298
|
-
const routeRules = createRouteRules(runtimeConfig);
|
|
1299
|
-
return `${imports}
|
|
1300
|
-
${pluginImports}
|
|
1301
|
-
${middlewareImports}
|
|
1302
|
-
${serverMiddlewareImports}
|
|
1303
|
-
${handlerImports}
|
|
1304
|
-
export const app = ${app ? app.id : "undefined"};
|
|
1305
|
-
export const errorComponent = ${error ? error.id : "undefined"};
|
|
1306
|
-
export const components = {
|
|
1307
|
-
${componentEntries}
|
|
1308
|
-
};
|
|
1309
|
-
export const layouts = {
|
|
1310
|
-
${layoutEntries}
|
|
1311
|
-
};
|
|
1312
|
-
export const modules = {
|
|
1313
|
-
${moduleEntries}
|
|
1314
|
-
};
|
|
1315
|
-
export const vueIslands = {
|
|
1316
|
-
${vueIslandEntries}
|
|
1317
|
-
};
|
|
1318
|
-
export const resuxPlugins = [
|
|
1319
|
-
${pluginEntries}
|
|
1320
|
-
];
|
|
1321
|
-
export const clientPlugins = [
|
|
1322
|
-
${clientPluginEntries}
|
|
1323
|
-
];
|
|
1324
|
-
export const middleware = [
|
|
1325
|
-
${middlewareEntries}
|
|
1326
|
-
];
|
|
1327
|
-
export const clientMiddleware = [
|
|
1328
|
-
${clientMiddlewareEntries}
|
|
1329
|
-
];
|
|
1330
|
-
export const serverMiddleware = [
|
|
1331
|
-
${serverMiddlewareEntries}
|
|
1332
|
-
];
|
|
1333
|
-
export const serverHandlers = [
|
|
1334
|
-
${serverHandlerEntries}
|
|
1335
|
-
];
|
|
1336
|
-
export const runtimeConfig = ${JSON.stringify(appRuntimeConfig, null, 2)};
|
|
1337
|
-
export const appHead = ${JSON.stringify(appHead, null, 2)};
|
|
1338
|
-
export const routeRules = ${JSON.stringify(routeRules, null, 2)};
|
|
1339
|
-
export const routes = [
|
|
1340
|
-
${routeEntries}
|
|
1341
|
-
];
|
|
1342
|
-
export function matchRoute(pathname) {
|
|
1343
|
-
for (const route of routes) {
|
|
1344
|
-
const params = route.match(pathname);
|
|
1345
|
-
if (params) return { route, params };
|
|
1346
|
-
}
|
|
1347
|
-
return null;
|
|
1348
|
-
}
|
|
1349
|
-
export function matchServerHandler(pathname) {
|
|
1350
|
-
for (const route of serverHandlers) {
|
|
1351
|
-
const params = route.match(pathname);
|
|
1352
|
-
if (params) return { route, params };
|
|
1353
|
-
}
|
|
1354
|
-
return null;
|
|
1355
|
-
}
|
|
1356
|
-
function createMatcher(pattern) {
|
|
1357
|
-
const names = [];
|
|
1358
|
-
const source = pattern === "/" ? "^/$" : "^" + pattern.split("/").map((part) => {
|
|
1359
|
-
if (!part) return "";
|
|
1360
|
-
if (part.startsWith(":") && part.endsWith("*")) {
|
|
1361
|
-
names.push(part.slice(1, -1));
|
|
1362
|
-
return "(?:/(.*))?";
|
|
1363
|
-
}
|
|
1364
|
-
if (part.startsWith(":")) {
|
|
1365
|
-
names.push(part.slice(1));
|
|
1366
|
-
return "/([^/]+)";
|
|
1367
|
-
}
|
|
1368
|
-
return "/" + part.replace(/[.*+?^${"${"}}()|[\\]\\\\]/g, "\\\\$&");
|
|
1369
|
-
}).join("") + "$";
|
|
1370
|
-
const regex = new RegExp(source);
|
|
1371
|
-
return (pathname) => {
|
|
1372
|
-
const match = regex.exec(pathname);
|
|
1373
|
-
if (!match) return null;
|
|
1374
|
-
const params = {};
|
|
1375
|
-
names.forEach((name, index) => {
|
|
1376
|
-
params[name] = decodeURIComponent(match[index + 1] ?? "");
|
|
1377
|
-
});
|
|
1378
|
-
return params;
|
|
1379
|
-
};
|
|
1380
|
-
}
|
|
1381
|
-
`;
|
|
1382
|
-
}
|
|
1383
|
-
function createAppHead(runtimeConfig) {
|
|
1384
|
-
const config = runtimeConfig;
|
|
1385
|
-
const head = typeof config.app === "object" && config.app && "head" in config.app
|
|
1386
|
-
? config.app.head
|
|
1387
|
-
: {};
|
|
1388
|
-
const links = Array.isArray(config.css)
|
|
1389
|
-
? config.css.map((href) => ({ rel: "stylesheet", href: String(href) }))
|
|
1390
|
-
: [];
|
|
1391
|
-
return {
|
|
1392
|
-
...(typeof head === "object" && head ? head : {}),
|
|
1393
|
-
link: [
|
|
1394
|
-
...((head?.link) ?? []),
|
|
1395
|
-
...links
|
|
1396
|
-
]
|
|
1397
|
-
};
|
|
1398
|
-
}
|
|
1399
|
-
function createRuntimeConfig(config) {
|
|
1400
|
-
const runtimeConfig = config.runtimeConfig;
|
|
1401
|
-
return {
|
|
1402
|
-
...(runtimeConfig ?? {}),
|
|
1403
|
-
public: {
|
|
1404
|
-
...((runtimeConfig?.public && typeof runtimeConfig.public === "object" && !Array.isArray(runtimeConfig.public))
|
|
1405
|
-
? runtimeConfig.public
|
|
1406
|
-
: {})
|
|
1407
|
-
}
|
|
1408
|
-
};
|
|
1409
|
-
}
|
|
1410
|
-
function createRouteRules(config) {
|
|
1411
|
-
const routeRules = config.routeRules;
|
|
1412
|
-
if (!routeRules || typeof routeRules !== "object" || Array.isArray(routeRules)) {
|
|
1413
|
-
return {};
|
|
1414
|
-
}
|
|
1415
|
-
const normalized = {};
|
|
1416
|
-
for (const [routePath, rule] of Object.entries(routeRules)) {
|
|
1417
|
-
if (!routePath.startsWith("/") || !rule || typeof rule !== "object" || Array.isArray(rule)) {
|
|
1418
|
-
continue;
|
|
1419
|
-
}
|
|
1420
|
-
const routeRule = rule;
|
|
1421
|
-
normalized[routePath] = {
|
|
1422
|
-
...(routeRule.headers && typeof routeRule.headers === "object" ? { headers: normalizeStringRecord(routeRule.headers) } : {}),
|
|
1423
|
-
...(typeof routeRule.redirect === "string" || isRedirectObject(routeRule.redirect)
|
|
1424
|
-
? { redirect: routeRule.redirect }
|
|
1425
|
-
: {}),
|
|
1426
|
-
...(isStatusCode(routeRule.statusCode) ? { statusCode: routeRule.statusCode } : {}),
|
|
1427
|
-
...(normalizeCacheRule(routeRule.cache) !== undefined ? { cache: normalizeCacheRule(routeRule.cache) } : {}),
|
|
1428
|
-
...(normalizeCorsRule(routeRule.cors) !== undefined ? { cors: normalizeCorsRule(routeRule.cors) } : {})
|
|
1429
|
-
};
|
|
1430
|
-
}
|
|
1431
|
-
return normalized;
|
|
1432
|
-
}
|
|
1433
|
-
function isStatusCode(value) {
|
|
1434
|
-
return Number.isInteger(value) && value >= 100 && value <= 599;
|
|
1435
|
-
}
|
|
1436
|
-
function normalizeCacheRule(value) {
|
|
1437
|
-
if (value === undefined) {
|
|
1438
|
-
return undefined;
|
|
1439
|
-
}
|
|
1440
|
-
if (value === false || typeof value === "string") {
|
|
1441
|
-
return value;
|
|
1442
|
-
}
|
|
1443
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1444
|
-
return undefined;
|
|
1445
|
-
}
|
|
1446
|
-
const cache = {};
|
|
1447
|
-
if (Number.isFinite(value.maxAge) && value.maxAge >= 0) {
|
|
1448
|
-
cache.maxAge = Math.floor(value.maxAge);
|
|
1449
|
-
}
|
|
1450
|
-
if (Number.isFinite(value.swr) && value.swr >= 0) {
|
|
1451
|
-
cache.swr = Math.floor(value.swr);
|
|
1452
|
-
}
|
|
1453
|
-
return Object.keys(cache).length > 0 ? cache : undefined;
|
|
1454
|
-
}
|
|
1455
|
-
function normalizeCorsRule(value) {
|
|
1456
|
-
if (value === undefined) {
|
|
1457
|
-
return undefined;
|
|
1458
|
-
}
|
|
1459
|
-
if (typeof value === "boolean") {
|
|
1460
|
-
return value;
|
|
1461
|
-
}
|
|
1462
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
1463
|
-
return undefined;
|
|
1464
|
-
}
|
|
1465
|
-
return {
|
|
1466
|
-
...(typeof value.origin === "string" ? { origin: value.origin } : {}),
|
|
1467
|
-
...(Array.isArray(value.methods) ? { methods: value.methods.map(String) } : {}),
|
|
1468
|
-
...(Array.isArray(value.headers) ? { headers: value.headers.map(String) } : {}),
|
|
1469
|
-
...(typeof value.credentials === "boolean" ? { credentials: value.credentials } : {})
|
|
1470
|
-
};
|
|
1471
|
-
}
|
|
1472
|
-
function normalizeStringRecord(record) {
|
|
1473
|
-
const output = {};
|
|
1474
|
-
for (const [key, value] of Object.entries(record)) {
|
|
1475
|
-
if (value !== undefined && value !== null) {
|
|
1476
|
-
output[key] = String(value);
|
|
1477
|
-
}
|
|
1478
|
-
}
|
|
1479
|
-
return output;
|
|
1480
|
-
}
|
|
1481
|
-
function isRedirectObject(value) {
|
|
1482
|
-
return Boolean(value
|
|
1483
|
-
&& typeof value === "object"
|
|
1484
|
-
&& !Array.isArray(value)
|
|
1485
|
-
&& typeof value.to === "string");
|
|
1486
|
-
}
|
|
1487
|
-
function layoutNameFromFile(file) {
|
|
1488
|
-
return kebabCase(path.basename(file, ".vue"));
|
|
1489
|
-
}
|
|
1490
|
-
function supportFileMode(file) {
|
|
1491
|
-
if (file.includes(".client.")) {
|
|
1492
|
-
return "client";
|
|
1493
|
-
}
|
|
1494
|
-
if (file.includes(".server.")) {
|
|
1495
|
-
return "server";
|
|
1496
|
-
}
|
|
1497
|
-
return "all";
|
|
1498
|
-
}
|
|
1499
|
-
function supportFileBaseName(file) {
|
|
1500
|
-
const extensionStripped = path.basename(file).replace(/\.[cm]?[tj]s$/, "");
|
|
1501
|
-
return extensionStripped.replace(/(?:\.global|\.client|\.server)+$/, "");
|
|
1502
|
-
}
|
|
1503
|
-
function compilePluginFile(file, index) {
|
|
1504
|
-
const mode = supportFileMode(file);
|
|
1505
|
-
return {
|
|
1506
|
-
id: `p${index}`,
|
|
1507
|
-
file,
|
|
1508
|
-
mode,
|
|
1509
|
-
serverSource: transpileSupportModule(file, `import { defineResuxPlugin } from "resuxjs/runtime";\n`),
|
|
1510
|
-
clientSource: transpileSupportModule(file, `import { defineResuxPlugin } from "/__resux/runtime-client.mjs";\n`)
|
|
1511
|
-
};
|
|
1512
|
-
}
|
|
1513
|
-
function compileMiddlewareFile(file, index) {
|
|
1514
|
-
const baseName = supportFileBaseName(file);
|
|
1515
|
-
const mode = supportFileMode(file);
|
|
1516
|
-
const global = /\.global(?:\.(?:client|server))?\.[cm]?[tj]s$/.test(file);
|
|
1517
|
-
return {
|
|
1518
|
-
id: `w${index}`,
|
|
1519
|
-
name: kebabCase(baseName),
|
|
1520
|
-
file,
|
|
1521
|
-
mode,
|
|
1522
|
-
global,
|
|
1523
|
-
serverSource: transpileSupportModule(file, `import { defineResuxRouteMiddleware, navigateTo, abortNavigation } from "resuxjs/runtime";\n`),
|
|
1524
|
-
clientSource: transpileSupportModule(file, `import { defineResuxRouteMiddleware, defineClientRouteRedirect as navigateTo, defineClientRouteAbort as abortNavigation } from "/__resux/runtime-client.mjs";\n`)
|
|
1525
|
-
};
|
|
1526
|
-
}
|
|
1527
|
-
function compileServerMiddlewareFile(file, index) {
|
|
1528
|
-
return {
|
|
1529
|
-
id: `s${index}`,
|
|
1530
|
-
file,
|
|
1531
|
-
source: transpileSupportModule(file, `import { defineServerMiddleware, defineEventHandler, eventHandler, readBody, getQuery, setHeader } from "resuxjs/runtime";\n`)
|
|
1532
|
-
};
|
|
1533
|
-
}
|
|
1534
|
-
function compileServerHandlerFile(file, index, routePath) {
|
|
1535
|
-
const params = collectRouteParams(routePath);
|
|
1536
|
-
return {
|
|
1537
|
-
id: `h${index}`,
|
|
1538
|
-
path: routePath,
|
|
1539
|
-
file,
|
|
1540
|
-
params,
|
|
1541
|
-
source: transpileSupportModule(file, `import { defineEventHandler, eventHandler, readBody, getQuery, setHeader } from "resuxjs/runtime";\n`)
|
|
1542
|
-
};
|
|
1543
|
-
}
|
|
1544
|
-
function transpileSupportModule(file, prefix = "") {
|
|
1545
|
-
const source = ts.sys.readFile(file) ?? "";
|
|
1546
|
-
return ts.transpileModule(`${prefix}${source}`, {
|
|
1547
|
-
compilerOptions: {
|
|
1548
|
-
target: ts.ScriptTarget.ES2022,
|
|
1549
|
-
module: ts.ModuleKind.ES2022,
|
|
1550
|
-
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Remove
|
|
1551
|
-
}
|
|
1552
|
-
}).outputText;
|
|
1553
|
-
}
|
|
1554
|
-
async function readRuntimeConfig(root, outDir) {
|
|
1555
|
-
const candidates = [
|
|
1556
|
-
"resux.config.ts",
|
|
1557
|
-
"resux.config.mjs",
|
|
1558
|
-
"resux.config.js"
|
|
1559
|
-
];
|
|
1560
|
-
for (const candidate of candidates) {
|
|
1561
|
-
const file = await optionalFile(path.join(root, candidate));
|
|
1562
|
-
if (!file) {
|
|
1563
|
-
continue;
|
|
1564
|
-
}
|
|
1565
|
-
const source = normalizeConfigHelpers(await readFile(file, "utf8"));
|
|
1566
|
-
const outputFile = path.join(outDir, "server", "config.mjs");
|
|
1567
|
-
const compiled = ts.transpileModule(source, {
|
|
1568
|
-
compilerOptions: {
|
|
1569
|
-
target: ts.ScriptTarget.ES2022,
|
|
1570
|
-
module: ts.ModuleKind.ES2022
|
|
1571
|
-
}
|
|
1572
|
-
}).outputText;
|
|
1573
|
-
await writeFile(outputFile, compiled, "utf8");
|
|
1574
|
-
const imported = await import(`${pathToFileURL(outputFile).href}?t=${Date.now()}`);
|
|
1575
|
-
const config = (imported.default ?? {});
|
|
1576
|
-
await applyConfiguredModules(config, root, outDir);
|
|
1577
|
-
delete config.modules;
|
|
1578
|
-
await writeFile(outputFile, `export default ${JSON.stringify(config, null, 2)};`, "utf8");
|
|
1579
|
-
return config;
|
|
1580
|
-
}
|
|
1581
|
-
return {};
|
|
1582
|
-
}
|
|
1583
|
-
function normalizeConfigHelpers(source) {
|
|
1584
|
-
return source
|
|
1585
|
-
.replace(/\bdefineResuxConfig\s*\(/g, "(")
|
|
1586
|
-
.replace(/\bdefineResuxModule\s*\(/g, "(");
|
|
1587
|
-
}
|
|
1588
|
-
async function applyConfiguredModules(config, root, outDir) {
|
|
1589
|
-
const modules = Array.isArray(config.modules) ? config.modules : [];
|
|
1590
|
-
const context = createModuleContext(config, root, outDir);
|
|
1591
|
-
for (const moduleEntry of modules) {
|
|
1592
|
-
const { module, options } = await resolveModuleEntry(moduleEntry, root, outDir);
|
|
1593
|
-
const setup = getModuleSetup(module);
|
|
1594
|
-
await setup(createModuleOptions(module, options), context);
|
|
1595
|
-
}
|
|
1596
|
-
}
|
|
1597
|
-
function createModuleContext(config, root, outDir) {
|
|
1598
|
-
return {
|
|
1599
|
-
rootDir: root,
|
|
1600
|
-
buildDir: outDir,
|
|
1601
|
-
options: config,
|
|
1602
|
-
addCss(href) {
|
|
1603
|
-
config.css = [...(Array.isArray(config.css) ? config.css : []), href];
|
|
1604
|
-
},
|
|
1605
|
-
addHead(head) {
|
|
1606
|
-
const app = config.app && typeof config.app === "object" ? config.app : {};
|
|
1607
|
-
const currentHead = app.head && typeof app.head === "object" ? app.head : {};
|
|
1608
|
-
app.head = mergeHeadConfig(currentHead, head);
|
|
1609
|
-
config.app = app;
|
|
1610
|
-
},
|
|
1611
|
-
addRouteRule(routePath, rule) {
|
|
1612
|
-
if (!routePath.startsWith("/")) {
|
|
1613
|
-
throw new Error(`Route rules need an absolute path. Received "${routePath}".`);
|
|
1614
|
-
}
|
|
1615
|
-
config.routeRules = {
|
|
1616
|
-
...(config.routeRules ?? {}),
|
|
1617
|
-
[routePath]: {
|
|
1618
|
-
...(config.routeRules?.[routePath] ?? {}),
|
|
1619
|
-
...rule,
|
|
1620
|
-
headers: {
|
|
1621
|
-
...(config.routeRules?.[routePath]?.headers ?? {}),
|
|
1622
|
-
...(rule.headers ?? {})
|
|
1623
|
-
}
|
|
1624
|
-
}
|
|
1625
|
-
};
|
|
1626
|
-
},
|
|
1627
|
-
extendRuntimeConfig(runtimeConfig) {
|
|
1628
|
-
config.runtimeConfig = deepMerge(config.runtimeConfig ?? {}, runtimeConfig);
|
|
1629
|
-
}
|
|
1630
|
-
};
|
|
1631
|
-
}
|
|
1632
|
-
function mergeHeadConfig(current, next) {
|
|
1633
|
-
return {
|
|
1634
|
-
...current,
|
|
1635
|
-
...next,
|
|
1636
|
-
meta: [
|
|
1637
|
-
...(Array.isArray(current.meta) ? current.meta : []),
|
|
1638
|
-
...(Array.isArray(next.meta) ? next.meta : [])
|
|
1639
|
-
],
|
|
1640
|
-
link: [
|
|
1641
|
-
...(Array.isArray(current.link) ? current.link : []),
|
|
1642
|
-
...(Array.isArray(next.link) ? next.link : [])
|
|
1643
|
-
]
|
|
1644
|
-
};
|
|
1645
|
-
}
|
|
1646
|
-
function deepMerge(base, next) {
|
|
1647
|
-
const output = { ...base };
|
|
1648
|
-
for (const [key, value] of Object.entries(next)) {
|
|
1649
|
-
const current = output[key];
|
|
1650
|
-
output[key] = isPlainObject(current) && isPlainObject(value)
|
|
1651
|
-
? deepMerge(current, value)
|
|
1652
|
-
: value;
|
|
1653
|
-
}
|
|
1654
|
-
return output;
|
|
1655
|
-
}
|
|
1656
|
-
function isPlainObject(value) {
|
|
1657
|
-
return Boolean(value && typeof value === "object" && !Array.isArray(value));
|
|
1658
|
-
}
|
|
1659
|
-
async function resolveModuleEntry(entry, root, outDir) {
|
|
1660
|
-
if (Array.isArray(entry)) {
|
|
1661
|
-
return {
|
|
1662
|
-
module: await loadModule(entry[0], root, outDir),
|
|
1663
|
-
options: entry[1] ?? {}
|
|
1664
|
-
};
|
|
1665
|
-
}
|
|
1666
|
-
return {
|
|
1667
|
-
module: await loadModule(entry, root, outDir),
|
|
1668
|
-
options: {}
|
|
1669
|
-
};
|
|
1670
|
-
}
|
|
1671
|
-
async function loadModule(module, root, outDir) {
|
|
1672
|
-
if (typeof module === "function" || isPlainObject(module)) {
|
|
1673
|
-
return module;
|
|
1674
|
-
}
|
|
1675
|
-
if (typeof module !== "string") {
|
|
1676
|
-
throw new Error("Resux modules must be functions, module objects, module specifiers, or [module, options] tuples.");
|
|
1677
|
-
}
|
|
1678
|
-
const builtin = resolveBuiltinModule(module);
|
|
1679
|
-
if (builtin) {
|
|
1680
|
-
return builtin;
|
|
1681
|
-
}
|
|
1682
|
-
const resolved = resolveModuleSpecifier(module, root);
|
|
1683
|
-
const imported = await import(`${await moduleImportUrl(resolved, outDir)}?t=${Date.now()}`);
|
|
1684
|
-
return imported.default ?? imported;
|
|
1685
|
-
}
|
|
1686
|
-
function getModuleSetup(module) {
|
|
1687
|
-
if (typeof module === "function") {
|
|
1688
|
-
return module;
|
|
1689
|
-
}
|
|
1690
|
-
if (isPlainObject(module) && typeof module.setup === "function") {
|
|
1691
|
-
return module.setup;
|
|
1692
|
-
}
|
|
1693
|
-
throw new Error("Resux modules must export a setup function or defineResuxModule({ setup }).");
|
|
1694
|
-
}
|
|
1695
|
-
function createModuleOptions(module, options) {
|
|
1696
|
-
if (isPlainObject(module) && isPlainObject(module.defaults)) {
|
|
1697
|
-
return {
|
|
1698
|
-
...module.defaults,
|
|
1699
|
-
...(isPlainObject(options) ? options : {})
|
|
1700
|
-
};
|
|
1701
|
-
}
|
|
1702
|
-
return options;
|
|
1703
|
-
}
|
|
1704
|
-
function resolveBuiltinModule(specifier) {
|
|
1705
|
-
const normalized = specifier.startsWith("resux:") ? specifier.slice("resux:".length) : specifier;
|
|
1706
|
-
return builtinModules[normalized] ?? null;
|
|
1707
|
-
}
|
|
1708
|
-
const builtinModules = {
|
|
1709
|
-
security: {
|
|
1710
|
-
defaults: {
|
|
1711
|
-
route: "/**",
|
|
1712
|
-
contentSecurityPolicy: false
|
|
1713
|
-
},
|
|
1714
|
-
setup(options, resux) {
|
|
1715
|
-
const input = isPlainObject(options) ? options : {};
|
|
1716
|
-
const routePath = firstString(input.route) ?? "/**";
|
|
1717
|
-
const headers = {
|
|
1718
|
-
"x-content-type-options": "nosniff",
|
|
1719
|
-
"referrer-policy": "strict-origin-when-cross-origin",
|
|
1720
|
-
"x-frame-options": "SAMEORIGIN",
|
|
1721
|
-
"cross-origin-opener-policy": "same-origin",
|
|
1722
|
-
"permissions-policy": "camera=(), microphone=(), geolocation=()",
|
|
1723
|
-
...(isPlainObject(input.headers) ? normalizeStringRecord(input.headers) : {})
|
|
1724
|
-
};
|
|
1725
|
-
const csp = firstString(input.contentSecurityPolicy);
|
|
1726
|
-
if (csp) {
|
|
1727
|
-
headers["content-security-policy"] = csp;
|
|
1728
|
-
}
|
|
1729
|
-
resux.addRouteRule(routePath, { headers });
|
|
1730
|
-
resux.extendRuntimeConfig({ public: { securityHeaders: true } });
|
|
1731
|
-
}
|
|
1732
|
-
},
|
|
1733
|
-
performance: {
|
|
1734
|
-
defaults: {
|
|
1735
|
-
assetMaxAge: 31536000,
|
|
1736
|
-
routePayloadNoStore: true
|
|
1737
|
-
},
|
|
1738
|
-
setup(options, resux) {
|
|
1739
|
-
const input = isPlainObject(options) ? options : {};
|
|
1740
|
-
const maxAge = Number.isFinite(input.assetMaxAge) ? Math.max(0, Math.floor(input.assetMaxAge)) : 31536000;
|
|
1741
|
-
const assetCache = { maxAge };
|
|
1742
|
-
resux.addRouteRule("/__resux/runtime-client.mjs", { cache: assetCache });
|
|
1743
|
-
resux.addRouteRule("/__resux/handlers/**", { cache: assetCache });
|
|
1744
|
-
resux.addRouteRule("/__resux/vue-islands/**", { cache: assetCache });
|
|
1745
|
-
resux.addRouteRule("/__resux/image", { cache: assetCache });
|
|
1746
|
-
if (input.routePayloadNoStore !== false) {
|
|
1747
|
-
resux.addRouteRule("/__resux/route", { cache: false });
|
|
1748
|
-
}
|
|
1749
|
-
resux.extendRuntimeConfig({ public: { performanceModule: { assetMaxAge: maxAge } } });
|
|
1750
|
-
}
|
|
1751
|
-
}
|
|
1752
|
-
};
|
|
1753
|
-
function firstString(value) {
|
|
1754
|
-
return typeof value === "string" && value.length > 0 ? value : null;
|
|
1755
|
-
}
|
|
1756
|
-
function resolveModuleSpecifier(specifier, root) {
|
|
1757
|
-
if (specifier.startsWith(".") || specifier.startsWith("/") || specifier.startsWith("file:")) {
|
|
1758
|
-
const resolved = specifier.startsWith("file:")
|
|
1759
|
-
? new URL(specifier)
|
|
1760
|
-
: path.resolve(root, specifier);
|
|
1761
|
-
const file = resolved instanceof URL ? fileURLToPath(resolved) : resolved;
|
|
1762
|
-
return resolveLocalModuleFile(file);
|
|
1763
|
-
}
|
|
1764
|
-
return require.resolve(specifier, { paths: [root] });
|
|
1765
|
-
}
|
|
1766
|
-
function resolveLocalModuleFile(file) {
|
|
1767
|
-
const candidates = [
|
|
1768
|
-
file,
|
|
1769
|
-
`${file}.ts`,
|
|
1770
|
-
`${file}.mts`,
|
|
1771
|
-
`${file}.js`,
|
|
1772
|
-
`${file}.mjs`,
|
|
1773
|
-
path.join(file, "index.ts"),
|
|
1774
|
-
path.join(file, "index.mts"),
|
|
1775
|
-
path.join(file, "index.js"),
|
|
1776
|
-
path.join(file, "index.mjs")
|
|
1777
|
-
];
|
|
1778
|
-
const found = candidates.find((candidate) => ts.sys.fileExists(candidate));
|
|
1779
|
-
if (!found) {
|
|
1780
|
-
throw new Error(`Cannot resolve Resux module "${file}".`);
|
|
1781
|
-
}
|
|
1782
|
-
return found;
|
|
1783
|
-
}
|
|
1784
|
-
async function moduleImportUrl(file, outDir) {
|
|
1785
|
-
if (/\.[cm]?ts$/.test(file)) {
|
|
1786
|
-
const outputFile = path.join(outDir, "server", "config-modules", `${safeModuleFileName(file)}.mjs`);
|
|
1787
|
-
await writeFile(outputFile, transpileConfigSupportModule(file), "utf8");
|
|
1788
|
-
return pathToFileURL(outputFile).href;
|
|
1789
|
-
}
|
|
1790
|
-
return pathToFileURL(file).href;
|
|
1791
|
-
}
|
|
1792
|
-
function transpileConfigSupportModule(file) {
|
|
1793
|
-
const source = normalizeConfigHelpers(ts.sys.readFile(file) ?? "");
|
|
1794
|
-
return ts.transpileModule(source, {
|
|
1795
|
-
compilerOptions: {
|
|
1796
|
-
target: ts.ScriptTarget.ES2022,
|
|
1797
|
-
module: ts.ModuleKind.ES2022,
|
|
1798
|
-
importsNotUsedAsValues: ts.ImportsNotUsedAsValues.Remove
|
|
1799
|
-
}
|
|
1800
|
-
}).outputText;
|
|
1801
|
-
}
|
|
1802
|
-
function safeModuleFileName(file) {
|
|
1803
|
-
return normalizePath(path.resolve(file)).replace(/[^A-Za-z0-9_-]+/g, "_");
|
|
1804
|
-
}
|
|
1805
|
-
function nextBindingId(state) {
|
|
1806
|
-
return `b${state.bindingIndex++}`;
|
|
1807
|
-
}
|
|
1808
|
-
function nextInlineHandlerName(state) {
|
|
1809
|
-
return `__rx_inline_${state.inlineHandlerIndex++}`;
|
|
1810
|
-
}
|
|
1811
|
-
function routeScore(routePath) {
|
|
1812
|
-
return routePath
|
|
1813
|
-
.split("/")
|
|
1814
|
-
.filter(Boolean)
|
|
1815
|
-
.reduce((score, part) => score + (part.startsWith(":") ? 1 : 3), 0);
|
|
1816
|
-
}
|
|
1817
|
-
async function discoverVueFiles(dir) {
|
|
1818
|
-
try {
|
|
1819
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
1820
|
-
const files = [];
|
|
1821
|
-
for (const entry of entries) {
|
|
1822
|
-
const fullPath = path.join(dir, entry.name);
|
|
1823
|
-
if (entry.isDirectory()) {
|
|
1824
|
-
files.push(...await discoverVueFiles(fullPath));
|
|
1825
|
-
}
|
|
1826
|
-
else if (entry.isFile() && entry.name.endsWith(".vue")) {
|
|
1827
|
-
files.push(fullPath);
|
|
1828
|
-
}
|
|
1829
|
-
}
|
|
1830
|
-
return files.sort();
|
|
1831
|
-
}
|
|
1832
|
-
catch {
|
|
1833
|
-
return [];
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
async function discoverAppVueFiles(root, dirName) {
|
|
1837
|
-
return unique([
|
|
1838
|
-
...await discoverVueFiles(path.join(root, dirName)),
|
|
1839
|
-
...await discoverVueFiles(path.join(root, "app", dirName))
|
|
1840
|
-
]);
|
|
1841
|
-
}
|
|
1842
|
-
async function discoverSupportTsFiles(root, dirName) {
|
|
1843
|
-
const dirs = [path.join(root, dirName), path.join(root, "app", dirName)];
|
|
1844
|
-
const files = [];
|
|
1845
|
-
for (const dir of dirs) {
|
|
1846
|
-
files.push(...await discoverSupportTsFilesInDir(dir));
|
|
1847
|
-
}
|
|
1848
|
-
return unique(files).sort((left, right) => normalizePath(left).localeCompare(normalizePath(right)));
|
|
1849
|
-
}
|
|
1850
|
-
async function discoverSupportTsFilesInDir(dir) {
|
|
1851
|
-
try {
|
|
1852
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
1853
|
-
const files = [];
|
|
1854
|
-
for (const entry of entries) {
|
|
1855
|
-
const fullPath = path.join(dir, entry.name);
|
|
1856
|
-
if (entry.isDirectory()) {
|
|
1857
|
-
files.push(...await discoverSupportTsFilesInDir(fullPath));
|
|
1858
|
-
continue;
|
|
1859
|
-
}
|
|
1860
|
-
if (!entry.isFile()) {
|
|
1861
|
-
continue;
|
|
1862
|
-
}
|
|
1863
|
-
if (!/\.[cm]?[tj]s$/.test(entry.name)) {
|
|
1864
|
-
continue;
|
|
1865
|
-
}
|
|
1866
|
-
if (/\.d\.[cm]?[tj]s$/.test(entry.name)) {
|
|
1867
|
-
continue;
|
|
1868
|
-
}
|
|
1869
|
-
files.push(fullPath);
|
|
1870
|
-
}
|
|
1871
|
-
return files;
|
|
1872
|
-
}
|
|
1873
|
-
catch {
|
|
1874
|
-
return [];
|
|
1875
|
-
}
|
|
1876
|
-
}
|
|
1877
|
-
async function discoverServerFiles(dir, routePrefix) {
|
|
1878
|
-
try {
|
|
1879
|
-
const entries = await readdir(dir, { withFileTypes: true });
|
|
1880
|
-
const files = [];
|
|
1881
|
-
for (const entry of entries) {
|
|
1882
|
-
const fullPath = path.join(dir, entry.name);
|
|
1883
|
-
if (entry.isDirectory()) {
|
|
1884
|
-
files.push(...await discoverServerFiles(fullPath, `${routePrefix}/${entry.name}`));
|
|
1885
|
-
}
|
|
1886
|
-
else if (entry.isFile() && /\.[cm]?[tj]s$/.test(entry.name)) {
|
|
1887
|
-
const name = entry.name.replace(/\.[cm]?[tj]s$/, "");
|
|
1888
|
-
const segment = name === "index" ? "" : `/${name}`;
|
|
1889
|
-
files.push({
|
|
1890
|
-
file: fullPath,
|
|
1891
|
-
path: normalizeRoutePath(`${routePrefix}${segment}`)
|
|
1892
|
-
});
|
|
1893
|
-
}
|
|
1894
|
-
}
|
|
1895
|
-
return files.sort((a, b) => a.path.localeCompare(b.path));
|
|
1896
|
-
}
|
|
1897
|
-
catch {
|
|
1898
|
-
return [];
|
|
1899
|
-
}
|
|
1900
|
-
}
|
|
1901
|
-
async function discoverVueIslands(root) {
|
|
1902
|
-
const islandRoot = path.join(root, "islands", "vue");
|
|
1903
|
-
const files = await discoverVueFiles(islandRoot);
|
|
1904
|
-
return files.map((file) => {
|
|
1905
|
-
const relative = path.relative(islandRoot, file).replace(/\.vue$/, "");
|
|
1906
|
-
return {
|
|
1907
|
-
name: pascalCase(relative),
|
|
1908
|
-
file
|
|
1909
|
-
};
|
|
1910
|
-
});
|
|
1911
|
-
}
|
|
1912
|
-
async function findFirstExisting(files) {
|
|
1913
|
-
for (const file of files) {
|
|
1914
|
-
const found = await optionalFile(file);
|
|
1915
|
-
if (found) {
|
|
1916
|
-
return found;
|
|
1917
|
-
}
|
|
1918
|
-
}
|
|
1919
|
-
return null;
|
|
1920
|
-
}
|
|
1921
|
-
function unique(items) {
|
|
1922
|
-
return [...new Set(items)];
|
|
1923
|
-
}
|
|
1924
|
-
async function optionalFile(file) {
|
|
89
|
+
syncBuiltinESMExports();
|
|
1925
90
|
try {
|
|
1926
|
-
await
|
|
1927
|
-
return file;
|
|
91
|
+
return await task();
|
|
1928
92
|
}
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
}
|
|
1933
|
-
function inferComponentName(root, file) {
|
|
1934
|
-
const relative = normalizePath(path.relative(root, file));
|
|
1935
|
-
if (relative === "app.vue") {
|
|
1936
|
-
return "App";
|
|
93
|
+
finally {
|
|
94
|
+
mutableFs.readFile = originalReadFile;
|
|
95
|
+
syncBuiltinESMExports();
|
|
1937
96
|
}
|
|
1938
|
-
return pascalCase(path.basename(file, ".vue"));
|
|
1939
97
|
}
|
|
1940
98
|
function pascalCase(value) {
|
|
1941
99
|
return value
|
|
@@ -1945,59 +103,4 @@ function pascalCase(value) {
|
|
|
1945
103
|
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
|
1946
104
|
.join("");
|
|
1947
105
|
}
|
|
1948
|
-
|
|
1949
|
-
return value
|
|
1950
|
-
.replace(/\[[^\]]+\]/g, (match) => match.slice(1, -1))
|
|
1951
|
-
.replace(/([a-z0-9])([A-Z])/g, "$1-$2")
|
|
1952
|
-
.replace(/\.global$/, "")
|
|
1953
|
-
.split(/[^A-Za-z0-9]+/)
|
|
1954
|
-
.filter(Boolean)
|
|
1955
|
-
.join("-")
|
|
1956
|
-
.toLowerCase();
|
|
1957
|
-
}
|
|
1958
|
-
function normalizePath(value) {
|
|
1959
|
-
return value.replaceAll("\\", "/");
|
|
1960
|
-
}
|
|
1961
|
-
function normalizeRoutePath(value) {
|
|
1962
|
-
const normalized = value
|
|
1963
|
-
.replaceAll("\\", "/")
|
|
1964
|
-
.replace(/\.[cm]?[tj]s$/, "")
|
|
1965
|
-
.replace(/\[\.\.\.([A-Za-z0-9_]+)\]/g, ":$1*")
|
|
1966
|
-
.replace(/\[([A-Za-z0-9_]+)\]/g, ":$1")
|
|
1967
|
-
.replace(/\/index$/, "");
|
|
1968
|
-
return (`/${normalized}`.replace(/\/+/g, "/").replace(/\/$/, "")) || "/";
|
|
1969
|
-
}
|
|
1970
|
-
function collectRouteParams(routePath) {
|
|
1971
|
-
return routePath
|
|
1972
|
-
.split("/")
|
|
1973
|
-
.filter((part) => part.startsWith(":"))
|
|
1974
|
-
.map((part) => part.slice(1).replace(/\*$/, ""));
|
|
1975
|
-
}
|
|
1976
|
-
function locationFromVueNode(file, node) {
|
|
1977
|
-
return {
|
|
1978
|
-
file,
|
|
1979
|
-
line: node.loc?.start?.line ?? 1,
|
|
1980
|
-
column: node.loc?.start?.column ?? 1
|
|
1981
|
-
};
|
|
1982
|
-
}
|
|
1983
|
-
function locationFromTsNode(file, sourceFile, node) {
|
|
1984
|
-
const nodeSourceFile = node.getSourceFile() ?? sourceFile;
|
|
1985
|
-
const position = nodeSourceFile.getLineAndCharacterOfPosition(node.getStart(nodeSourceFile));
|
|
1986
|
-
return {
|
|
1987
|
-
file,
|
|
1988
|
-
line: position.line + 1,
|
|
1989
|
-
column: position.character + 1
|
|
1990
|
-
};
|
|
1991
|
-
}
|
|
1992
|
-
function locationFromOffset(file, source, offset) {
|
|
1993
|
-
const lines = source.slice(0, offset).split(/\r?\n/);
|
|
1994
|
-
return {
|
|
1995
|
-
file,
|
|
1996
|
-
line: lines.length,
|
|
1997
|
-
column: lines[lines.length - 1].length + 1
|
|
1998
|
-
};
|
|
1999
|
-
}
|
|
2000
|
-
export function fileUrl(file) {
|
|
2001
|
-
return pathToFileURL(file).href;
|
|
2002
|
-
}
|
|
2003
|
-
//# sourceMappingURL=index.js.map
|
|
106
|
+
//# sourceMappingURL=adapter.js.map
|