@ng-native/migrate 0.27.0 → 0.28.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/migrations.cjs CHANGED
@@ -97,7 +97,7 @@ const migrations = [
97
97
  name: 'vitest-tsconfig-paths',
98
98
  version: '0.18.1',
99
99
  description:
100
- "Resolve the workspace's path aliases in Nx Vitest configs with vite-tsconfig-paths, as nxViteTsPaths does nothing from Nx 24.",
100
+ "Resolve the workspace's path aliases in Nx Vitest configs with Vite's resolve.tsconfigPaths where Vite 8 or later runs the tests and any Vitest listed is 4.1 or later, and with vite-tsconfig-paths otherwise, as nxViteTsPaths does nothing from Nx 24.",
101
101
  run: vitestTsconfigPaths,
102
102
  },
103
103
  {
package/native-app.cjs CHANGED
@@ -36,7 +36,7 @@ const dependencies = {
36
36
  'expo-system-ui': '~58.0.6',
37
37
  react: '19.3.0',
38
38
  'react-native': '0.88.0',
39
- 'react-native-safe-area-context': '~5.10.1',
39
+ 'react-native-safe-area-context': '~5.11.1',
40
40
  };
41
41
 
42
42
  /** @type {Record<string, string>} */
@@ -181,7 +181,7 @@ const desktopOnlyDependencies = Object.fromEntries(
181
181
  ...desktopDependencies,
182
182
  '@ng-native/expo': version,
183
183
  // Exactly the version the prebuilt desktop host is built with, so the app runs on it.
184
- 'react-native-safe-area-context': '5.10.1',
184
+ 'react-native-safe-area-context': '5.11.1',
185
185
  })
186
186
  .filter(([name]) => name !== 'expo-status-bar' && name !== 'expo-system-ui')
187
187
  .sort(([a], [b]) => (a < b ? -1 : 1)),
@@ -388,7 +388,35 @@ function conflicts(manifest, { linter = 'none' } = {}) {
388
388
  return problems;
389
389
  }
390
390
 
