Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

feat: implement code splitting by routes #18

Merged
merged 1 commit into from
Jan 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[*]
root = true
indent_size = 2
indent_style = space
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vite.config.ts
dist
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pnpm-lock.yaml
dist
9 changes: 9 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,36 @@ Here's how to contribute a pull request:

1. **Fork the repository**: If you don't already have a copy of the project on your own GitHub account, you'll need to fork the repository to create one. To do this, click the "Fork" button in the top-right corner of the repository page on GitHub. This will create a copy of the repository on your own account.
2. **Clone the repository**: Once you have a copy of the repository on your own account, you'll need to clone it to your local machine. To do this, open a terminal and navigate to the directory where you want to store the project. Then run the following command, replacing [YOUR_USERNAME] with your GitHub username and [YOUR_REPOSITORY] with the name of the repository:

```sh
git clone https://github.com/[YOUR_USERNAME]/[YOUR_REPOSITORY].git
```

3. **Create a new branch**: Before you start making changes to the code, it's a good idea to create a new branch. This allows you to work on your changes without affecting the main branch of the repository. To create a new branch, run the following command:

```sh
git checkout -b [BRANCH_NAME]
```

Replace [BRANCH_NAME] with a descriptive name for your branch, such as "fix-typo" or "add-feature".

4. **Make your changes**: Now you can start making the changes you want to contribute to the project. Make sure to test your changes thoroughly to ensure that they work as intended.
5. Commit your changes: When you're satisfied with your changes, you'll need to commit them to your local repository. To do this, run the following command

```sh
git commit -am "[COMMIT_MESSAGE]"
```

Replace [COMMIT_MESSAGE] with a brief description of your changes.

6. Push your changes to GitHub: Now that your changes are committed to your local repository, you'll need to push them to your fork on GitHub. To do this, run the following command:

```sh
git push origin [BRANCH_NAME]
```

7. **Submit a pull request**: Once your changes are pushed to your fork on GitHub, you can submit a pull request to the project maintainers. To do this, go to the repository page on GitHub and click the "Compare & pull request" button. This will open a form where you can write a brief description of your changes and submit the pull request for review.

## Code of Conduct

We want the Polinema Innovation Tribe project to be an inclusive and welcoming community for everyone. To that end, we have a code of conduct that we ask all contributors to follow. Please make sure you read it before participating.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"devDependencies": {
"@iconify-json/heroicons": "^1.1.6",
"@svgr/core": "^6.5.1",
"@types/node": "^18.11.18",
"@types/react": "^18.0.26",
"@types/react-dom": "^18.0.9",
"@typescript-eslint/eslint-plugin": "^5.47.1",
Expand Down
13 changes: 10 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Empty file removed src/App.css
Empty file.
28 changes: 0 additions & 28 deletions src/App.tsx

This file was deleted.

17 changes: 17 additions & 0 deletions src/components/Layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Suspense } from "react";
import { Outlet } from "react-router-dom";
import Navbar from "./Navbar";
import { PendingFallback } from "./PendingFallback";

export function Layout() {
return (
<>
<Navbar />
<main className="m-8">
<Suspense fallback={<PendingFallback />}>
<Outlet />
</Suspense>
</main>
</>
);
}
4 changes: 2 additions & 2 deletions src/components/Navbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ function Navbar() {
<div className="md:flex md:items-center lg:max-w-[92rem] md:px-8 mx-auto px-4 justify-between">
<div id="left-side">
<div className="flex items-center justify-between py-3 md:py-5 md:block">
<a href="/">
<NavLink to="/">
<h2 className="text-2xl font-bold text-white">Politribe</h2>
</a>
</NavLink>
<div className="md:hidden">
<button
className="p-2 text-gray-700 rounded-md outline-none "
Expand Down
28 changes: 28 additions & 0 deletions src/components/PendingFallback.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export function PendingFallback() {
return (
<div
aria-label="Loading"
className="w-full h-full flex items-center justify-center"
>
<div role="status">
<svg
aria-hidden="true"
className="mr-2 w-24 h-24 text-gray-700 animate-spin dark:text-gray-600 fill-blue-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
</div>
);
}
6 changes: 6 additions & 0 deletions src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
@apply font-bold text-white;
}

