Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

examples: migrate to vite #128

Merged
merged 2 commits into from
May 21, 2024
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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,15 @@ The [examples](examples) folder contains working examples.
You can run one of them with

```bash
PORT=8080 yarn run examples:01_counter
PORT=8080 pnpm run examples:01_counter
```

and open <http://localhost:8080> in your web browser.

You can also try them in codesandbox.io:
[01](https://codesandbox.io/s/github/dai-shi/use-context-selector/tree/main/examples/01_counter)
[02](https://codesandbox.io/s/github/dai-shi/use-context-selector/tree/main/examples/02_person)
[03](https://codesandbox.io/s/github/dai-shi/use-context-selector/tree/main/examples/03_suspense)
You can also try them directly:
[01](https://stackblitz.com/github/dai-shi/use-context-selector/tree/main/examples/01_counter)
[02](https://stackblitz.com/github/dai-shi/use-context-selector/tree/main/examples/02_person)
[03](https://stackblitz.com/github/dai-shi/use-context-selector/tree/main/examples/03_suspense)

## Projects that use use-context-selector

Expand Down
9 changes: 9 additions & 0 deletions examples/01_counter/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
17 changes: 8 additions & 9 deletions examples/01_counter/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
"name": "example",
"version": "0.0.0",
"private": true,
"type": "commonjs",
"type": "module",
"dependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest",
"typescript": "latest",
"use-context-selector": "latest"
},
"devDependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"typescript": "latest",
"vite": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"dev": "vite"
}
}
8 changes: 0 additions & 8 deletions examples/01_counter/public/index.html

This file was deleted.

13 changes: 0 additions & 13 deletions examples/01_counter/src/index.tsx

This file was deleted.

10 changes: 10 additions & 0 deletions examples/01_counter/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import App from './app';

createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
);
14 changes: 14 additions & 0 deletions examples/01_counter/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"strict": true,
"target": "es2018",
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"skipLibCheck": true,
"allowJs": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"jsx": "react-jsx"
}
}
9 changes: 9 additions & 0 deletions examples/02_person/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
17 changes: 8 additions & 9 deletions examples/02_person/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
"name": "example",
"version": "0.0.0",
"private": true,
"type": "commonjs",
"type": "module",
"dependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest",
"typescript": "latest",
"use-context-selector": "latest"
},
"devDependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"typescript": "latest",
"vite": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"dev": "vite"
}
}
8 changes: 0 additions & 8 deletions examples/02_person/public/index.html

This file was deleted.

13 changes: 0 additions & 13 deletions examples/02_person/src/index.tsx

This file was deleted.

10 changes: 10 additions & 0 deletions examples/02_person/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import App from './app';

createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
);
14 changes: 14 additions & 0 deletions examples/02_person/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"strict": true,
"target": "es2018",
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"skipLibCheck": true,
"allowJs": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"jsx": "react-jsx"
}
}
9 changes: 9 additions & 0 deletions examples/03_suspense/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<head>
<title>example</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
17 changes: 8 additions & 9 deletions examples/03_suspense/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,19 @@
"name": "example",
"version": "0.0.0",
"private": true,
"type": "commonjs",
"type": "module",
"dependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"react": "latest",
"react-dom": "latest",
"react-scripts": "latest",
"typescript": "latest",
"use-context-selector": "latest"
},
"devDependencies": {
"@types/react": "latest",
"@types/react-dom": "latest",
"typescript": "latest",
"vite": "latest"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
"dev": "vite"
}
}
8 changes: 0 additions & 8 deletions examples/03_suspense/public/index.html

This file was deleted.

13 changes: 0 additions & 13 deletions examples/03_suspense/src/index.tsx

This file was deleted.

10 changes: 10 additions & 0 deletions examples/03_suspense/src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';

import App from './app';

createRoot(document.getElementById('root')!).render(
<StrictMode>
<App />
</StrictMode>,
);
14 changes: 14 additions & 0 deletions examples/03_suspense/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"compilerOptions": {
"strict": true,
"target": "es2018",
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "bundler",
"skipLibCheck": true,
"allowJs": true,
"noUncheckedIndexedAccess": true,
"exactOptionalPropertyTypes": true,
"jsx": "react-jsx"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
"scheduler": "^0.23.2",
"ts-expect": "^1.3.0",
"typescript": "^5.4.5",
"use-context-selector": "link:.",
"vite": "^5.2.11",
"vitest": "^1.6.0"
},
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

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

1 change: 0 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"compilerOptions": {
"strict": true,
"target": "es2018",
"downlevelIteration": true,
"esModuleInterop": true,
"module": "nodenext",
"skipLibCheck": true,
Expand Down
39 changes: 0 additions & 39 deletions vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
/// <reference types="vitest" />

import { resolve } from 'node:path';
import { readFileSync } from 'node:fs';
import { defineConfig } from 'vite';
import type { Plugin } from 'vite';

const { DIR, PORT = '8080' } = process.env;

Expand All @@ -24,42 +22,5 @@ export default defineConfig(({ mode }) => {
root: resolve('examples', DIR),
server: { port: Number(PORT) },
resolve: { alias: { 'use-context-selector': resolve('src') } },
plugins: [indexHtml(resolve('examples', DIR, 'public', 'index.html'))],
};
});

function indexHtml(file: string): Plugin {
const html = readFileSync(file, 'utf8');
return {
name: 'index-html-plugin',
configureServer(server) {
return () => {
server.middlewares.use((req, res) => {
server
.transformIndexHtml(req.url || '', html)
.then((content) => {
res.statusCode = 200;
res.setHeader('content-type', 'text/html; charset=utf-8');
res.end(content);
})
.catch((err) => {
console.error('Error transforming index.html', err);
res.statusCode = 500;
res.end('Internal Server Error');
});
});
};
},
config() {
return { optimizeDeps: { entries: ['src/index'] } };
},
transformIndexHtml() {
return [
{
tag: 'script',
attrs: { type: 'module', src: '/src/index' },
},
];
},
};
}
Loading