391
+ /**
392
+ * Whether the tests' path aliases can be left to Vite's own `resolve.tsconfigPaths`, which Vite 8
393
+ * added, in a workspace whose root `package.json` is `manifest`: the Vite it lists is 8 or later,
394
+ * or it lists none, and the Vitest it lists, if any, is confirmed to take Vite 8 (Vitest 4.1 and
395
+ * later do). A Vitest older than 4.1 keeps `vite-tsconfig-paths` whatever Vite is listed, since it
396
+ * runs on its own Vite 6 or 7, and so does one whose version cannot be read: `workspace:`,
397
+ * `catalog:`, a tag other than `latest`, a URL. A range, an exact version, an `npm:vitest@<range>`
398
+ * alias and `latest` (the newest release) are read. A Vite it lists that is older, or that is not
399
+ * a range, keeps the plugin too.
400
+ *
401
+ * @param {{ dependencies?: Record<string, string>, devDependencies?: Record<string, string> }} manifest
402
+ */
403
+ function viteResolvesPaths(manifest) {
404
+ const listed = { ...manifest.dependencies, ...manifest.devDependencies };
405
+ const first = (range) => (semver.validRange(range) ? semver.minVersion(range) : null);
406
+ const vitestVersion = (spec) => {
407
+ const range = spec.replace(/^npm:vitest@/, '');
408
+ return range === 'latest' ? semver.coerce('999.0.0') : first(range);
409
+ };
410
+ // Vitest loads the config with the Vite it depends on, so one older than 4.1 (Vite 6 or 7), or
411
+ // one that is not known to be newer, wins over a Vite 8 at the root.
412
+ const vitest = listed.vitest === undefined ? undefined : vitestVersion(listed.vitest);
413
+ const vitestTakesVite8 = vitest === undefined || (vitest !== null && semver.gte(vitest, '4.1.0'));
414
+ if (listed.vite) return (first(listed.vite)?.major ?? 0) >= 8 && vitestTakesVite8;
415
+ return vitestTakesVite8;
416
+ }
417
+
391
418
  module.exports = {
419
+ viteResolvesPaths,
392
420
  dependencies,
393
421
  devDependencies,
394
422
  lintDevDependencies,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ng-native/migrate",
3
- "version": "0.27.0",
3
+ "version": "0.28.0",
4
4
  "description": "The migrations that update an Angular Native app to a new release, for nx migrate, ng update and npx.",
5
5
  "keywords": [
6
6
  "angular",
@@ -8,44 +8,81 @@
8
8
  * imports `tsconfigPaths` from `vite-tsconfig-paths` in its place, and the root `package.json`
9
9
  * gains `vite-tsconfig-paths` when it lists neither. A config the generator did not write is left
10
10
  * as it is.
11
+ *
12
+ * Where the workspace's Vite resolves tsconfig paths itself (Vite 8 and later, with a Vitest that
13
+ * takes it: the same test the generators use), each config gets Vite's `resolve.tsconfigPaths`
14
+ * instead, with no import, and the root `package.json` gains `vite` where it lists none. A config
15
+ * that cannot take the option, one with a `resolve` of its own or the call somewhere other than
16
+ * first in a `plugins` list, gets the plugin as before. A config already on `vite-tsconfig-paths`
17
+ * or on `resolve.tsconfigPaths` no longer matches and is left as it is, so running the migration
18
+ * on a workspace that has had it changes nothing.
11
19
  */
12
20
  const { files } = require('./host.cjs');
13
21
  const { formatted } = require('./sync-app-versions.cjs');
22
+ const { viteResolvesPaths } = require('./native-app.cjs');
14
23
 
15
24
  // Either quote, as the workspace's Prettier formats it.
16
25
  const FROM =
17
26
  /import \{ nxViteTsPaths \} from (['"])@nx\/vite\/plugins\/nx-tsconfig-paths\.plugin\1;/;
18
27
  const TO = 'import tsconfigPaths from $1vite-tsconfig-paths$1;';
19
- const DEPENDENCY = { name: 'vite-tsconfig-paths', range: '^5.1.4' };
28
+ const PLUGIN = { name: 'vite-tsconfig-paths', range: '^5.1.4' };
29
+ const VITE = { name: 'vite', range: '^8.0.0' };
30
+ const FIRST_PLUGIN = /plugins: \[nxViteTsPaths\(\), /g;
31
+
32
+ /** The config with Vite's own option in place of the plugin, or null where it cannot take it. */
33
+ function viaVite(text) {
34
+ if (/\bresolve['"]?\s*:/.test(text)) return null;
35
+ const next = text
36
+ .replace(new RegExp(`${FROM.source}\\r?\\n?`), '')
37
+ .replace(FIRST_PLUGIN, 'resolve: { tsconfigPaths: true }, plugins: [');
38
+ return next.includes('nxViteTsPaths') ? null : next;
39
+ }
20
40
 
21
41
  /**
22
- * Rewrites each config the generators wrote, and answers whether there was one.
42
+ * Rewrites each config the generators wrote, and answers which dependency the root `package.json`
43
+ * needs for the ones it rewrote. `vite` is the one a config that
44
+ * left the aliases to Vite needs; `vite-tsconfig-paths` is the one any other config does.
23
45
  *
24
46
  * @param {import('./host.cjs').Host} host
47
+ * @param {() => boolean} resolvesPaths whether the workspace's Vite resolves the aliases itself,
48
+ * asked only once a config needs the answer
25
49
  */
26
- function rewriteConfigs(host) {
27
- let changed = false;
50
+ function rewriteConfigs(host, resolvesPaths) {
51
+ const needed = new Set();
28
52
  for (const file of files(host)) {
29
53
  if (file !== 'vitest.config.mts' && !file.endsWith('/vitest.config.mts')) continue;
30
54
  const text = host.read(file) ?? '';
31
55
  if (!FROM.test(text) || !text.includes('@ng-native/testing/vitest')) continue;
32
- host.write(file, text.replace(FROM, TO).replace(/\bnxViteTsPaths\(\)/g, 'tsconfigPaths()'));
33
- changed = true;
56
+ const native = resolvesPaths() ? viaVite(text) : null;
57
+ host.write(
58
+ file,
59
+ native ?? text.replace(FROM, TO).replace(/\bnxViteTsPaths\(\)/g, 'tsconfigPaths()'),
60
+ );
61
+ needed.add(native === null ? PLUGIN : VITE);
34
62
  }
35
- return changed;
63
+ return needed;
36
64
  }
37
65
 
38
66
  /** @param {import('./host.cjs').Host} host */
39
67
  function vitestTsconfigPaths(host) {
40
- const original = rewriteConfigs(host) ? host.read('package.json') : null;
41
- if (original === null) return [];
68
+ const original = host.read('package.json');
69
+ // With no manifest to read the Vite from, the plugin works on any Vite.
70
+ const needed = rewriteConfigs(
71
+ host,
72
+ () => original !== null && viteResolvesPaths(JSON.parse(original)),
73
+ );
74
+ if (original === null || needed.size === 0) return [];
42
75
  const manifest = JSON.parse(original);
43
- if (manifest.dependencies?.[DEPENDENCY.name] || manifest.devDependencies?.[DEPENDENCY.name]) {
44
- return [];
45
- }
46
- manifest.devDependencies = { ...manifest.devDependencies, [DEPENDENCY.name]: DEPENDENCY.range };
76
+ const added = [...needed].filter(
77
+ ({ name }) => !manifest.dependencies?.[name] && !manifest.devDependencies?.[name],
78
+ );
79
+ if (added.length === 0) return [];
80
+ manifest.devDependencies = {
81
+ ...manifest.devDependencies,
82
+ ...Object.fromEntries(added.map(({ name, range }) => [name, range])),
83
+ };
47
84
  host.write('package.json', formatted(manifest, original));
48
- return ['Install vite-tsconfig-paths, which the root package.json now lists.'];
85
+ return added.map(({ name }) => `Install ${name}, which the root package.json now lists.`);
49
86
  }
50
87
 
51
88
  module.exports = { vitestTsconfigPaths };