UNPKG

@tanstack/react-router

Version:

Modern and scalable routing for React applications

145 lines (144 loc) 5.24 kB
const require_useRouter = require("./useRouter.cjs"); const require_useMatch = require("./useMatch.cjs"); const require_useLoaderData = require("./useLoaderData.cjs"); const require_useLoaderDeps = require("./useLoaderDeps.cjs"); const require_useParams = require("./useParams.cjs"); const require_useSearch = require("./useSearch.cjs"); const require_useNavigate = require("./useNavigate.cjs"); const require_useRouteContext = require("./useRouteContext.cjs"); const require_route = require("./route.cjs"); //#region src/fileRoute.ts /** * Creates a file-based Route factory for a given path. * * Used by TanStack Router's file-based routing to associate a file with a * route. The returned function accepts standard route options. In normal usage * the `path` string is inserted and maintained by the `tsr` generator. * * @param path File path literal for the route (usually auto-generated). * @returns A function that accepts Route options and returns a Route instance. * @link https://tanstack.com/router/latest/docs/framework/react/api/router/createFileRouteFunction */ function createFileRoute(path) { if (typeof path === "object") return new FileRoute(path, { silent: true }).createRoute(path); return new FileRoute(path, { silent: true }).createRoute; } /** @deprecated It's no longer recommended to use the `FileRoute` class directly. Instead, use `createFileRoute('/path/to/file')(options)` to create a file route. */ var FileRoute = class { constructor(path, _opts) { this.path = path; this.createRoute = (options) => { if (process.env.NODE_ENV !== "production") { if (!this.silent) console.warn("Warning: FileRoute is deprecated and will be removed in the next major version. Use the createFileRoute(path)(options) function instead."); } const route = require_route.createRoute(options); route.isRoot = false; return route; }; this.silent = _opts?.silent; } }; /** @deprecated It's recommended not to split loaders into separate files. Instead, place the loader function in the main route file via `createFileRoute`. */ function FileRouteLoader(_path) { if (process.env.NODE_ENV !== "production") console.warn(`Warning: FileRouteLoader is deprecated and will be removed in the next major version. Please place the loader function in the the main route file, inside the \`createFileRoute('/path/to/file')(options)\` options`); return (loaderFn) => loaderFn; } var LazyRoute = class { constructor(opts) { this.useMatch = (opts) => { return require_useMatch.useMatch({ select: opts?.select, from: this.options.id, structuralSharing: opts?.structuralSharing }); }; this.useRouteContext = (opts) => { return require_useRouteContext.useRouteContext({ ...opts, from: this.options.id }); }; this.useSearch = (opts) => { return require_useSearch.useSearch({ select: opts?.select, structuralSharing: opts?.structuralSharing, from: this.options.id }); }; this.useParams = (opts) => { return require_useParams.useParams({ select: opts?.select, structuralSharing: opts?.structuralSharing, from: this.options.id }); }; this.useLoaderDeps = (opts) => { return require_useLoaderDeps.useLoaderDeps({ ...opts, from: this.options.id }); }; this.useLoaderData = (opts) => { return require_useLoaderData.useLoaderData({ ...opts, from: this.options.id }); }; this.useNavigate = () => { return require_useNavigate.useNavigate({ from: require_useRouter.useRouter().routesById[this.options.id].fullPath }); }; this.options = opts; } }; /** * Creates a lazily-configurable code-based route stub by ID. * * Use this for code-splitting with code-based routes. The returned function * accepts only non-critical route options like `component`, `pendingComponent`, * `errorComponent`, and `notFoundComponent` which are applied when the route * is matched. * * @param id Route ID string literal to associate with the lazy route. * @returns A function that accepts lazy route options and returns a `LazyRoute`. * @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyRouteFunction */ function createLazyRoute(id) { return (opts) => { return new LazyRoute({ id, ...opts }); }; } /** * Creates a lazily-configurable file-based route stub by file path. * * Use this for code-splitting with file-based routes (eg. `.lazy.tsx` files). * The returned function accepts only non-critical route options like * `component`, `pendingComponent`, `errorComponent`, and `notFoundComponent`. * * @param id File path literal for the route file. * @returns A function that accepts lazy route options and returns a `LazyRoute`. * @link https://tanstack.com/router/latest/docs/framework/react/api/router/createLazyFileRouteFunction */ function createLazyFileRoute(id) { if (typeof id === "object") return new LazyRoute(id); return (opts) => new LazyRoute({ id, ...opts }); } //#endregion exports.FileRoute = FileRoute; exports.FileRouteLoader = FileRouteLoader; exports.LazyRoute = LazyRoute; exports.createFileRoute = createFileRoute; exports.createLazyFileRoute = createLazyFileRoute; exports.createLazyRoute = createLazyRoute; //# sourceMappingURL=fileRoute.cjs.map