Skip to content

Commit edf454b

Browse files
committed
Update packages to latest versions and reduce initial bundle size
1 parent d66228d commit edf454b

File tree

17 files changed

+515
-613
lines changed

17 files changed

+515
-613
lines changed

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
save-prefix=

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
22
"editor.formatOnSave": true
3-
}
3+
}

package.json

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,41 +3,43 @@
33
"dev": "npm run server",
44
"prod": "npm run build && npm run server:prod",
55
"build": "vite build",
6-
"server": "ts-node ./server/index.ts",
7-
"server:prod": "cross-env NODE_ENV=production ts-node ./server/index.ts"
6+
"server": "tsx ./server/index.ts",
7+
"server:prod": "cross-env NODE_ENV=production tsx ./server/index.ts"
88
},
99
"dependencies": {
10-
"@types/compression": "^1.7.2",
11-
"@types/express": "^4.17.14",
12-
"@types/node": "^18.11.9",
13-
"@types/react": "^18.0.8",
14-
"@types/react-dom": "^18.0.3",
15-
"@vitejs/plugin-react": "^3.0.0",
16-
"compression": "^1.7.4",
17-
"cross-env": "^7.0.3",
18-
"express": "^4.18.1",
19-
"indent-string": "^5.0.0",
20-
"mobx": "^6.9.0",
21-
"mobx-react-lite": "^3.4.3",
22-
"node-sql-parser": "^4.6.6",
23-
"react": "^18.2.0",
24-
"react-dom": "^18.2.0",
25-
"sirv": "^2.0.2",
26-
"sql-formatter": "^12.2.0",
27-
"ts-node": "^10.9.1",
28-
"typescript": "^4.9.4",
29-
"vite": "^4.0.3",
30-
"vite-plugin-ssr": "^0.4.112"
10+
"@types/compression": "1.7.5",
11+
"@types/express": "4.17.21",
12+
"@types/node": "20.11.7",
13+
"@types/react": "18.2.48",
14+
"@types/react-dom": "18.2.18",
15+
"@vitejs/plugin-react": "4.2.1",
16+
"compression": "1.7.4",
17+
"cross-env": "7.0.3",
18+
"express": "4.18.2",
19+
"indent-string": "5.0.0",
20+
"lodash-es": "4.17.21",
21+
"mobx": "6.12.0",
22+
"mobx-react-lite": "4.0.5",
23+
"node-sql-parser": "4.18.0",
24+
"react": "18.2.0",
25+
"react-dom": "18.2.0",
26+
"sirv": "2.0.4",
27+
"sql-formatter": "15.1.3",
28+
"typescript": "5.3.3",
29+
"vike": "0.4.160",
30+
"vite": "5.0.12"
3131
},
3232
"type": "module",
3333
"devDependencies": {
34-
"@tailwindcss/forms": "^0.5.3",
35-
"@tailwindcss/typography": "^0.5.9",
36-
"autoprefixer": "^10.4.14",
37-
"postcss": "^8.4.21",
38-
"prettier": "^2.8.7",
39-
"serve": "^14.2.0",
40-
"tailwindcss": "^3.3.1"
34+
"@tailwindcss/forms": "0.5.7",
35+
"@tailwindcss/typography": "0.5.10",
36+
"@types/lodash-es": "4.17.12",
37+
"autoprefixer": "10.4.17",
38+
"postcss": "8.4.33",
39+
"prettier": "3.2.4",
40+
"serve": "14.2.1",
41+
"tailwindcss": "3.4.1",
42+
"tsx": "4.7.0"
4143
},
4244
"packageManager": "[email protected]+sha1.872b21adeaae4e71acd5c551ba848d927d6aa446"
4345
}

