Skip to content

Commit 29f799b

Browse files
authored
refactor(dark-mode): Switch to V2 + Vite (#505)
1 parent 23d2cc5 commit 29f799b

11 files changed

+152
-51
lines changed

dark-mode/.eslintrc.cjs

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* This is intended to be a basic starting point for linting in your app.
3+
* It relies on recommended configs out of the box for simplicity, but you can
4+
* and should modify this configuration to best suit your team's needs.
5+
*/
6+
7+
/** @type {import('eslint').Linter.Config} */
8+
module.exports = {
9+
root: true,
10+
parserOptions: {
11+
ecmaVersion: "latest",
12+
sourceType: "module",
13+
ecmaFeatures: {
14+
jsx: true,
15+
},
16+
},
17+
env: {
18+
browser: true,
19+
commonjs: true,
20+
es6: true,
21+
},
22+
ignorePatterns: ["!**/.server", "!**/.client"],
23+
24+
// Base config
25+
extends: ["eslint:recommended"],
26+
27+
overrides: [
28+
// React
29+
{
30+
files: ["**/*.{js,jsx,ts,tsx}"],
31+
plugins: ["react", "jsx-a11y"],
32+
extends: [
33+
"plugin:react/recommended",
34+
"plugin:react/jsx-runtime",
35+
"plugin:react-hooks/recommended",
36+
"plugin:jsx-a11y/recommended",
37+
],
38+
settings: {
39+
react: {
40+
version: "detect",
41+
},
42+
formComponents: ["Form"],
43+
linkComponents: [
44+
{ name: "Link", linkAttribute: "to" },
45+
{ name: "NavLink", linkAttribute: "to" },
46+
],
47+
"import/resolver": {
48+
typescript: {},
49+
},
50+
},
51+
},
52+
53+
// Typescript
54+
{
55+
files: ["**/*.{ts,tsx}"],
56+
plugins: ["@typescript-eslint", "import"],
57+
parser: "@typescript-eslint/parser",
58+
settings: {
59+
"import/internal-regex": "^~/",
60+
"import/resolver": {
61+
node: {
62+
extensions: [".ts", ".tsx"],
63+
},
64+
typescript: {
65+
alwaysTryTypes: true,
66+
},
67+
},
68+
},
69+
extends: [
70+
"plugin:@typescript-eslint/recommended",
71+
"plugin:import/recommended",
72+
"plugin:import/typescript",
73+
],
74+
},
75+
76+
// Node
77+
{
78+
files: [".eslintrc.cjs"],
79+
env: {
80+
node: true,
81+
},
82+
},
83+
],
84+
};

dark-mode/.eslintrc.js

-4
This file was deleted.

dark-mode/.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,4 @@ node_modules
22

33
/.cache
44
/build
5-
/public/build
65
.env

dark-mode/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Open this example on [CodeSandbox](https://codesandbox.com):
1010

1111
## Example
1212

13-
In this example, we have a button that toggles the current theme (light / dark). The current value is stored in React context and is used as the className of `<html>` (see [app/root.tsx](app/root.tsx)). Clicking the button toggles the theme, which updates the className, which updates the CSS variables, resulting in the background colour updating (see [app/styles/styles.css](app/styles/styles.css)).
13+
In this example, we have a button that toggles the current theme (light / dark). The current value is stored in React context and is used as the className of `<html>` (see [app/root.tsx](app/root.tsx)). Clicking the button toggles the theme, which updates the className, which updates the CSS variables, resulting in the background color updating (see [app/styles/styles.css](app/styles/styles.css)).
1414

1515
When the theme value updates, we update a cookie value so that the server can persist the theme value in the future (e.g. when the page is refreshed).
1616

dark-mode/app/root.tsx

+10-14
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import type { LoaderArgs, MetaFunction } from "@remix-run/node";
2-
import { json } from "@remix-run/node";
1+
import type { LoaderFunctionArgs } from "@remix-run/node";
32
import {
43
Links,
5-
LiveReload,
64
Meta,
75
Outlet,
86
Scripts,
97
ScrollRestoration,
8+
json,
109
useLoaderData,
1110
} from "@remix-run/react";
1211

@@ -18,37 +17,32 @@ import {
1817
} from "~/utils/theme-provider";
1918
import { getThemeSession } from "~/utils/theme.server";
2019

21-
export const loader = async ({ request }: LoaderArgs) => {
20+
export const loader = async ({ request }: LoaderFunctionArgs) => {
2221
const themeSession = await getThemeSession(request);
2322

2423
return json({
2524
theme: themeSession.getTheme(),
2625
});
2726
};
2827

29-
export const meta: MetaFunction = () => ({
30-
charset: "utf-8",
31-
title: "New Remix App",
32-
viewport: "width=device-width,initial-scale=1",
33-
});
34-
35-
function App() {
28+
function App({ children }: { children: React.ReactNode }) {
3629
const data = useLoaderData<typeof loader>();
3730
const [theme] = useTheme();
3831

3932
return (
4033
<html lang="en" className={theme ?? ""}>
4134
<head>
35+
<meta charSet="utf-8" />
36+
<meta name="viewport" content="width=device-width, initial-scale=1" />
4237
<Meta />
4338
<Links />
4439
<ThemeHead ssrTheme={Boolean(data.theme)} />
4540
</head>
4641
<body>
47-
<Outlet />
42+
{children}
4843
<ThemeBody ssrTheme={Boolean(data.theme)} />
4944
<ScrollRestoration />
5045
<Scripts />
51-
<LiveReload />
5246
</body>
5347
</html>
5448
);
@@ -59,7 +53,9 @@ export default function AppWithProviders() {
5953

6054
return (
6155
<ThemeProvider specifiedTheme={data.theme}>
62-
<App />
56+
<App>
57+
<Outlet />
58+
</App>
6359
</ThemeProvider>
6460
);
6561
}

dark-mode/app/routes/_index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { LinksFunction } from "@remix-run/node";
22

3-
import styles from "~/styles/styles.css";
3+
import styles from "~/styles/styles.css?url";
44
import { Theme, Themed, useTheme } from "~/utils/theme-provider";
55

66
export const links: LinksFunction = () => [{ rel: "stylesheet", href: styles }];

dark-mode/package.json

+25-12
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,40 @@
11
{
2+
"name": "dark-mode",
23
"private": true,
34
"sideEffects": false,
5+
"type": "module",
46
"scripts": {
5-
"build": "remix build",
6-
"dev": "remix dev",
7-
"start": "remix-serve build",
7+
"build": "remix vite:build",
8+
"dev": "remix vite:dev",
9+
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
10+
"start": "remix-serve ./build/server/index.js",
811
"typecheck": "tsc"
912
},
1013
"dependencies": {
11-
"@remix-run/node": "^1.19.3",
12-
"@remix-run/react": "^1.19.3",
13-
"@remix-run/serve": "^1.19.3",
14-
"isbot": "^3.6.5",
14+
"@remix-run/node": "^2.9.2",
15+
"@remix-run/react": "^2.9.2",
16+
"@remix-run/serve": "^2.9.2",
17+
"isbot": "^4.1.0",
1518
"react": "^18.2.0",
1619
"react-dom": "^18.2.0"
1720
},
1821
"devDependencies": {
19-
"@remix-run/dev": "^1.19.3",
20-
"@types/react": "^18.0.25",
21-
"@types/react-dom": "^18.0.8",
22-
"typescript": "^4.8.4"
22+
"@remix-run/dev": "^2.9.2",
23+
"@types/react": "^18.2.20",
24+
"@types/react-dom": "^18.2.7",
25+
"@typescript-eslint/eslint-plugin": "^6.7.4",
26+
"@typescript-eslint/parser": "^6.7.4",
27+
"eslint": "^8.38.0",
28+
"eslint-import-resolver-typescript": "^3.6.1",
29+
"eslint-plugin-import": "^2.28.1",
30+
"eslint-plugin-jsx-a11y": "^6.7.1",
31+
"eslint-plugin-react": "^7.33.2",
32+
"eslint-plugin-react-hooks": "^4.6.0",
33+
"typescript": "^5.1.6",
34+
"vite": "^5.1.0",
35+
"vite-tsconfig-paths": "^4.2.1"
2336
},
2437
"engines": {
25-
"node": ">=14.0.0"
38+
"node": ">=20.0.0"
2639
}
2740
}

dark-mode/remix.config.js

-11
This file was deleted.

dark-mode/remix.env.d.ts

-2
This file was deleted.

dark-mode/tsconfig.json

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
11
{
2-
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
2+
"include": [
3+
"**/*.ts",
4+
"**/*.tsx",
5+
"**/.server/**/*.ts",
6+
"**/.server/**/*.tsx",
7+
"**/.client/**/*.ts",
8+
"**/.client/**/*.tsx"
9+
],
310
"compilerOptions": {
4-
"lib": ["DOM", "DOM.Iterable", "ES2019"],
11+
"lib": ["DOM", "DOM.Iterable", "ES2022"],
12+
"types": ["@remix-run/node", "vite/client"],
513
"isolatedModules": true,
614
"esModuleInterop": true,
715
"jsx": "react-jsx",
8-
"moduleResolution": "node",
16+
"module": "ESNext",
17+
"moduleResolution": "Bundler",
918
"resolveJsonModule": true,
10-
"target": "ES2019",
19+
"target": "ES2022",
1120
"strict": true,
1221
"allowJs": true,
22+
"skipLibCheck": true,
1323
"forceConsistentCasingInFileNames": true,
1424
"baseUrl": ".",
1525
"paths": {
1626
"~/*": ["./app/*"]
1727
},
1828

19-
// Remix takes care of building everything in `remix build`.
29+
// Vite takes care of building everything, not tsc.
2030
"noEmit": true
2131
}
2232
}

dark-mode/vite.config.ts

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { vitePlugin as remix } from "@remix-run/dev";
2+
import { defineConfig } from "vite";
3+
import tsconfigPaths from "vite-tsconfig-paths";
4+
5+
export default defineConfig({
6+
plugins: [
7+
remix({
8+
future: {
9+
v3_fetcherPersist: true,
10+
v3_relativeSplatPath: true,
11+
v3_throwAbortReason: true,
12+
},
13+
}),
14+
tsconfigPaths(),
15+
],
16+
});

0 commit comments

Comments
 (0)