@gjsify/nativescript-vite 0.13.0 → 0.13.2

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,42 @@ 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
+ * Stabilise the NativeScript build output so the Android Static Binding
36
+ * Generator never sees a native class extended twice.
37
+ *
38
+ * `@nativescript/vite@8` names non-vendor / non-worker chunks `[name]-[hash].mjs`
39
+ * and leaves `build.emptyOutDir` unset. The NS CLI then copies the staging dir
40
+ * (`.ns-vite-build`) into `platforms/android/app/src/main/assets/app/`
41
+ * ADDITIVELY — it never deletes stale files. So every build whose content
42
+ * changes emits a fresh `activity.android-<hash>.mjs`, and `assets/app` ends up
43
+ * holding TWO of them: the SBG scans both, sees `NativeScriptActivity`'s
44
+ * `extends` declaration twice and aborts with "File already exists … change the
45
+ * name of one of the extended classes". The documented workaround was a manual
46
+ * `rm -rf .ns-vite-build` + `assets/app/*.mjs` before every build.
47
+ *
48
+ * The entry (`bundle.mjs`) and vendor chunk (`vendor.mjs`) are ALREADY stable
49
+ * upstream; this extends that to every chunk (so a rebuild OVERWRITES rather
50
+ * than accumulates) and empties the staging dir each build. Merged over the
51
+ * upstream config but under the user's, so an app can still override it.
52
+ *
53
+ * NOTE: a one-time clean of a pre-existing `assets/app/*.mjs` is still needed to
54
+ * drop chunks that already accumulated; from then on the names stay stable.
55
+ */
56
+ export declare function nativescriptSbgBundleSyncFix(): UserConfig;
57
+ /**
58
+ * Pure core of {@link repointMistargetedCoreAlias} (exported for testing, since
59
+ * the wrapper resolves the real `@nativescript/core` from disk).
60
+ *
61
+ * Mutates `aliases` in place: for every entry whose `find` matches
62
+ * `@nativescript/core`, repoint its string `replacement` at `realRoot` UNLESS the
63
+ * replacement's current root directory already resolves to a real
64
+ * `@nativescript/core` — decided by `nameOf(dir)` returning `'@nativescript/core'`.
65
+ * A trailing `/$1` capture-group suffix is preserved.
66
+ *
67
+ * @internal
68
+ */
69
+ export declare function repointCoreAliasEntries(aliases: Alias[], realRoot: string, nameOf: (dir: string) => string | undefined): void;
34
70
  /**
35
71
  * Detect the installed `@nativescript/vite` major version. `@nativescript/vite` is
36
72
  * 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
@@ -88,13 +92,136 @@ export function defineNativescriptConfig(options = {}, userConfig) {
88
92
  const withTransforms = mergeConfig(fixed, {
89
93
  plugins: [...gjsifyNativescriptTransforms(options)],
90
94
  });
95
+ // Stabilise the build output so repeated `ns prepare`/`ns build` runs do
96
+ // not accumulate stale, hashed chunk files the Android SBG then sees as a
97
+ // duplicate native `extends` (see {@link nativescriptSbgBundleSyncFix}).
98
+ const withSbgFix = mergeConfig(withTransforms, nativescriptSbgBundleSyncFix());
91
99
  if (userConfig === undefined)
92
- return withTransforms;
100
+ return withSbgFix;
93
101
  const resolved = typeof userConfig === 'function' ? await userConfig(env) : userConfig;
94
- return mergeConfig(withTransforms, resolved);
102
+ return mergeConfig(withSbgFix, resolved);
95
103
  };
96
104
  }
97
105
  export default defineNativescriptConfig;
106
+ /**
107
+ * Stabilise the NativeScript build output so the Android Static Binding
108
+ * Generator never sees a native class extended twice.
109
+ *
110
+ * `@nativescript/vite@8` names non-vendor / non-worker chunks `[name]-[hash].mjs`
111
+ * and leaves `build.emptyOutDir` unset. The NS CLI then copies the staging dir
112
+ * (`.ns-vite-build`) into `platforms/android/app/src/main/assets/app/`
113
+ * ADDITIVELY — it never deletes stale files. So every build whose content
114
+ * changes emits a fresh `activity.android-<hash>.mjs`, and `assets/app` ends up
115
+ * holding TWO of them: the SBG scans both, sees `NativeScriptActivity`'s
116
+ * `extends` declaration twice and aborts with "File already exists … change the
117
+ * name of one of the extended classes". The documented workaround was a manual
118
+ * `rm -rf .ns-vite-build` + `assets/app/*.mjs` before every build.
119
+ *
120
+ * The entry (`bundle.mjs`) and vendor chunk (`vendor.mjs`) are ALREADY stable
121
+ * upstream; this extends that to every chunk (so a rebuild OVERWRITES rather
122
+ * than accumulates) and empties the staging dir each build. Merged over the
123
+ * upstream config but under the user's, so an app can still override it.
124
+ *
125
+ * NOTE: a one-time clean of a pre-existing `assets/app/*.mjs` is still needed to
126
+ * drop chunks that already accumulated; from then on the names stay stable.
127
+ */
128
+ export function nativescriptSbgBundleSyncFix() {
129
+ return {
130
+ build: {
131
+ // Clear the staging dir each (non-watch) build so stale chunks don't linger.
132
+ emptyOutDir: true,
133
+ rolldownOptions: {
134
+ output: {
135
+ // Drop the content hash from chunk names — matching the
136
+ // already-stable `bundle.mjs` entry + `vendor.mjs` — so a
137
+ // rebuild overwrites the same files instead of piling up
138
+ // `activity.android-<hash>.mjs` copies for the SBG to trip on.
139
+ chunkFileNames: (chunk) => {
140
+ if (chunk.name === 'vendor')
141
+ return 'vendor.mjs';
142
+ if (chunk.name && chunk.name.includes('worker'))
143
+ return '[name].js';
144
+ return '[name].mjs';
145
+ },
146
+ },
147
+ },
148
+ },
149
+ };
150
+ }
151
+ /**
152
+ * Repoint a mis-targeted `@nativescript/core` resolve.alias.
153
+ *
154
+ * `@nativescript/vite`'s base config (`configuration/base.js`) sets the
155
+ * @nativescript/core root to `<project>/../../packages/core` whenever that path
156
+ * EXISTS — without verifying the directory's package name. It assumes the
157
+ * NativeScript monorepo layout, where `packages/core` IS @nativescript/core. In
158
+ * ANY OTHER monorepo whose own `packages/core` is a DIFFERENT package — e.g.
159
+ * `@learn6502/core` in the Learn6502 workspace, or any `packages/core` — that
160
+ * heuristic mis-fires: @nativescript/core is aliased to the wrong package, and
161
+ * every `@nativescript/core/<sub>` import (`globals/index`, `application`,
162
+ * `ui/frame/activity.android`, `inspector_modules`, …) fails to resolve with
163
+ * "No such file or directory", breaking the bundle. (A non-monorepo app, or one
164
+ * without a sibling `packages/core`, falls through to node_modules resolution and
165
+ * is unaffected — which is why it bites only some layouts.)
166
+ *
167
+ * Fix: scan the composed `resolve.alias` and, for any entry targeting
168
+ * @nativescript/core whose replacement directory's `package.json#name` is NOT
169
+ * `@nativescript/core`, repoint the replacement at the REAL installed
170
+ * @nativescript/core (resolved from the project root, `process.cwd()` — where
171
+ * Vite runs the config). Correctly-targeted aliases (a real @nativescript/core,
172
+ * source or node_modules) are left untouched, so this is a safe no-op outside the
173
+ * name-collision case.
174
+ */
175
+ function repointMistargetedCoreAlias(config) {
176
+ const alias = config.resolve?.alias;
177
+ if (!Array.isArray(alias))
178
+ return;
179
+ let realRoot;
180
+ try {
181
+ const req = createRequire(import.meta.url);
182
+ realRoot = dirname(req.resolve('@nativescript/core/package.json', { paths: [process.cwd()] })).replace(/\\/g, '/');
183
+ }
184
+ catch {
185
+ return; // @nativescript/core not resolvable from the project — leave config as-is
186
+ }
187
+ repointCoreAliasEntries(alias, realRoot, (dir) => {
188
+ try {
189
+ return JSON.parse(readFileSync(join(dir, 'package.json'), 'utf8')).name;
190
+ }
191
+ catch {
192
+ return undefined; // unreadable target → treat as mis-targeted
193
+ }
194
+ });
195
+ }
196
+ /**
197
+ * Pure core of {@link repointMistargetedCoreAlias} (exported for testing, since
198
+ * the wrapper resolves the real `@nativescript/core` from disk).
199
+ *
200
+ * Mutates `aliases` in place: for every entry whose `find` matches
201
+ * `@nativescript/core`, repoint its string `replacement` at `realRoot` UNLESS the
202
+ * replacement's current root directory already resolves to a real
203
+ * `@nativescript/core` — decided by `nameOf(dir)` returning `'@nativescript/core'`.
204
+ * A trailing `/$1` capture-group suffix is preserved.
205
+ *
206
+ * @internal
207
+ */
208
+ export function repointCoreAliasEntries(aliases, realRoot, nameOf) {
209
+ for (const entry of aliases) {
210
+ const { find, replacement } = entry;
211
+ if (typeof replacement !== 'string')
212
+ continue;
213
+ // `find` is a RegExp like /^@nativescript\/core$/; strip escapes to match.
214
+ if (!String(find).replace(/\\/g, '').includes('@nativescript/core'))
215
+ continue;
216
+ // Replacements look like `${ROOT}` or `${ROOT}/$1` — recover the root dir.
217
+ const currentRoot = replacement.replace(/\/\$1$/, '').replace(/\/+$/, '');
218
+ if (currentRoot === realRoot)
219
+ continue; // already correct
220
+ if (nameOf(currentRoot) === '@nativescript/core')
221
+ continue; // a real core elsewhere — keep it
222
+ entry.replacement = replacement.endsWith('/$1') ? `${realRoot}/$1` : realRoot;
223
+ }
224
+ }
98
225
  /**
99
226
  * Resolve `@nativescript/vite`'s TypeScript config factory (an optional peer).
100
227
  * 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.2",
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.2"
42
42
  },
43
43
  "peerDependencies": {
44
44
  "@nativescript/canvas": "*",