|
1 | | -import { createRequire } from 'node:module'; |
2 | | -import { dirname, join } from 'node:path'; |
3 | | -import { existsSync } from 'node:fs'; |
4 | 1 | import { fileURLToPath } from 'node:url'; |
5 | | -import { tanstackRouter } from '@tanstack/router-plugin/vite'; |
6 | 2 |
|
7 | | -const require = createRequire(import.meta.url); |
8 | | -const projectRoot = process.cwd(); |
9 | | - |
10 | | -let solidJsRuntimePath; |
11 | | -let solidJsHPath; |
12 | 3 | const solidWebShimPath = fileURLToPath(new URL('./solid-web-shim.js', import.meta.url)); |
13 | | -const jsxRuntimeShimPath = fileURLToPath(new URL('./solid-js-jsx-runtime-shim.js', import.meta.url)); |
14 | | -const tsrConfigPath = join(projectRoot, 'tsr.config.json'); |
15 | | -const defaultRoutesDirectory = join(projectRoot, 'src', 'routes'); |
16 | | - |
17 | | -function hasFileBasedRoutingConfig() { |
18 | | - return existsSync(tsrConfigPath) || existsSync(defaultRoutesDirectory); |
19 | | -} |
20 | | - |
21 | | -function forcePreEnforce(plugin) { |
22 | | - if (!plugin || typeof plugin !== 'object') { |
23 | | - return plugin; |
24 | | - } |
25 | | - |
26 | | - if (Array.isArray(plugin)) { |
27 | | - return plugin.map(forcePreEnforce); |
28 | | - } |
29 | | - |
30 | | - return { |
31 | | - ...plugin, |
32 | | - enforce: plugin.enforce ?? 'pre', |
33 | | - }; |
34 | | -} |
35 | | - |
36 | | -function getRouterPlugins() { |
37 | | - if (!hasFileBasedRoutingConfig()) { |
38 | | - return []; |
39 | | - } |
40 | | - |
41 | | - const plugin = tanstackRouter({ |
42 | | - // with other frameworks, we can embed different configs |
43 | | - target: 'solid', |
44 | | - autoCodeSplitting: false, |
45 | | - enableRouteGeneration: true, |
46 | | - }); |
47 | | - |
48 | | - const normalized = forcePreEnforce(plugin); |
49 | | - return Array.isArray(normalized) ? normalized : [normalized]; |
50 | | -} |
51 | | - |
52 | | -try { |
53 | | - const solidJsPackagePath = require.resolve('solid-js/package.json'); |
54 | | - const solidJsRoot = dirname(solidJsPackagePath); |
55 | | - |
56 | | - const candidateRuntimePath = join(solidJsRoot, 'h', 'jsx-runtime', 'dist', 'jsx.js'); |
57 | | - const candidateHPath = join(solidJsRoot, 'h', 'dist', 'h.js'); |
58 | | - |
59 | | - if (existsSync(candidateRuntimePath)) { |
60 | | - solidJsRuntimePath = candidateRuntimePath; |
61 | | - } |
62 | | - |
63 | | - if (existsSync(candidateHPath)) { |
64 | | - solidJsHPath = candidateHPath; |
65 | | - } |
66 | | - |
67 | | - if (!solidJsRuntimePath || !solidJsHPath) { |
68 | | - console.warn( |
69 | | - '[nativescript/tanstack-router] Could not resolve one or more SolidJS runtime alias paths for Vite.', |
70 | | - { |
71 | | - runtimeFound: !!solidJsRuntimePath, |
72 | | - hFound: !!solidJsHPath, |
73 | | - solidJsRoot, |
74 | | - }, |
75 | | - ); |
76 | | - } |
77 | | -} catch (error) { |
78 | | - console.warn( |
79 | | - '[nativescript/tanstack-router] Failed to resolve SolidJS package location for Vite aliases.', |
80 | | - error instanceof Error ? error.message : error, |
81 | | - ); |
82 | | -} |
83 | 4 |
|
84 | 5 | export default () => ({ |
85 | | - plugins: [...getRouterPlugins()], |
86 | 6 | resolve: { |
87 | | - alias: { |
88 | | - // Keep Vite behavior aligned with nativescript.webpack.js integration. |
89 | | - 'solid-js/web': solidWebShimPath, |
90 | | - // NOTE: do NOT alias `solid-js/jsx-runtime` / `solid-js/jsx-dev-runtime` to a |
91 | | - // shim file INSIDE this package. @nativescript/vite (solid flavor) already provides |
92 | | - // its own jsx-runtime shim and serves it over HTTP. When this alias pointed at |
93 | | - // `./solid-js-jsx-runtime-shim.js` (a subpath of this vendored runtime plugin), the |
94 | | - // @nativescript/vite device rewriter collapsed that subpath to the vendor-package |
95 | | - // ROOT binding — `jsx = __nsPick("@nativescript/tanstack-router", "jsx")` — and the |
96 | | - // root has no `jsx` export, so `jsx` was `undefined`. Every `jsx(...)` call in the |
97 | | - // bundled router JSX (e.g. renderPage's `<RouterContextProvider><SafeRouteView/>`) |
98 | | - // then threw "jsx is not a function", the route Page never rendered, the Frame stayed |
99 | | - // empty, and the app showed a blank screen under HMR. Letting @nativescript/vite's |
100 | | - // HTTP-served shim resolve these keeps `jsx` a real function. (The webpack path still |
101 | | - // aliases them via nativescript.webpack.js, which has its own shim handling.) |
102 | | - ...(solidJsHPath |
103 | | - ? { |
104 | | - 'solid-js/h': solidJsHPath, |
105 | | - } |
106 | | - : {}), |
107 | | - }, |
| 7 | + alias: [ |
| 8 | + // Anchored: `@solidjs/web/*` subpaths (server-functions, storage) are |
| 9 | + // renderer-independent and must keep resolving to the real package. |
| 10 | + { find: /^@solidjs\/web$/, replacement: solidWebShimPath }, |
| 11 | + ], |
108 | 12 | }, |
109 | 13 | }); |
0 commit comments