@nativescript/vite 8.0.0-alpha.3 → 8.0.0-alpha.5
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/base.js +57 -0
- package/configuration/base.js.map +1 -1
- package/helpers/config-as-json.js +10 -0
- package/helpers/config-as-json.js.map +1 -1
- package/helpers/main-entry.js +167 -13
- package/helpers/main-entry.js.map +1 -1
- package/helpers/ns-core-url.d.ts +84 -0
- package/helpers/ns-core-url.js +168 -0
- package/helpers/ns-core-url.js.map +1 -0
- package/hmr/server/core-sanitize.d.ts +8 -4
- package/hmr/server/core-sanitize.js +71 -41
- package/hmr/server/core-sanitize.js.map +1 -1
- package/hmr/server/import-map.js +7 -3
- package/hmr/server/import-map.js.map +1 -1
- package/hmr/server/ns-core-cjs-shape.d.ts +206 -0
- package/hmr/server/ns-core-cjs-shape.js +273 -0
- package/hmr/server/ns-core-cjs-shape.js.map +1 -0
- package/hmr/server/websocket-core-bridge.d.ts +0 -2
- package/hmr/server/websocket-core-bridge.js +60 -58
- package/hmr/server/websocket-core-bridge.js.map +1 -1
- package/hmr/server/websocket-module-specifiers.js +12 -0
- package/hmr/server/websocket-module-specifiers.js.map +1 -1
- 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 +47 -0
- package/hmr/server/websocket-ns-m-paths.js.map +1 -0
- package/hmr/server/websocket-ns-m-request.d.ts +35 -0
- package/hmr/server/websocket-ns-m-request.js +203 -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 +286 -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 +589 -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 +1117 -0
- package/hmr/server/websocket-vue-sfc.js.map +1 -0
- package/hmr/server/websocket.d.ts +1 -1
- package/hmr/server/websocket.js +327 -79
- package/hmr/server/websocket.js.map +1 -1
- package/hmr/shared/vendor/manifest.js +114 -12
- package/hmr/shared/vendor/manifest.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CJS/ESM interop shape (Invariant D).
|
|
3
|
+
*
|
|
4
|
+
* See HMR_CORE_REALM_DETERMINISTIC_PLAN.md § "Invariant D — CJS/ESM interop shape".
|
|
5
|
+
*
|
|
6
|
+
* PROBLEM
|
|
7
|
+
* -------
|
|
8
|
+
* ESM Module Namespace Objects (ECMA §9.4.6) have [[Prototype]] = null.
|
|
9
|
+
* They do NOT inherit Object.prototype — no `hasOwnProperty`, no `toString`,
|
|
10
|
+
* no `isPrototypeOf`. CJS consumers (most notably zone.js's `patchMethod`)
|
|
11
|
+
* assume their require() result is a plain object that has those methods:
|
|
12
|
+
*
|
|
13
|
+
* let proto = target;
|
|
14
|
+
* while (proto && !proto.hasOwnProperty(name)) // ← crashes on null-proto
|
|
15
|
+
* proto = Object.getPrototypeOf(proto);
|
|
16
|
+
*
|
|
17
|
+
* `@nativescript/core/index.js` re-exports several sub-modules as namespaces:
|
|
18
|
+
*
|
|
19
|
+
* export * as Utils from './utils';
|
|
20
|
+
* export * as Http from './http';
|
|
21
|
+
* export * as Connectivity from './connectivity';
|
|
22
|
+
* export * as ApplicationSettings from './application-settings';
|
|
23
|
+
*
|
|
24
|
+
* When bundle.mjs does `import { Utils } from '@nativescript/core'`, `Utils`
|
|
25
|
+
* binds to that null-proto sub-namespace. zone.js then does
|
|
26
|
+
* `patchMethod(Utils, 'mainThreadify', …)` and crashes.
|
|
27
|
+
*
|
|
28
|
+
* WHY globalThis.require shims ALONE DON'T FIX THIS
|
|
29
|
+
* -------------------------------------------------
|
|
30
|
+
* ESM imports never pass through `globalThis.require`. They go through the
|
|
31
|
+
* host's ESM linker (V8 on iOS). Shimming `require` only intercepts the CJS
|
|
32
|
+
* path (vendor packages that use `require('@nativescript/core')`); direct
|
|
33
|
+
* ESM imports of `Utils` still see the raw null-proto namespace.
|
|
34
|
+
*
|
|
35
|
+
* THE FIX
|
|
36
|
+
* -------
|
|
37
|
+
* Rewrite `export * as X from 'Y'` at the /ns/core bridge response level:
|
|
38
|
+
*
|
|
39
|
+
* // before
|
|
40
|
+
* export * as Utils from './utils';
|
|
41
|
+
*
|
|
42
|
+
* // after
|
|
43
|
+
* import * as __ns_re_Utils__ from './utils';
|
|
44
|
+
* export const Utils = __NS_CJS_SHAPE__(__ns_re_Utils__);
|
|
45
|
+
*
|
|
46
|
+
* Now `Utils` is a plain Object (inherits Object.prototype). Zone.js's
|
|
47
|
+
* `patchMethod(Utils, ...)` succeeds. The shape function is recursive —
|
|
48
|
+
* nested namespaces get the same treatment — and identity-preserving
|
|
49
|
+
* (WeakMap-cached) so mutations stick across lookups.
|
|
50
|
+
*
|
|
51
|
+
* WHY THE SHAPE INSTALL MUST RUN IN MODULE BODY (NOT FOOTER)
|
|
52
|
+
* ---------------------------------------------------------
|
|
53
|
+
* `export const Utils = __NS_CJS_SHAPE__(__ns_re_Utils__)` is body code. It
|
|
54
|
+
* executes during module evaluation, BEFORE the registration footer. So the
|
|
55
|
+
* shape function must be installed as a body-level statement that runs
|
|
56
|
+
* before the rewritten body — we can't rely on the footer's install.
|
|
57
|
+
*
|
|
58
|
+
* Each served /ns/core module independently installs the shape function via
|
|
59
|
+
* an idempotent `|| (globalThis.__NS_CJS_SHAPE__ = ...)` assignment. The
|
|
60
|
+
* first module to evaluate wins; subsequent evaluations are no-ops. This
|
|
61
|
+
* also handles dependency-before-importer order (depth-first ESM eval):
|
|
62
|
+
* `/ns/core/utils` evaluates before `/ns/core` main, so the shape helper
|
|
63
|
+
* is in place whenever any body needs it.
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Body-code statements that idempotently install `globalThis.__NS_CJS_SHAPE__`
|
|
67
|
+
* and `globalThis.__NS_CJS_SHAPE_CACHE__`. Must execute BEFORE any transformed
|
|
68
|
+
* `export const X = __NS_CJS_SHAPE__(...)` statement in the same module.
|
|
69
|
+
*
|
|
70
|
+
* Properties:
|
|
71
|
+
* - Recursive: traverses nested namespaces so `Utils.Something` is also
|
|
72
|
+
* a plain Object (if that sub-namespace is itself null-proto).
|
|
73
|
+
* - Identity-preserving: WeakMap keyed on the underlying ESM namespace.
|
|
74
|
+
* zone.js mutates its patch target; a fresh copy per call would lose
|
|
75
|
+
* mutations. Every lookup of the same namespace returns the same
|
|
76
|
+
* shaped object.
|
|
77
|
+
* - Cycle-safe: records the output in the cache BEFORE recursing into
|
|
78
|
+
* children. Handles `core ↔ platform` style cycles.
|
|
79
|
+
* - TDZ-safe: wraps each property read in try/catch. Some exports
|
|
80
|
+
* (Angular zone.js with early-access patterns) are still in their
|
|
81
|
+
* temporal dead zone when the namespace is first snapshotted. A thrown
|
|
82
|
+
* property read is skipped rather than failing the whole shape.
|
|
83
|
+
*/
|
|
84
|
+
export declare function buildShapeInstallHeader(): string;
|
|
85
|
+
/**
|
|
86
|
+
* Rewrite namespace re-exports from CJS-incompatible ESM namespace form into
|
|
87
|
+
* shape-aware const exports. Handles the two forms Vite/esbuild may emit:
|
|
88
|
+
*
|
|
89
|
+
* (1) Original ES2020 syntax preserved unchanged:
|
|
90
|
+
* export * as X from 'Y';
|
|
91
|
+
*
|
|
92
|
+
* (2) Transpiled form esbuild emits when targeting older ES:
|
|
93
|
+
* import * as _foo from 'Y';
|
|
94
|
+
* export { _foo as X };
|
|
95
|
+
*
|
|
96
|
+
* Both become:
|
|
97
|
+
* import * as __ns_re_X__ from 'Y';
|
|
98
|
+
* export const X = __NS_CJS_SHAPE__(__ns_re_X__);
|
|
99
|
+
*
|
|
100
|
+
* Notes on the transform:
|
|
101
|
+
* - The internal binding uses a derived name (`__ns_re_<X>__`) so
|
|
102
|
+
* collisions with user bindings are extremely unlikely. Core doesn't
|
|
103
|
+
* declare identifiers matching that shape.
|
|
104
|
+
* - `__NS_CJS_SHAPE__` is read from globalThis with a defensive fallback
|
|
105
|
+
* (identity function) in case installation failed or runs in an
|
|
106
|
+
* environment where it hasn't been initialized yet. This makes the
|
|
107
|
+
* transform safe to apply to modules that don't always go through the
|
|
108
|
+
* /ns/core handler's headers.
|
|
109
|
+
* - We do NOT shape `export *` (star re-exports without alias). Those
|
|
110
|
+
* spread individual named exports — they don't create a nested
|
|
111
|
+
* namespace and aren't affected by the null-proto issue.
|
|
112
|
+
* - We do NOT shape `export { x } from 'y'` forms. Those copy individual
|
|
113
|
+
* named exports directly; the consumer never sees the namespace.
|
|
114
|
+
*
|
|
115
|
+
* The regex is intentionally precise. It matches only the `export * as X from
|
|
116
|
+
* 'Y'` syntactic form (and its transpiled equivalent) — no accidental matches
|
|
117
|
+
* in string literals or comments because those can't start a top-level
|
|
118
|
+
* statement with `export`.
|
|
119
|
+
*/
|
|
120
|
+
export declare function rewriteNamespaceReExportsForShape(code: string): string;
|
|
121
|
+
/**
|
|
122
|
+
* Convenience: returns true if the given code appears to contain at least one
|
|
123
|
+
* namespace re-export that would benefit from the shape transform. Used to
|
|
124
|
+
* skip the regex replace for modules that don't need it (keeps the response
|
|
125
|
+
* identical for most submodules, which just have plain `export * from` or
|
|
126
|
+
* named exports).
|
|
127
|
+
*/
|
|
128
|
+
export declare function hasNamespaceReExport(code: string): boolean;
|
|
129
|
+
/**
|
|
130
|
+
* Default-export bridge footer (Invariant D, round 3).
|
|
131
|
+
*
|
|
132
|
+
* BACKGROUND
|
|
133
|
+
* ----------
|
|
134
|
+
* Upstream consumer rewrites in the /ns/m handler convert
|
|
135
|
+
* `import { X } from '@nativescript/core'` into
|
|
136
|
+
* `import __ns_core_ns from '/ns/core'; const { X } = __ns_core_ns;`
|
|
137
|
+
* — a DEFAULT import followed by destructuring. The inline comment on that
|
|
138
|
+
* rewrite says explicitly: "This makes `import { Frame } from
|
|
139
|
+
* '@nativescript/core'` work even if the bridge provides only a default
|
|
140
|
+
* export."
|
|
141
|
+
*
|
|
142
|
+
* The original `@nativescript/core/index.js` has no `export default` — all
|
|
143
|
+
* named exports. Without this footer, consumers of the transformed import
|
|
144
|
+
* fail at ESM link time:
|
|
145
|
+
*
|
|
146
|
+
* SyntaxError: The requested module '/ns/core/<ver>' does not
|
|
147
|
+
* provide an export named 'default'
|
|
148
|
+
*
|
|
149
|
+
* THE FIX
|
|
150
|
+
* -------
|
|
151
|
+
* Emit `export default __ns_core_self_ns__;` at the end of the served
|
|
152
|
+
* module body. `__ns_core_self_ns__` is the namespace import the server
|
|
153
|
+
* already emits at the top of every /ns/core response for self-
|
|
154
|
+
* registration purposes. ESM spec guarantees a module re-entering itself
|
|
155
|
+
* during evaluation returns the in-progress namespace — so no extra
|
|
156
|
+
* evaluation occurs, and the default binding receives an object whose
|
|
157
|
+
* properties are every named export of the same module.
|
|
158
|
+
*
|
|
159
|
+
* CONSUMER MATRIX AFTER THIS FIX
|
|
160
|
+
* ------------------------------
|
|
161
|
+
* • `import X from '/ns/core'` (default) → X = self namespace
|
|
162
|
+
* • `import * as X from '/ns/core'` → X = self namespace + X.default = self
|
|
163
|
+
* • `import { X } from '/ns/core'` (named) → individual named binding (untouched)
|
|
164
|
+
* • `const { X } = <default-import>` → destructure from self namespace
|
|
165
|
+
* • `require('@nativescript/core')` (CJS) → shaped self via registry
|
|
166
|
+
*
|
|
167
|
+
* SKIP CONDITIONS
|
|
168
|
+
* ---------------
|
|
169
|
+
* Returns empty string when the source already declares a default export
|
|
170
|
+
* (some deep subpaths do — e.g. a file that does `export default MyClass`
|
|
171
|
+
* would SyntaxError on a duplicate). The consumer rewrite is also gated
|
|
172
|
+
* on `isDeepCoreSubpath`, so those paths don't normally hit the default-
|
|
173
|
+
* import pattern anyway.
|
|
174
|
+
*
|
|
175
|
+
* WHY THE DEFAULT ISN'T SHAPED
|
|
176
|
+
* ----------------------------
|
|
177
|
+
* The default is the raw null-proto MNO. This is intentional:
|
|
178
|
+
*
|
|
179
|
+
* 1. All current default-import consumers either destructure or do
|
|
180
|
+
* property access. Neither requires Object.prototype in the chain.
|
|
181
|
+
* 2. Shaping the default inside the module body would require calling
|
|
182
|
+
* __NS_CJS_SHAPE__ on a namespace whose own `default` slot is still
|
|
183
|
+
* being initialized — temporal dead zone hazard.
|
|
184
|
+
* 3. CJS consumers that need `.hasOwnProperty` go through the registry
|
|
185
|
+
* (shaped at registration), not through the default import.
|
|
186
|
+
*
|
|
187
|
+
* If a future consumer surfaces that calls `.hasOwnProperty` on the
|
|
188
|
+
* default, we can revisit. The current rule-of-least-surprise is:
|
|
189
|
+
* shape at the CJS boundary, not at the ESM boundary.
|
|
190
|
+
*/
|
|
191
|
+
export declare function buildDefaultExportFooter(rewrittenCode: string): string;
|
|
192
|
+
/**
|
|
193
|
+
* Detects whether a module body already declares a default export in any of
|
|
194
|
+
* the syntactic forms the ESM spec allows. Mirrors the logic in
|
|
195
|
+
* `websocket-core-bridge.ts:hasModuleDefaultExport` — kept here so the
|
|
196
|
+
* default-export bridge helper is self-contained and unit-testable.
|
|
197
|
+
*
|
|
198
|
+
* Handles:
|
|
199
|
+
* - `export default <expr>;`
|
|
200
|
+
* - `export default function|class ...`
|
|
201
|
+
* - `export { x as default };`
|
|
202
|
+
* - `export { default };` (implicit `default as default`)
|
|
203
|
+
* - `export { default as default };`
|
|
204
|
+
* - `export { foo, default };`
|
|
205
|
+
*/
|
|
206
|
+
export declare function hasExistingDefaultExport(code: string): boolean;
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CJS/ESM interop shape (Invariant D).
|
|
3
|
+
*
|
|
4
|
+
* See HMR_CORE_REALM_DETERMINISTIC_PLAN.md § "Invariant D — CJS/ESM interop shape".
|
|
5
|
+
*
|
|
6
|
+
* PROBLEM
|
|
7
|
+
* -------
|
|
8
|
+
* ESM Module Namespace Objects (ECMA §9.4.6) have [[Prototype]] = null.
|
|
9
|
+
* They do NOT inherit Object.prototype — no `hasOwnProperty`, no `toString`,
|
|
10
|
+
* no `isPrototypeOf`. CJS consumers (most notably zone.js's `patchMethod`)
|
|
11
|
+
* assume their require() result is a plain object that has those methods:
|
|
12
|
+
*
|
|
13
|
+
* let proto = target;
|
|
14
|
+
* while (proto && !proto.hasOwnProperty(name)) // ← crashes on null-proto
|
|
15
|
+
* proto = Object.getPrototypeOf(proto);
|
|
16
|
+
*
|
|
17
|
+
* `@nativescript/core/index.js` re-exports several sub-modules as namespaces:
|
|
18
|
+
*
|
|
19
|
+
* export * as Utils from './utils';
|
|
20
|
+
* export * as Http from './http';
|
|
21
|
+
* export * as Connectivity from './connectivity';
|
|
22
|
+
* export * as ApplicationSettings from './application-settings';
|
|
23
|
+
*
|
|
24
|
+
* When bundle.mjs does `import { Utils } from '@nativescript/core'`, `Utils`
|
|
25
|
+
* binds to that null-proto sub-namespace. zone.js then does
|
|
26
|
+
* `patchMethod(Utils, 'mainThreadify', …)` and crashes.
|
|
27
|
+
*
|
|
28
|
+
* WHY globalThis.require shims ALONE DON'T FIX THIS
|
|
29
|
+
* -------------------------------------------------
|
|
30
|
+
* ESM imports never pass through `globalThis.require`. They go through the
|
|
31
|
+
* host's ESM linker (V8 on iOS). Shimming `require` only intercepts the CJS
|
|
32
|
+
* path (vendor packages that use `require('@nativescript/core')`); direct
|
|
33
|
+
* ESM imports of `Utils` still see the raw null-proto namespace.
|
|
34
|
+
*
|
|
35
|
+
* THE FIX
|
|
36
|
+
* -------
|
|
37
|
+
* Rewrite `export * as X from 'Y'` at the /ns/core bridge response level:
|
|
38
|
+
*
|
|
39
|
+
* // before
|
|
40
|
+
* export * as Utils from './utils';
|
|
41
|
+
*
|
|
42
|
+
* // after
|
|
43
|
+
* import * as __ns_re_Utils__ from './utils';
|
|
44
|
+
* export const Utils = __NS_CJS_SHAPE__(__ns_re_Utils__);
|
|
45
|
+
*
|
|
46
|
+
* Now `Utils` is a plain Object (inherits Object.prototype). Zone.js's
|
|
47
|
+
* `patchMethod(Utils, ...)` succeeds. The shape function is recursive —
|
|
48
|
+
* nested namespaces get the same treatment — and identity-preserving
|
|
49
|
+
* (WeakMap-cached) so mutations stick across lookups.
|
|
50
|
+
*
|
|
51
|
+
* WHY THE SHAPE INSTALL MUST RUN IN MODULE BODY (NOT FOOTER)
|
|
52
|
+
* ---------------------------------------------------------
|
|
53
|
+
* `export const Utils = __NS_CJS_SHAPE__(__ns_re_Utils__)` is body code. It
|
|
54
|
+
* executes during module evaluation, BEFORE the registration footer. So the
|
|
55
|
+
* shape function must be installed as a body-level statement that runs
|
|
56
|
+
* before the rewritten body — we can't rely on the footer's install.
|
|
57
|
+
*
|
|
58
|
+
* Each served /ns/core module independently installs the shape function via
|
|
59
|
+
* an idempotent `|| (globalThis.__NS_CJS_SHAPE__ = ...)` assignment. The
|
|
60
|
+
* first module to evaluate wins; subsequent evaluations are no-ops. This
|
|
61
|
+
* also handles dependency-before-importer order (depth-first ESM eval):
|
|
62
|
+
* `/ns/core/utils` evaluates before `/ns/core` main, so the shape helper
|
|
63
|
+
* is in place whenever any body needs it.
|
|
64
|
+
*/
|
|
65
|
+
/**
|
|
66
|
+
* Body-code statements that idempotently install `globalThis.__NS_CJS_SHAPE__`
|
|
67
|
+
* and `globalThis.__NS_CJS_SHAPE_CACHE__`. Must execute BEFORE any transformed
|
|
68
|
+
* `export const X = __NS_CJS_SHAPE__(...)` statement in the same module.
|
|
69
|
+
*
|
|
70
|
+
* Properties:
|
|
71
|
+
* - Recursive: traverses nested namespaces so `Utils.Something` is also
|
|
72
|
+
* a plain Object (if that sub-namespace is itself null-proto).
|
|
73
|
+
* - Identity-preserving: WeakMap keyed on the underlying ESM namespace.
|
|
74
|
+
* zone.js mutates its patch target; a fresh copy per call would lose
|
|
75
|
+
* mutations. Every lookup of the same namespace returns the same
|
|
76
|
+
* shaped object.
|
|
77
|
+
* - Cycle-safe: records the output in the cache BEFORE recursing into
|
|
78
|
+
* children. Handles `core ↔ platform` style cycles.
|
|
79
|
+
* - TDZ-safe: wraps each property read in try/catch. Some exports
|
|
80
|
+
* (Angular zone.js with early-access patterns) are still in their
|
|
81
|
+
* temporal dead zone when the namespace is first snapshotted. A thrown
|
|
82
|
+
* property read is skipped rather than failing the whole shape.
|
|
83
|
+
*/
|
|
84
|
+
export function buildShapeInstallHeader() {
|
|
85
|
+
return [
|
|
86
|
+
`/* Invariant D: CJS/ESM interop shape installer */`,
|
|
87
|
+
`try { if (typeof globalThis !== 'undefined') {`,
|
|
88
|
+
` const __nsShapeCache = globalThis.__NS_CJS_SHAPE_CACHE__ || (globalThis.__NS_CJS_SHAPE_CACHE__ = new WeakMap());`,
|
|
89
|
+
` if (typeof globalThis.__NS_CJS_SHAPE__ !== 'function') {`,
|
|
90
|
+
` globalThis.__NS_CJS_SHAPE__ = function __nsShape(obj) {`,
|
|
91
|
+
` if (!obj || typeof obj !== 'object') return obj;`,
|
|
92
|
+
` let isNsModule = false;`,
|
|
93
|
+
` try { isNsModule = obj[Symbol.toStringTag] === 'Module'; } catch (e) {}`,
|
|
94
|
+
` const proto = Object.getPrototypeOf(obj);`,
|
|
95
|
+
` if (proto !== null && !isNsModule) return obj;`,
|
|
96
|
+
` if (__nsShapeCache.has(obj)) return __nsShapeCache.get(obj);`,
|
|
97
|
+
` const out = {};`,
|
|
98
|
+
` __nsShapeCache.set(obj, out);`,
|
|
99
|
+
` try {`,
|
|
100
|
+
` const keys = Object.keys(obj);`,
|
|
101
|
+
` for (let i = 0; i < keys.length; i++) {`,
|
|
102
|
+
` const k = keys[i];`,
|
|
103
|
+
` try { out[k] = __nsShape(obj[k]); } catch (e) { /* TDZ / unreadable */ }`,
|
|
104
|
+
` }`,
|
|
105
|
+
` } catch (e) {}`,
|
|
106
|
+
` return out;`,
|
|
107
|
+
` };`,
|
|
108
|
+
` }`,
|
|
109
|
+
`} } catch (e) { try { console.warn('[ns-core] shape installer failed:', (e && e.message) || e); } catch {} }`,
|
|
110
|
+
].join('\n');
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Rewrite namespace re-exports from CJS-incompatible ESM namespace form into
|
|
114
|
+
* shape-aware const exports. Handles the two forms Vite/esbuild may emit:
|
|
115
|
+
*
|
|
116
|
+
* (1) Original ES2020 syntax preserved unchanged:
|
|
117
|
+
* export * as X from 'Y';
|
|
118
|
+
*
|
|
119
|
+
* (2) Transpiled form esbuild emits when targeting older ES:
|
|
120
|
+
* import * as _foo from 'Y';
|
|
121
|
+
* export { _foo as X };
|
|
122
|
+
*
|
|
123
|
+
* Both become:
|
|
124
|
+
* import * as __ns_re_X__ from 'Y';
|
|
125
|
+
* export const X = __NS_CJS_SHAPE__(__ns_re_X__);
|
|
126
|
+
*
|
|
127
|
+
* Notes on the transform:
|
|
128
|
+
* - The internal binding uses a derived name (`__ns_re_<X>__`) so
|
|
129
|
+
* collisions with user bindings are extremely unlikely. Core doesn't
|
|
130
|
+
* declare identifiers matching that shape.
|
|
131
|
+
* - `__NS_CJS_SHAPE__` is read from globalThis with a defensive fallback
|
|
132
|
+
* (identity function) in case installation failed or runs in an
|
|
133
|
+
* environment where it hasn't been initialized yet. This makes the
|
|
134
|
+
* transform safe to apply to modules that don't always go through the
|
|
135
|
+
* /ns/core handler's headers.
|
|
136
|
+
* - We do NOT shape `export *` (star re-exports without alias). Those
|
|
137
|
+
* spread individual named exports — they don't create a nested
|
|
138
|
+
* namespace and aren't affected by the null-proto issue.
|
|
139
|
+
* - We do NOT shape `export { x } from 'y'` forms. Those copy individual
|
|
140
|
+
* named exports directly; the consumer never sees the namespace.
|
|
141
|
+
*
|
|
142
|
+
* The regex is intentionally precise. It matches only the `export * as X from
|
|
143
|
+
* 'Y'` syntactic form (and its transpiled equivalent) — no accidental matches
|
|
144
|
+
* in string literals or comments because those can't start a top-level
|
|
145
|
+
* statement with `export`.
|
|
146
|
+
*/
|
|
147
|
+
export function rewriteNamespaceReExportsForShape(code) {
|
|
148
|
+
const shapeExpr = `(typeof globalThis.__NS_CJS_SHAPE__ === 'function' ? globalThis.__NS_CJS_SHAPE__ : function (x) { return x; })`;
|
|
149
|
+
// Form (1): export * as X from 'Y'; (ES2020 aggregate re-export)
|
|
150
|
+
let out = code.replace(/(^|\n)(\s*)export\s+\*\s+as\s+([A-Za-z_$][\w$]*)\s+from\s+(['"`])([^'"`\n]+)\4\s*;?/g, (_match, prefix, indent, name, quote, spec) => {
|
|
151
|
+
const internal = `__ns_re_${name}__`;
|
|
152
|
+
return `${prefix}${indent}import * as ${internal} from ${quote}${spec}${quote};\n${indent}export const ${name} = ${shapeExpr}(${internal});`;
|
|
153
|
+
});
|
|
154
|
+
// Form (2): import * as _x from 'Y'; export { _x as X };
|
|
155
|
+
// This pattern appears when esbuild targets older ES. We detect the pair
|
|
156
|
+
// by matching the import and its paired export on the same or adjacent
|
|
157
|
+
// lines. Rather than try to join two regex passes, we transform the
|
|
158
|
+
// `export { _x as X }` branch by replacing the `_x` in the export with a
|
|
159
|
+
// shape-wrapped const. Keeping the import intact is safe because the
|
|
160
|
+
// local binding is still needed for the shape call.
|
|
161
|
+
out = out.replace(/export\s*\{\s*([A-Za-z_$][\w$]*)\s+as\s+([A-Za-z_$][\w$]*)\s*\}\s*;?/g, (match, localName, exportName) => {
|
|
162
|
+
// Only transform if the local name looks like an esbuild-generated
|
|
163
|
+
// namespace binding (leading underscore) AND the export name is a
|
|
164
|
+
// PascalCase identifier. This avoids touching user re-exports.
|
|
165
|
+
if (!/^_/.test(localName))
|
|
166
|
+
return match;
|
|
167
|
+
if (!/^[A-Z]/.test(exportName))
|
|
168
|
+
return match;
|
|
169
|
+
return `export const ${exportName} = ${shapeExpr}(${localName});`;
|
|
170
|
+
});
|
|
171
|
+
return out;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Convenience: returns true if the given code appears to contain at least one
|
|
175
|
+
* namespace re-export that would benefit from the shape transform. Used to
|
|
176
|
+
* skip the regex replace for modules that don't need it (keeps the response
|
|
177
|
+
* identical for most submodules, which just have plain `export * from` or
|
|
178
|
+
* named exports).
|
|
179
|
+
*/
|
|
180
|
+
export function hasNamespaceReExport(code) {
|
|
181
|
+
return /export\s+\*\s+as\s+[A-Za-z_$][\w$]*\s+from\s+['"`]/.test(code);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Default-export bridge footer (Invariant D, round 3).
|
|
185
|
+
*
|
|
186
|
+
* BACKGROUND
|
|
187
|
+
* ----------
|
|
188
|
+
* Upstream consumer rewrites in the /ns/m handler convert
|
|
189
|
+
* `import { X } from '@nativescript/core'` into
|
|
190
|
+
* `import __ns_core_ns from '/ns/core'; const { X } = __ns_core_ns;`
|
|
191
|
+
* — a DEFAULT import followed by destructuring. The inline comment on that
|
|
192
|
+
* rewrite says explicitly: "This makes `import { Frame } from
|
|
193
|
+
* '@nativescript/core'` work even if the bridge provides only a default
|
|
194
|
+
* export."
|
|
195
|
+
*
|
|
196
|
+
* The original `@nativescript/core/index.js` has no `export default` — all
|
|
197
|
+
* named exports. Without this footer, consumers of the transformed import
|
|
198
|
+
* fail at ESM link time:
|
|
199
|
+
*
|
|
200
|
+
* SyntaxError: The requested module '/ns/core/<ver>' does not
|
|
201
|
+
* provide an export named 'default'
|
|
202
|
+
*
|
|
203
|
+
* THE FIX
|
|
204
|
+
* -------
|
|
205
|
+
* Emit `export default __ns_core_self_ns__;` at the end of the served
|
|
206
|
+
* module body. `__ns_core_self_ns__` is the namespace import the server
|
|
207
|
+
* already emits at the top of every /ns/core response for self-
|
|
208
|
+
* registration purposes. ESM spec guarantees a module re-entering itself
|
|
209
|
+
* during evaluation returns the in-progress namespace — so no extra
|
|
210
|
+
* evaluation occurs, and the default binding receives an object whose
|
|
211
|
+
* properties are every named export of the same module.
|
|
212
|
+
*
|
|
213
|
+
* CONSUMER MATRIX AFTER THIS FIX
|
|
214
|
+
* ------------------------------
|
|
215
|
+
* • `import X from '/ns/core'` (default) → X = self namespace
|
|
216
|
+
* • `import * as X from '/ns/core'` → X = self namespace + X.default = self
|
|
217
|
+
* • `import { X } from '/ns/core'` (named) → individual named binding (untouched)
|
|
218
|
+
* • `const { X } = <default-import>` → destructure from self namespace
|
|
219
|
+
* • `require('@nativescript/core')` (CJS) → shaped self via registry
|
|
220
|
+
*
|
|
221
|
+
* SKIP CONDITIONS
|
|
222
|
+
* ---------------
|
|
223
|
+
* Returns empty string when the source already declares a default export
|
|
224
|
+
* (some deep subpaths do — e.g. a file that does `export default MyClass`
|
|
225
|
+
* would SyntaxError on a duplicate). The consumer rewrite is also gated
|
|
226
|
+
* on `isDeepCoreSubpath`, so those paths don't normally hit the default-
|
|
227
|
+
* import pattern anyway.
|
|
228
|
+
*
|
|
229
|
+
* WHY THE DEFAULT ISN'T SHAPED
|
|
230
|
+
* ----------------------------
|
|
231
|
+
* The default is the raw null-proto MNO. This is intentional:
|
|
232
|
+
*
|
|
233
|
+
* 1. All current default-import consumers either destructure or do
|
|
234
|
+
* property access. Neither requires Object.prototype in the chain.
|
|
235
|
+
* 2. Shaping the default inside the module body would require calling
|
|
236
|
+
* __NS_CJS_SHAPE__ on a namespace whose own `default` slot is still
|
|
237
|
+
* being initialized — temporal dead zone hazard.
|
|
238
|
+
* 3. CJS consumers that need `.hasOwnProperty` go through the registry
|
|
239
|
+
* (shaped at registration), not through the default import.
|
|
240
|
+
*
|
|
241
|
+
* If a future consumer surfaces that calls `.hasOwnProperty` on the
|
|
242
|
+
* default, we can revisit. The current rule-of-least-surprise is:
|
|
243
|
+
* shape at the CJS boundary, not at the ESM boundary.
|
|
244
|
+
*/
|
|
245
|
+
export function buildDefaultExportFooter(rewrittenCode) {
|
|
246
|
+
if (hasExistingDefaultExport(rewrittenCode))
|
|
247
|
+
return '';
|
|
248
|
+
return ['/* Invariant D: default export bridge — see HMR_CORE_REALM_DETERMINISTIC_PLAN.md */', 'export default __ns_core_self_ns__;'].join('\n');
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* Detects whether a module body already declares a default export in any of
|
|
252
|
+
* the syntactic forms the ESM spec allows. Mirrors the logic in
|
|
253
|
+
* `websocket-core-bridge.ts:hasModuleDefaultExport` — kept here so the
|
|
254
|
+
* default-export bridge helper is self-contained and unit-testable.
|
|
255
|
+
*
|
|
256
|
+
* Handles:
|
|
257
|
+
* - `export default <expr>;`
|
|
258
|
+
* - `export default function|class ...`
|
|
259
|
+
* - `export { x as default };`
|
|
260
|
+
* - `export { default };` (implicit `default as default`)
|
|
261
|
+
* - `export { default as default };`
|
|
262
|
+
* - `export { foo, default };`
|
|
263
|
+
*/
|
|
264
|
+
export function hasExistingDefaultExport(code) {
|
|
265
|
+
if (!code || typeof code !== 'string')
|
|
266
|
+
return false;
|
|
267
|
+
if (/\bexport\s+default\b/.test(code))
|
|
268
|
+
return true;
|
|
269
|
+
if (/\bexport\s*\{[^}]*\bdefault\b[^}]*\}/.test(code))
|
|
270
|
+
return true;
|
|
271
|
+
return false;
|
|
272
|
+
}
|
|
273
|
+
//# sourceMappingURL=ns-core-cjs-shape.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ns-core-cjs-shape.js","sourceRoot":"","sources":["../../../../../packages/vite/hmr/server/ns-core-cjs-shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AAEH;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,UAAU,uBAAuB;IACtC,OAAO;QACN,oDAAoD;QACpD,gDAAgD;QAChD,oHAAoH;QACpH,4DAA4D;QAC5D,6DAA6D;QAC7D,wDAAwD;QACxD,+BAA+B;QAC/B,+EAA+E;QAC/E,iDAAiD;QACjD,sDAAsD;QACtD,oEAAoE;QACpE,uBAAuB;QACvB,qCAAqC;QACrC,aAAa;QACb,wCAAwC;QACxC,iDAAiD;QACjD,8BAA8B;QAC9B,oFAAoF;QACpF,WAAW;QACX,sBAAsB;QACtB,mBAAmB;QACnB,QAAQ;QACR,KAAK;QACL,8GAA8G;KAC9G,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACd,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAkCG;AACH,MAAM,UAAU,iCAAiC,CAAC,IAAY;IAC7D,MAAM,SAAS,GAAG,gHAAgH,CAAC;IAEnI,kEAAkE;IAClE,IAAI,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,sFAAsF,EAAE,CAAC,MAAM,EAAE,MAAc,EAAE,MAAc,EAAE,IAAY,EAAE,KAAa,EAAE,IAAY,EAAE,EAAE;QACpM,MAAM,QAAQ,GAAG,WAAW,IAAI,IAAI,CAAC;QACrC,OAAO,GAAG,MAAM,GAAG,MAAM,eAAe,QAAQ,SAAS,KAAK,GAAG,IAAI,GAAG,KAAK,MAAM,MAAM,gBAAgB,IAAI,MAAM,SAAS,IAAI,QAAQ,IAAI,CAAC;IAC9I,CAAC,CAAC,CAAC;IAEH,yDAAyD;IACzD,yEAAyE;IACzE,uEAAuE;IACvE,oEAAoE;IACpE,yEAAyE;IACzE,qEAAqE;IACrE,oDAAoD;IACpD,GAAG,GAAG,GAAG,CAAC,OAAO,CAAC,uEAAuE,EAAE,CAAC,KAAK,EAAE,SAAiB,EAAE,UAAkB,EAAE,EAAE;QAC3I,mEAAmE;QACnE,kEAAkE;QAClE,+DAA+D;QAC/D,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;YAAE,OAAO,KAAK,CAAC;QACxC,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,UAAU,CAAC;YAAE,OAAO,KAAK,CAAC;QAC7C,OAAO,gBAAgB,UAAU,MAAM,SAAS,IAAI,SAAS,IAAI,CAAC;IACnE,CAAC,CAAC,CAAC;IAEH,OAAO,GAAG,CAAC;AACZ,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAAY;IAChD,OAAO,oDAAoD,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxE,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6DG;AACH,MAAM,UAAU,wBAAwB,CAAC,aAAqB;IAC7D,IAAI,wBAAwB,CAAC,aAAa,CAAC;QAAE,OAAO,EAAE,CAAC;IACvD,OAAO,CAAC,qFAAqF,EAAE,qCAAqC,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AAClJ,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,wBAAwB,CAAC,IAAY;IACpD,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ;QAAE,OAAO,KAAK,CAAC;IACpD,IAAI,sBAAsB,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACnD,IAAI,sCAAsC,CAAC,IAAI,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAC;IACnE,OAAO,KAAK,CAAC;AACd,CAAC"}
|
|
@@ -18,6 +18,4 @@ export declare function collectStaticExportOriginsFromFile(modulePath: string, r
|
|
|
18
18
|
export declare function normalizeCoreExportOriginsForRuntime(exportOrigins: Record<string, CoreExportOrigin[]>, resolveModuleId: (moduleId: string) => Promise<string | null> | string | null, rootModulePath: string): Promise<Record<string, CoreExportOrigin[]>>;
|
|
19
19
|
export declare function ensureVersionedCoreImports(code: string, _origin: string, ver: number): string;
|
|
20
20
|
export declare function hasModuleDefaultExport(moduleCode: string): boolean;
|
|
21
|
-
export declare function buildVersionedCoreSubpathAliasModule(sub: string, ver: number | string, namedExports?: string[], hasDefaultExport?: boolean): string;
|
|
22
|
-
export declare function buildVersionedCoreMainBridgeModule(key: string, ver: number | string, namedExports?: string[], exportOrigins?: Record<string, CoreExportOrigin[]>): string;
|
|
23
21
|
export declare function parseCoreBridgeRequest(pathname: string, searchParams: URLSearchParams, currentGraphVersion: number): ParsedCoreBridgeRequest | null;
|