@nativescript/vite 8.0.0-alpha.0 → 8.0.0-alpha.10
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/configuration/angular.d.ts +1 -1
- package/configuration/angular.js +486 -140
- package/configuration/angular.js.map +1 -1
- package/configuration/base.js +159 -41
- package/configuration/base.js.map +1 -1
- package/configuration/javascript.js +3 -3
- package/configuration/javascript.js.map +1 -1
- package/configuration/solid.js +7 -0
- package/configuration/solid.js.map +1 -1
- package/configuration/typescript.js +4 -4
- package/configuration/typescript.js.map +1 -1
- package/helpers/angular/angular-linker.js +38 -42
- package/helpers/angular/angular-linker.js.map +1 -1
- package/helpers/angular/inject-component-hmr-registration.d.ts +112 -0
- package/helpers/angular/inject-component-hmr-registration.js +359 -0
- package/helpers/angular/inject-component-hmr-registration.js.map +1 -0
- package/helpers/angular/inline-decorator-component-templates.d.ts +3 -0
- package/helpers/angular/inline-decorator-component-templates.js +400 -0
- package/helpers/angular/inline-decorator-component-templates.js.map +1 -0
- package/helpers/angular/shared-linker.d.ts +7 -0
- package/helpers/angular/shared-linker.js +37 -1
- package/helpers/angular/shared-linker.js.map +1 -1
- package/helpers/angular/synthesize-decorator-ctor-parameters.d.ts +1 -0
- package/helpers/angular/synthesize-decorator-ctor-parameters.js +256 -0
- package/helpers/angular/synthesize-decorator-ctor-parameters.js.map +1 -0
- package/helpers/angular/synthesize-injectable-factories.d.ts +3 -0
- package/helpers/angular/synthesize-injectable-factories.js +414 -0
- package/helpers/angular/synthesize-injectable-factories.js.map +1 -0
- package/helpers/angular/util.d.ts +1 -0
- package/helpers/angular/util.js +88 -0
- package/helpers/angular/util.js.map +1 -1
- package/helpers/commonjs-plugins.d.ts +5 -2
- package/helpers/commonjs-plugins.js +126 -0
- package/helpers/commonjs-plugins.js.map +1 -1
- package/helpers/config-as-json.js +10 -0
- package/helpers/config-as-json.js.map +1 -1
- package/helpers/esbuild-platform-resolver.js +5 -5
- package/helpers/esbuild-platform-resolver.js.map +1 -1
- package/helpers/external-configs.d.ts +9 -1
- package/helpers/external-configs.js +31 -6
- package/helpers/external-configs.js.map +1 -1
- package/helpers/global-defines.d.ts +51 -0
- package/helpers/global-defines.js +77 -0
- package/helpers/global-defines.js.map +1 -1
- package/helpers/import-meta-path.d.ts +4 -0
- package/helpers/import-meta-path.js +5 -0
- package/helpers/import-meta-path.js.map +1 -0
- package/helpers/import-specifier.d.ts +1 -0
- package/helpers/import-specifier.js +18 -0
- package/helpers/import-specifier.js.map +1 -0
- package/helpers/logging.d.ts +1 -0
- package/helpers/logging.js +63 -3
- package/helpers/logging.js.map +1 -1
- package/helpers/main-entry.d.ts +5 -2
- package/helpers/main-entry.js +365 -116
- package/helpers/main-entry.js.map +1 -1
- package/helpers/nativeclass-transform.js +8 -127
- package/helpers/nativeclass-transform.js.map +1 -1
- package/helpers/nativeclass-transformer-plugin.d.ts +19 -1
- package/helpers/nativeclass-transformer-plugin.js +318 -31
- package/helpers/nativeclass-transformer-plugin.js.map +1 -1
- package/helpers/ns-core-url.d.ts +83 -0
- package/helpers/ns-core-url.js +167 -0
- package/helpers/ns-core-url.js.map +1 -0
- package/helpers/prelink-angular.js +1 -4
- package/helpers/prelink-angular.js.map +1 -1
- package/helpers/preserve-imports.js +2 -17
- package/helpers/preserve-imports.js.map +1 -1
- package/helpers/project.d.ts +35 -0
- package/helpers/project.js +120 -2
- package/helpers/project.js.map +1 -1
- package/helpers/ts-config-paths.js +50 -2
- package/helpers/ts-config-paths.js.map +1 -1
- package/helpers/workers.d.ts +20 -19
- package/helpers/workers.js +620 -3
- package/helpers/workers.js.map +1 -1
- package/hmr/client/css-handler.js +60 -19
- package/hmr/client/css-handler.js.map +1 -1
- package/hmr/client/hmr-pending-overlay.d.ts +27 -0
- package/hmr/client/hmr-pending-overlay.js +50 -0
- package/hmr/client/hmr-pending-overlay.js.map +1 -0
- package/hmr/client/index.js +597 -24
- package/hmr/client/index.js.map +1 -1
- package/hmr/client/utils.d.ts +5 -0
- package/hmr/client/utils.js +212 -21
- package/hmr/client/utils.js.map +1 -1
- package/hmr/entry-runtime.d.ts +10 -0
- package/hmr/entry-runtime.js +330 -42
- package/hmr/entry-runtime.js.map +1 -1
- package/hmr/frameworks/angular/client/index.d.ts +3 -1
- package/hmr/frameworks/angular/client/index.js +821 -25
- package/hmr/frameworks/angular/client/index.js.map +1 -1
- package/hmr/frameworks/angular/server/linker.js +37 -6
- package/hmr/frameworks/angular/server/linker.js.map +1 -1
- package/hmr/frameworks/angular/server/strategy.js +30 -6
- package/hmr/frameworks/angular/server/strategy.js.map +1 -1
- package/hmr/frameworks/typescript/server/strategy.js +8 -2
- package/hmr/frameworks/typescript/server/strategy.js.map +1 -1
- package/hmr/frameworks/vue/client/index.js +18 -42
- package/hmr/frameworks/vue/client/index.js.map +1 -1
- package/hmr/helpers/ast-normalizer.js +22 -10
- package/hmr/helpers/ast-normalizer.js.map +1 -1
- package/hmr/helpers/cjs-named-exports.d.ts +23 -0
- package/hmr/helpers/cjs-named-exports.js +152 -0
- package/hmr/helpers/cjs-named-exports.js.map +1 -0
- package/hmr/server/constants.d.ts +1 -0
- package/hmr/server/constants.js +14 -3
- package/hmr/server/constants.js.map +1 -1
- package/hmr/server/core-sanitize.d.ts +49 -2
- package/hmr/server/core-sanitize.js +267 -24
- package/hmr/server/core-sanitize.js.map +1 -1
- package/hmr/server/import-map.d.ts +65 -0
- package/hmr/server/import-map.js +222 -0
- package/hmr/server/import-map.js.map +1 -0
- package/hmr/server/index.d.ts +2 -1
- package/hmr/server/index.js.map +1 -1
- package/hmr/server/ns-core-cjs-shape.d.ts +204 -0
- package/hmr/server/ns-core-cjs-shape.js +271 -0
- package/hmr/server/ns-core-cjs-shape.js.map +1 -0
- package/hmr/server/perf-instrumentation.d.ts +114 -0
- package/hmr/server/perf-instrumentation.js +195 -0
- package/hmr/server/perf-instrumentation.js.map +1 -0
- package/hmr/server/runtime-graph-filter.d.ts +5 -0
- package/hmr/server/runtime-graph-filter.js +21 -0
- package/hmr/server/runtime-graph-filter.js.map +1 -0
- package/hmr/server/shared-transform-request.d.ts +12 -0
- package/hmr/server/shared-transform-request.js +144 -0
- package/hmr/server/shared-transform-request.js.map +1 -0
- package/hmr/server/vite-plugin.d.ts +21 -1
- package/hmr/server/vite-plugin.js +461 -22
- package/hmr/server/vite-plugin.js.map +1 -1
- package/hmr/server/websocket-angular-entry.d.ts +2 -0
- package/hmr/server/websocket-angular-entry.js +68 -0
- package/hmr/server/websocket-angular-entry.js.map +1 -0
- package/hmr/server/websocket-angular-hot-update.d.ts +78 -0
- package/hmr/server/websocket-angular-hot-update.js +413 -0
- package/hmr/server/websocket-angular-hot-update.js.map +1 -0
- package/hmr/server/websocket-core-bridge.d.ts +21 -0
- package/hmr/server/websocket-core-bridge.js +357 -0
- package/hmr/server/websocket-core-bridge.js.map +1 -0
- package/hmr/server/websocket-graph-upsert.d.ts +21 -0
- package/hmr/server/websocket-graph-upsert.js +33 -0
- package/hmr/server/websocket-graph-upsert.js.map +1 -0
- package/hmr/server/websocket-hmr-pending.d.ts +43 -0
- package/hmr/server/websocket-hmr-pending.js +55 -0
- package/hmr/server/websocket-hmr-pending.js.map +1 -0
- package/hmr/server/websocket-module-bindings.d.ts +6 -0
- package/hmr/server/websocket-module-bindings.js +471 -0
- package/hmr/server/websocket-module-bindings.js.map +1 -0
- package/hmr/server/websocket-module-specifiers.d.ts +101 -0
- package/hmr/server/websocket-module-specifiers.js +820 -0
- package/hmr/server/websocket-module-specifiers.js.map +1 -0
- package/hmr/server/websocket-ns-m-finalize.d.ts +22 -0
- package/hmr/server/websocket-ns-m-finalize.js +88 -0
- package/hmr/server/websocket-ns-m-finalize.js.map +1 -0
- package/hmr/server/websocket-ns-m-paths.d.ts +3 -0
- package/hmr/server/websocket-ns-m-paths.js +92 -0
- package/hmr/server/websocket-ns-m-paths.js.map +1 -0
- package/hmr/server/websocket-ns-m-request.d.ts +45 -0
- package/hmr/server/websocket-ns-m-request.js +196 -0
- package/hmr/server/websocket-ns-m-request.js.map +1 -0
- package/hmr/server/websocket-runtime-compat.d.ts +19 -0
- package/hmr/server/websocket-runtime-compat.js +287 -0
- package/hmr/server/websocket-runtime-compat.js.map +1 -0
- package/hmr/server/websocket-served-module-helpers.d.ts +36 -0
- package/hmr/server/websocket-served-module-helpers.js +631 -0
- package/hmr/server/websocket-served-module-helpers.js.map +1 -0
- package/hmr/server/websocket-txn.d.ts +6 -0
- package/hmr/server/websocket-txn.js +45 -0
- package/hmr/server/websocket-txn.js.map +1 -0
- package/hmr/server/websocket-vendor-unifier.d.ts +10 -0
- package/hmr/server/websocket-vendor-unifier.js +51 -0
- package/hmr/server/websocket-vendor-unifier.js.map +1 -0
- package/hmr/server/websocket-vue-sfc.d.ts +27 -0
- package/hmr/server/websocket-vue-sfc.js +1069 -0
- package/hmr/server/websocket-vue-sfc.js.map +1 -0
- package/hmr/server/websocket.d.ts +26 -3
- package/hmr/server/websocket.js +2233 -796
- package/hmr/server/websocket.js.map +1 -1
- package/hmr/shared/package-classifier.d.ts +9 -0
- package/hmr/shared/package-classifier.js +58 -0
- package/hmr/shared/package-classifier.js.map +1 -0
- package/hmr/shared/runtime/boot-timeline.d.ts +17 -0
- package/hmr/shared/runtime/boot-timeline.js +51 -0
- package/hmr/shared/runtime/boot-timeline.js.map +1 -0
- package/hmr/shared/runtime/browser-runtime-contract.d.ts +64 -0
- package/hmr/shared/runtime/browser-runtime-contract.js +54 -0
- package/hmr/shared/runtime/browser-runtime-contract.js.map +1 -0
- package/hmr/shared/runtime/dev-overlay.d.ts +85 -0
- package/hmr/shared/runtime/dev-overlay.js +1236 -0
- package/hmr/shared/runtime/dev-overlay.js.map +1 -0
- package/hmr/shared/runtime/http-only-boot.d.ts +1 -0
- package/hmr/shared/runtime/http-only-boot.js +53 -6
- package/hmr/shared/runtime/http-only-boot.js.map +1 -1
- package/hmr/shared/runtime/module-provenance.d.ts +1 -0
- package/hmr/shared/runtime/module-provenance.js +63 -0
- package/hmr/shared/runtime/module-provenance.js.map +1 -0
- package/hmr/shared/runtime/platform-polyfills.d.ts +26 -0
- package/hmr/shared/runtime/platform-polyfills.js +122 -0
- package/hmr/shared/runtime/platform-polyfills.js.map +1 -0
- package/hmr/shared/runtime/root-placeholder.d.ts +1 -0
- package/hmr/shared/runtime/root-placeholder.js +552 -82
- package/hmr/shared/runtime/root-placeholder.js.map +1 -1
- package/hmr/shared/runtime/session-bootstrap.d.ts +1 -0
- package/hmr/shared/runtime/session-bootstrap.js +195 -0
- package/hmr/shared/runtime/session-bootstrap.js.map +1 -0
- package/hmr/shared/runtime/vendor-bootstrap.js +52 -15
- package/hmr/shared/runtime/vendor-bootstrap.js.map +1 -1
- package/hmr/shared/vendor/manifest.d.ts +37 -0
- package/hmr/shared/vendor/manifest.js +677 -57
- package/hmr/shared/vendor/manifest.js.map +1 -1
- package/hmr/shared/vendor/registry.js +104 -7
- package/hmr/shared/vendor/registry.js.map +1 -1
- package/index.d.ts +1 -0
- package/index.js +5 -0
- package/index.js.map +1 -1
- package/package.json +14 -2
- package/runtime/core-aliases-early.js +94 -67
- package/runtime/core-aliases-early.js.map +1 -1
- package/shims/solid-jsx-runtime.d.ts +7 -0
- package/shims/solid-jsx-runtime.js +17 -0
- package/shims/solid-jsx-runtime.js.map +1 -0
|
@@ -0,0 +1,1069 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import { createHash } from 'crypto';
|
|
3
|
+
import { readFileSync } from 'fs';
|
|
4
|
+
import * as path from 'path';
|
|
5
|
+
import babelCore from '@babel/core';
|
|
6
|
+
import { parse as babelParse } from '@babel/parser';
|
|
7
|
+
import traverse from '@babel/traverse';
|
|
8
|
+
import * as t from '@babel/types';
|
|
9
|
+
import { genCode } from '../helpers/babel.js';
|
|
10
|
+
import { astExtractImportsAndStripTypes } from '../helpers/ast-extract.js';
|
|
11
|
+
import { astNormalizeModuleImportsAndHelpers, astVerifyAndAnnotateDuplicates } from '../helpers/ast-normalizer.js';
|
|
12
|
+
import { stripRtCoreSentinel, stripDanglingViteCjsImports } from '../helpers/sanitize.js';
|
|
13
|
+
import { vueSfcCompiler } from '../frameworks/vue/server/compiler.js';
|
|
14
|
+
import { buildInlineTemplateBlock, extractTemplateRender, processTemplateVariantMinimal } from '../frameworks/vue/server/sfc-transforms.js';
|
|
15
|
+
import { NS_NATIVE_TAGS } from './compiler.js';
|
|
16
|
+
import { ensureDestructureCoreImports, ensureGuardPlainDynamicImports, ensureVariableDynamicImportHelper, ensureVersionedRtImports, extractExportMetadata } from './websocket-served-module-helpers.js';
|
|
17
|
+
const babelTraverse = traverse?.default || traverse;
|
|
18
|
+
const { parse, compileTemplate, compileScript } = vueSfcCompiler;
|
|
19
|
+
const pluginTransformTypescript = (() => {
|
|
20
|
+
const requireFromHere = createRequire(import.meta.url);
|
|
21
|
+
const loaded = requireFromHere('@babel/plugin-transform-typescript');
|
|
22
|
+
return loaded?.default || loaded;
|
|
23
|
+
})();
|
|
24
|
+
function setJavascriptResponseHeaders(res) {
|
|
25
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
26
|
+
res.setHeader('Content-Type', 'application/javascript; charset=utf-8');
|
|
27
|
+
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
|
|
28
|
+
res.setHeader('Pragma', 'no-cache');
|
|
29
|
+
res.setHeader('Expires', '0');
|
|
30
|
+
}
|
|
31
|
+
function setJsonResponseHeaders(res) {
|
|
32
|
+
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
33
|
+
res.setHeader('Content-Type', 'application/json; charset=utf-8');
|
|
34
|
+
res.setHeader('Cache-Control', 'no-store, no-cache, must-revalidate, max-age=0');
|
|
35
|
+
res.setHeader('Pragma', 'no-cache');
|
|
36
|
+
res.setHeader('Expires', '0');
|
|
37
|
+
}
|
|
38
|
+
export function parseVersionedEndpointPath(basePath, pathname) {
|
|
39
|
+
const rawRemainder = pathname.slice(basePath.length) || '';
|
|
40
|
+
let verFromPath = null;
|
|
41
|
+
let pathStyle = rawRemainder;
|
|
42
|
+
if (rawRemainder && rawRemainder.startsWith('/')) {
|
|
43
|
+
const parts = rawRemainder.split('/');
|
|
44
|
+
if (parts.length > 2 && /^[0-9]+$/.test(parts[1] || '')) {
|
|
45
|
+
verFromPath = parts[1];
|
|
46
|
+
pathStyle = '/' + parts.slice(2).join('/');
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return { verFromPath, pathStyle };
|
|
50
|
+
}
|
|
51
|
+
export function buildSfcDelegatedModule(importerPath, version) {
|
|
52
|
+
const asmPath = `/ns/asm/${version}?path=${encodeURIComponent(importerPath)}`;
|
|
53
|
+
return `// [sfc] kind=full (delegated to assembler) path=${importerPath}\nexport * from ${JSON.stringify(asmPath)};\nexport { default } from ${JSON.stringify(asmPath)};\n`;
|
|
54
|
+
}
|
|
55
|
+
export function templateHasRender(templateCode) {
|
|
56
|
+
return /export\s+function\s+render\s*\(/.test(templateCode) || /(?:^|\n)\s*function\s+render\s*\(/.test(templateCode) || /export\s+(?:const|let|var)\s+render\s*=/.test(templateCode) || /(?:^|\n)\s*(?:const|let|var)\s+render\s*=/.test(templateCode) || /\brender\s*[:=]\s*/.test(templateCode) || /export\s*\{\s*render\s*(?:as\s*render)?\s*\}/.test(templateCode);
|
|
57
|
+
}
|
|
58
|
+
function normalizeRequestedSpec(spec, appVirtualWithSlash) {
|
|
59
|
+
let normalized = spec || '';
|
|
60
|
+
if (normalized.startsWith('@/'))
|
|
61
|
+
normalized = appVirtualWithSlash + normalized.slice(2);
|
|
62
|
+
if (!normalized.startsWith('/'))
|
|
63
|
+
normalized = '/' + normalized;
|
|
64
|
+
return normalized;
|
|
65
|
+
}
|
|
66
|
+
export function registerVueSfcHandlers(server, options) {
|
|
67
|
+
server.middlewares.use(async (req, res, next) => {
|
|
68
|
+
try {
|
|
69
|
+
const urlObj = new URL(req.url || '', 'http://localhost');
|
|
70
|
+
const pathname = urlObj.pathname;
|
|
71
|
+
const isNs = pathname === '/ns/sfc' || pathname.startsWith('/ns/sfc/');
|
|
72
|
+
if (!isNs)
|
|
73
|
+
return next();
|
|
74
|
+
if (pathname.startsWith('/ns/asm') || pathname.startsWith('/ns/sfc-meta'))
|
|
75
|
+
return next();
|
|
76
|
+
setJavascriptResponseHeaders(res);
|
|
77
|
+
const basePath = '/ns/sfc';
|
|
78
|
+
let pathParam = urlObj.searchParams.get('path') || '';
|
|
79
|
+
const { verFromPath, pathStyle: rawPathStyle } = parseVersionedEndpointPath(basePath, pathname);
|
|
80
|
+
let pathStyle = rawPathStyle;
|
|
81
|
+
if (pathStyle && pathStyle !== '/' && !pathParam) {
|
|
82
|
+
if (!pathStyle.startsWith('/'))
|
|
83
|
+
pathStyle = '/' + pathStyle;
|
|
84
|
+
pathParam = pathStyle + (urlObj.search || '');
|
|
85
|
+
}
|
|
86
|
+
let fullSpec = pathParam || '';
|
|
87
|
+
if (!fullSpec) {
|
|
88
|
+
res.statusCode = 200;
|
|
89
|
+
res.end('export {}\n');
|
|
90
|
+
return;
|
|
91
|
+
}
|
|
92
|
+
fullSpec = normalizeRequestedSpec(fullSpec, options.appVirtualWithSlash);
|
|
93
|
+
const isVariant = /[?&]vue&type=/.test(fullSpec);
|
|
94
|
+
const variantTypeMatch = /[?&]type=([^&]+)/.exec(fullSpec);
|
|
95
|
+
const variantType = variantTypeMatch?.[1] || null;
|
|
96
|
+
const isStyleVariant = /[?&]type=style\b/.test(fullSpec);
|
|
97
|
+
let candidate = fullSpec;
|
|
98
|
+
let transformed = null;
|
|
99
|
+
if (!isVariant) {
|
|
100
|
+
const baseFilePath = fullSpec.replace(/[?#].*$/, '');
|
|
101
|
+
const candidates = [baseFilePath + (baseFilePath.includes('?') ? '&' : '?') + 'vue', baseFilePath];
|
|
102
|
+
for (const currentCandidate of candidates) {
|
|
103
|
+
try {
|
|
104
|
+
const result = await server.transformRequest(currentCandidate);
|
|
105
|
+
if (result?.code) {
|
|
106
|
+
transformed = result;
|
|
107
|
+
candidate = currentCandidate;
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
catch { }
|
|
112
|
+
}
|
|
113
|
+
if (!transformed?.code) {
|
|
114
|
+
if (options.verbose) {
|
|
115
|
+
console.warn('[sfc][serve] transform miss for', fullSpec);
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const tried = candidates.slice(0, 8);
|
|
119
|
+
const out = `// [sfc] transform miss kind=full path=${fullSpec.replace(/\n/g, '')} tried=${tried.length}\n` + `throw new Error(${JSON.stringify('[ns/sfc] transform failed for full SFC: ' + fullSpec + ' (tried ' + tried.length + ')')});\nexport {}\n`;
|
|
120
|
+
res.statusCode = 404;
|
|
121
|
+
res.end(out);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
catch {
|
|
125
|
+
res.statusCode = 404;
|
|
126
|
+
res.end('export {}\n');
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
try {
|
|
133
|
+
transformed = await server.transformRequest(candidate);
|
|
134
|
+
}
|
|
135
|
+
catch { }
|
|
136
|
+
if (!transformed?.code) {
|
|
137
|
+
try {
|
|
138
|
+
const out = `// [sfc] transform miss kind=variant path=${fullSpec.replace(/\n/g, '')}\n` + `throw new Error(${JSON.stringify('[ns/sfc] transform failed for variant: ' + fullSpec)});\nexport {}\n`;
|
|
139
|
+
res.statusCode = 404;
|
|
140
|
+
res.end(out);
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
res.statusCode = 404;
|
|
145
|
+
res.end('export {}\n');
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
if (isStyleVariant) {
|
|
151
|
+
const sig = `// [sfc] kind=variant:style path=${fullSpec.replace(/\n/g, '')} len=0 default=false\n`;
|
|
152
|
+
res.statusCode = 200;
|
|
153
|
+
res.end(`${sig}export {}\n`);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
let code = transformed.code;
|
|
157
|
+
code = options.requireGuardSnippet + code;
|
|
158
|
+
const projectRoot = server.config?.root || process.cwd();
|
|
159
|
+
if (!isVariant) {
|
|
160
|
+
const importerPath = fullSpec.replace(/[?#].*$/, '');
|
|
161
|
+
const version = verFromPath || '0';
|
|
162
|
+
res.statusCode = 200;
|
|
163
|
+
res.end(buildSfcDelegatedModule(importerPath, version));
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
if (variantType === 'template') {
|
|
167
|
+
const preferSelfCompile = !!process.env.NS_HMR_SELF_COMPILE_TEMPLATE;
|
|
168
|
+
if (preferSelfCompile) {
|
|
169
|
+
try {
|
|
170
|
+
const projectRootTemplate = server.config?.root || process.cwd();
|
|
171
|
+
const baseFilePath = fullSpec.replace(/[?#].*$/, '');
|
|
172
|
+
const absolutePath = path.join(projectRootTemplate, baseFilePath.replace(/^\//, ''));
|
|
173
|
+
let sfcSource = '';
|
|
174
|
+
try {
|
|
175
|
+
sfcSource = readFileSync(absolutePath, 'utf-8');
|
|
176
|
+
}
|
|
177
|
+
catch { }
|
|
178
|
+
if (sfcSource) {
|
|
179
|
+
const { descriptor } = parse(sfcSource, { filename: absolutePath });
|
|
180
|
+
const id = createHash('md5').update(absolutePath).digest('hex').slice(0, 8);
|
|
181
|
+
let bindingMetadata = undefined;
|
|
182
|
+
try {
|
|
183
|
+
const script = compileScript(descriptor, {
|
|
184
|
+
id,
|
|
185
|
+
inlineTemplate: false,
|
|
186
|
+
reactivityTransform: false,
|
|
187
|
+
});
|
|
188
|
+
bindingMetadata = script?.bindings;
|
|
189
|
+
}
|
|
190
|
+
catch { }
|
|
191
|
+
const templateSource = descriptor.template?.content || '';
|
|
192
|
+
const compiledTemplate = compileTemplate({
|
|
193
|
+
source: templateSource,
|
|
194
|
+
id,
|
|
195
|
+
filename: absolutePath,
|
|
196
|
+
isProd: false,
|
|
197
|
+
ssr: false,
|
|
198
|
+
compilerOptions: {
|
|
199
|
+
bindingMetadata,
|
|
200
|
+
isCustomElement: (tag) => NS_NATIVE_TAGS.has(tag),
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
let out = (compiledTemplate && (compiledTemplate.code || '')) || '';
|
|
204
|
+
try {
|
|
205
|
+
out = out.replace(/from\s+["'](?:nativescript-vue|vue)[^"']*["']/g, 'from "/ns/rt"');
|
|
206
|
+
}
|
|
207
|
+
catch { }
|
|
208
|
+
code = processTemplateVariantMinimal(out);
|
|
209
|
+
}
|
|
210
|
+
else {
|
|
211
|
+
code = 'export {}\n';
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
catch (templateError) {
|
|
215
|
+
if (options.verbose) {
|
|
216
|
+
console.warn('[sfc][template][self-compile][fail]', fullSpec, templateError?.message);
|
|
217
|
+
}
|
|
218
|
+
code = transformed.code || 'export {}\n';
|
|
219
|
+
code = processTemplateVariantMinimal(code);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
code = transformed.code || 'export {}\n';
|
|
224
|
+
code = processTemplateVariantMinimal(code);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
try {
|
|
228
|
+
const ast = babelParse(code, {
|
|
229
|
+
sourceType: 'module',
|
|
230
|
+
plugins: ['typescript'],
|
|
231
|
+
});
|
|
232
|
+
const templateBindings = new Set();
|
|
233
|
+
const navToLocals = [];
|
|
234
|
+
const navBackLocals = [];
|
|
235
|
+
babelTraverse(ast, {
|
|
236
|
+
ImportDeclaration(pathNode) {
|
|
237
|
+
const spec = pathNode.node.source.value || '';
|
|
238
|
+
if (typeof spec === 'string' && /\.vue\?[^\n]*type=template/.test(spec)) {
|
|
239
|
+
const ids = [];
|
|
240
|
+
for (const currentSpecifier of pathNode.node.specifiers) {
|
|
241
|
+
if (t.isImportSpecifier(currentSpecifier)) {
|
|
242
|
+
const imported = t.isIdentifier(currentSpecifier.imported) ? currentSpecifier.imported.name : undefined;
|
|
243
|
+
const local = t.isIdentifier(currentSpecifier.local) ? currentSpecifier.local.name : undefined;
|
|
244
|
+
if ((imported === 'render' || imported === undefined) && local)
|
|
245
|
+
ids.push(local);
|
|
246
|
+
}
|
|
247
|
+
else if (t.isImportDefaultSpecifier(currentSpecifier) || t.isImportNamespaceSpecifier(currentSpecifier)) {
|
|
248
|
+
if (t.isIdentifier(currentSpecifier.local))
|
|
249
|
+
ids.push(currentSpecifier.local.name);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
ids.forEach((name) => templateBindings.add(name));
|
|
253
|
+
pathNode.remove();
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const isNsVue = typeof spec === 'string' && (/nativescript-vue/.test(spec) || /vendor\.mjs$/.test(spec) || /\/node_modules\/\.vite\/deps\/nativescript-vue\.js/.test(spec));
|
|
257
|
+
if (isNsVue) {
|
|
258
|
+
const remain = [];
|
|
259
|
+
for (const currentSpecifier of pathNode.node.specifiers) {
|
|
260
|
+
if (t.isImportSpecifier(currentSpecifier)) {
|
|
261
|
+
const imported = t.isIdentifier(currentSpecifier.imported) ? currentSpecifier.imported.name : undefined;
|
|
262
|
+
const local = t.isIdentifier(currentSpecifier.local) ? currentSpecifier.local.name : undefined;
|
|
263
|
+
if (local && (imported === '$navigateTo' || imported === 'navigateTo')) {
|
|
264
|
+
navToLocals.push(local);
|
|
265
|
+
continue;
|
|
266
|
+
}
|
|
267
|
+
if (local && (imported === '$navigateBack' || imported === 'navigateBack')) {
|
|
268
|
+
navBackLocals.push(local);
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
remain.push(currentSpecifier);
|
|
273
|
+
}
|
|
274
|
+
if (remain.length) {
|
|
275
|
+
pathNode.node.specifiers = remain;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
pathNode.remove();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
},
|
|
282
|
+
});
|
|
283
|
+
if (templateBindings.size) {
|
|
284
|
+
babelTraverse(ast, {
|
|
285
|
+
Identifier(pathNode) {
|
|
286
|
+
if (templateBindings.has(pathNode.node.name)) {
|
|
287
|
+
pathNode.replaceWith(t.identifier('undefined'));
|
|
288
|
+
}
|
|
289
|
+
},
|
|
290
|
+
AssignmentExpression(pathNode) {
|
|
291
|
+
if (t.isMemberExpression(pathNode.node.left) && t.isIdentifier(pathNode.node.left.property, { name: 'render' })) {
|
|
292
|
+
const e = t.identifier('__e');
|
|
293
|
+
const guarded = t.tryStatement(t.blockStatement([t.variableDeclaration('const', [t.variableDeclarator(e, pathNode.node.right)]), t.ifStatement(t.logicalExpression('&&', t.binaryExpression('!==', t.unaryExpression('typeof', pathNode.node.left.object, true), t.stringLiteral('undefined')), t.binaryExpression('!==', t.unaryExpression('typeof', e, true), t.stringLiteral('undefined'))), t.blockStatement([t.expressionStatement(t.assignmentExpression('=', pathNode.node.left, e))]))]), t.catchClause(t.identifier('_e'), t.blockStatement([])));
|
|
294
|
+
pathNode.replaceWithMultiple([guarded]);
|
|
295
|
+
}
|
|
296
|
+
},
|
|
297
|
+
});
|
|
298
|
+
}
|
|
299
|
+
let outCode = genCode(ast).code;
|
|
300
|
+
if (navToLocals.length || navBackLocals.length) {
|
|
301
|
+
const shimLines = [];
|
|
302
|
+
for (const currentName of navToLocals)
|
|
303
|
+
shimLines.push(`import __ns_rt_nav_to_mod from "/ns/rt";\nconst ${currentName} = (...args) => __ns_rt_nav_to_mod.$navigateTo(...args);`);
|
|
304
|
+
for (const currentName of navBackLocals)
|
|
305
|
+
shimLines.push(`import __ns_rt_nav_back_mod from "/ns/rt";\nconst ${currentName} = (...args) => __ns_rt_nav_back_mod.$navigateBack(...args);`);
|
|
306
|
+
outCode = shimLines.join('\n') + '\n' + outCode;
|
|
307
|
+
}
|
|
308
|
+
code = outCode;
|
|
309
|
+
}
|
|
310
|
+
catch { }
|
|
311
|
+
code = options.processCodeForDevice(code, false, true, /(?:^|\/)node_modules\//.test(fullSpec), fullSpec);
|
|
312
|
+
try {
|
|
313
|
+
const importerPath = fullSpec.replace(/[?#].*$/, '');
|
|
314
|
+
const version = verFromPath || '0';
|
|
315
|
+
const ast = babelParse(code, {
|
|
316
|
+
sourceType: 'module',
|
|
317
|
+
plugins: ['typescript'],
|
|
318
|
+
});
|
|
319
|
+
babelTraverse(ast, {
|
|
320
|
+
ImportDeclaration(pathNode) {
|
|
321
|
+
const src = pathNode.node.source.value || '';
|
|
322
|
+
if (typeof src !== 'string')
|
|
323
|
+
return;
|
|
324
|
+
if (/^https?:\/\//.test(src))
|
|
325
|
+
return;
|
|
326
|
+
if (/\.vue(?:$|\?)/.test(src)) {
|
|
327
|
+
let spec = src;
|
|
328
|
+
if (spec.startsWith('./') || spec.startsWith('../')) {
|
|
329
|
+
spec = path.posix.normalize(path.posix.join(path.posix.dirname(importerPath), spec));
|
|
330
|
+
if (!spec.startsWith('/'))
|
|
331
|
+
spec = '/' + spec;
|
|
332
|
+
}
|
|
333
|
+
else if (!spec.startsWith('/')) {
|
|
334
|
+
if (spec.startsWith('@@/'))
|
|
335
|
+
spec = '/' + spec.slice(2);
|
|
336
|
+
if (spec.startsWith('@/'))
|
|
337
|
+
spec = options.appVirtualWithSlash + spec.slice(2);
|
|
338
|
+
}
|
|
339
|
+
if (!/\bvue&type=/.test(src)) {
|
|
340
|
+
spec = spec.replace(/[?#].*$/, '');
|
|
341
|
+
const asmUrl = `/ns/asm/${version}?path=${encodeURIComponent(spec)}&mode=inline`;
|
|
342
|
+
pathNode.node.source = t.stringLiteral(asmUrl);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
},
|
|
346
|
+
});
|
|
347
|
+
code = genCode(ast).code;
|
|
348
|
+
}
|
|
349
|
+
catch { }
|
|
350
|
+
try {
|
|
351
|
+
const importerPath = fullSpec.replace(/[?#].*$/, '');
|
|
352
|
+
const tsResult = await babelCore.transformAsync(code, {
|
|
353
|
+
plugins: [[pluginTransformTypescript, { allowDeclareFields: true }]],
|
|
354
|
+
sourceType: 'module',
|
|
355
|
+
filename: importerPath.endsWith('.vue') ? importerPath.replace(/\.vue$/, '.ts') : importerPath + '.ts',
|
|
356
|
+
comments: true,
|
|
357
|
+
configFile: false,
|
|
358
|
+
babelrc: false,
|
|
359
|
+
});
|
|
360
|
+
if (tsResult?.code) {
|
|
361
|
+
code = tsResult.code;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
catch (variantTsError) {
|
|
365
|
+
if (options.verbose) {
|
|
366
|
+
console.warn('[sfc][variant:script][babel-ts][fail]', fullSpec, variantTsError?.message);
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
const importerPath = fullSpec.replace(/[?#].*$/, '');
|
|
370
|
+
if (variantType !== 'template') {
|
|
371
|
+
code = options.cleanCode(code);
|
|
372
|
+
}
|
|
373
|
+
code = options.rewriteImports(code, importerPath, options.sfcFileMap, options.depFileMap, projectRoot, !!options.verbose, undefined, options.getServerOrigin(server));
|
|
374
|
+
code = ensureVariableDynamicImportHelper(code);
|
|
375
|
+
try {
|
|
376
|
+
const versionNumber = Number(verFromPath || '0');
|
|
377
|
+
const currentVersion = Number.isFinite(versionNumber) && versionNumber > 0 ? versionNumber : options.getGraphVersion();
|
|
378
|
+
const origin = options.getServerOrigin(server);
|
|
379
|
+
code = ensureVersionedRtImports(code, origin, currentVersion);
|
|
380
|
+
code = options.getStrategy().ensureVersionedImports(code, origin, currentVersion);
|
|
381
|
+
code = options.ensureVersionedCoreImports(code, origin, currentVersion);
|
|
382
|
+
}
|
|
383
|
+
catch { }
|
|
384
|
+
try {
|
|
385
|
+
code = ensureDestructureCoreImports(code);
|
|
386
|
+
}
|
|
387
|
+
catch { }
|
|
388
|
+
try {
|
|
389
|
+
code = astNormalizeModuleImportsAndHelpers(code);
|
|
390
|
+
}
|
|
391
|
+
catch { }
|
|
392
|
+
try {
|
|
393
|
+
code = stripRtCoreSentinel(code);
|
|
394
|
+
}
|
|
395
|
+
catch { }
|
|
396
|
+
try {
|
|
397
|
+
const versionNumber = Number(verFromPath || '0');
|
|
398
|
+
const currentVersion = Number.isFinite(versionNumber) && versionNumber > 0 ? versionNumber : options.getGraphVersion();
|
|
399
|
+
const origin = options.getServerOrigin(server);
|
|
400
|
+
code = ensureVersionedRtImports(code, origin, currentVersion);
|
|
401
|
+
code = options.ensureVersionedCoreImports(code, origin, currentVersion);
|
|
402
|
+
}
|
|
403
|
+
catch { }
|
|
404
|
+
try {
|
|
405
|
+
code = stripDanglingViteCjsImports(code);
|
|
406
|
+
}
|
|
407
|
+
catch { }
|
|
408
|
+
const hasDefault = /\bexport\s+default\b/.test(code);
|
|
409
|
+
const kind = isVariant ? `variant:${variantType || 'unknown'}` : 'full';
|
|
410
|
+
const sig = `// [sfc] kind=${kind} path=${importerPath} len=${code.length} default=${hasDefault} wrapped=${false}\n`;
|
|
411
|
+
if (options.verbose) {
|
|
412
|
+
console.log(`[sfc][serve] ${fullSpec} kind=${kind} default=${hasDefault} bytes=${code.length}`);
|
|
413
|
+
}
|
|
414
|
+
if (!hasDefault) {
|
|
415
|
+
const match = code.match(/\b(?:const|let|var)\s+(__ns_sfc__|_sfc_main)\b/);
|
|
416
|
+
if (match && match[1]) {
|
|
417
|
+
code += `\nexport default ${match[1]};`;
|
|
418
|
+
}
|
|
419
|
+
else if (/\b_defineComponent\s*\(|\bdefineComponent\s*\(/.test(code)) {
|
|
420
|
+
code += '\nexport default (typeof __ns_sfc__ !== "undefined" ? __ns_sfc__ : (typeof _sfc_main !== "undefined" ? _sfc_main : undefined));';
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
res.statusCode = 200;
|
|
424
|
+
res.end(sig + code);
|
|
425
|
+
}
|
|
426
|
+
catch {
|
|
427
|
+
res.statusCode = 500;
|
|
428
|
+
res.end('export {}\n');
|
|
429
|
+
}
|
|
430
|
+
});
|
|
431
|
+
server.middlewares.use(async (req, res, next) => {
|
|
432
|
+
try {
|
|
433
|
+
const urlObj = new URL(req.url || '', 'http://localhost');
|
|
434
|
+
if (!urlObj.pathname.startsWith('/ns/sfc-meta'))
|
|
435
|
+
return next();
|
|
436
|
+
setJsonResponseHeaders(res);
|
|
437
|
+
let spec = urlObj.searchParams.get('path') || '';
|
|
438
|
+
if (!spec) {
|
|
439
|
+
res.statusCode = 400;
|
|
440
|
+
res.end(JSON.stringify({ error: 'missing path' }));
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
spec = normalizeRequestedSpec(spec, options.appVirtualWithSlash);
|
|
444
|
+
const base = spec.replace(/[?#].*$/, '');
|
|
445
|
+
const [scriptResult, templateResult] = await Promise.all([server.transformRequest(base + '?vue&type=script'), server.transformRequest(base + '?vue&type=template')]);
|
|
446
|
+
const scriptCode = scriptResult?.code || '';
|
|
447
|
+
const templateCode = templateResult?.code || '';
|
|
448
|
+
const scriptMeta = extractExportMetadata(scriptCode);
|
|
449
|
+
const hasRender = templateHasRender(templateCode);
|
|
450
|
+
if (hasRender && options.verbose) {
|
|
451
|
+
console.log('[sfc-meta] detected render for', base);
|
|
452
|
+
}
|
|
453
|
+
else if (!hasRender && options.verbose) {
|
|
454
|
+
console.warn('[sfc-meta] render NOT detected for', base);
|
|
455
|
+
}
|
|
456
|
+
const hash = createHash('md5').update(base).digest('hex').slice(0, 8);
|
|
457
|
+
const payload = {
|
|
458
|
+
path: base,
|
|
459
|
+
hasScript: !!scriptCode,
|
|
460
|
+
hasTemplate: !!templateCode,
|
|
461
|
+
hasStyle: false,
|
|
462
|
+
scriptExports: scriptMeta.named,
|
|
463
|
+
scriptHasDefault: scriptMeta.hasDefault,
|
|
464
|
+
templateHasRender: hasRender,
|
|
465
|
+
hmrId: hash,
|
|
466
|
+
};
|
|
467
|
+
res.statusCode = 200;
|
|
468
|
+
res.end(JSON.stringify(payload));
|
|
469
|
+
}
|
|
470
|
+
catch (error) {
|
|
471
|
+
res.statusCode = 500;
|
|
472
|
+
res.end(JSON.stringify({ error: error?.message || String(error) }));
|
|
473
|
+
}
|
|
474
|
+
});
|
|
475
|
+
server.middlewares.use(async (req, res, next) => {
|
|
476
|
+
try {
|
|
477
|
+
const urlObj = new URL(req.url || '', 'http://localhost');
|
|
478
|
+
if (!urlObj.pathname.startsWith('/ns/asm'))
|
|
479
|
+
return next();
|
|
480
|
+
setJavascriptResponseHeaders(res);
|
|
481
|
+
const asmBase = '/ns/asm';
|
|
482
|
+
const { verFromPath } = parseVersionedEndpointPath(asmBase, urlObj.pathname);
|
|
483
|
+
let spec = urlObj.searchParams.get('path') || '';
|
|
484
|
+
const diag = urlObj.searchParams.get('diag') === '1';
|
|
485
|
+
if (!spec) {
|
|
486
|
+
res.statusCode = 400;
|
|
487
|
+
res.end('export {}\n');
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
spec = normalizeRequestedSpec(spec, options.appVirtualWithSlash);
|
|
491
|
+
const base = spec.replace(/[?#].*$/, '');
|
|
492
|
+
if (diag) {
|
|
493
|
+
const code = `// [sfc-asm] ${base} (diag)\n` + `// vue shim for diag-only instantiation\n` + `var _createElementVNode = globalThis.createElementVNode || globalThis._createElementVNode;\n` + `const __ns_sfc__ = { name: ${JSON.stringify(base.split('/').pop() || 'DiagComp')}, render(){ return _createElementVNode ? _createElementVNode('StackLayout') : (globalThis.createElementVNode ? globalThis.createElementVNode('StackLayout') : {}); } };\nexport default __ns_sfc__;\n`;
|
|
494
|
+
res.statusCode = 200;
|
|
495
|
+
res.end(code);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
const projectRoot = server.config?.root || process.cwd();
|
|
499
|
+
const safeTransform = async (candidate) => {
|
|
500
|
+
try {
|
|
501
|
+
return await server.transformRequest(candidate);
|
|
502
|
+
}
|
|
503
|
+
catch {
|
|
504
|
+
return null;
|
|
505
|
+
}
|
|
506
|
+
};
|
|
507
|
+
const scriptResult = await safeTransform(base + '?vue&type=script');
|
|
508
|
+
const templateResult = await safeTransform(base + '?vue&type=template');
|
|
509
|
+
await safeTransform(base + '?vue');
|
|
510
|
+
const origin = options.getServerOrigin(server);
|
|
511
|
+
const version = String(verFromPath || options.getGraphVersion() || Date.now());
|
|
512
|
+
const scriptUrl = `${origin}/ns/sfc/${version}${base}?vue&type=script`;
|
|
513
|
+
const templateCode = templateResult?.code || '';
|
|
514
|
+
try {
|
|
515
|
+
const root = server.config?.root || process.cwd();
|
|
516
|
+
const absolutePath = path.join(root, base.replace(/^\//, ''));
|
|
517
|
+
let sfcSource = '';
|
|
518
|
+
try {
|
|
519
|
+
sfcSource = readFileSync(absolutePath, 'utf-8');
|
|
520
|
+
}
|
|
521
|
+
catch { }
|
|
522
|
+
if (sfcSource) {
|
|
523
|
+
const { descriptor } = parse(sfcSource, { filename: absolutePath });
|
|
524
|
+
const id = createHash('md5').update(absolutePath).digest('hex').slice(0, 8);
|
|
525
|
+
let compiledScript = '';
|
|
526
|
+
let bindingMetadata = undefined;
|
|
527
|
+
let usedInlineScript = false;
|
|
528
|
+
try {
|
|
529
|
+
const isNSNative = (tag) => NS_NATIVE_TAGS.has(tag);
|
|
530
|
+
const inlineScript = compileScript(descriptor, {
|
|
531
|
+
id,
|
|
532
|
+
inlineTemplate: true,
|
|
533
|
+
reactivityTransform: false,
|
|
534
|
+
templateOptions: {
|
|
535
|
+
compilerOptions: { isCustomElement: isNSNative },
|
|
536
|
+
},
|
|
537
|
+
});
|
|
538
|
+
if (/export\s+default/.test(inlineScript?.content || '')) {
|
|
539
|
+
compiledScript = inlineScript.content;
|
|
540
|
+
bindingMetadata = inlineScript?.bindings;
|
|
541
|
+
usedInlineScript = true;
|
|
542
|
+
}
|
|
543
|
+
else {
|
|
544
|
+
const fallbackScript = compileScript(descriptor, {
|
|
545
|
+
id,
|
|
546
|
+
inlineTemplate: false,
|
|
547
|
+
reactivityTransform: false,
|
|
548
|
+
});
|
|
549
|
+
compiledScript = fallbackScript?.content || '';
|
|
550
|
+
bindingMetadata = fallbackScript?.bindings;
|
|
551
|
+
}
|
|
552
|
+
}
|
|
553
|
+
catch (scriptError) {
|
|
554
|
+
if (options.verbose) {
|
|
555
|
+
console.warn('[sfc-asm][compileScript] failed', base, scriptError?.message);
|
|
556
|
+
}
|
|
557
|
+
try {
|
|
558
|
+
const fallbackScript = compileScript(descriptor, {
|
|
559
|
+
id,
|
|
560
|
+
inlineTemplate: false,
|
|
561
|
+
reactivityTransform: false,
|
|
562
|
+
});
|
|
563
|
+
compiledScript = fallbackScript?.content || '';
|
|
564
|
+
bindingMetadata = fallbackScript?.bindings;
|
|
565
|
+
}
|
|
566
|
+
catch (fallbackError) {
|
|
567
|
+
if (options.verbose) {
|
|
568
|
+
console.warn('[sfc-asm][compileScript][no-inline-fallback] failed', base, fallbackError?.message);
|
|
569
|
+
}
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
if (!compiledScript && scriptResult?.code) {
|
|
573
|
+
compiledScript = scriptResult.code;
|
|
574
|
+
}
|
|
575
|
+
if (usedInlineScript) {
|
|
576
|
+
try {
|
|
577
|
+
const noInlineScript = compileScript(descriptor, {
|
|
578
|
+
id,
|
|
579
|
+
inlineTemplate: false,
|
|
580
|
+
reactivityTransform: false,
|
|
581
|
+
});
|
|
582
|
+
compiledScript = noInlineScript?.content || compiledScript;
|
|
583
|
+
bindingMetadata = noInlineScript?.bindings || bindingMetadata;
|
|
584
|
+
}
|
|
585
|
+
catch (noInlineError) {
|
|
586
|
+
if (options.verbose) {
|
|
587
|
+
console.warn('[sfc-asm][compileScript][no-inline-fallback] failed', base, noInlineError?.message);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
}
|
|
591
|
+
let compiledTemplateCode = '';
|
|
592
|
+
try {
|
|
593
|
+
const templateSource = descriptor.template?.content || '';
|
|
594
|
+
if (templateSource) {
|
|
595
|
+
const compiledTemplate = compileTemplate({
|
|
596
|
+
source: templateSource,
|
|
597
|
+
id,
|
|
598
|
+
filename: absolutePath,
|
|
599
|
+
isProd: false,
|
|
600
|
+
ssr: false,
|
|
601
|
+
compilerOptions: {
|
|
602
|
+
bindingMetadata,
|
|
603
|
+
isCustomElement: (tag) => NS_NATIVE_TAGS.has(tag),
|
|
604
|
+
},
|
|
605
|
+
});
|
|
606
|
+
compiledTemplateCode = (compiledTemplate && (compiledTemplate.code || '')) || '';
|
|
607
|
+
if (compiledTemplate?.errors?.length && options.verbose) {
|
|
608
|
+
console.warn('[sfc-asm][compileTemplate][errors]', base, compiledTemplate.errors);
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
catch (templateError) {
|
|
613
|
+
if (options.verbose) {
|
|
614
|
+
console.warn('[sfc-asm][compileTemplate] failed', base, templateError?.message);
|
|
615
|
+
}
|
|
616
|
+
if (templateResult?.code)
|
|
617
|
+
compiledTemplateCode = templateResult.code;
|
|
618
|
+
}
|
|
619
|
+
if (!compiledTemplateCode) {
|
|
620
|
+
compiledTemplateCode = "export function render(){ const _ = (globalThis.createElementVNode||globalThis._createElementVNode); return _? _('StackLayout') : {}; }\n";
|
|
621
|
+
}
|
|
622
|
+
let scriptBody = compiledScript || '';
|
|
623
|
+
if (scriptBody) {
|
|
624
|
+
scriptBody = scriptBody.replace(/(^|\n)\s*import\s+([^;\n]+)\s+from\s+["'](?:vue|nativescript-vue|~\/vendor\.mjs)(?:\/[^\"]*)?["'];?/g, (_match, prefix, clause) => `${prefix}import ${clause} from "/ns/rt";`);
|
|
625
|
+
try {
|
|
626
|
+
const importerDir = path.posix.dirname(base);
|
|
627
|
+
scriptBody = scriptBody.replace(/(^|\n)\s*import\s+([^;\n]+)\s+from\s+["']([^"'\n]+\.vue)(?:\?[^"'\n]*)?["'];?/g, (_match, prefix, clause, importSpec) => {
|
|
628
|
+
let absoluteImport = importSpec;
|
|
629
|
+
if (importSpec.startsWith('./') || importSpec.startsWith('../')) {
|
|
630
|
+
absoluteImport = path.posix.normalize(path.posix.join(importerDir, importSpec));
|
|
631
|
+
if (!absoluteImport.startsWith('/'))
|
|
632
|
+
absoluteImport = '/' + absoluteImport;
|
|
633
|
+
}
|
|
634
|
+
else if (!importSpec.startsWith('/')) {
|
|
635
|
+
if (absoluteImport.startsWith('@/'))
|
|
636
|
+
absoluteImport = options.appVirtualWithSlash + absoluteImport.slice(2);
|
|
637
|
+
}
|
|
638
|
+
const asmUrl = `/ns/asm/${version}?path=${encodeURIComponent(absoluteImport)}&mode=inline`;
|
|
639
|
+
return `${prefix}import ${clause} from ${JSON.stringify(asmUrl)};`;
|
|
640
|
+
});
|
|
641
|
+
}
|
|
642
|
+
catch { }
|
|
643
|
+
}
|
|
644
|
+
let helperBindings = '';
|
|
645
|
+
let renderDecl = '';
|
|
646
|
+
let inlineBlock;
|
|
647
|
+
let renderOk = false;
|
|
648
|
+
if (compiledTemplateCode) {
|
|
649
|
+
try {
|
|
650
|
+
inlineBlock = buildInlineTemplateBlock(compiledTemplateCode) || undefined;
|
|
651
|
+
if (!inlineBlock) {
|
|
652
|
+
const extracted = extractTemplateRender(compiledTemplateCode);
|
|
653
|
+
helperBindings = extracted.helperBindings;
|
|
654
|
+
renderDecl = extracted.renderDecl;
|
|
655
|
+
inlineBlock = extracted.inlineBlock;
|
|
656
|
+
renderOk = extracted.ok;
|
|
657
|
+
}
|
|
658
|
+
else {
|
|
659
|
+
renderOk = true;
|
|
660
|
+
}
|
|
661
|
+
}
|
|
662
|
+
catch (extractError) {
|
|
663
|
+
if (options.verbose) {
|
|
664
|
+
console.warn('[sfc-asm][extractTemplateRender] failed', base, extractError?.message);
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
if (!renderOk && !inlineBlock) {
|
|
669
|
+
try {
|
|
670
|
+
const templateUrl = `${origin}/ns/sfc/${version}${base}?vue&type=template`;
|
|
671
|
+
const importLine = `import * as __template from ${JSON.stringify(templateUrl)};`;
|
|
672
|
+
helperBindings += `\n${importLine}`;
|
|
673
|
+
renderDecl += `\nfunction __ns_getRender(){\n try {\n if (__template && __template.render) return __template.render;\n } catch (_e) {}\n try {\n const _ = globalThis.createElementVNode || globalThis._createElementVNode;\n return _ ? function(){ return _('StackLayout'); } : function(){ return {}; };\n } catch (_e) { return function(){ return {}; }; }\n}\n`;
|
|
674
|
+
renderOk = true;
|
|
675
|
+
}
|
|
676
|
+
catch { }
|
|
677
|
+
}
|
|
678
|
+
let scriptTransformed = scriptBody;
|
|
679
|
+
if (scriptTransformed) {
|
|
680
|
+
scriptTransformed = scriptTransformed.replace(/(^|\n)\s*export\s+default\s+/g, '$1const __ns_sfc__ = ').replace(/(^|\n)\s*export\s*\{[^}]*\}\s*;?\s*/g, '\n/* removed named exports for inline asm */\n');
|
|
681
|
+
scriptTransformed = scriptTransformed.replace(/(^|[\n;])\s*(?:const|let|var)\s+__ns_sfc__\s*=\s*/g, '$1__ns_sfc__ = ');
|
|
682
|
+
if (!/(^|[\n;])\s*(?:const|let|var)\s+__ns_sfc__\b/.test(scriptTransformed)) {
|
|
683
|
+
scriptTransformed = 'let __ns_sfc__;\n' + scriptTransformed;
|
|
684
|
+
}
|
|
685
|
+
scriptTransformed = scriptTransformed.replace(/^\s*\}+(?=\s*[^}])/, (value) => `/* [asm-fix] removed ${value.length} stray leading braces */\n`);
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
try {
|
|
689
|
+
const componentName = (base.split('/').pop() || 'Component').replace(/\.vue$/i, '') || 'Component';
|
|
690
|
+
scriptTransformed = `import { defineComponent as _defineComponent } from "/ns/rt";\nlet __ns_sfc__;\n__ns_sfc__ = /*@__PURE__*/_defineComponent({ __name: ${JSON.stringify(componentName)} });`;
|
|
691
|
+
}
|
|
692
|
+
catch {
|
|
693
|
+
scriptTransformed = 'import { defineComponent as _defineComponent } from "/ns/rt";\nlet __ns_sfc__;\n__ns_sfc__ = /*@__PURE__*/_defineComponent({});';
|
|
694
|
+
}
|
|
695
|
+
}
|
|
696
|
+
const parts = [];
|
|
697
|
+
parts.push(`// [sfc-asm] ${base} (inline-compiled)`);
|
|
698
|
+
if (helperBindings)
|
|
699
|
+
parts.push(helperBindings);
|
|
700
|
+
parts.push(scriptTransformed);
|
|
701
|
+
parts.push(renderDecl);
|
|
702
|
+
parts.push(`try { if (!__ns_sfc__.render) Object.defineProperty(__ns_sfc__, 'render', { configurable: true, enumerable: true, get(){ const r = (typeof __ns_getRender==='function' ? __ns_getRender() : undefined); Object.defineProperty(__ns_sfc__, 'render', { value: r, writable: true, configurable: true, enumerable: true }); return r; }, set(v){ Object.defineProperty(__ns_sfc__, 'render', { value: v, writable: true, configurable: true, enumerable: true }); } }); } catch(_e){}`);
|
|
703
|
+
parts.push("export function render(){ const f = (typeof __ns_getRender==='function' ? __ns_getRender() : (__ns_sfc__ && __ns_sfc__.render)); return typeof f==='function' ? f.apply(this, arguments) : undefined; }");
|
|
704
|
+
parts.push('export default __ns_sfc__');
|
|
705
|
+
let inlineCode = parts.filter(Boolean).join('\n');
|
|
706
|
+
inlineCode = options.processCodeForDevice(inlineCode, false, true);
|
|
707
|
+
try {
|
|
708
|
+
inlineCode = options.ensureVersionedCoreImports(inlineCode, options.getServerOrigin(server), Number(version));
|
|
709
|
+
}
|
|
710
|
+
catch { }
|
|
711
|
+
try {
|
|
712
|
+
inlineCode = ensureDestructureCoreImports(inlineCode);
|
|
713
|
+
}
|
|
714
|
+
catch { }
|
|
715
|
+
try {
|
|
716
|
+
const tsResult = await babelCore.transformAsync(scriptTransformed, {
|
|
717
|
+
plugins: [[pluginTransformTypescript, { allowDeclareFields: true }]],
|
|
718
|
+
ast: false,
|
|
719
|
+
sourceType: 'module',
|
|
720
|
+
configFile: false,
|
|
721
|
+
babelrc: false,
|
|
722
|
+
});
|
|
723
|
+
if (tsResult?.code)
|
|
724
|
+
scriptTransformed = tsResult.code;
|
|
725
|
+
}
|
|
726
|
+
catch (tsError) {
|
|
727
|
+
if (options.verbose) {
|
|
728
|
+
console.warn('[sfc-asm][babel-ts][fail]', base, tsError?.message);
|
|
729
|
+
}
|
|
730
|
+
}
|
|
731
|
+
let importLines = [];
|
|
732
|
+
try {
|
|
733
|
+
const astResult = astExtractImportsAndStripTypes(scriptTransformed);
|
|
734
|
+
importLines = astResult.imports;
|
|
735
|
+
scriptTransformed = astResult.body;
|
|
736
|
+
if (astResult.diagnostics.length && options.verbose) {
|
|
737
|
+
console.warn('[sfc-asm][ast]', base, astResult.diagnostics.join('; '));
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
catch (astError) {
|
|
741
|
+
if (options.verbose) {
|
|
742
|
+
console.warn('[sfc-asm][ast][fail]', base, astError?.message);
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
if (renderDecl && /(^|\n)\s*(?:export\s+)?function\s+__ns_render\s*\(/.test(renderDecl) && !/\}\s*$/.test(renderDecl)) {
|
|
746
|
+
renderDecl = renderDecl.trimEnd() + '\n}';
|
|
747
|
+
}
|
|
748
|
+
const outParts = [];
|
|
749
|
+
outParts.push(`// [sfc-asm] ${base} (inline-compiled)`);
|
|
750
|
+
outParts.push('// [sfc-asm][canonical]');
|
|
751
|
+
if (importLines.length)
|
|
752
|
+
outParts.push(Array.from(new Set(importLines)).join('\n'));
|
|
753
|
+
outParts.push(scriptTransformed);
|
|
754
|
+
if (inlineBlock) {
|
|
755
|
+
outParts.push(inlineBlock);
|
|
756
|
+
}
|
|
757
|
+
else {
|
|
758
|
+
if (helperBindings)
|
|
759
|
+
outParts.push(helperBindings);
|
|
760
|
+
if (renderDecl && renderDecl.trim())
|
|
761
|
+
outParts.push(renderDecl);
|
|
762
|
+
}
|
|
763
|
+
outParts.push(`try { if (!__ns_sfc__.render) Object.defineProperty(__ns_sfc__, 'render', { configurable: true, enumerable: true, get(){ const r = (typeof __ns_getRender==='function' ? __ns_getRender() : (typeof __ns_render==='function' ? __ns_render : undefined)); Object.defineProperty(__ns_sfc__, 'render', { value: r, writable: true, configurable: true, enumerable: true }); return r; }, set(v){ Object.defineProperty(__ns_sfc__, 'render', { value: v, writable: true, configurable: true, enumerable: true }); } }); } catch(_e){}`);
|
|
764
|
+
outParts.push('export function render(){ const f = (typeof __ns_getRender==="function" ? __ns_getRender() : (typeof __ns_render==="function" ? __ns_render : (__ns_sfc__ && __ns_sfc__.render))); return typeof f === "function" ? f.apply(this, arguments) : undefined; }');
|
|
765
|
+
outParts.push('export default __ns_sfc__');
|
|
766
|
+
let inlineCode2 = outParts.filter(Boolean).join('\n');
|
|
767
|
+
inlineCode2 = options.processCodeForDevice(inlineCode2, false, true);
|
|
768
|
+
try {
|
|
769
|
+
inlineCode2 = options.ensureVersionedCoreImports(inlineCode2, options.getServerOrigin(server), Number(version));
|
|
770
|
+
}
|
|
771
|
+
catch { }
|
|
772
|
+
try {
|
|
773
|
+
inlineCode2 = ensureDestructureCoreImports(inlineCode2);
|
|
774
|
+
}
|
|
775
|
+
catch { }
|
|
776
|
+
try {
|
|
777
|
+
const lateImportPattern = /^(?!\/\/).*^\s*import\s+[^;]+;?$/gm;
|
|
778
|
+
const allImports = [];
|
|
779
|
+
inlineCode2 = inlineCode2.replace(lateImportPattern, (value) => {
|
|
780
|
+
allImports.push(value);
|
|
781
|
+
return '';
|
|
782
|
+
});
|
|
783
|
+
if (allImports.length) {
|
|
784
|
+
inlineCode2 = inlineCode2.replace(/(\/\/ \[sfc-asm\]\[canonical\]\n)/, `$1${Array.from(new Set(allImports)).join('\n')}\n/* [asm-fix] re-hoisted ${allImports.length} imports */\n`);
|
|
785
|
+
}
|
|
786
|
+
}
|
|
787
|
+
catch { }
|
|
788
|
+
try {
|
|
789
|
+
inlineCode2 = astNormalizeModuleImportsAndHelpers(inlineCode2);
|
|
790
|
+
}
|
|
791
|
+
catch { }
|
|
792
|
+
try {
|
|
793
|
+
inlineCode2 = astVerifyAndAnnotateDuplicates(inlineCode2);
|
|
794
|
+
if (/^\s*\/\/ \[ast-verify\]\[duplicate-bindings\]/m.test(inlineCode2)) {
|
|
795
|
+
const diagnosticLine = (inlineCode2.match(/^\s*\/\/ \[ast-verify\]\[duplicate-bindings\][^\n]*/m) || [])[0] || '// [ast-verify][duplicate-bindings]';
|
|
796
|
+
const brief = diagnosticLine.replace(/^[^:]*:?\s?/, '');
|
|
797
|
+
const escaped = brief.replace(/["\\]/g, '\\$&');
|
|
798
|
+
const thrower = `throw new Error("[nsv-hmr] Duplicate top-level bindings detected post-hoist: ${escaped}");`;
|
|
799
|
+
inlineCode2 = `${thrower}\n` + inlineCode2;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
catch { }
|
|
803
|
+
try {
|
|
804
|
+
inlineCode2 = inlineCode2.replace(/\bdefault\b\s*(?=\}|,|\n)/g, 'default: undefined');
|
|
805
|
+
inlineCode2 = inlineCode2.replace(/<unknown>/g, '');
|
|
806
|
+
inlineCode2 = inlineCode2.replace(/}\s*=>\s*\{/g, '');
|
|
807
|
+
}
|
|
808
|
+
catch { }
|
|
809
|
+
try {
|
|
810
|
+
inlineCode2 = options.rewriteImports(inlineCode2, base, options.sfcFileMap, options.depFileMap, projectRoot, !!options.verbose, undefined, options.getServerOrigin(server));
|
|
811
|
+
}
|
|
812
|
+
catch { }
|
|
813
|
+
try {
|
|
814
|
+
const finalTs = await babelCore.transformAsync(inlineCode2, {
|
|
815
|
+
plugins: [[pluginTransformTypescript, { allowDeclareFields: true }]],
|
|
816
|
+
ast: false,
|
|
817
|
+
sourceType: 'module',
|
|
818
|
+
configFile: false,
|
|
819
|
+
babelrc: false,
|
|
820
|
+
});
|
|
821
|
+
if (finalTs?.code)
|
|
822
|
+
inlineCode2 = finalTs.code;
|
|
823
|
+
}
|
|
824
|
+
catch { }
|
|
825
|
+
try {
|
|
826
|
+
const missingElse = /"onUpdate:modelValue"\s*:\s*_cache\[(\d+)\]\s*\|\|\s*\(_cache\[\1\]\s*=\s*\$event\s*=>\s*_isRef\(\s*([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\s*\)\s*\?\s*\2\.value\s*=\s*\$event\s*\)/g;
|
|
827
|
+
inlineCode2 = inlineCode2.replace(missingElse, (_match, idx, expr) => `"onUpdate:modelValue": _cache[${idx}] || (_cache[${idx}] = $event => (_isRef(${expr}) ? (${expr}.value = $event) : (${expr} = $event)))`);
|
|
828
|
+
const malformed = /"onUpdate:modelValue"\s*:\s*_cache\[(\d+)\]\s*\|\|\s*\(_cache\[\1\]\s*=\s*[^=]*\(\s*([A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*)\s*\)[^=]*=\s*\$event\s*\)\s*\)/g;
|
|
829
|
+
inlineCode2 = inlineCode2.replace(malformed, (_match, idx, expr) => `"onUpdate:modelValue": _cache[${idx}] || (_cache[${idx}] = $event => (_isRef(${expr}) ? (${expr}.value = $event) : (${expr} = $event)))`);
|
|
830
|
+
}
|
|
831
|
+
catch { }
|
|
832
|
+
try {
|
|
833
|
+
const firstImportIndex = inlineCode2.search(/^[\t ]*import\b/m);
|
|
834
|
+
if (firstImportIndex > 0) {
|
|
835
|
+
const prefix = inlineCode2.slice(0, firstImportIndex);
|
|
836
|
+
let open = 0;
|
|
837
|
+
let close = 0;
|
|
838
|
+
let inSingle = false;
|
|
839
|
+
let inDouble = false;
|
|
840
|
+
let inTemplate = false;
|
|
841
|
+
let inLineComment = false;
|
|
842
|
+
let inBlockComment = false;
|
|
843
|
+
for (let index = 0; index < prefix.length; index++) {
|
|
844
|
+
const ch = prefix[index];
|
|
845
|
+
const nextChar = prefix[index + 1];
|
|
846
|
+
if (inLineComment) {
|
|
847
|
+
if (ch === '\n')
|
|
848
|
+
inLineComment = false;
|
|
849
|
+
continue;
|
|
850
|
+
}
|
|
851
|
+
if (inBlockComment) {
|
|
852
|
+
if (ch === '*' && nextChar === '/') {
|
|
853
|
+
inBlockComment = false;
|
|
854
|
+
index++;
|
|
855
|
+
}
|
|
856
|
+
continue;
|
|
857
|
+
}
|
|
858
|
+
if (inSingle) {
|
|
859
|
+
if (ch === '\\') {
|
|
860
|
+
index++;
|
|
861
|
+
continue;
|
|
862
|
+
}
|
|
863
|
+
if (ch === "'")
|
|
864
|
+
inSingle = false;
|
|
865
|
+
continue;
|
|
866
|
+
}
|
|
867
|
+
if (inDouble) {
|
|
868
|
+
if (ch === '\\') {
|
|
869
|
+
index++;
|
|
870
|
+
continue;
|
|
871
|
+
}
|
|
872
|
+
if (ch === '"')
|
|
873
|
+
inDouble = false;
|
|
874
|
+
continue;
|
|
875
|
+
}
|
|
876
|
+
if (inTemplate) {
|
|
877
|
+
if (ch === '\\') {
|
|
878
|
+
index++;
|
|
879
|
+
continue;
|
|
880
|
+
}
|
|
881
|
+
if (ch === '`')
|
|
882
|
+
inTemplate = false;
|
|
883
|
+
continue;
|
|
884
|
+
}
|
|
885
|
+
if (ch === '/' && nextChar === '/') {
|
|
886
|
+
inLineComment = true;
|
|
887
|
+
index++;
|
|
888
|
+
continue;
|
|
889
|
+
}
|
|
890
|
+
if (ch === '/' && nextChar === '*') {
|
|
891
|
+
inBlockComment = true;
|
|
892
|
+
index++;
|
|
893
|
+
continue;
|
|
894
|
+
}
|
|
895
|
+
if (ch === "'") {
|
|
896
|
+
inSingle = true;
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
if (ch === '"') {
|
|
900
|
+
inDouble = true;
|
|
901
|
+
continue;
|
|
902
|
+
}
|
|
903
|
+
if (ch === '`') {
|
|
904
|
+
inTemplate = true;
|
|
905
|
+
continue;
|
|
906
|
+
}
|
|
907
|
+
if (ch === '{')
|
|
908
|
+
open++;
|
|
909
|
+
else if (ch === '}')
|
|
910
|
+
close++;
|
|
911
|
+
}
|
|
912
|
+
const missing = open - close;
|
|
913
|
+
if (missing > 0) {
|
|
914
|
+
inlineCode2 = inlineCode2.slice(0, firstImportIndex) + '}'.repeat(missing) + '\n' + inlineCode2.slice(firstImportIndex);
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
}
|
|
918
|
+
catch { }
|
|
919
|
+
try {
|
|
920
|
+
const finalTs = await babelCore.transformAsync(inlineCode2, {
|
|
921
|
+
plugins: [[pluginTransformTypescript, { allowDeclareFields: true }]],
|
|
922
|
+
ast: false,
|
|
923
|
+
sourceType: 'module',
|
|
924
|
+
configFile: false,
|
|
925
|
+
babelrc: false,
|
|
926
|
+
});
|
|
927
|
+
if (finalTs?.code)
|
|
928
|
+
inlineCode2 = finalTs.code;
|
|
929
|
+
}
|
|
930
|
+
catch { }
|
|
931
|
+
inlineCode2 = ensureVariableDynamicImportHelper(inlineCode2);
|
|
932
|
+
inlineCode2 = ensureGuardPlainDynamicImports(inlineCode2, origin);
|
|
933
|
+
inlineCode2 = options.requireGuardSnippet + inlineCode2;
|
|
934
|
+
try {
|
|
935
|
+
const lacksRender = !/__ns_render\b/.test(inlineCode2) && !/__ns_sfc__\.render\s*=/.test(inlineCode2);
|
|
936
|
+
if (lacksRender) {
|
|
937
|
+
const err = `throw new Error("[sfc-asm] ${base}: no render generated by assembler");\nexport default {};`;
|
|
938
|
+
res.statusCode = 200;
|
|
939
|
+
res.end(err);
|
|
940
|
+
return;
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
catch { }
|
|
944
|
+
try {
|
|
945
|
+
inlineCode2 = inlineCode2.replace(/(\/\/ \[sfc-asm\]\[canonical\])(?!\n)/, '$1\n');
|
|
946
|
+
}
|
|
947
|
+
catch { }
|
|
948
|
+
try {
|
|
949
|
+
const origin = options.getServerOrigin(server);
|
|
950
|
+
inlineCode2 = ensureVersionedRtImports(inlineCode2, origin, Number(version));
|
|
951
|
+
inlineCode2 = options.getStrategy().ensureVersionedImports(inlineCode2, origin, Number(version));
|
|
952
|
+
inlineCode2 = options.ensureVersionedCoreImports(inlineCode2, origin, Number(version));
|
|
953
|
+
}
|
|
954
|
+
catch { }
|
|
955
|
+
try {
|
|
956
|
+
inlineCode2 = astNormalizeModuleImportsAndHelpers(inlineCode2);
|
|
957
|
+
}
|
|
958
|
+
catch { }
|
|
959
|
+
try {
|
|
960
|
+
const hasDecl = /(^|[\n;])\s*(?:const|let|var)\s+__ns_sfc__\b/.test(inlineCode2);
|
|
961
|
+
if (!hasDecl) {
|
|
962
|
+
inlineCode2 = inlineCode2.replace(/(\/\/ \[sfc-asm\]\[canonical\]\n)/, '$1let __ns_sfc__ = {};\n');
|
|
963
|
+
}
|
|
964
|
+
inlineCode2 = inlineCode2.replace(/(^|[\n;])\s*let\s+__ns_sfc__\s*;?/g, '$1let __ns_sfc__ = {};');
|
|
965
|
+
inlineCode2 = inlineCode2.replace(/(^|[\n;])\s*var\s+__ns_sfc__\s*;?/g, '$1var __ns_sfc__ = {};');
|
|
966
|
+
}
|
|
967
|
+
catch { }
|
|
968
|
+
if (!/export\s+default\s+__ns_sfc__/.test(inlineCode2) && /__ns_sfc__/.test(inlineCode2))
|
|
969
|
+
inlineCode2 += '\nexport default __ns_sfc__';
|
|
970
|
+
res.statusCode = 200;
|
|
971
|
+
res.end(inlineCode2);
|
|
972
|
+
return;
|
|
973
|
+
}
|
|
974
|
+
}
|
|
975
|
+
catch { }
|
|
976
|
+
let inlineOk = false;
|
|
977
|
+
let helperBindings = '';
|
|
978
|
+
let renderDecl = '';
|
|
979
|
+
let inlineBlock;
|
|
980
|
+
try {
|
|
981
|
+
const root = server.config?.root || process.cwd();
|
|
982
|
+
const absolutePath = path.join(root, base.replace(/^\//, ''));
|
|
983
|
+
let sfcSource = '';
|
|
984
|
+
try {
|
|
985
|
+
sfcSource = readFileSync(absolutePath, 'utf-8');
|
|
986
|
+
}
|
|
987
|
+
catch { }
|
|
988
|
+
if (sfcSource) {
|
|
989
|
+
const { descriptor } = parse(sfcSource, { filename: absolutePath });
|
|
990
|
+
const templateSource = descriptor.template?.content || '';
|
|
991
|
+
if (templateSource) {
|
|
992
|
+
const id = createHash('md5').update(absolutePath).digest('hex').slice(0, 8);
|
|
993
|
+
const compiledTemplate = compileTemplate({
|
|
994
|
+
source: templateSource,
|
|
995
|
+
id,
|
|
996
|
+
filename: absolutePath,
|
|
997
|
+
isProd: false,
|
|
998
|
+
ssr: false,
|
|
999
|
+
compilerOptions: {
|
|
1000
|
+
isCustomElement: (tag) => NS_NATIVE_TAGS.has(tag),
|
|
1001
|
+
},
|
|
1002
|
+
});
|
|
1003
|
+
const compiled = (compiledTemplate?.code || '');
|
|
1004
|
+
if (compiled) {
|
|
1005
|
+
inlineBlock = buildInlineTemplateBlock(compiled) || undefined;
|
|
1006
|
+
if (inlineBlock) {
|
|
1007
|
+
inlineOk = true;
|
|
1008
|
+
}
|
|
1009
|
+
else {
|
|
1010
|
+
const extracted = extractTemplateRender(compiled);
|
|
1011
|
+
inlineOk = extracted.ok;
|
|
1012
|
+
helperBindings = extracted.helperBindings;
|
|
1013
|
+
renderDecl = extracted.renderDecl;
|
|
1014
|
+
inlineBlock = extracted.inlineBlock;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
catch { }
|
|
1021
|
+
if (!inlineOk) {
|
|
1022
|
+
const extracted = extractTemplateRender(templateCode);
|
|
1023
|
+
inlineOk = extracted.ok;
|
|
1024
|
+
helperBindings = extracted.helperBindings;
|
|
1025
|
+
renderDecl = extracted.renderDecl;
|
|
1026
|
+
inlineBlock = extracted.inlineBlock;
|
|
1027
|
+
}
|
|
1028
|
+
if (!inlineOk) {
|
|
1029
|
+
res.statusCode = 500;
|
|
1030
|
+
res.end(`throw new Error('[sfc-asm] ${base}: template extraction failed');\nexport default {};`);
|
|
1031
|
+
return;
|
|
1032
|
+
}
|
|
1033
|
+
let asm;
|
|
1034
|
+
if (inlineBlock && inlineBlock.trim()) {
|
|
1035
|
+
asm = [`// [sfc-asm] ${base} (inlined template body)`, `export * from ${JSON.stringify(scriptUrl)};`, `import * as __script from ${JSON.stringify(scriptUrl)};`, inlineBlock, `const __ns_sfc__ = (__script && __script.default) ? __script.default : {};`, `try { if (typeof __ns_render === 'function' && !__ns_sfc__.render) __ns_sfc__.render = __ns_render; } catch {}`, 'export default __ns_sfc__;'].join('\n');
|
|
1036
|
+
}
|
|
1037
|
+
else {
|
|
1038
|
+
asm = [`// [sfc-asm] ${base} (inlined template)`, `export * from ${JSON.stringify(scriptUrl)};`, `import * as __script from ${JSON.stringify(scriptUrl)};`, helperBindings, renderDecl, `const __ns_sfc__ = (__script && __script.default) ? __script.default : {};`, `try { if (typeof __ns_render === 'function' && !__ns_sfc__.render) __ns_sfc__.render = __ns_render; } catch {}`, 'export default __ns_sfc__;'].filter(Boolean).join('\n');
|
|
1039
|
+
}
|
|
1040
|
+
let code = options.requireGuardSnippet + asm;
|
|
1041
|
+
code = options.processCodeForDevice(code, false, true, /(?:^|\/)node_modules\//.test(base), base);
|
|
1042
|
+
try {
|
|
1043
|
+
code = options.ensureVersionedCoreImports(code, options.getServerOrigin(server), Number(version));
|
|
1044
|
+
}
|
|
1045
|
+
catch { }
|
|
1046
|
+
code = options.rewriteImports(code, base, options.sfcFileMap, options.depFileMap, projectRoot, !!options.verbose, undefined, options.getServerOrigin(server));
|
|
1047
|
+
try {
|
|
1048
|
+
code = ensureDestructureCoreImports(code);
|
|
1049
|
+
}
|
|
1050
|
+
catch { }
|
|
1051
|
+
code = ensureVariableDynamicImportHelper(code);
|
|
1052
|
+
code = ensureGuardPlainDynamicImports(code, origin);
|
|
1053
|
+
try {
|
|
1054
|
+
const origin = options.getServerOrigin(server);
|
|
1055
|
+
code = ensureVersionedRtImports(code, origin, Number(version));
|
|
1056
|
+
code = options.getStrategy().ensureVersionedImports(code, origin, Number(version));
|
|
1057
|
+
code = options.ensureVersionedCoreImports(code, origin, Number(version));
|
|
1058
|
+
}
|
|
1059
|
+
catch { }
|
|
1060
|
+
res.statusCode = 200;
|
|
1061
|
+
res.end(code);
|
|
1062
|
+
}
|
|
1063
|
+
catch {
|
|
1064
|
+
res.statusCode = 500;
|
|
1065
|
+
res.end('export {}\n');
|
|
1066
|
+
}
|
|
1067
|
+
});
|
|
1068
|
+
}
|
|
1069
|
+
//# sourceMappingURL=websocket-vue-sfc.js.map
|