renderer/_error.page.tsx renamed to pages/_error/+Page.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
import React from 'react';
2-
3-
export { Page };
4-
5-
function Page({ is404 }: { is404: boolean }) {
1+
export function Page({ is404 }: { is404: boolean }) {
62
if (is404) {
73
return (
84
<>

pages/index/index.page.tsx renamed to pages/index/+Page.tsx

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
import { action, remove } from 'mobx';
21
import { observer } from 'mobx-react-lite';
3-
import { formatDialect, sqlite } from 'sql-formatter';
4-
import { DEFAULT_QUERY, Query, parseOne, state } from './state';
5-
import { FC } from 'react';
2+
import { Fragment } from 'react';
3+
import {
4+
bindMobxInput,
5+
formatQueryAction,
6+
parseOne,
7+
removeQueryAction,
8+
state,
9+
type Query,
10+
} from './state';
611

712
export const Page = observer(function Page() {
813
const parsed = state.parsed;
@@ -24,21 +29,17 @@ export const Page = observer(function Page() {
2429
</div>
2530

2631
{state.queries.map((query, index) => (
27-
<>
32+
<Fragment key={index}>
2833
{index !== 0 ? <hr /> : null}
29-
<QueryEditor key={index} index={index} query={query} />
30-
</>
34+
<QueryEditor index={index} query={query} />
35+
</Fragment>
3136
))}
3237

3338
<hr />
3439

3540
<div>
3641
<button
37-
onClick={action(() => {
38-
const newQuery = { ...DEFAULT_QUERY };
39-
newQuery.name += '_' + (state.queries.length + 1);
40-
state.queries.push(newQuery);
41-
})}
42+
onClick={state.addQuery}
4243
className="p-1 border bg-gray-100 hover:bg-gray-200 active:bg-gray-200"
4344
>
4445
Add another query
@@ -71,31 +72,22 @@ const QueryEditor = observer(function QueryEditor({
7172
<input
7273
type="text"
7374
className="form-input rounded"
74-
value={query.name}
75-
onChange={action((event) => (query.name = event.target.value))}
75+
{...bindMobxInput(query, 'name')}
7676
/>
7777
<textarea
7878
className="form-textarea w-full rounded"
7979
rows={10}
80-
value={query.sql}
81-
onChange={action((event) => (query.sql = event.target.value))}
80+
{...bindMobxInput(query, 'sql')}
8281
/>
8382
<div className="flex gap-2">
8483
<button
85-
onClick={action(() => {
86-
query.sql = formatDialect(query.sql, {
87-
dialect: sqlite,
88-
tabWidth: 2,
89-
});
90-
})}
84+
onClick={formatQueryAction(query)}
9185
className="p-1 border bg-gray-100 hover:bg-gray-200 active:bg-gray-200"
9286
>
9387
Format
9488
</button>
9589
<button
96-
onClick={action(() => {
97-
remove(state.queries, index as any);
98-
})}
90+
onClick={removeQueryAction(index)}
9991
className="p-1 border bg-gray-100 hover:bg-gray-200 active:bg-gray-200"
10092
>
10193
Remove query

pages/index/state.ts

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import indentString from 'indent-string';
2-
import { observable } from 'mobx';
2+
import { once } from 'lodash-es';
3+
import { action, makeAutoObservable, remove, runInAction } from 'mobx';
34
import type { Column } from 'node-sql-parser';
4-
import { Parser } from 'node-sql-parser';
5+
import nodeSqlParser from 'node-sql-parser'; // https://vitejs.dev/guide/migration#ssr-externalized-modules-value-now-matches-production
6+
import type { ChangeEvent } from 'react';
7+
8+
const getParser = once(() => {
9+
const { Parser } = nodeSqlParser;
10+
return new Parser();
11+
});
512

613
export type Query = {
714
name: string;
@@ -20,19 +27,64 @@ limit
2027
2`,
2128
};
2229

23-
export const state = observable({
24-
queries: [DEFAULT_QUERY] as Query[],
30+
export const state = makeAutoObservable(
31+
{
32+
queries: [DEFAULT_QUERY] as Query[],
2533

26-
get parsed() {
27-
try {
28-
const jsonQuery = generateJsonQuery(this.queries);
29-
return jsonQuery;
30-
} catch (err) {
31-
console.warn(err);
32-
return undefined;
33-
}
34+
get parsed() {
35+
try {
36+
const jsonQuery = generateJsonQuery(this.queries);
37+
return jsonQuery;
38+
} catch (err) {
39+
console.warn(err);
40+
return undefined;
41+
}
42+
},
43+
44+
addQuery() {
45+
const newQuery = { ...DEFAULT_QUERY };
46+
newQuery.name += '_' + (this.queries.length + 1);
47+
this.queries.push(newQuery);
48+
},
3449
},
35-
});
50+
{},
51+
{
52+
autoBind: true,
53+
},
54+
);
55+
56+
// TODO: is there a way to define the action more
57+
// efficiently?
58+
export const formatQueryAction = (query: Query) => async () => {
59+
const { formatDialect, sqlite } = await import('sql-formatter');
60+
61+
const sql = formatDialect(query.sql, {
62+
dialect: sqlite,
63+
tabWidth: 2,
64+
});
65+
66+
runInAction(() => {
67+
query.sql = sql;
68+
});
69+
};
70+
71+
// TODO: is there a way to define the action more
72+
// efficiently?
73+
export const removeQueryAction = (index: number) =>
74+
action(() => {
75+
remove(state.queries, index as any);
76+
});
77+
78+
export function bindMobxInput<T, K extends keyof T>(model: T, field: K) {
79+
return {
80+
value: model[field],
81+
onChange: action(
82+
(event: ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
83+
(model as any)[field] = event.target.value;
84+
},
85+
),
86+
};
87+
}
3688

3789
function getColumnNames(columns: Column[]) {
3890
const names: string[] = [];
@@ -52,7 +104,7 @@ function getColumnNames(columns: Column[]) {
52104

53105
export function parseOne(query: Query) {
54106
try {
55-
const parser = new Parser();
107+
const parser = getParser();
56108
const ast = parser.astify(query.sql, { database: 'sqlite' });
57109
console.log(ast);
58110
const first = Array.isArray(ast) ? ast[0] : ast;

0 commit comments

Comments
 (0)