@gjsify/nativescript-vite 0.11.0 → 0.12.0
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/lib/index.d.ts +25 -1
- package/lib/index.js +379 -34
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -31,11 +31,35 @@ export type UserConfigInput = UserConfig | ((env: ConfigEnv) => UserConfig | Pro
|
|
|
31
31
|
*/
|
|
32
32
|
export declare function defineNativescriptConfig(options?: GjsifyNativescriptViteOptions, userConfig?: UserConfigInput): (env: ConfigEnv) => Promise<UserConfig>;
|
|
33
33
|
export default defineNativescriptConfig;
|
|
34
|
+
/**
|
|
35
|
+
* Detect the installed `@nativescript/vite` major version. `@nativescript/vite` is
|
|
36
|
+
* an OPTIONAL peer (not installed in this repo), so detection can fail — in which
|
|
37
|
+
* case we return `undefined` and the caller assumes the patches are still needed
|
|
38
|
+
* (fail-safe to the historical full-patch behavior).
|
|
39
|
+
*
|
|
40
|
+
* Reads the package root's `package.json` (located WITHOUT relying on the
|
|
41
|
+
* exports-gated `/package.json` subpath, see `resolveNativescriptViteRoot`) and
|
|
42
|
+
* parses its `version`. Any failure (unresolvable peer, unreadable/unparseable
|
|
43
|
+
* version) is swallowed → `undefined`.
|
|
44
|
+
*/
|
|
45
|
+
export declare function detectNativescriptViteMajor(): number | undefined;
|
|
34
46
|
/**
|
|
35
47
|
* Apply the Vite 8 / Rolldown + type-check fixes to a returned `@nativescript/vite`
|
|
36
48
|
* config: drop function-replacement aliases, the explicit `@rollup/plugin-commonjs`,
|
|
37
49
|
* and the vite-side `ns-typescript-check` (gjsify type-checks via `gjsify tsc`).
|
|
38
50
|
* Returns a shallow copy with the fixed `resolve` and `plugins` — the input is not
|
|
39
51
|
* mutated.
|
|
52
|
+
*
|
|
53
|
+
* CONDITIONAL on the installed `@nativescript/vite` major:
|
|
54
|
+
* - `>= 8` (the Vite-8 + Rolldown + HMR rewrite): SKIP the function-alias drop
|
|
55
|
+
* and the `@rollup/plugin-commonjs` strip — upstream already ships native
|
|
56
|
+
* `rolldownOptions`, string-only aliases and no explicit commonjs plugin, so
|
|
57
|
+
* those fixes are no-ops there. The `ns-typescript-check` strip is still
|
|
58
|
+
* applied (no-op-if-absent). Emits a one-time informational notice.
|
|
59
|
+
* - `<= 2` / UNKNOWN (unresolvable peer): apply the FULL fix set, exactly as
|
|
60
|
+
* before — the teapot showcase on `@nativescript/vite@2.0.3` must keep building.
|
|
61
|
+
*
|
|
62
|
+
* The optional `nsViteMajor` arg overrides the auto-detected major (used by the
|
|
63
|
+
* e2e fixtures to exercise both branches without two installs).
|
|
40
64
|
*/
|
|
41
|
-
export declare function applyVite8Fixes(config: UserConfig): UserConfig;
|
|
65
|
+
export declare function applyVite8Fixes(config: UserConfig, nsViteMajor?: number): UserConfig;
|
package/lib/index.js
CHANGED
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
// `@gjsify/nativescript-vite` — Vite 8 / Rolldown compatibility + gjsify
|
|
2
2
|
// transforms for building NativeScript apps.
|
|
3
3
|
//
|
|
4
|
-
// `@nativescript/vite` (the upstream NativeScript Vite integration
|
|
5
|
-
// build under Vite 8 / Rolldown: its config uses two
|
|
6
|
-
// rejects. This package COMPOSES the upstream config and
|
|
7
|
-
//
|
|
8
|
-
// on top — so a NativeScript app builds under Vite 8
|
|
4
|
+
// `@nativescript/vite@2.x` (the upstream NativeScript Vite integration, the
|
|
5
|
+
// Vite-7-era line) does not build under Vite 8 / Rolldown: its config uses two
|
|
6
|
+
// constructs Rolldown rejects. This package COMPOSES the upstream config and
|
|
7
|
+
// fixes exactly those pieces on the returned config object, then layers
|
|
8
|
+
// gjsify's NS transforms on top — so a NativeScript app builds under Vite 8
|
|
9
|
+
// with `gjsify`'s stack.
|
|
9
10
|
//
|
|
10
|
-
// The
|
|
11
|
+
// The upstream incompatibilities (verified by running real builds against
|
|
12
|
+
// `@nativescript/vite@2.0.3`):
|
|
11
13
|
// 1. Function-replacement `resolve.alias` entries (platform-`main` + tsconfig
|
|
12
14
|
// wildcard + `@nativescript/core/.../index` canonicalization) →
|
|
13
15
|
// `Failed to convert builtin plugin 'ViteAlias' … function replacement
|
|
@@ -17,6 +19,24 @@
|
|
|
17
19
|
// 2. The explicit `@rollup/plugin-commonjs` plugin → `Cannot read properties
|
|
18
20
|
// of undefined (reading 'currentLoadingModule')`. It is DROPPED — Rolldown
|
|
19
21
|
// handles CommonJS (`@nativescript/core`'s modules) natively.
|
|
22
|
+
// 3. The vite-side `ns-typescript-check` plugin → a bundler should bundle,
|
|
23
|
+
// not type-check; gjsify defers the authoritative type gate to
|
|
24
|
+
// `gjsify tsc`. It is DROPPED (the strip is no-op-if-absent).
|
|
25
|
+
//
|
|
26
|
+
// CONDITIONAL on the installed `@nativescript/vite` major (`detectNativescriptViteMajor()`):
|
|
27
|
+
// - major <= 2 (or UNKNOWN/unresolvable — fail-safe to "patches still needed"):
|
|
28
|
+
// apply the FULL fix set above, exactly as before. The teapot showcase on
|
|
29
|
+
// `@nativescript/vite@2.0.3` must keep building.
|
|
30
|
+
// - major >= 8: upstream `@nativescript/vite@8.x` is a ground-up Vite-8 +
|
|
31
|
+
// Rolldown + HMR rewrite that ALREADY ships native `rolldownOptions`,
|
|
32
|
+
// string-only aliases and no explicit `@rollup/plugin-commonjs`, so fixes
|
|
33
|
+
// (1) and (2) are no-ops there — they are SKIPPED (the intent is explicit;
|
|
34
|
+
// the strips were already no-op-if-absent). Fix (3) is still applied
|
|
35
|
+
// (no-op-if-absent regardless of line). A one-time informational log notes
|
|
36
|
+
// that 8.x handles Vite-8/Rolldown natively so minimal patching is applied.
|
|
37
|
+
//
|
|
38
|
+
// Every strip is idempotent + no-op-if-absent, so the patching stays safe on
|
|
39
|
+
// either line regardless of the detected major.
|
|
20
40
|
//
|
|
21
41
|
// On top, it spreads `@gjsify/vite-plugin-gjsify`'s `gjsifyNativescript()`
|
|
22
42
|
// preset (gi:// → empty, platform file resolution, platform defines, the
|
|
@@ -25,6 +45,10 @@
|
|
|
25
45
|
// `@nativescript/vite`, `@nativescript/core`, the `nativescript` CLI, the
|
|
26
46
|
// optional `@nativescript/canvas*` plugins and `vite` are all OPTIONAL peer
|
|
27
47
|
// dependencies — installed by the consumer only when targeting NativeScript.
|
|
48
|
+
import { existsSync, readdirSync, readFileSync, statSync } from 'node:fs';
|
|
49
|
+
import module, { createRequire } from 'node:module';
|
|
50
|
+
import { dirname, join } from 'node:path';
|
|
51
|
+
import { fileURLToPath } from 'node:url';
|
|
28
52
|
import { mergeConfig } from 'vite';
|
|
29
53
|
import { gjsifyNativescript as gjsifyNativescriptTransforms } from '@gjsify/vite-plugin-gjsify';
|
|
30
54
|
/**
|
|
@@ -78,6 +102,19 @@ export default defineNativescriptConfig;
|
|
|
78
102
|
* actionable error if neither resolves or the export is missing.
|
|
79
103
|
*/
|
|
80
104
|
async function loadUpstreamConfig(env) {
|
|
105
|
+
// `@nativescript/vite@8.x`'s config chain (`configuration/base.js` → the HMR
|
|
106
|
+
// server → the per-framework strategies) STATICALLY imports the framework
|
|
107
|
+
// compilers (`@vue/compiler-sfc`, `react-reconciler`, `vite-plugin-solid`,
|
|
108
|
+
// `@vitejs/plugin-vue{,-jsx}`, `vue-tsc`, `@analogjs/vite-plugin-angular`,
|
|
109
|
+
// `@angular/build`) at module-eval. They are upstream `peerDependencies`, so a
|
|
110
|
+
// framework-LESS NativeScript-Core app (no Vue/Solid/React/Angular) cannot even
|
|
111
|
+
// `import('@nativescript/vite')` — Node throws a cryptic
|
|
112
|
+
// `ERR_MODULE_NOT_FOUND: Cannot find package '@vue/compiler-sfc'`. Stub the
|
|
113
|
+
// ones the project did NOT install with `@gjsify/empty`-shaped no-op modules so
|
|
114
|
+
// a Core app loads the config. Must run BEFORE the dynamic import below —
|
|
115
|
+
// `resolve.alias` (a Vite config field) would be far too late: the eager import
|
|
116
|
+
// is executed by Node's ESM loader the moment we `import()` the config module.
|
|
117
|
+
stubMissingFrameworkPeers();
|
|
81
118
|
// String-variable specifiers: `@nativescript/vite` is an OPTIONAL peer, not
|
|
82
119
|
// installed in this repo, so a literal `import('@nativescript/vite')` would
|
|
83
120
|
// fail type resolution. Non-literal specifiers resolve at runtime only (the
|
|
@@ -104,44 +141,352 @@ async function loadUpstreamConfig(env) {
|
|
|
104
141
|
throw new Error('@gjsify/nativescript-vite requires the optional peer "@nativescript/vite" exporting `typescriptConfig` ' +
|
|
105
142
|
'(install it alongside @nativescript/core in your NativeScript app).', { cause: lastError });
|
|
106
143
|
}
|
|
144
|
+
/** One-time guard so the missing-framework-peer stub notice prints at most once per process. */
|
|
145
|
+
let stubbedPeersNoticeShown = false;
|
|
146
|
+
/** Idempotency guard: register the Node resolve/load hooks at most once per process. */
|
|
147
|
+
let frameworkPeerHooksRegistered = false;
|
|
148
|
+
/**
|
|
149
|
+
* Read `@nativescript/vite`'s declared framework `peerDependencies`. These are the
|
|
150
|
+
* Vue/Solid/React/Angular compilers (`@vue/compiler-sfc`, `react-reconciler`,
|
|
151
|
+
* `vite-plugin-solid`, `@vitejs/plugin-vue{,-jsx}`, `vue-tsc`,
|
|
152
|
+
* `@analogjs/vite-plugin-angular`, `@angular/build`) that the 8.x config chain
|
|
153
|
+
* STATICALLY imports at module-eval. Returns `[]` when the package can't be
|
|
154
|
+
* located (optional peer absent → nothing to stub, the upstream import will fail
|
|
155
|
+
* with its own clear error).
|
|
156
|
+
*/
|
|
157
|
+
function nativescriptViteFrameworkPeers() {
|
|
158
|
+
const root = resolveNativescriptViteRoot();
|
|
159
|
+
if (root === undefined)
|
|
160
|
+
return [];
|
|
161
|
+
try {
|
|
162
|
+
const pkg = JSON.parse(readFileSync(join(root, 'package.json'), 'utf8'));
|
|
163
|
+
return Object.keys(pkg.peerDependencies ?? {});
|
|
164
|
+
}
|
|
165
|
+
catch {
|
|
166
|
+
return [];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Find every framework `peerDependency` of `@nativescript/vite` that the consuming
|
|
171
|
+
* project did NOT install, resolved relative to `@nativescript/vite`'s own location
|
|
172
|
+
* (peers resolve from the package that declares them) with a `process.cwd()`
|
|
173
|
+
* fallback. NEVER stubs a framework the app actually has — only the absent ones.
|
|
174
|
+
*/
|
|
175
|
+
function missingFrameworkPeers() {
|
|
176
|
+
const root = resolveNativescriptViteRoot();
|
|
177
|
+
if (root === undefined)
|
|
178
|
+
return [];
|
|
179
|
+
const require = createRequire(import.meta.url);
|
|
180
|
+
const paths = [root, process.cwd()];
|
|
181
|
+
return nativescriptViteFrameworkPeers().filter((peer) => {
|
|
182
|
+
try {
|
|
183
|
+
require.resolve(peer, { paths });
|
|
184
|
+
return false; // resolvable → installed → keep it
|
|
185
|
+
}
|
|
186
|
+
catch {
|
|
187
|
+
return true; // unresolvable → missing → stub it
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
/** Synthetic-module URL scheme for a stubbed missing framework peer. */
|
|
192
|
+
const STUB_PEER_SCHEME = 'gjsify-ns-empty-peer:';
|
|
193
|
+
/**
|
|
194
|
+
* Scan `@nativescript/vite`'s on-disk source for the NAMED imports of `spec`
|
|
195
|
+
* (`import { parse, compileScript } from '<spec>'`). ESM validates named bindings
|
|
196
|
+
* at link time, so a default-only stub (like `@gjsify/empty`) cannot satisfy
|
|
197
|
+
* `import { compileScript } from '@vue/compiler-sfc'` — the generated stub must
|
|
198
|
+
* declare exactly those names. Version-agnostic: reads whatever the installed
|
|
199
|
+
* `@nativescript/vite` actually imports.
|
|
200
|
+
*/
|
|
201
|
+
function collectNamedImports(root, spec) {
|
|
202
|
+
const names = new Set();
|
|
203
|
+
const escaped = spec.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
|
204
|
+
const re = new RegExp(`import\\s*\\{([^}]*)\\}\\s*from\\s*["']${escaped}["']`, 'g');
|
|
205
|
+
const walk = (dir) => {
|
|
206
|
+
let entries;
|
|
207
|
+
try {
|
|
208
|
+
entries = readdirSync(dir);
|
|
209
|
+
}
|
|
210
|
+
catch {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
for (const name of entries) {
|
|
214
|
+
const p = join(dir, name);
|
|
215
|
+
let isDir;
|
|
216
|
+
try {
|
|
217
|
+
isDir = statSync(p).isDirectory();
|
|
218
|
+
}
|
|
219
|
+
catch {
|
|
220
|
+
continue;
|
|
221
|
+
}
|
|
222
|
+
if (isDir) {
|
|
223
|
+
if (name !== 'node_modules')
|
|
224
|
+
walk(p);
|
|
225
|
+
}
|
|
226
|
+
else if (p.endsWith('.js') || p.endsWith('.mjs')) {
|
|
227
|
+
let code;
|
|
228
|
+
try {
|
|
229
|
+
code = readFileSync(p, 'utf8');
|
|
230
|
+
}
|
|
231
|
+
catch {
|
|
232
|
+
continue;
|
|
233
|
+
}
|
|
234
|
+
let m;
|
|
235
|
+
re.lastIndex = 0;
|
|
236
|
+
while ((m = re.exec(code)) !== null) {
|
|
237
|
+
for (const raw of m[1].split(',')) {
|
|
238
|
+
const imported = raw.trim().split(/\s+as\s+/)[0]?.trim();
|
|
239
|
+
if (imported && /^[A-Za-z_$][\w$]*$/.test(imported))
|
|
240
|
+
names.add(imported);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
walk(root);
|
|
247
|
+
return [...names];
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* Make a framework-LESS NativeScript-Core app able to load `@nativescript/vite@8.x`'s
|
|
251
|
+
* config: stub the framework compiler `peerDependencies` the project did not install
|
|
252
|
+
* with no-op modules, so the chain's eager `import '@vue/compiler-sfc'` (etc.)
|
|
253
|
+
* resolve to empty stubs instead of throwing `ERR_MODULE_NOT_FOUND`.
|
|
254
|
+
*
|
|
255
|
+
* Uses Node's synchronous module hooks (`module.registerHooks`, Node 22.15+/24):
|
|
256
|
+
* - a `resolve` hook redirects each missing peer specifier to a synthetic stub URL;
|
|
257
|
+
* - a `load` hook returns ESM source exporting a no-op default Proxy PLUS a named
|
|
258
|
+
* export (also the Proxy) for every name `@nativescript/vite` imports from that
|
|
259
|
+
* peer — satisfying namespace / default / named imports of any shape.
|
|
260
|
+
*
|
|
261
|
+
* Idempotent (registers at most once) and a no-op when no framework peer is missing
|
|
262
|
+
* (a Vue/Solid/React/Angular app keeps its real compilers). Falls back gracefully —
|
|
263
|
+
* if `registerHooks` is unavailable (older Node) it logs the actionable
|
|
264
|
+
* `npm install` hint and lets the upstream import surface its own error.
|
|
265
|
+
*/
|
|
266
|
+
function stubMissingFrameworkPeers() {
|
|
267
|
+
if (frameworkPeerHooksRegistered)
|
|
268
|
+
return;
|
|
269
|
+
const missing = missingFrameworkPeers();
|
|
270
|
+
if (missing.length === 0)
|
|
271
|
+
return;
|
|
272
|
+
const root = resolveNativescriptViteRoot();
|
|
273
|
+
const registerHooks = module.registerHooks;
|
|
274
|
+
if (root === undefined || typeof registerHooks !== 'function') {
|
|
275
|
+
// Can't intercept — give the user the exact remedy instead of the raw
|
|
276
|
+
// `ERR_MODULE_NOT_FOUND` they'd otherwise hit at config-load.
|
|
277
|
+
console.warn(`[@gjsify/nativescript-vite] @nativescript/vite eagerly imports framework compilers that are not ` +
|
|
278
|
+
`installed and they cannot be stubbed on this Node version. Install the ones your app uses, ` +
|
|
279
|
+
`or all of them to unblock the build:\n npm install -D ${missing.join(' ')}`);
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
frameworkPeerHooksRegistered = true;
|
|
283
|
+
const missingSet = new Set(missing);
|
|
284
|
+
// Pre-compute the named-export superset per missing peer so the load hook stays
|
|
285
|
+
// synchronous and cheap (the scan reads the upstream source once).
|
|
286
|
+
const namedExports = new Map();
|
|
287
|
+
for (const peer of missing)
|
|
288
|
+
namedExports.set(peer, collectNamedImports(root, peer));
|
|
289
|
+
if (!stubbedPeersNoticeShown) {
|
|
290
|
+
stubbedPeersNoticeShown = true;
|
|
291
|
+
// stderr (console.warn) — a build diagnostic must not pollute stdout.
|
|
292
|
+
console.warn(`[@gjsify/nativescript-vite] @nativescript/vite declares framework compilers as peerDependencies and ` +
|
|
293
|
+
`imports them eagerly at config-load; stubbing the ${missing.length} not installed in this project ` +
|
|
294
|
+
`with no-op modules so a framework-less NativeScript-Core app can load the config: ${missing.join(', ')}. ` +
|
|
295
|
+
`(Install a framework's compiler to use it — only the absent ones are stubbed.)`);
|
|
296
|
+
}
|
|
297
|
+
registerHooks({
|
|
298
|
+
resolve(specifier, context, nextResolve) {
|
|
299
|
+
if (missingSet.has(specifier)) {
|
|
300
|
+
return { url: STUB_PEER_SCHEME + encodeURIComponent(specifier), shortCircuit: true };
|
|
301
|
+
}
|
|
302
|
+
return nextResolve(specifier, context);
|
|
303
|
+
},
|
|
304
|
+
load(url, context, nextLoad) {
|
|
305
|
+
if (url.startsWith(STUB_PEER_SCHEME)) {
|
|
306
|
+
const spec = decodeURIComponent(url.slice(STUB_PEER_SCHEME.length));
|
|
307
|
+
const named = namedExports.get(spec) ?? [];
|
|
308
|
+
let source = 'const noop = () => {};\n' +
|
|
309
|
+
'const stub = new Proxy(noop, { get: (t, p) => (p in t ? t[p] : noop) });\n' +
|
|
310
|
+
'export default stub;\n';
|
|
311
|
+
for (const name of named)
|
|
312
|
+
source += `export const ${name} = stub;\n`;
|
|
313
|
+
return { format: 'module', source, shortCircuit: true };
|
|
314
|
+
}
|
|
315
|
+
return nextLoad(url, context);
|
|
316
|
+
},
|
|
317
|
+
});
|
|
318
|
+
}
|
|
107
319
|
/** Upstream `@nativescript/vite` TypeScript-check plugin name (see `helpers/typescript-check.js`). */
|
|
108
320
|
const TS_CHECK_PLUGIN = 'ns-typescript-check';
|
|
321
|
+
/** First `@nativescript/vite` major that ships native Vite-8 / Rolldown support. */
|
|
322
|
+
const NS_VITE_NATIVE_VITE8_MAJOR = 8;
|
|
323
|
+
/** One-time guard so the "8.x handles Vite-8 natively" notice prints at most once per process. */
|
|
324
|
+
let nativeViteNoticeShown = false;
|
|
325
|
+
/**
|
|
326
|
+
* Locate the on-disk `@nativescript/vite` package root (the directory holding its
|
|
327
|
+
* `package.json`), or `undefined` when the optional peer is not installed.
|
|
328
|
+
*
|
|
329
|
+
* It must NOT resolve the `@nativescript/vite/package.json` SUBPATH directly:
|
|
330
|
+
* `@nativescript/vite@8.x`'s `package.json#exports` map does not expose
|
|
331
|
+
* `./package.json`, so `createRequire(...).resolve('@nativescript/vite/package.json')`
|
|
332
|
+
* (and `import.meta.resolve`) throw `ERR_PACKAGE_PATH_NOT_EXPORTED` — the bug that
|
|
333
|
+
* silently broke major detection on the 8.x line (the gate never fired, the full
|
|
334
|
+
* legacy patch set ran). Instead resolve the package's MAIN entry (always exported)
|
|
335
|
+
* and walk up to the nearest ancestor `package.json` whose `name` is
|
|
336
|
+
* `@nativescript/vite`. This is robust for both modern `exports`-only packages and
|
|
337
|
+
* classic ones.
|
|
338
|
+
*/
|
|
339
|
+
function resolveNativescriptViteRoot() {
|
|
340
|
+
const specifier = '@nativescript/vite';
|
|
341
|
+
const require = createRequire(import.meta.url);
|
|
342
|
+
// Resolve from the consumer's project root (`process.cwd()` — where Vite runs
|
|
343
|
+
// the config) FIRST, then relative to this lib's own location. In a flat npm
|
|
344
|
+
// install both find the same copy; resolving from the project root first is the
|
|
345
|
+
// honest source of truth (the app's installed `@nativescript/vite` is the one
|
|
346
|
+
// its config will load) and handles pnpm/nested layouts where the lib's
|
|
347
|
+
// `node_modules` differs from the project's.
|
|
348
|
+
let entry;
|
|
349
|
+
try {
|
|
350
|
+
entry = require.resolve(specifier, { paths: [process.cwd(), dirname(fileURLToPath(import.meta.url))] });
|
|
351
|
+
}
|
|
352
|
+
catch {
|
|
353
|
+
// fall through to import.meta.resolve (location-relative)
|
|
354
|
+
}
|
|
355
|
+
if (entry === undefined) {
|
|
356
|
+
const metaResolve = import.meta.resolve;
|
|
357
|
+
if (typeof metaResolve === 'function') {
|
|
358
|
+
try {
|
|
359
|
+
const resolved = metaResolve(specifier);
|
|
360
|
+
entry = resolved.startsWith('file:') ? fileURLToPath(resolved) : resolved;
|
|
361
|
+
}
|
|
362
|
+
catch {
|
|
363
|
+
return undefined; // genuinely not installed
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
else {
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
// Walk up from the resolved entry file to the nearest `package.json` named
|
|
371
|
+
// `@nativescript/vite` (skips a possible inner `package.json` in a subdir).
|
|
372
|
+
let dir = dirname(entry);
|
|
373
|
+
for (;;) {
|
|
374
|
+
const pkgPath = join(dir, 'package.json');
|
|
375
|
+
if (existsSync(pkgPath)) {
|
|
376
|
+
try {
|
|
377
|
+
const name = JSON.parse(readFileSync(pkgPath, 'utf8')).name;
|
|
378
|
+
if (name === specifier)
|
|
379
|
+
return dir;
|
|
380
|
+
}
|
|
381
|
+
catch {
|
|
382
|
+
// unreadable/unparseable — keep walking up
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
const parent = dirname(dir);
|
|
386
|
+
if (parent === dir)
|
|
387
|
+
return undefined; // reached filesystem root
|
|
388
|
+
dir = parent;
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
/**
|
|
392
|
+
* Detect the installed `@nativescript/vite` major version. `@nativescript/vite` is
|
|
393
|
+
* an OPTIONAL peer (not installed in this repo), so detection can fail — in which
|
|
394
|
+
* case we return `undefined` and the caller assumes the patches are still needed
|
|
395
|
+
* (fail-safe to the historical full-patch behavior).
|
|
396
|
+
*
|
|
397
|
+
* Reads the package root's `package.json` (located WITHOUT relying on the
|
|
398
|
+
* exports-gated `/package.json` subpath, see `resolveNativescriptViteRoot`) and
|
|
399
|
+
* parses its `version`. Any failure (unresolvable peer, unreadable/unparseable
|
|
400
|
+
* version) is swallowed → `undefined`.
|
|
401
|
+
*/
|
|
402
|
+
export function detectNativescriptViteMajor() {
|
|
403
|
+
const root = resolveNativescriptViteRoot();
|
|
404
|
+
if (root === undefined)
|
|
405
|
+
return undefined; // not resolvable — assume patches still needed
|
|
406
|
+
try {
|
|
407
|
+
const raw = readFileSync(join(root, 'package.json'), 'utf8');
|
|
408
|
+
const version = JSON.parse(raw).version;
|
|
409
|
+
if (typeof version !== 'string')
|
|
410
|
+
return undefined;
|
|
411
|
+
const major = Number.parseInt(version.split('.', 1)[0] ?? '', 10);
|
|
412
|
+
return Number.isFinite(major) ? major : undefined;
|
|
413
|
+
}
|
|
414
|
+
catch {
|
|
415
|
+
return undefined; // unreadable / unparseable — assume patches still needed
|
|
416
|
+
}
|
|
417
|
+
}
|
|
109
418
|
/**
|
|
110
419
|
* Apply the Vite 8 / Rolldown + type-check fixes to a returned `@nativescript/vite`
|
|
111
420
|
* config: drop function-replacement aliases, the explicit `@rollup/plugin-commonjs`,
|
|
112
421
|
* and the vite-side `ns-typescript-check` (gjsify type-checks via `gjsify tsc`).
|
|
113
422
|
* Returns a shallow copy with the fixed `resolve` and `plugins` — the input is not
|
|
114
423
|
* mutated.
|
|
424
|
+
*
|
|
425
|
+
* CONDITIONAL on the installed `@nativescript/vite` major:
|
|
426
|
+
* - `>= 8` (the Vite-8 + Rolldown + HMR rewrite): SKIP the function-alias drop
|
|
427
|
+
* and the `@rollup/plugin-commonjs` strip — upstream already ships native
|
|
428
|
+
* `rolldownOptions`, string-only aliases and no explicit commonjs plugin, so
|
|
429
|
+
* those fixes are no-ops there. The `ns-typescript-check` strip is still
|
|
430
|
+
* applied (no-op-if-absent). Emits a one-time informational notice.
|
|
431
|
+
* - `<= 2` / UNKNOWN (unresolvable peer): apply the FULL fix set, exactly as
|
|
432
|
+
* before — the teapot showcase on `@nativescript/vite@2.0.3` must keep building.
|
|
433
|
+
*
|
|
434
|
+
* The optional `nsViteMajor` arg overrides the auto-detected major (used by the
|
|
435
|
+
* e2e fixtures to exercise both branches without two installs).
|
|
115
436
|
*/
|
|
116
|
-
export function applyVite8Fixes(config) {
|
|
437
|
+
export function applyVite8Fixes(config, nsViteMajor) {
|
|
438
|
+
const major = nsViteMajor ?? detectNativescriptViteMajor();
|
|
439
|
+
// Fail-safe: an unknown major (optional peer not resolvable) is treated as
|
|
440
|
+
// the legacy <= 2 line so the full patch set still applies.
|
|
441
|
+
const skipVite8Patches = major !== undefined && major >= NS_VITE_NATIVE_VITE8_MAJOR;
|
|
117
442
|
const out = { ...config };
|
|
118
|
-
if (
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
//
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
443
|
+
if (skipVite8Patches) {
|
|
444
|
+
// One-time informational notice — upstream 8.x handles Vite-8/Rolldown
|
|
445
|
+
// natively, so only the bundler-shouldn't-type-check strip is applied.
|
|
446
|
+
if (!nativeViteNoticeShown) {
|
|
447
|
+
nativeViteNoticeShown = true;
|
|
448
|
+
console.info(`[@gjsify/nativescript-vite] detected @nativescript/vite@${major}.x — upstream handles ` +
|
|
449
|
+
'Vite 8 / Rolldown natively (rolldownOptions, string-only aliases, no @rollup/plugin-commonjs), ' +
|
|
450
|
+
'so the function-alias drop and commonjs strip are skipped; only the vite-side ' +
|
|
451
|
+
`"${TS_CHECK_PLUGIN}" type-check plugin is removed.`);
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
else {
|
|
455
|
+
// Fix (1): drop function-replacement `resolve.alias` entries (Rolldown
|
|
456
|
+
// rejects them). Only on the <= 2 / unknown line — 8.x ships string aliases.
|
|
457
|
+
if (config.resolve?.alias) {
|
|
458
|
+
out.resolve = { ...config.resolve, alias: dropFunctionAliases(config.resolve.alias) };
|
|
459
|
+
}
|
|
460
|
+
// Fix (2): remove the explicit `@rollup/plugin-commonjs` plugin (Rolldown
|
|
461
|
+
// crashes on it). Only on the <= 2 / unknown line — 8.x has no such plugin.
|
|
462
|
+
if (Array.isArray(config.plugins)) {
|
|
463
|
+
const before = countPluginByName(config.plugins, 'commonjs');
|
|
464
|
+
out.plugins = stripPluginByName(config.plugins, 'commonjs');
|
|
465
|
+
// Observability: the strip depends on `@rollup/plugin-commonjs` registering
|
|
466
|
+
// exactly `name: 'commonjs'` as a synchronous, top-level (or nested-array)
|
|
467
|
+
// plugin object. If a future `@nativescript/vite` renames it or wraps it in
|
|
468
|
+
// a Promise, the strip silently misses and Rolldown crashes again with the
|
|
469
|
+
// `currentLoadingModule` error this package exists to prevent — so warn loud
|
|
470
|
+
// when the plugin we expect to remove was NOT found.
|
|
471
|
+
if (before === 0) {
|
|
472
|
+
// one-time build-time diagnostic — surfaces a silent upstream rename
|
|
473
|
+
console.warn('[@gjsify/nativescript-vite] no plugin named "commonjs" found in the upstream config — ' +
|
|
474
|
+
'if your @nativescript/vite version renamed/wrapped @rollup/plugin-commonjs, the Rolldown ' +
|
|
475
|
+
'CommonJS fix may not have applied.');
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
// Fix (3): drop the vite-side `ns-typescript-check` plugin on EITHER line. A
|
|
480
|
+
// bundler should bundle, not type-check — gjsify defers the authoritative
|
|
481
|
+
// TypeScript gate to `gjsify tsc` / the app's own `check` script (the same
|
|
482
|
+
// separation Vite's esbuild/SWC pipeline already makes). The vite-side check
|
|
483
|
+
// additionally runs a SEPARATE program that loads the full `@nativescript/types`
|
|
484
|
+
// android globals and, under TS 6+, fails the build on the *standard*
|
|
485
|
+
// NativeScript `createNativeView(): android.view.View` override — a covariance
|
|
486
|
+
// the app's own `tsc --noEmit` accepts. Removing it (not silencing it) keeps the
|
|
487
|
+
// build honest; type errors surface in `gjsify tsc`, the real gate. No-op when
|
|
488
|
+
// the plugin is absent (8.x may not register it).
|
|
489
|
+
if (Array.isArray(out.plugins)) {
|
|
145
490
|
const tsCheckBefore = countPluginByName(out.plugins, TS_CHECK_PLUGIN);
|
|
146
491
|
out.plugins = stripPluginByName(out.plugins, TS_CHECK_PLUGIN);
|
|
147
492
|
if (tsCheckBefore > 0) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/nativescript-vite",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Vite 8 / Rolldown compatibility + gjsify transforms for building NativeScript apps — composes @nativescript/vite and fixes the pieces Rolldown rejects.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
],
|
|
39
39
|
"license": "MIT",
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@gjsify/vite-plugin-gjsify": "^0.
|
|
41
|
+
"@gjsify/vite-plugin-gjsify": "^0.12.0"
|
|
42
42
|
},
|
|
43
43
|
"peerDependencies": {
|
|
44
44
|
"@nativescript/canvas": "*",
|