@gjsify/nativescript-vite 0.13.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ConfigEnv, UserConfig } from 'vite';
1
+ import type { ConfigEnv, UserConfig, Alias } from 'vite';
2
2
  import type { GjsifyNativescriptOptions } from '@gjsify/vite-plugin-gjsify';
3
3
  export type GjsifyNativescriptViteOptions = GjsifyNativescriptOptions;
4
4
  /** A user-supplied Vite config (or config factory) merged in as the final layer. */
@@ -31,6 +31,19 @@ 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
+ * Pure core of {@link repointMistargetedCoreAlias} (exported for testing, since
36
+ * the wrapper resolves the real `@nativescript/core` from disk).
37
+ *
38
+ * Mutates `aliases` in place: for every entry whose `find` matches
39
+ * `@nativescript/core`, repoint its string `replacement` at `realRoot` UNLESS the
40
+ * replacement's current root directory already resolves to a real
41
+ * `@nativescript/core` — decided by `nameOf(dir)` returning `'@nativescript/core'`.
42
+ * A trailing `/$1` capture-group suffix is preserved.
43
+ *
44
+ * @internal
45
+ */
46
+ export declare function repointCoreAliasEntries(aliases: Alias[], realRoot: string, nameOf: (dir: string) => string | undefined): void;
34
47
  /**
35
48
  * Detect the installed `@nativescript/vite` major version. `@nativescript/vite` is
36
49
  * an OPTIONAL peer (not installed in this repo), so detection can fail — in which
package/lib/index.js CHANGED
@@ -81,6 +81,10 @@ export function defineNativescriptConfig(options = {}, userConfig) {
81
81
  return async (env) => {
82
82
  const base = await loadUpstreamConfig(env);
83
83
  const fixed = applyVite8Fixes(base);
84
+ // Correct `@nativescript/vite`'s fragile "monorepo `packages/core` IS
85
+ // @nativescript/core" heuristic when the HOST monorepo's own packages/core
86
+ // is a DIFFERENT package (see {@link repointMistargetedCoreAlias}).
87
+ repointMistargetedCoreAlias(fixed);
84
88
  // Layer gjsify's NS transforms (a Vite plugin array whose `config()` hook
85
89
  // supplies the gi:// → empty redirect, platform resolution, defines, and
86
90
  // node-builtin aliases) — the same composition proven to produce a
@@ -95,6 +99,80 @@ export function defineNativescriptConfig(options = {}, userConfig) {
95
99
  };
96
100
  }
97
101
  export default defineNativescriptConfig;
102
+ /**
103
+ * Repoint a mis-targeted `@nativescript/core` resolve.alias.
104
+ *
105
+ * `@nativescript/vite`'s base config (`configuration/base.js`) sets the
106
+ * @nativescript/core root to `<project>/../../packages/core` whenever that path
107
+ * EXISTS — without verifying the directory's package name. It assumes the
108
+ * NativeScript monorepo layout, where `packages/core` IS @nativescript/core. In
109
+ * ANY OTHER monorepo whose own `packages/core` is a DIFFERENT package — e.g.
110
+ * `@learn6502/core` in the Learn6502 workspace, or any `packages/core` — that
111
+ * heuristic mis-fires: @nativescript/core is aliased to the wrong package, and
112
+ * every `@nativescript/core/<sub>` import (`globals/index`, `application`,
113
+ * `ui/frame/activity.android`, `inspector_modules`, …) fails to resolve with
114
+ * "No such file or directory", breaking the bundle. (A non-monorepo app, or one
115
+ * without a sibling `packages/core`, falls through to node_modules resolution and
116
+ * is unaffected — which is why it bites only some layouts.)
117
+ *
118
+ * Fix: scan the composed `resolve.alias` and, for any entry targeting
119
+ * @nativescript/core whose replacement directory's `package.json#name` is NOT
120
+ * `@nativescript/core`, repoint the replacement at the REAL installed
121
+ * @nativescript/core (resolved from the project root, `process.cwd()` — where
122
+ * Vite runs the config). Correctly-targeted aliases (a real @nativescript/core,
123
+ * source or node_modules) are left untouched, so this is a safe no-op outside the
124
+ * name-collision case.
125
+ */
126
+ function repointMistargetedCoreAlias(config) {
127
+ const alias = config.resolve?.alias;
128
+ if (!Array.isArray(alias))
129
+ return;
130
+ let realRoot;
131
+ try {
132
+ const req = createRequire(import.meta.url);
133
+ realRoot = dirname(req.resolve('@nativescript/core/package.json', { paths: [process.cwd()] })).replace(/\\/g, '/');
134
+ }
135
+ catch {
136
+ return; // @nativescript/core not resolvable from the project — leave config as-is
137
+ }
138
+ repointCoreAliasEntries(alias, realRoot, (dir) => {
139
+ try {
140
+ return JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).name;
141
+ }
142
+ catch {
143
+ return undefined; // unreadable target → treat as mis-targeted
144
+ }
145
+ });
146
+ }
147
+ /**
148
+ * Pure core of {@link repointMistargetedCoreAlias} (exported for testing, since
149
+ * the wrapper resolves the real `@nativescript/core` from disk).
150
+ *
151
+ * Mutates `aliases` in place: for every entry whose `find` matches
152
+ * `@nativescript/core`, repoint its string `replacement` at `realRoot` UNLESS the
153
+ * replacement's current root directory already resolves to a real
154
+ * `@nativescript/core` — decided by `nameOf(dir)` returning `'@nativescript/core'`.
155
+ * A trailing `/$1` capture-group suffix is preserved.
156
+ *
157
+ * @internal
158
+ */
159
+ export function repointCoreAliasEntries(aliases, realRoot, nameOf) {
160
+ for (const entry of aliases) {
161
+ const { find, replacement } = entry;
162
+ if (typeof replacement !== 'string')
163
+ continue;
164
+ // `find` is a RegExp like /^@nativescript\/core$/; strip escapes to match.
165
+ if (!String(find).replace(/\\/g, '').includes('@nativescript/core'))
166
+ continue;
167
+ // Replacements look like `${ROOT}` or `${ROOT}/$1` — recover the root dir.
168
+ const currentRoot = replacement.replace(/\/\$1$/, '').replace(/\/+$/, '');
169
+ if (currentRoot === realRoot)
170
+ continue; // already correct
171
+ if (nameOf(currentRoot) === '@nativescript/core')
172
+ continue; // a real core elsewhere — keep it
173
+ entry.replacement = replacement.endsWith('/$1') ? `${realRoot}/$1` : realRoot;
174
+ }
175
+ }
98
176
  /**
99
177
  * Resolve `@nativescript/vite`'s TypeScript config factory (an optional peer).
100
178
  * Prefers the documented root entry (`@nativescript/vite`) and falls back to the
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/nativescript-vite",
3
- "version": "0.13.0",
3
+ "version": "0.13.1",
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.13.0"
41
+ "@gjsify/vite-plugin-gjsify": "^0.13.1"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@nativescript/canvas": "*",