@nstudio/nativescript-compiler 9.2.0-alpha.10 → 9.2.0-alpha.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/ir.js +7 -1
- package/dist/cli/runtime.cjs +7 -1
- package/dist/css/css.js +30 -6
- package/dist/driver/cli.js +2 -0
- package/dist/driver/compile.js +23 -10
- package/dist/driver/paths.js +7 -0
- package/dist/driver/unimplemented.js +15 -0
- package/dist/kotlin/access.js +23 -5
- package/dist/kotlin/arguments.js +5 -0
- package/dist/kotlin/builtins.js +0 -4
- package/dist/kotlin/calls.js +6 -2
- package/dist/kotlin/class-methods.js +9 -3
- package/dist/kotlin/components.js +13 -8
- package/dist/kotlin/conversion.js +17 -5
- package/dist/kotlin/core-api.js +2 -13
- package/dist/kotlin/declarations.js +2 -0
- package/dist/kotlin/expression-statements.js +5 -4
- package/dist/kotlin/expressions.js +8 -3
- package/dist/kotlin/functions.js +13 -4
- package/dist/kotlin/global-calls.js +11 -0
- package/dist/kotlin/literals.js +58 -14
- package/dist/kotlin/names.js +2 -2
- package/dist/kotlin/native/api.js +9 -1
- package/dist/kotlin/native/calls.js +84 -3
- package/dist/kotlin/native/subclasses.js +4 -3
- package/dist/kotlin/native/types.js +8 -5
- package/dist/kotlin/operators.js +4 -1
- package/dist/kotlin/overrides.js +7 -5
- package/dist/kotlin/placement.js +3 -1
- package/dist/kotlin/prototype-calls.js +5 -0
- package/dist/kotlin/render.js +62 -0
- package/dist/kotlin/statements.js +9 -0
- package/dist/kotlin/static-calls.js +10 -7
- package/dist/kotlin/translator.js +31 -11
- package/dist/kotlin/type-facts.js +10 -2
- package/dist/kotlin/types.js +6 -6
- package/dist/plugins/native-android.js +37 -5
- package/dist/plugins/native-ios.js +2 -0
- package/dist/plugins/source.js +73 -23
- package/dist/program/app-modules.js +91 -16
- package/dist/program/app-paths.js +80 -12
- package/dist/program/class-mixins.js +463 -0
- package/dist/program/core-facts.js +7 -0
- package/dist/program/glob.js +274 -0
- package/dist/program/platform.js +32 -4
- package/dist/program/program.js +239 -38
- package/dist/program/source-lines.js +4 -1
- package/dist/program/vite-plugins.js +144 -0
- package/dist/semantics/apis/globals.js +2 -2
- package/dist/semantics/apis/object.js +1 -0
- package/dist/semantics/apis/statics.js +6 -0
- package/dist/semantics/ast.js +20 -2
- package/dist/semantics/classes.js +36 -1
- package/dist/semantics/facts.js +28 -1
- package/dist/semantics/globals.js +3 -0
- package/dist/semantics/lang.js +13 -1
- package/dist/semantics/nullish.js +29 -11
- package/dist/semantics/refuse-receivers.js +3 -3
- package/dist/semantics/refuse-scope.js +2 -7
- package/dist/semantics/runtime.js +1 -0
- package/dist/semantics/shapes.js +21 -1
- package/dist/semantics/states/value-states.js +1 -1
- package/dist/semantics/throws.js +1 -0
- package/dist/semantics/translator-methods.js +1 -1
- package/dist/semantics/types.js +21 -1
- package/dist/swift/access.js +25 -10
- package/dist/swift/arguments.js +10 -2
- package/dist/swift/assignment.js +12 -1
- package/dist/swift/builtins.js +0 -4
- package/dist/swift/calls.js +6 -2
- package/dist/swift/class-methods.js +3 -3
- package/dist/swift/components.js +28 -8
- package/dist/swift/construction.js +4 -0
- package/dist/swift/conversion.js +1 -1
- package/dist/swift/core-api.js +9 -12
- package/dist/swift/from-any.js +2 -2
- package/dist/swift/functions.js +13 -2
- package/dist/swift/generics.js +5 -1
- package/dist/swift/global-calls.js +2 -0
- package/dist/swift/literals.js +57 -10
- package/dist/swift/modules.js +1 -1
- package/dist/swift/names.js +1 -1
- package/dist/swift/native/sdk-tables.js +52 -4
- package/dist/swift/native/sdk.js +9 -1
- package/dist/swift/operators.js +4 -1
- package/dist/swift/parameters.js +5 -2
- package/dist/swift/placement.js +44 -4
- package/dist/swift/prototype-calls.js +17 -2
- package/dist/swift/render.js +64 -0
- package/dist/swift/results.js +1 -1
- package/dist/swift/statements.js +9 -0
- package/dist/swift/static-calls.js +13 -6
- package/dist/swift/types.js +9 -9
- package/dist/targets/android/build.js +18 -12
- package/dist/targets/ios/build.js +11 -10
- package/dist/targets/ios/dependencies.js +12 -0
- package/dist/targets/resources.js +15 -1
- package/dist/template/plan.js +105 -8
- package/dist/types/adapter-api.d.ts +13 -0
- package/dist/types/ir.d.ts +47 -0
- package/kit-android/generated/kotlin/org/nativescript/kit/__Objects.kt +102 -7485
- package/kit-android/generated/kotlin/org/nativescript/kit/application_application.kt +4 -4
- package/kit-android/generated/kotlin/org/nativescript/kit/image_source_index.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_animation_animation_common.kt +7 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_animation_index.kt +3 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_base_index.kt +2 -4
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_index.kt +5 -6
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_core_view_view_common.kt +1 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_dialogs_index.kt +11 -6
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_fragment_transitions.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_frame_common.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_frame_helper_for_android.kt +7 -5
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_frame_index.kt +6 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_gestures_touch_manager.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_layouts_flexbox_layout_index.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_search_bar_index.kt +4 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_segmented_bar_index.kt +2 -2
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_text_base_formatted_string.kt +1 -3
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_text_base_index.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_transition_fade_transition.kt +1 -1
- package/kit-android/generated/kotlin/org/nativescript/kit/ui_transition_flip_transition.kt +8 -8
- package/kit-android/generated/kotlin/org/nativescript/kit/utils_index.kt +1 -1
- package/kit-android/generated/source-lines.json +1 -1
- package/kit-android/gradlew.bat +94 -0
- package/kit-android/src/fromcore/kotlin/org/nativescript/kit/CoreBridge.kt +67 -1
- package/kit-android/src/main/kotlin/org/nativescript/kit/Signals.kt +15 -2
- package/kit-android/src/main/kotlin/org/nativescript/kit/runtime/Prototypes.kt +26 -0
- package/kit-apple/Overlays/xml-free/Core/ui.core.bindable.index.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity1.swift +26 -161
- package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity2.swift +9 -284
- package/kit-apple/Sources/NativeScriptKit/Core/FunctionCells.Arity3Plus.swift +11 -7
- package/kit-apple/Sources/NativeScriptKit/Core/Object_callback_thisArg_once.swift +1 -1
- package/kit-apple/Sources/NativeScriptKit/Core/Object_eventName_object_index_section_view_ios_android.swift +7 -5
- package/kit-apple/Sources/NativeScriptKit/Core/Object_eventName_object_oldIndex_newIndex.swift +4 -2
- package/kit-apple/Sources/NativeScriptKit/Core/{Object_iterator_prototype_for_keyFor_hasInstance_isConcatSpreadable_and_19_more_91b5f3.swift → Object_iterator_prototype_for_keyFor_hasInstance_isConcatSpreadable_and_19_more_fa18e1.swift} +28 -103
- package/kit-apple/Sources/NativeScriptKit/Core/__Namespace.Utils.swift +17 -15
- package/kit-apple/Sources/NativeScriptKit/Core/application-settings.index.ios.swift +8 -8
- package/kit-apple/Sources/NativeScriptKit/Core/application.application-common.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/color.color-common.swift +7 -7
- package/kit-apple/Sources/NativeScriptKit/Core/data.observable.index.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/http.http-request-internal.index.ios.swift +2 -1
- package/kit-apple/Sources/NativeScriptKit/Core/image-source.index.ios.swift +1 -1
- package/kit-apple/Sources/NativeScriptKit/Core/kit-index.json +40 -246
- package/kit-apple/Sources/NativeScriptKit/Core/manifest.json +1 -2
- package/kit-apple/Sources/NativeScriptKit/Core/ui.action-bar.index.ios.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.button.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.content-view.index.swift +8 -8
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.bindable.index.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.index.ios.swift +8 -8
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.view-common.swift +1 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.view.view-helper.view-helper-common.swift +7 -7
- package/kit-apple/Sources/NativeScriptKit/Core/ui.core.weak-event-listener.index.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.frame.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.gestures.index.ios.swift +2 -2
- package/kit-apple/Sources/NativeScriptKit/Core/ui.html-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.image.image-common.swift +2 -2
- package/kit-apple/Sources/NativeScriptKit/Core/ui.image.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.label.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.absolute-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.dock-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.flexbox-layout.index.ios.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.grid-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.stack-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.layouts.wrap-layout.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.list-view.index.ios.swift +3 -3
- package/kit-apple/Sources/NativeScriptKit/Core/ui.page.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.repeater.index.swift +10 -6
- package/kit-apple/Sources/NativeScriptKit/Core/ui.scroll-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.split-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.styling.background.ios.swift +1 -1
- package/kit-apple/Sources/NativeScriptKit/Core/ui.tab-view.index.ios.swift +4 -4
- package/kit-apple/Sources/NativeScriptKit/Core/ui.text-base.formatted-string.swift +2 -1
- package/kit-apple/Sources/NativeScriptKit/Core/utils.common.swift +9 -9
- package/kit-apple/Sources/NativeScriptKit/Core/utils.layout-helper.index.ios.swift +10 -10
- package/kit-apple/Sources/NativeScriptKit/CoreBridge.swift +67 -1
- package/kit-apple/Sources/NativeScriptKit/Runtime/Lowering.swift +4 -0
- package/kit-apple/Sources/NativeScriptKit/Runtime/Prototypes.swift +25 -0
- package/kit-apple/Sources/NativeScriptKit/Runtime/RegExp.swift +6 -0
- package/kit-apple/Sources/NativeScriptKit/Signals.swift +20 -2
- package/package.json +2 -1
- package/kit-apple/Sources/NativeScriptKit/Core/Object_toString_charAt_charCodeAt_concat_indexOf_lastIndexOf_and_44_more_fc25df.swift +0 -481
package/dist/program/program.js
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
1
|
import ts from 'typescript';
|
|
2
|
+
import { createHash } from 'node:crypto';
|
|
2
3
|
import { existsSync, readFileSync } from 'node:fs';
|
|
3
4
|
import { basename, dirname, isAbsolute, resolve, sep } from 'node:path';
|
|
4
5
|
import { foldPlatform } from "./platform.js";
|
|
5
6
|
import { desugarConstructorFactories } from "./constructor-factories.js";
|
|
7
|
+
import { desugarClassMixins } from "./class-mixins.js";
|
|
8
|
+
import { expandImportMetaGlob } from "./glob.js";
|
|
9
|
+
import { NO_VITE_PLUGINS } from "./vite-plugins.js";
|
|
6
10
|
import { NATIVE_CONTROLLERS, NATIVE_VIEWS, nativeViewOf } from "./core-facts.js";
|
|
7
11
|
import { packageOf, runtimeFile } from "../plugins/resolve.js";
|
|
8
12
|
import { localFilePackages } from "../targets/resources.js";
|
|
@@ -21,9 +25,9 @@ const SHIMS = {
|
|
|
21
25
|
/** Deeply reactive: arrays and objects it holds notify on mutation. */
|
|
22
26
|
export interface DeepRef<T> { value: T }
|
|
23
27
|
export declare function $ref<T>(value: T): DeepRef<T>;
|
|
24
|
-
import type { ItemEventData, PanGestureEventData, PinchGestureEventData, RotationGestureEventData, SwipeGestureEventData, TouchGestureEventData, Pointer as CorePointer } from '@nativescript/core';
|
|
28
|
+
import type { ItemEventData, PanGestureEventData, PinchGestureEventData, PropertyChangeData, RotationGestureEventData, SwipeGestureEventData, TouchGestureEventData, Pointer as CorePointer } from '@nativescript/core';
|
|
25
29
|
/** A template handler's \$event: whichever of core's event types the handler declares. */
|
|
26
|
-
export type EventData = ItemEventData & PanGestureEventData & PinchGestureEventData & RotationGestureEventData & SwipeGestureEventData & TouchGestureEventData & { value: any; item: any };
|
|
30
|
+
export type EventData = ItemEventData & PanGestureEventData & PinchGestureEventData & PropertyChangeData & RotationGestureEventData & SwipeGestureEventData & TouchGestureEventData & { value: any; item: any; target?: any; currentTarget?: any; preventDefault?(): void; stopPropagation?(): void };
|
|
27
31
|
export type Pointer = CorePointer;
|
|
28
32
|
export declare function $navigate(page: () => any): void;
|
|
29
33
|
export interface WritableSignal<T> { (): T; set(value: T): void; update(fn: (value: T) => T): void; $write(value: T | ((previous: T) => T)): void }
|
|
@@ -140,6 +144,22 @@ const WORKER_GLOBALS = `
|
|
|
140
144
|
declare function postMessage(message: any, transfer?: any[]): void;
|
|
141
145
|
declare function close(): void;
|
|
142
146
|
`;
|
|
147
|
+
/** Minimal non-DOM globals added when the app's tsconfig includes `lib: ["dom"]` (which already declares console, URL, Worker, crypto, etc.). */
|
|
148
|
+
const DOM_COMPAT_GLOBALS = `
|
|
149
|
+
declare function setTimeout(callback: (...args: any[]) => void): number;
|
|
150
|
+
declare function setInterval(callback: (...args: any[]) => void): number;
|
|
151
|
+
declare namespace NodeJS { type Timeout = number; type Timer = number; type Immediate = number }
|
|
152
|
+
declare var global: typeof globalThis;
|
|
153
|
+
declare function cancelAnimationFrame(id: number | undefined): void;
|
|
154
|
+
declare function __nsRegisterAppModules(modules: any): void;
|
|
155
|
+
declare function __nsClass(make: () => any): any;
|
|
156
|
+
declare function __nsRegisterCompiledBinding(key: string, binding: any): void;
|
|
157
|
+
declare function __nsRegisterPage(file: string, factory: ((exports: any, moduleName: string, xmlModule: string) => any) | undefined, codeModule?: string, codeExports?: any): void;
|
|
158
|
+
interface SymbolConstructor {
|
|
159
|
+
readonly dispose: unique symbol;
|
|
160
|
+
readonly asyncDispose: unique symbol;
|
|
161
|
+
}
|
|
162
|
+
`;
|
|
143
163
|
/** The platform's native API typings, as an app's `references.d.ts` includes them. */
|
|
144
164
|
const PLATFORM_TYPES = { ios: '@nativescript/types-ios/index.d.ts', android: '@nativescript/types-android/index.d.ts' };
|
|
145
165
|
/** The nearest `node_modules` above `from` that has @nativescript/core. */
|
|
@@ -156,6 +176,41 @@ export function nodeModules(from) {
|
|
|
156
176
|
* the framework's are its adapter's, and the rest are typings or tooling.
|
|
157
177
|
*/
|
|
158
178
|
const NOT_PLUGINS = /^(@nativescript\/(core|types|types-ios|types-android|types-minimal|webpack|vite|tailwind|android|ios)|tslib|typescript|vite)$/;
|
|
179
|
+
/**
|
|
180
|
+
* Rewrites ES `export default <expr>;` (`ts.ExportAssignment`) into `export { <id> as default };` (introducing
|
|
181
|
+
* `const __nsDefault = <expr>;` when `<expr>` is not already a plain identifier) so TypeScript's `getAliasedSymbol`
|
|
182
|
+
* resolves default and namespace imports directly to a real declaration in both Swift and Kotlin backends.
|
|
183
|
+
*/
|
|
184
|
+
function desugarDefaultExport(code, name) {
|
|
185
|
+
// Desugar standard UMD `if (typeof module !== 'undefined' && module.exports) { module.exports = X; }` to an ES default export.
|
|
186
|
+
if (/\.m?js$/.test(name) && code.includes('module.exports') && !/\brequire\s*\(/.test(code)) {
|
|
187
|
+
code = code.replace(/if\s*\(\s*typeof\s+module\s*!==?\s*['"]undefined['"]\s*&&\s*module\.exports\s*\)\s*(?:\{\s*module\.exports\s*=\s*([^;{}]+?);?\s*\}|module\.exports\s*=\s*([^;]+?);?)/g, (_, a, b) => {
|
|
188
|
+
const rhs = (a ?? b ?? '').trim();
|
|
189
|
+
return /^[A-Za-z_$][\w$]*$/.test(rhs) ? `export { ${rhs} as default };` : `const __nsDefault = ${rhs}; export { __nsDefault as default };`;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
if (!code.includes('export default'))
|
|
193
|
+
return code;
|
|
194
|
+
const sf = ts.createSourceFile(name, code, ts.ScriptTarget.Latest, true);
|
|
195
|
+
const edits = [];
|
|
196
|
+
for (const st of sf.statements) {
|
|
197
|
+
if (!ts.isExportAssignment(st) || st.isExportEquals)
|
|
198
|
+
continue;
|
|
199
|
+
const exprText = code.slice(st.expression.getStart(sf), st.expression.getEnd());
|
|
200
|
+
if (ts.isIdentifier(st.expression)) {
|
|
201
|
+
edits.push({ start: st.getStart(sf), end: st.getEnd(), text: `export { ${exprText} as default };` });
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
edits.push({ start: st.getStart(sf), end: st.getEnd(), text: `const __nsDefault = ${exprText}; export { __nsDefault as default };` });
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (!edits.length)
|
|
208
|
+
return code;
|
|
209
|
+
let out = code;
|
|
210
|
+
for (const e of edits.sort((a, b) => b.start - a.start))
|
|
211
|
+
out = out.slice(0, e.start) + e.text + out.slice(e.end);
|
|
212
|
+
return out;
|
|
213
|
+
}
|
|
159
214
|
/**
|
|
160
215
|
* A program over the app's modules and the components' virtual classes,
|
|
161
216
|
* typed by the real ES2022 library, @nativescript/core's own declarations
|
|
@@ -163,17 +218,20 @@ const NOT_PLUGINS = /^(@nativescript\/(core|types|types-ios|types-android|types-
|
|
|
163
218
|
* own configuration is non-strict where no tsconfig.json of its says (a test's). `build`: the framework adapter's module
|
|
164
219
|
* rules, the plugins built as stand-ins and the bundler's defines it is read with (by default, none).
|
|
165
220
|
*/
|
|
166
|
-
export function createProgram(roots, virtual, platform = 'ios', modulesDir, plugins, declarations = [], replacements = {}, overrides = {}, aliased = () => undefined, appLenient, build = new BuildContext()) {
|
|
221
|
+
export function createProgram(roots, virtual, platform = 'ios', modulesDir, plugins, declarations = [], replacements = {}, overrides = {}, aliased = () => undefined, appLenient, build = new BuildContext(), vitePlugins = NO_VITE_PLUGINS) {
|
|
167
222
|
const shimPath = (m) => `/__shims__/${m.replace(/[@/]/g, '_')}.d.ts`;
|
|
168
223
|
const rules = build.adapter.rules;
|
|
169
224
|
const notPlugin = (pkg) => NOT_PLUGINS.test(pkg) || build.adapter.isFrameworkPackage(pkg) || build.stubbedPlugins.has(pkg);
|
|
170
225
|
// A build that compiles packages from their source compiles some packages the framework's adapter otherwise shims.
|
|
171
226
|
const shims = { ...SHIMS, ...rules.shims, ...(plugins ? {} : rules.fallbackShims) };
|
|
172
|
-
const
|
|
173
|
-
const
|
|
227
|
+
const normPath = (p) => p.replace(/\\/g, '/');
|
|
228
|
+
const substituted = new Set(Object.values(rules.substitutions ?? {}).map((s) => normPath(s.file)));
|
|
229
|
+
const virtualNorm = new Set([...virtual.keys()].map(normPath));
|
|
230
|
+
const files = new Map([...virtual].map(([k, v]) => [normPath(k), v]));
|
|
174
231
|
for (const [m, text] of Object.entries(shims))
|
|
175
232
|
files.set(shimPath(m), text);
|
|
176
|
-
|
|
233
|
+
const hasDom = appIncludesDomLib(roots[0] ?? [...virtual.keys()][0]);
|
|
234
|
+
files.set('/__shims__/globals.d.ts', hasDom ? DOM_COMPAT_GLOBALS : GLOBALS + WORKER_GLOBALS);
|
|
177
235
|
const modules = modulesDir ?? nodeModules(dirname([...roots, ...virtual.keys()][0]));
|
|
178
236
|
const platformTypes = resolve(modules, PLATFORM_TYPES[platform]);
|
|
179
237
|
if (!existsSync(platformTypes))
|
|
@@ -184,7 +242,7 @@ export function createProgram(roots, virtual, platform = 'ios', modulesDir, plug
|
|
|
184
242
|
moduleResolution: ts.ModuleResolutionKind.Bundler,
|
|
185
243
|
// Of ES2024, only what both runtimes implement (groupBy, withResolvers): the rest would type-check and then not translate.
|
|
186
244
|
strict: true,
|
|
187
|
-
lib: ['lib.es2023.d.ts', 'lib.es2024.object.d.ts', 'lib.es2024.collection.d.ts', 'lib.es2024.promise.d.ts'],
|
|
245
|
+
lib: ['lib.es2023.d.ts', 'lib.es2024.object.d.ts', 'lib.es2024.collection.d.ts', 'lib.es2024.promise.d.ts', ...(hasDom ? ['lib.dom.d.ts', 'lib.dom.iterable.d.ts'] : [])],
|
|
188
246
|
types: [],
|
|
189
247
|
skipLibCheck: true,
|
|
190
248
|
experimentalDecorators: true,
|
|
@@ -204,10 +262,13 @@ export function createProgram(roots, virtual, platform = 'ios', modulesDir, plug
|
|
|
204
262
|
// the other platform's types name what code for that platform holds (`signal<android.view.View | null>`).
|
|
205
263
|
const pluginFiles = new Set();
|
|
206
264
|
const extraRoots = new Set();
|
|
207
|
-
const isSource = (name) =>
|
|
208
|
-
|
|
265
|
+
const isSource = (name) => {
|
|
266
|
+
const n = normPath(name);
|
|
267
|
+
return !n.startsWith('/__shims__/') && !n.endsWith('.d.ts') && (build.adapter.isRuntimeSource(n) || !n.includes('/node_modules/'));
|
|
268
|
+
};
|
|
269
|
+
const isApp = (name) => isSource(name) && !pluginFiles.has(normPath(name));
|
|
209
270
|
// The app's declarations are read for the native APIs they declare: the frameworks' own typings they reference would replace the shims.
|
|
210
|
-
const appTypings = new Set(declarations);
|
|
271
|
+
const appTypings = new Set(declarations.map(normPath));
|
|
211
272
|
const nativeReferences = (text, file) => text.replace(/^\/\/\/\s*<reference\s+(path|types)="([^"]*)"\s*\/>.*$/gm, (line, kind, ref) => {
|
|
212
273
|
const target = resolve(dirname(file), ref);
|
|
213
274
|
const pkg = /[\\/]node_modules[\\/]((?:@[^\\/]+[\\/])?[^\\/]+)/.exec(target)?.[1].replace(/\\/g, '/');
|
|
@@ -216,31 +277,38 @@ export function createProgram(roots, virtual, platform = 'ios', modulesDir, plug
|
|
|
216
277
|
// TypeScript loads a file's `/// <reference path>` sources (a package's index referencing its operators barrel) before resolving
|
|
217
278
|
// the file's imports, so a plugin's referenced source is claimed here or its imports resolve as the app's.
|
|
218
279
|
const declaredExports = (name, code) => {
|
|
219
|
-
if (!pluginFiles.has(name))
|
|
280
|
+
if (!pluginFiles.has(normPath(name)))
|
|
220
281
|
return code;
|
|
221
282
|
for (const [, ref] of code.matchAll(/^\/\/\/\s*<reference\s+path="([^"]*\.tsx?)"\s*\/>/gm))
|
|
222
283
|
if (!ref.endsWith('.d.ts'))
|
|
223
|
-
pluginFiles.add(resolve(dirname(name), ref));
|
|
284
|
+
pluginFiles.add(normPath(resolve(dirname(name), ref)));
|
|
224
285
|
return pluginDeclaredExports(code, name, platform);
|
|
225
286
|
};
|
|
287
|
+
const appRoot = plugins?.options?.app ? normPath(resolve(plugins.options.app)) : null;
|
|
288
|
+
const rootSet = new Set(roots.map(normPath));
|
|
226
289
|
host.getSourceFile = (name, version, onError) => {
|
|
227
|
-
const
|
|
290
|
+
const key = normPath(name);
|
|
291
|
+
const text = files.get(key) ?? (isSource(key) && existsSync(name) ? readFileSync(name, 'utf8') : appTypings.has(key) ? nativeReferences(readFileSync(name, 'utf8'), name) : undefined);
|
|
228
292
|
if (text === undefined) {
|
|
229
293
|
// A package's own TypeScript (or pure-JS ESM) source.
|
|
230
294
|
if (!/\.(tsx?|m?js)$/.test(name) || name.endsWith('.d.ts') || !existsSync(name))
|
|
231
295
|
return readLib(name, version, onError);
|
|
232
|
-
const raw = readFileSync(name, 'utf8');
|
|
233
|
-
if (/\.m?js$/.test(name))
|
|
234
|
-
|
|
235
|
-
|
|
296
|
+
const raw = desugarDefaultExport(expandImportMetaGlob(readFileSync(name, 'utf8'), name, appRoot), name);
|
|
297
|
+
if (/\.m?js$/.test(name)) {
|
|
298
|
+
const typedefs = pluginJsTypedefs(name, plugins);
|
|
299
|
+
return ts.createSourceFile(name, typedefs ? `${raw}\n${typedefs}\n` : raw, version, true, ts.ScriptKind.JS);
|
|
300
|
+
}
|
|
301
|
+
return ts.createSourceFile(name, desugarClassMixins(desugarConstructorFactories(declaredExports(name, raw), name), name), version, true);
|
|
236
302
|
}
|
|
237
|
-
const
|
|
238
|
-
|
|
303
|
+
const transformed = !key.startsWith('/__shims__/') && !key.startsWith('/__raw__/') && !key.startsWith('/__vite__/') && !key.endsWith('.d.ts') ? vitePlugins.transform(text, name) : text;
|
|
304
|
+
const expanded = !key.startsWith('/__shims__/') && !key.endsWith('.d.ts') ? desugarDefaultExport(expandImportMetaGlob(transformed, name, appRoot), name) : transformed;
|
|
305
|
+
const code = /\.tsx?$/.test(key) && !key.endsWith('.d.ts') ? desugarClassMixins(desugarConstructorFactories(declaredExports(name, expanded), name), name) : expanded;
|
|
306
|
+
return ts.createSourceFile(name, isSource(key) ? foldPlatform(code, name, platform, modules, build.defines) : code, version, true);
|
|
239
307
|
};
|
|
240
308
|
const fileExists = host.fileExists.bind(host);
|
|
241
|
-
host.fileExists = (name) => files.has(name) || fileExists(name);
|
|
309
|
+
host.fileExists = (name) => files.has(normPath(name)) || fileExists(name);
|
|
242
310
|
const readFile = host.readFile.bind(host);
|
|
243
|
-
host.readFile = (name) => files.get(name) ?? readFile(name);
|
|
311
|
+
host.readFile = (name) => files.get(normPath(name)) ?? readFile(name);
|
|
244
312
|
const resolutions = new Map();
|
|
245
313
|
// A plugin's augmentations of other packages (`declare module '@nativescript/firebase-core'`), which its source keeps in
|
|
246
314
|
// the declarations beside it (`index.d.ts`), apply as its published typings apply them.
|
|
@@ -258,17 +326,71 @@ export function createProgram(roots, virtual, platform = 'ios', modulesDir, plug
|
|
|
258
326
|
resolutions.set(`${containing}\0${lit.text}`, r.resolvedModule.resolvedFileName);
|
|
259
327
|
return r;
|
|
260
328
|
});
|
|
261
|
-
const appRoot = plugins?.options?.app ? resolve(plugins.options.app) : null;
|
|
262
329
|
const localPackages = appRoot ? localFilePackages(appRoot) : new Map();
|
|
263
330
|
const resolveLiteral = (lit, containing) => {
|
|
264
331
|
{
|
|
265
|
-
//
|
|
266
|
-
|
|
332
|
+
// Vite `?raw` asset import (`import css from 'pkg/file.css?raw'` or `'./example.ts?raw'`): inline file contents as default export.
|
|
333
|
+
if (lit.text.endsWith('?raw')) {
|
|
334
|
+
const rawSpec = lit.text.slice(0, -4);
|
|
335
|
+
const aliasedRaw = isSource(containing) ? aliased(rawSpec, containing) : undefined;
|
|
336
|
+
const candidate = aliasedRaw ?? (rawSpec.startsWith('.') || isAbsolute(rawSpec) ? resolve(dirname(containing), rawSpec) : resolve(modules, rawSpec));
|
|
337
|
+
if (existsSync(candidate)) {
|
|
338
|
+
const hash = createHash('sha256').update(candidate).digest('hex').slice(0, 16);
|
|
339
|
+
const rawPath = `/__raw__/${hash}.ts`;
|
|
340
|
+
if (!files.has(rawPath))
|
|
341
|
+
files.set(rawPath, `const __nsRaw_${hash}: string = ${JSON.stringify(readFileSync(candidate, 'utf8'))};\nexport { __nsRaw_${hash} as default };\n`);
|
|
342
|
+
pluginFiles.add(rawPath);
|
|
343
|
+
return { resolvedModule: { resolvedFileName: rawPath, extension: ts.Extension.Ts } };
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
// JSON module import (`import data from 'pkg/data.json'` or `'./data.json'`): parse at runtime via JSON.parse so large data maps don't explode into native struct definitions.
|
|
347
|
+
if (lit.text.endsWith('.json') && !containing.startsWith('/__shims__/')) {
|
|
348
|
+
const aliasedJson = isSource(containing) ? aliased(lit.text, containing) : undefined;
|
|
349
|
+
let candidate = aliasedJson ?? (lit.text.startsWith('.') || isAbsolute(lit.text) ? resolve(dirname(containing), lit.text) : resolve(modules, lit.text));
|
|
350
|
+
if (!existsSync(candidate) && !lit.text.startsWith('.') && !isAbsolute(lit.text)) {
|
|
351
|
+
for (let dir = dirname(containing);; dir = dirname(dir)) {
|
|
352
|
+
const probe = resolve(dir, 'node_modules', lit.text);
|
|
353
|
+
if (existsSync(probe)) {
|
|
354
|
+
candidate = probe;
|
|
355
|
+
break;
|
|
356
|
+
}
|
|
357
|
+
if (dirname(dir) === dir)
|
|
358
|
+
break;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
if (existsSync(candidate)) {
|
|
362
|
+
const hash = createHash('sha256').update(candidate).digest('hex').slice(0, 16);
|
|
363
|
+
const jsonPath = `/__json__/${hash}.ts`;
|
|
364
|
+
if (!files.has(jsonPath))
|
|
365
|
+
files.set(jsonPath, `const __nsJson_${hash}: any = JSON.parse(${JSON.stringify(readFileSync(candidate, 'utf8'))});\nexport { __nsJson_${hash} as default };\n`);
|
|
366
|
+
pluginFiles.add(jsonPath);
|
|
367
|
+
return { resolvedModule: { resolvedFileName: jsonPath, extension: ts.Extension.Ts } };
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
// Custom Vite plugin `load(id)` hook for query-suffixed virtual modules (e.g. `.md?ns-analog-content`).
|
|
371
|
+
const qIdx = lit.text.indexOf('?');
|
|
372
|
+
if (qIdx > 0) {
|
|
373
|
+
const baseSpec = lit.text.slice(0, qIdx);
|
|
374
|
+
const query = lit.text.slice(qIdx);
|
|
375
|
+
const resolvedBase = baseSpec.startsWith('.') || isAbsolute(baseSpec) ? resolve(dirname(containing), baseSpec) : (isSource(containing) ? aliased(baseSpec, containing) : undefined) ?? resolve(modules, baseSpec);
|
|
376
|
+
const loaded = vitePlugins.load(resolvedBase + query);
|
|
377
|
+
if (loaded !== undefined) {
|
|
378
|
+
const hash = createHash('sha256').update(resolvedBase + query).digest('hex').slice(0, 16);
|
|
379
|
+
const vPath = `/__vite__/${hash}.ts`;
|
|
380
|
+
if (!files.has(vPath))
|
|
381
|
+
files.set(vPath, desugarDefaultExport(loaded, vPath));
|
|
382
|
+
pluginFiles.add(vPath);
|
|
383
|
+
return { resolvedModule: { resolvedFileName: vPath, extension: ts.Extension.Ts } };
|
|
384
|
+
}
|
|
385
|
+
}
|
|
386
|
+
// The app's own tsconfig `paths` / Vite aliases (`~/app/x`), as its bundler resolves them: workspace sources outside node_modules share them.
|
|
387
|
+
const aliasTarget = isSource(containing) ? aliased(lit.text, containing) : undefined;
|
|
267
388
|
const m = aliasTarget ?? lit.text;
|
|
268
389
|
const substitution = rules.substitutions && Object.hasOwn(rules.substitutions, m) && !replacements[m] ? rules.substitutions[m] : undefined;
|
|
269
390
|
if (substitution && (plugins || !substitution.sourcesOnly)) {
|
|
270
|
-
|
|
271
|
-
|
|
391
|
+
const subFile = normPath(substitution.file);
|
|
392
|
+
pluginFiles.add(subFile);
|
|
393
|
+
return { resolvedModule: { resolvedFileName: subFile, extension: ts.Extension.Ts } };
|
|
272
394
|
}
|
|
273
395
|
if (substitution?.otherwise)
|
|
274
396
|
return { resolvedModule: { resolvedFileName: shimPath(substitution.otherwise), extension: ts.Extension.Dts } };
|
|
@@ -276,28 +398,35 @@ export function createProgram(roots, virtual, platform = 'ios', modulesDir, plug
|
|
|
276
398
|
return { resolvedModule: { resolvedFileName: shimPath(m), extension: ts.Extension.Dts } };
|
|
277
399
|
const stub = !m.startsWith('.') && !isAbsolute(m) ? build.pluginStubModules.get(packageOf(m)) : undefined;
|
|
278
400
|
if (stub) {
|
|
279
|
-
|
|
280
|
-
|
|
401
|
+
const stubFile = normPath(stub);
|
|
402
|
+
pluginFiles.add(stubFile);
|
|
403
|
+
return { resolvedModule: { resolvedFileName: stubFile, extension: ts.Extension.Ts } };
|
|
281
404
|
}
|
|
282
405
|
// The build's own glue imports app modules by absolute path (`__elements.release.ts`).
|
|
283
406
|
if (m.startsWith('.') || (isAbsolute(m) && !m.startsWith('/__shims__/'))) {
|
|
284
|
-
const base = resolve(dirname(containing), m);
|
|
407
|
+
const base = normPath(resolve(dirname(containing), m));
|
|
285
408
|
const stem = base.replace(/\.m?js$/, '');
|
|
409
|
+
const containingNorm = normPath(containing);
|
|
286
410
|
// A platform's own file first, as NativeScript's bundler resolves `./x` to `x.ios.ts`.
|
|
287
411
|
// A platform file importing its own module's name (`import type { HingeListener } from './hinge-tracker'` in
|
|
288
412
|
// hinge-tracker.ios.ts) means the module's declarations: `x.d.ts`, else the platforms' shared `x.ts`.
|
|
289
413
|
const typeOnly = (ts.isImportDeclaration(lit.parent) && !!lit.parent.importClause?.isTypeOnly) || (ts.isExportDeclaration(lit.parent) && lit.parent.isTypeOnly);
|
|
290
414
|
// A type-only import is TypeScript's, which reads `x.d.ts` beside the platforms' files; only the bundler picks `x.ios.ts`.
|
|
291
|
-
const own =
|
|
415
|
+
const own = containingNorm === `${stem}.${platform}.ts` || (typeOnly && existsSync(stem + '.d.ts'));
|
|
292
416
|
const direct = /\.(tsx?|d\.ts)$/.test(base) ? [base] : [];
|
|
293
|
-
const jsFallback = pluginFiles.has(
|
|
417
|
+
const jsFallback = pluginFiles.has(containingNorm) ? [...(/\.m?js$/.test(base) ? [base] : []), stem + '.js', stem + '.mjs', stem + '/index.js', stem + '/index.mjs'] : [];
|
|
294
418
|
for (const candidate of own ? [...direct, stem + '.d.ts', stem + '.ts'] : [...direct, `${stem}.${platform}.ts`, stem + '.ts', stem + '.tsx', `${stem}/index.${platform}.ts`, stem + '/index.ts', ...jsFallback]) {
|
|
295
419
|
if (files.has(candidate) || existsSync(candidate)) {
|
|
296
420
|
if (candidate.endsWith('.d.ts'))
|
|
297
421
|
return { resolvedModule: { resolvedFileName: candidate, extension: ts.Extension.Dts } };
|
|
422
|
+
// A component file replaced by a compiled `.release.ts` virtual file must resolve to the virtual file everywhere so classes with private members stay nominally identical.
|
|
423
|
+
const released = candidate.endsWith('.ts') && !candidate.endsWith('.release.ts') ? candidate.replace(/\.ts$/, '.release.ts') : undefined;
|
|
424
|
+
if (released && virtualNorm.has(released) && containingNorm !== released)
|
|
425
|
+
return { resolvedModule: { resolvedFileName: released, extension: ts.Extension.Ts } };
|
|
298
426
|
// A plugin's own modules, a substitution's file, or a monorepo workspace library outside the app folder
|
|
299
|
-
// are compiled through closed-world reachability
|
|
300
|
-
|
|
427
|
+
// (not already compiled as an app/component root) are compiled through closed-world reachability.
|
|
428
|
+
const isCompiledRoot = rootSet.has(candidate) || virtualNorm.has(candidate) || virtualNorm.has(candidate.replace(/\.ts$/, '.release.ts'));
|
|
429
|
+
if (!isCompiledRoot && (pluginFiles.has(containingNorm) || substituted.has(candidate) || (aliasTarget && appRoot && !candidate.startsWith(appRoot + '/'))))
|
|
301
430
|
pluginFiles.add(candidate);
|
|
302
431
|
const extension = candidate.endsWith('.tsx') ? ts.Extension.Tsx : candidate.endsWith('.mjs') ? ts.Extension.Mjs : candidate.endsWith('.js') ? ts.Extension.Js : ts.Extension.Ts;
|
|
303
432
|
return { resolvedModule: { resolvedFileName: candidate, extension } };
|
|
@@ -315,7 +444,9 @@ export function createProgram(roots, virtual, platform = 'ios', modulesDir, plug
|
|
|
315
444
|
const typeOnly = !containing.startsWith('/__shims__/') && ts.isImportDeclaration(lit.parent) && !!lit.parent.importClause?.isTypeOnly && !(lit.parent.parent && ts.isSourceFile(lit.parent.parent) && lit.parent.parent.statements.some(valueImport));
|
|
316
445
|
// A plugin's augmentation of another's module augments the module the program compiles: that package's source.
|
|
317
446
|
const augments = ts.isModuleDeclaration(lit.parent) && lit.parent.name === lit && !!plugins?.all().some((p) => containing.startsWith(p.root + '/'));
|
|
318
|
-
|
|
447
|
+
// Every import of `rxjs` (including `import type` and `.d.ts` files like `@angular/cdk`) must resolve to the same compiled `rxjs` source so Observable/OperatorFunction types match across the whole program.
|
|
448
|
+
const sharedRxjs = m === 'rxjs' || m.startsWith('rxjs/');
|
|
449
|
+
if (plugins && (isSource(containing) || pluginFiles.has(containing) || augments || containing.startsWith('/__shims__/') || (containing.endsWith('.d.ts') && sharedRxjs)) && !m.startsWith('.') && !m.startsWith('/') && !notPlugin(packageOf(m)) && (!typeOnly || sharedRxjs)) {
|
|
319
450
|
const js = runtimeFile(m, modules, platform, localPackages);
|
|
320
451
|
if (js) {
|
|
321
452
|
plugins.get(js.packageDir);
|
|
@@ -649,6 +780,19 @@ function appDefinesFields(from) {
|
|
|
649
780
|
}
|
|
650
781
|
return undefined;
|
|
651
782
|
}
|
|
783
|
+
/** Whether the app's tsconfig.json (its `extends` followed) includes the DOM library (`"dom"`). */
|
|
784
|
+
function appIncludesDomLib(from) {
|
|
785
|
+
if (!from)
|
|
786
|
+
return false;
|
|
787
|
+
for (let dir = dirname(from); dirname(dir) !== dir; dir = dirname(dir)) {
|
|
788
|
+
const file = resolve(dir, 'tsconfig.json');
|
|
789
|
+
if (!existsSync(file))
|
|
790
|
+
continue;
|
|
791
|
+
const libs = parseTsConfig(file)?.options?.lib ?? [];
|
|
792
|
+
return libs.some((l) => /^lib\.dom(\.|$)/i.test(l) || /^dom$/i.test(l));
|
|
793
|
+
}
|
|
794
|
+
return false;
|
|
795
|
+
}
|
|
652
796
|
/** Whether the app's tsconfig.json (its `extends` followed) leaves strict checking off. */
|
|
653
797
|
function appStrictness(from) {
|
|
654
798
|
if (!from)
|
|
@@ -662,6 +806,41 @@ function appStrictness(from) {
|
|
|
662
806
|
}
|
|
663
807
|
return false;
|
|
664
808
|
}
|
|
809
|
+
/**
|
|
810
|
+
* For a pure-JS plugin compiled from `.js`/`.mjs` (e.g. `clsx`), exposes type/interface names declared in its
|
|
811
|
+
* package `.d.ts` as JSDoc `@typedef` so `import { clsx, type ClassValue } from 'clsx'` type-checks.
|
|
812
|
+
*/
|
|
813
|
+
function pluginJsTypedefs(file, plugins) {
|
|
814
|
+
const owner = plugins?.all().find((p) => file.startsWith(p.root + sep) || file.startsWith(p.dir + sep));
|
|
815
|
+
if (!owner)
|
|
816
|
+
return '';
|
|
817
|
+
const pkgPath = resolve(owner.dir, 'package.json');
|
|
818
|
+
if (!existsSync(pkgPath))
|
|
819
|
+
return '';
|
|
820
|
+
let typesRel;
|
|
821
|
+
try {
|
|
822
|
+
const pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
|
|
823
|
+
typesRel = typeof pkg.types === 'string' ? pkg.types : typeof pkg.typings === 'string' ? pkg.typings : undefined;
|
|
824
|
+
}
|
|
825
|
+
catch {
|
|
826
|
+
return '';
|
|
827
|
+
}
|
|
828
|
+
if (!typesRel)
|
|
829
|
+
return '';
|
|
830
|
+
const dtsPath = resolve(owner.dir, typesRel);
|
|
831
|
+
if (!existsSync(dtsPath))
|
|
832
|
+
return '';
|
|
833
|
+
const dts = ts.createSourceFile(dtsPath, readFileSync(dtsPath, 'utf8'), ts.ScriptTarget.Latest, false);
|
|
834
|
+
const names = new Set();
|
|
835
|
+
const scan = (st) => {
|
|
836
|
+
if ((ts.isTypeAliasDeclaration(st) || ts.isInterfaceDeclaration(st)) && st.name)
|
|
837
|
+
names.add(st.name.text);
|
|
838
|
+
else if (ts.isModuleDeclaration(st) && st.body && ts.isModuleBlock(st.body))
|
|
839
|
+
st.body.statements.forEach(scan);
|
|
840
|
+
};
|
|
841
|
+
dts.statements.forEach(scan);
|
|
842
|
+
return [...names].map((n) => `/** @typedef {any} ${n} */`).join('\n');
|
|
843
|
+
}
|
|
665
844
|
/**
|
|
666
845
|
* A plugin's platform file (`index.ios.ts`) with the exports of the declarations beside it (`index.d.ts`) that it
|
|
667
846
|
* lacks: an app is written against those declarations, and the build compiles the platform file in their place. A
|
|
@@ -705,8 +884,30 @@ export function pluginDeclaredExports(code, file, platform) {
|
|
|
705
884
|
}
|
|
706
885
|
return { exported, local, stars, from };
|
|
707
886
|
};
|
|
708
|
-
const
|
|
709
|
-
const
|
|
887
|
+
const ownSf = ts.createSourceFile(file, code, ts.ScriptTarget.Latest, true);
|
|
888
|
+
const declaredSf = ts.createSourceFile(`${stem}.d.ts`, readFileSync(`${stem}.d.ts`, 'utf8'), ts.ScriptTarget.Latest, false);
|
|
889
|
+
const own = exportsOf(ownSf);
|
|
890
|
+
const declared = exportsOf(declaredSf);
|
|
891
|
+
// Give unannotated exported functions in a plugin's platform file their sibling `.d.ts` return type so callers see the published contract.
|
|
892
|
+
const dtsFnReturn = new Map();
|
|
893
|
+
for (const st of declaredSf.statements) {
|
|
894
|
+
if (!ts.isFunctionDeclaration(st) || !st.name || !st.type || st.typeParameters?.length)
|
|
895
|
+
continue;
|
|
896
|
+
dtsFnReturn.set(st.name.text, (dtsFnReturn.get(st.name.text) ?? 0) + 1);
|
|
897
|
+
}
|
|
898
|
+
const fnEdits = [];
|
|
899
|
+
const relBase = `./${basename(stem)}`;
|
|
900
|
+
for (const st of ownSf.statements) {
|
|
901
|
+
if (!ts.isFunctionDeclaration(st) || !st.name || st.type || !st.body || st.typeParameters?.length)
|
|
902
|
+
continue;
|
|
903
|
+
const isExported = !!ts.getModifiers(st)?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
904
|
+
if (!isExported || dtsFnReturn.get(st.name.text) !== 1)
|
|
905
|
+
continue;
|
|
906
|
+
fnEdits.push({ at: st.body.getStart(ownSf), text: `: ReturnType<typeof import(${JSON.stringify(relBase)}).${st.name.text}> ` });
|
|
907
|
+
}
|
|
908
|
+
let patched = code;
|
|
909
|
+
for (const e of fnEdits.sort((a, b) => b.at - a.at))
|
|
910
|
+
patched = patched.slice(0, e.at) + e.text + patched.slice(e.at);
|
|
710
911
|
const lines = [];
|
|
711
912
|
for (const name of declared.exported) {
|
|
712
913
|
if (own.exported.has(name))
|
|
@@ -717,10 +918,10 @@ export function pluginDeclaredExports(code, file, platform) {
|
|
|
717
918
|
else if (spec)
|
|
718
919
|
lines.push(`export { ${name} } from ${JSON.stringify(spec)};`);
|
|
719
920
|
else if (declared.local.get(name) === 'type')
|
|
720
|
-
lines.push(`export type { ${name} } from ${JSON.stringify(
|
|
921
|
+
lines.push(`export type { ${name} } from ${JSON.stringify(relBase)};`);
|
|
721
922
|
}
|
|
722
923
|
for (const spec of declared.stars)
|
|
723
924
|
if (!own.stars.has(spec))
|
|
724
925
|
lines.push(`export * from ${JSON.stringify(spec)};`);
|
|
725
|
-
return lines.length ? `${
|
|
926
|
+
return lines.length ? `${patched}\n${lines.join('\n')}\n` : patched;
|
|
726
927
|
}
|
|
@@ -40,7 +40,10 @@ export class SourceLines {
|
|
|
40
40
|
/** Lines of a virtual file whose source is known exactly (a template binding's), which no matching then guesses. */
|
|
41
41
|
pinned = new Map();
|
|
42
42
|
pin(virtualFile, file, lines) {
|
|
43
|
-
this.pinned.
|
|
43
|
+
const map = this.pinned.get(virtualFile) ?? new Map();
|
|
44
|
+
for (const [v, line] of lines)
|
|
45
|
+
map.set(v, { file, line });
|
|
46
|
+
this.pinned.set(virtualFile, map);
|
|
44
47
|
}
|
|
45
48
|
/** The marker for the statement `s`; one that ends the current region when its source line is unknown. */
|
|
46
49
|
mark(s) {
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
|
|
2
|
+
if (typeof path === "string" && /^\.\.?\//.test(path)) {
|
|
3
|
+
return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
|
|
4
|
+
return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
|
|
5
|
+
});
|
|
6
|
+
}
|
|
7
|
+
return path;
|
|
8
|
+
};
|
|
9
|
+
// Loads app-authored Vite plugins from an app's `vite.config.*` so ahead-of-time compilation honors
|
|
10
|
+
// custom `resolveId`, `transform`, and `load` hooks (e.g. monorepo component shims, virtual markdown modules).
|
|
11
|
+
import { existsSync } from 'node:fs';
|
|
12
|
+
import { createRequire } from 'node:module';
|
|
13
|
+
import { resolve } from 'node:path';
|
|
14
|
+
import { pathToFileURL } from 'node:url';
|
|
15
|
+
export const NO_VITE_PLUGINS = {
|
|
16
|
+
resolveId: () => undefined,
|
|
17
|
+
transform: (code) => code,
|
|
18
|
+
load: () => undefined,
|
|
19
|
+
};
|
|
20
|
+
/** Built-in Vite, @nativescript/vite, and framework bundler plugins skipped when running app-level hooks. */
|
|
21
|
+
const BUILTIN_PLUGIN_PREFIX = /^(vite:|builtin:|native:|ns-|nsvite-|@analogjs\/|vite-plugin-static-copy)/;
|
|
22
|
+
const BUILTIN_PLUGIN_NAMES = new Set([
|
|
23
|
+
'alias',
|
|
24
|
+
'replace',
|
|
25
|
+
'main-entry',
|
|
26
|
+
'source-map-js-compat',
|
|
27
|
+
'html-entities-compat',
|
|
28
|
+
'fast-xml-parser-compat',
|
|
29
|
+
'angular-template-deps',
|
|
30
|
+
'nativescript-package-resolver',
|
|
31
|
+
'nativescript-worker-loader-stub',
|
|
32
|
+
'nativescript-angular-worker-url-preserve',
|
|
33
|
+
'nativescript-app-components',
|
|
34
|
+
'nativescript-worker-url-transform',
|
|
35
|
+
'nativescript-dynamic-import-fix',
|
|
36
|
+
'nativescript-cli-integration',
|
|
37
|
+
]);
|
|
38
|
+
function hookFn(hook) {
|
|
39
|
+
if (typeof hook === 'function')
|
|
40
|
+
return hook;
|
|
41
|
+
if (hook && typeof hook.handler === 'function')
|
|
42
|
+
return hook.handler;
|
|
43
|
+
return undefined;
|
|
44
|
+
}
|
|
45
|
+
const cache = new Map();
|
|
46
|
+
/** Resolves the app's `vite.config.*` (if any) and returns a synchronous bridge to its custom app plugins. */
|
|
47
|
+
export async function loadViteAppPlugins(root) {
|
|
48
|
+
const absRoot = resolve(root);
|
|
49
|
+
const cached = cache.get(absRoot);
|
|
50
|
+
if (cached)
|
|
51
|
+
return cached;
|
|
52
|
+
const configFile = ['vite.config.mts', 'vite.config.ts', 'vite.config.mjs', 'vite.config.js'].map((f) => resolve(absRoot, f)).find(existsSync);
|
|
53
|
+
if (!configFile || !existsSync(resolve(absRoot, 'package.json'))) {
|
|
54
|
+
cache.set(absRoot, NO_VITE_PLUGINS);
|
|
55
|
+
return NO_VITE_PLUGINS;
|
|
56
|
+
}
|
|
57
|
+
const prevCwd = process.cwd();
|
|
58
|
+
const prevIgnore = process.env.VITE_CONFIG_NATIVE_IGNORE_WARNING;
|
|
59
|
+
let plugins = [];
|
|
60
|
+
try {
|
|
61
|
+
process.env.VITE_CONFIG_NATIVE_IGNORE_WARNING = 'true';
|
|
62
|
+
process.chdir(absRoot);
|
|
63
|
+
const req = createRequire(resolve(absRoot, 'package.json'));
|
|
64
|
+
const vite = await import(__rewriteRelativeImportExtension(pathToFileURL(req.resolve('vite')).href, true));
|
|
65
|
+
const cfg = await vite.resolveConfig({ configFile, root: absRoot, mode: 'production' }, 'build', 'production', 'production');
|
|
66
|
+
plugins = (cfg.plugins ?? []).filter((p) => {
|
|
67
|
+
const name = p?.name ?? '';
|
|
68
|
+
return !!name && !BUILTIN_PLUGIN_PREFIX.test(name) && !BUILTIN_PLUGIN_NAMES.has(name);
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
plugins = [];
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
process.chdir(prevCwd);
|
|
76
|
+
if (prevIgnore === undefined)
|
|
77
|
+
delete process.env.VITE_CONFIG_NATIVE_IGNORE_WARNING;
|
|
78
|
+
else
|
|
79
|
+
process.env.VITE_CONFIG_NATIVE_IGNORE_WARNING = prevIgnore;
|
|
80
|
+
}
|
|
81
|
+
if (!plugins.length) {
|
|
82
|
+
cache.set(absRoot, NO_VITE_PLUGINS);
|
|
83
|
+
return NO_VITE_PLUGINS;
|
|
84
|
+
}
|
|
85
|
+
const resolvers = plugins.map((p) => hookFn(p.resolveId)).filter((fn) => !!fn);
|
|
86
|
+
const transformers = plugins.map((p) => hookFn(p.transform)).filter((fn) => !!fn);
|
|
87
|
+
const loaders = plugins.map((p) => hookFn(p.load)).filter((fn) => !!fn);
|
|
88
|
+
const ctx = { warn: () => { }, error: (e) => { throw new Error(String(e)); } };
|
|
89
|
+
const bridge = {
|
|
90
|
+
resolveId(specifier, importer) {
|
|
91
|
+
for (const fn of resolvers) {
|
|
92
|
+
try {
|
|
93
|
+
const out = fn.call(ctx, specifier, importer, {});
|
|
94
|
+
if (out && typeof out.then === 'function')
|
|
95
|
+
continue;
|
|
96
|
+
const id = typeof out === 'string' ? out : out && typeof out.id === 'string' ? out.id : undefined;
|
|
97
|
+
if (id && !id.startsWith('\0'))
|
|
98
|
+
return id;
|
|
99
|
+
}
|
|
100
|
+
catch {
|
|
101
|
+
// Ignore plugins that rely on full Rollup context for unrelated specifiers.
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return undefined;
|
|
105
|
+
},
|
|
106
|
+
transform(code, id) {
|
|
107
|
+
let cur = code;
|
|
108
|
+
for (const fn of transformers) {
|
|
109
|
+
try {
|
|
110
|
+
const out = fn.call(ctx, cur, id);
|
|
111
|
+
if (out && typeof out.then === 'function')
|
|
112
|
+
continue;
|
|
113
|
+
if (typeof out === 'string')
|
|
114
|
+
cur = out;
|
|
115
|
+
else if (out && typeof out.code === 'string')
|
|
116
|
+
cur = out.code;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
// Ignore plugins that cannot transform this file synchronously.
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return cur;
|
|
123
|
+
},
|
|
124
|
+
load(id) {
|
|
125
|
+
for (const fn of loaders) {
|
|
126
|
+
try {
|
|
127
|
+
const out = fn.call(ctx, id);
|
|
128
|
+
if (out && typeof out.then === 'function')
|
|
129
|
+
continue;
|
|
130
|
+
if (typeof out === 'string')
|
|
131
|
+
return out;
|
|
132
|
+
if (out && typeof out.code === 'string')
|
|
133
|
+
return out.code;
|
|
134
|
+
}
|
|
135
|
+
catch {
|
|
136
|
+
// Ignore plugins that do not handle this id.
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return undefined;
|
|
140
|
+
},
|
|
141
|
+
};
|
|
142
|
+
cache.set(absRoot, bridge);
|
|
143
|
+
return bridge;
|
|
144
|
+
}
|
|
@@ -13,8 +13,8 @@ const FACTS = {
|
|
|
13
13
|
clearTimeout: { arity: 0, result: 'undefined', runtime: 'jsClearTimeout', host: true },
|
|
14
14
|
clearInterval: { arity: 0, result: 'undefined', runtime: 'jsClearInterval', host: true },
|
|
15
15
|
queueMicrotask: { arity: 1, result: 'undefined', runtime: 'jsQueueMicrotask', args: ['callback'] },
|
|
16
|
-
requestAnimationFrame: { arity: 1, result: 'number', host: true
|
|
17
|
-
cancelAnimationFrame: { arity: 1, result: 'undefined', host: true
|
|
16
|
+
requestAnimationFrame: { arity: 1, result: 'number', host: true },
|
|
17
|
+
cancelAnimationFrame: { arity: 1, result: 'undefined', host: true },
|
|
18
18
|
unescape: { arity: 1, result: 'string', runtime: 'jsUnescape', args: ['string'] },
|
|
19
19
|
atob: { arity: 1, result: 'string', throws: true, runtime: 'jsAtob', args: ['string'] },
|
|
20
20
|
btoa: { arity: 1, result: 'string', throws: true, runtime: 'jsBtoa', args: ['string'] },
|
|
@@ -21,5 +21,6 @@ export const OBJECT_STATICS = apiTable('static', {
|
|
|
21
21
|
keys: { arity: 1, result: 'object' },
|
|
22
22
|
preventExtensions: { arity: 1, result: 'object' },
|
|
23
23
|
seal: { arity: 1, result: 'object' },
|
|
24
|
+
setPrototypeOf: { arity: 2, result: 'object', throws: true, runtime: 'jsSetPrototypeOf', args: ['any', 'any'] },
|
|
24
25
|
values: { arity: 1, result: 'object' },
|
|
25
26
|
});
|
|
@@ -51,6 +51,12 @@ export function libFunctionValueKind(owner, name) {
|
|
|
51
51
|
return 'console';
|
|
52
52
|
if (owner === 'String' && name === 'fromCharCode')
|
|
53
53
|
return 'fromCharCode';
|
|
54
|
+
if (owner === 'String' && name === 'fromCodePoint')
|
|
55
|
+
return 'fromCodePoint';
|
|
56
|
+
if (owner === 'Math' && name === 'random')
|
|
57
|
+
return 'mathRandom';
|
|
58
|
+
if (owner === 'Math' && (name === 'min' || name === 'max'))
|
|
59
|
+
return 'mathVariadic';
|
|
54
60
|
if (TYPED_ARRAYS.includes(owner) && name === 'from')
|
|
55
61
|
return 'typedArrayFrom';
|
|
56
62
|
if (owner === 'Array' && name === 'isArray')
|