html,
body,
#root {
@apply h-full w-full;
}

body {
@apply bg-[#131A22];
}
Expand Down
37 changes: 30 additions & 7 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { AppRoutes } from "./routes/AppRoutes";
import "./index.css";

ReactDOM.createRoot(document.getElementById("root") as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
const root = document.getElementById("root") as HTMLElement;
createRoot(root).render(
<StrictMode>
<AppRoutes />
</StrictMode>
);

const showErrorOverlay = (err: unknown) => {
const elementName = "vite-error-overlay";
const ErrorOverlay = customElements.get(elementName);

// prevent double overlay
const isAlreadyAppear = document.body.contains(
document.querySelector(elementName)
);
const isDev = import.meta.env.DEV;

if (!ErrorOverlay || isAlreadyAppear || !isDev) {
return;
}

document.body.appendChild(new ErrorOverlay(err));
};

window.addEventListener("error", showErrorOverlay);
window.addEventListener("unhandledrejection", ({ reason }) =>
showErrorOverlay(reason)
);
34 changes: 34 additions & 0 deletions src/routes/AppRoutes.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import {
createBrowserRouter,
Navigate,
RouterProvider,
} from "react-router-dom";
import { Layout } from "~/components/Layout";
import { PendingFallback } from "~/components/PendingFallback";
import { lazyImport } from "~/utils/lazy-import";

const { CompetitionPage } = lazyImport(
() => import("./CompetitionPage"),
"CompetitionPage"
);
const { HomePage } = lazyImport(() => import("./HomePage"), "HomePage");
const { IdeaPage } = lazyImport(() => import("./IdeaPage"), "IdeaPage");

export function AppRoutes() {
const router = createBrowserRouter([
{
path: "*",
element: <Layout />,
children: [
{ index: true, element: <HomePage /> },
{ path: "idea", element: <IdeaPage /> },
{ path: "competition", element: <CompetitionPage /> },
{ path: "*", element: <Navigate replace to="/" /> },
],
},
]);

return (
<RouterProvider fallbackElement={<PendingFallback />} router={router} />
);
}
4 changes: 1 addition & 3 deletions src/pages/Competition.tsx → src/routes/CompetitionPage.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import React from "react";

function Competition() {
export function CompetitionPage() {
return <div className={"text-white"}>Competition page</div>;
}

export default Competition;
5 changes: 2 additions & 3 deletions src/pages/Home.tsx → src/routes/HomePage.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import React, { useEffect } from "react";
import Hero from "../parts/HomePage/Hero";

function Home() {
export function HomePage() {
useEffect(() => {
document.title = "Politribe | Home";
}, []);

return (
<div>
<Hero />
<div className={"border-slate-700 border mt-12 block w-full"}></div>
</div>
);
}

export default Home;
4 changes: 1 addition & 3 deletions src/pages/Idea.tsx → src/routes/IdeaPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React, { useEffect } from "react";

function Idea() {
export function IdeaPage() {
useEffect(() => {
document.title = "Politribe | Idea";
}, []);
return <div className={"text-white"}>Idea page</div>;
}

export default Idea;
23 changes: 23 additions & 0 deletions src/utils/lazy-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as React from "react";

/**
* Named imports for React.lazy
*
* @links {@see https://github.com/facebook/react/issues/14603#issuecomment-726551598 GitHub comment}
*
* @usage
* ```tsx
* const { Home } = lazyImport(() => import("./Home"), "Home");
* ```
*/
export function lazyImport<
T extends React.ComponentType,
I extends { [K2 in K]: T },
K extends keyof I
>(factory: () => Promise<I>, name: K): I {
return Object.create({
[name]: React.lazy(() =>
factory().then((module) => ({ default: module[name] }))
),
});
}
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"types": ["unplugin-icons/types/react"]
"types": ["unplugin-icons/types/react"],
"baseUrl": ".",
"paths": {
"~/*": ["src/*"]
}
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
Expand Down
6 changes: 6 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import Icons from "unplugin-icons/vite";
import path from "node:path";

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), Icons({ compiler: "jsx", jsx: "react" })],
resolve: {
alias: {
"~": path.resolve("src"),
},
},
});