Last active
August 6, 2022 23:24
-
-
Save dalcib/a29da20abc2bb28d9bcf491ea9e12a8f to your computer and use it in GitHub Desktop.
react-native-web vite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// src/routes.tsx | |
import { Fragment, lazy, Suspense } from 'react' | |
import { Routes as BrowserRoutes, Route } from 'react-router-dom' | |
const PRESERVED = import.meta.glob('/src/pages/(_app|404).tsx', {eager: true}) | |
const ROUTES = import.meta.glob('/src/pages/**/[a-z[]*.tsx') | |
const _ROUTES = { | |
'/src/pages/index.tsx': { default: ƒ Index(), ... }, | |
'/src/pages/posts/index.tsx': { default: ƒ PostsIndex(), ... }, | |
'/src/pages/posts/[slug].tsx': { default: ƒ PostsSlug(), ... }, | |
... | |
} | |
const _PRESERVED = { | |
'/src/pages/_app.tsx': { default: ƒ App(), ... }, | |
'/src/pages/404.tsx': { default: ƒ NotFound(), ... } | |
} | |
const preserved = Object.keys(PRESERVED).reduce((preserved, file) => { | |
const key = file.replace(/\/src\/pages\/|\.tsx$/g, '') | |
return { ...preserved, [key]: PRESERVED[file].default } | |
}, {}) | |
const _routes = [ | |
{ path: '/', component: ƒ Index() }, | |
{ path: '/posts', component: ƒ PostsIndex() }, | |
{ path: '/posts/:slug', component: ƒ PostsSlug() } | |
] | |
const routes = Object.keys(ROUTES).map((route) => { | |
const path = route | |
.replace(/\/src\/pages|index|\.tsx$/g, '') | |
.replace(/\[\.{3}.+\]/, '*') | |
.replace(/\[(.+)\]/, ':$1') | |
return { path, component: lazy(ROUTES[route]) } | |
}) | |
export const Routes = () => { | |
const App = preserved?.['_app'] || Fragment | |
const NotFound = preserved?.['404'] || Fragment | |
return ( | |
<App> | |
<Suspense fallback={'Loading...'}> | |
<BrowserRoutes> | |
{routes.map(({ path, component: Component = Fragment }) => ( | |
<Route key={path} path={path} element={<Component />} /> | |
))} | |
<Route path="*" element={<NotFound />} /> | |
</BrowserRoutes> | |
</Suspense> | |
</App> | |
) | |
} | |
function findFiles(entry: string): string[] { | |
return readdirSync(entry).flatMap((file) => { | |
const filepath = join(entry, file); | |
if ( | |
statSync(filepath).isDirectory() && | |
!filepath.includes("node_modules") | |
) { | |
return findFiles(filepath); | |
} | |
return filepath; | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { defineConfig } from "vite"; | |
import react from "@vitejs/plugin-react"; | |
import { viteCommonjs, esbuildCommonjs } from "@originjs/vite-plugin-commonjs"; | |
// https://vitejs.dev/config/ | |
export default defineConfig({ | |
define: { | |
global: "window", | |
}, | |
optimizeDeps: { | |
include: ["@react-navigation/native"], | |
esbuildOptions: { | |
mainFields: ["module", "main"], | |
resolveExtensions: [".web.js", ".js", ".ts"], | |
plugins: [esbuildCommonjs(["@react-navigation/elements"])], | |
}, | |
}, | |
resolve: { | |
extensions: [".web.tsx", ".web.jsx", ".web.js", ".tsx", ".ts", ".js"], | |
alias: { | |
"react-native": "react-native-web", | |
}, | |
}, | |
plugins: [viteCommonjs(), react()], | |
build: { | |
commonjsOptions: { | |
transformMixedEsModules: true, | |
}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment