Skip to content

Commit

Permalink
fix: auto generate declaration file (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
ksocha committed Apr 24, 2020
1 parent 048a5b2 commit 250b85b
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 159 deletions.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"author": "Justin Schrader <me@justin.beer>",
"license": "MIT",
"private": false,
"types": "types/use-react-router-breadcrumbs/index.d.ts",
"types": "dist/index.d.ts",
"peerDependencies": {
"react": ">=16.8",
"react-router": ">=5.1.0"
Expand All @@ -19,9 +19,7 @@
"build": "rollup -c",
"test": "jest",
"test-build": "sh ./scripts/test-build.sh",
"types": "yarn type-src && yarn type-descriptions",
"type-src": "tsc -p tsconfig.json",
"type-descriptions": "tsc -p types/use-react-router-breadcrumbs",
"types": "tsc -p tsconfig.json",
"lint": "eslint ./src/**"
},
"husky": {
Expand Down Expand Up @@ -66,6 +64,7 @@
"react-router": "^5.1.2",
"rollup": "^2.1.0",
"rollup-plugin-babel": "^4.4.0",
"rollup-plugin-typescript2": "^0.27.0",
"rollup-plugin-uglify": "^6.0.2",
"typescript": "^3.8.3"
},
Expand Down
11 changes: 11 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import babel from 'rollup-plugin-babel';
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import { uglify } from 'rollup-plugin-uglify';
import typescript from 'rollup-plugin-typescript2';
import ts from 'typescript';

const pkg = require('./package.json');

Expand All @@ -10,6 +12,15 @@ const external = Object.keys(pkg.peerDependencies);
const extensions = ['.js', '.tsx'];

const plugins = [
typescript({
useTsconfigDeclarationDir: true,
tsconfigOverride: {
typescript: ts,
compilerOptions: {
module: 'es2015',
},
},
}),
babel({
exclude: 'node_modules/**',
extensions,
Expand Down
67 changes: 44 additions & 23 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,34 @@
import React, { createElement } from 'react';
import { matchPath, useLocation } from 'react-router';

// eslint-disable-next-line import/extensions, import/no-unresolved, no-unused-vars
import * as types from '../types/use-react-router-breadcrumbs/index';
type Location = ReturnType<typeof useLocation>;

export interface Options {
currentSection?: string;
disableDefaults?: boolean;
excludePaths?: string[];
pathSection?: string;
}

export interface MatchOptions {
exact?: boolean;
strict?: boolean;
sensitive?: boolean;
}

export interface BreadcrumbsRoute {
path: string;
breadcrumb?: React.ComponentType | React.ElementType | string;
matchOptions?: MatchOptions;
routes?: BreadcrumbsRoute[];
}

export interface BreadcrumbData {
match: { url: string },
location: Location,
key: string,
breadcrumb: React.ReactNode
}

const DEFAULT_MATCH_OPTIONS = { exact: true };
const NO_BREADCRUMB = 'NO_BREADCRUMB';
Expand All @@ -49,13 +75,8 @@ const render = ({
}: {
breadcrumb: React.ComponentType | string,
match: { url: string },
location: types.Location
}): {
match: { url: string },
location: types.Location,
key: string,
breadcrumb: React.ReactNode
} => {
location: Location
}): BreadcrumbData => {
const componentProps = { match, location, key: match.url, ...rest };

return {
Expand All @@ -75,7 +96,7 @@ const getDefaultBreadcrumb = ({
pathSection,
}: {
currentSection: string,
location: types.Location,
location: Location,
pathSection: string,
}) => {
const match = matchPath(pathSection, { ...DEFAULT_MATCH_OPTIONS, path: pathSection })
Expand Down Expand Up @@ -104,11 +125,11 @@ const getBreadcrumbMatch = ({
currentSection: string,
disableDefaults?: boolean,
excludePaths?: string[],
location: { pathname: string },
location: Location,
pathSection: string,
routes: types.BreadcrumbsRoute[]
routes: BreadcrumbsRoute[]
}) => {
let breadcrumb;
let breadcrumb: BreadcrumbData | typeof NO_BREADCRUMB | undefined;

// Check the optional `excludePaths` option in `options` to see if the
// current path should not include a breadcrumb.
Expand Down Expand Up @@ -189,12 +210,12 @@ export const getBreadcrumbs = (
location,
options = {},
}: {
routes: types.BreadcrumbsRoute[],
location: types.Location,
options?: types.Options
routes: BreadcrumbsRoute[],
location: Location,
options?: Options
},
): Array<React.ReactNode | string> => {
const matches:Array<React.ReactNode | string> = [];
): Array<BreadcrumbData> => {
const matches: Array<BreadcrumbData> = [];
const { pathname } = location;

pathname
Expand Down Expand Up @@ -238,20 +259,20 @@ export const getBreadcrumbs = (
* Takes a route array and recursively flattens it IF there are
* nested routes in the config.
*/
const flattenRoutes = (routes: types.BreadcrumbsRoute[]) => (routes)
.reduce((arr, route: types.BreadcrumbsRoute): types.BreadcrumbsRoute[] => {
const flattenRoutes = (routes: BreadcrumbsRoute[]) => (routes)
.reduce((arr, route: BreadcrumbsRoute): BreadcrumbsRoute[] => {
if (route.routes) {
return arr.concat([route, ...flattenRoutes(route.routes)]);
}
return arr.concat(route);
}, [] as types.BreadcrumbsRoute[]);
}, [] as BreadcrumbsRoute[]);

/**
* Default hook function export.
*/
const useReactRouterBreadcrumbs = (
routes?: types.BreadcrumbsRoute[],
options?: types.Options,
routes?: BreadcrumbsRoute[],
options?: Options,
) => getBreadcrumbs({
routes: flattenRoutes(routes || []),
location: useLocation(),
Expand Down
11 changes: 8 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
"checkJs": true,
"esModuleInterop": true,
"jsx": "react",
"lib": ["es2015", "dom"],
"lib": [
"es2015",
"dom"
],
"module": "commonjs",
"moduleResolution": "node",
"noEmit": true,
Expand All @@ -17,9 +20,11 @@
"strictFunctionTypes": true,
"strictNullChecks": true,
"strictPropertyInitialization": true,
"target": "es5"
"target": "es5",
"declaration": true,
"declarationDir": "./dist"
},
"files": [
"src/index.tsx"
]
}
}
10 changes: 0 additions & 10 deletions types/use-react-router-breadcrumbs/index-tests.tsx

This file was deleted.

36 changes: 0 additions & 36 deletions types/use-react-router-breadcrumbs/index.d.ts

This file was deleted.

27 changes: 0 additions & 27 deletions types/use-react-router-breadcrumbs/tsconfig.json

This file was deleted.

This file was deleted.

Loading

0 comments on commit 250b85b

Please sign in to comment.