Skip to content

Commit 3ccbba8

Browse files
committed
basic cleanup, add TSX, imporve production bundle size
1 parent d0a9912 commit 3ccbba8

10 files changed

+385
-3235
lines changed

Diff for: manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "react-starter",
33
"name": "React Starter",
4-
"description": "react starter...",
4+
"description": "Obsidian + ReactJS",
55
"version": "0.0.0",
66
"author": "Liam Cain",
77
"authorUrl": "https://github.com/liamcain/",

Diff for: package.json

+17-28
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,44 @@
11
{
2-
"name": "react-starter",
2+
"name": "obsidian-react-starter",
33
"version": "0.0.0",
4-
"description": "Sync Things.app Logbook with Obsidian",
4+
"description": "Obsidian + ReactJS",
55
"author": "liamcain",
66
"main": "main.js",
77
"license": "MIT",
88
"scripts": {
99
"lint": "eslint . --ext .ts",
10-
"build": "npm run lint && rollup -c",
11-
"build:nolint": "rollup -c",
10+
"dev": "npm run lint && rollup -c",
11+
"build:nolint": "NODE_ENV=production rollup -c",
12+
"build": "NODE_ENV=production npm run lint && rollup -c",
1213
"test": "jest",
1314
"test:watch": "yarn test -- --watch"
1415
},
1516
"dependencies": {
16-
"@types/react": "^17.0.3",
17-
"@types/react-dom": "^17.0.2",
17+
"@types/react": "17.0.3",
18+
"@types/react-dom": "17.0.2",
1819
"obsidian": "obsidianmd/obsidian-api#master",
19-
"obsidian-daily-notes-interface": "0.7.7",
20-
"papaparse": "5.3.0",
21-
"react": "^17.0.1",
22-
"react-dom": "^17.0.1",
23-
"tslib": "^2.1.0"
20+
"react": "17.0.1",
21+
"react-dom": "17.0.1",
22+
"tslib": "2.1.0"
2423
},
2524
"devDependencies": {
26-
"@babel/preset-react": "^7.12.13",
27-
"@rollup/plugin-babel": "^5.3.0",
25+
"@babel/core": "7.13.8",
26+
"@babel/preset-react": "7.12.13",
27+
"@babel/preset-typescript": "7.13.0",
28+
"@rollup/plugin-babel": "5.3.0",
2829
"@rollup/plugin-commonjs": "17.1.0",
2930
"@rollup/plugin-json": "4.1.0",
31+
"@rollup/plugin-typescript": "8.2.0",
3032
"@rollup/plugin-node-resolve": "11.2.0",
3133
"@rollup/plugin-replace": "2.4.1",
32-
"@types/jest": "26.0.20",
3334
"@types/moment": "2.13.0",
3435
"@types/node": "14.14.34",
3536
"@types/papaparse": "5.2.5",
3637
"@typescript-eslint/eslint-plugin": "4.17.0",
3738
"@typescript-eslint/parser": "4.17.0",
38-
"babel": "^6.23.0",
39+
"babel": "6.23.0",
3940
"eslint": "7.22.0",
40-
"jest": "26.6.3",
41-
"moment": "2.29.1",
4241
"rollup": "2.41.2",
43-
"rollup-plugin-typescript2": "^0.30.0",
44-
"typescript": "^4.2.3"
45-
},
46-
"jest": {
47-
"moduleNameMapper": {
48-
"src/(.*)": "<rootDir>/src/$1"
49-
},
50-
"moduleFileExtensions": [
51-
"js",
52-
"ts"
53-
]
42+
"typescript": "4.2.3"
5443
}
5544
}

Diff for: rollup.config.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import babel from "@rollup/plugin-babel";
2-
import resolve from "@rollup/plugin-node-resolve";
32
import commonjs from "@rollup/plugin-commonjs";
3+
import replace from "@rollup/plugin-replace";
4+
import resolve from "@rollup/plugin-node-resolve";
45
import typescript from "@rollup/plugin-typescript";
6+
import { env } from "process";
57

68
export default {
79
input: "src/index.ts",
@@ -16,8 +18,11 @@ export default {
1618
resolve({
1719
browser: true,
1820
}),
21+
replace({
22+
"process.env.NODE_ENV": JSON.stringify(env.NODE_ENV),
23+
}),
1924
babel({
20-
presets: ["@babel/preset-react"],
25+
presets: ["@babel/preset-react", "@babel/preset-typescript"],
2126
}),
2227
commonjs(),
2328
],

Diff for: src/constants.ts

-3
This file was deleted.

Diff for: src/fileUtils.ts

-12
This file was deleted.

Diff for: src/index.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import ReactDOM from "react-dom";
44

55
import DiceRoller from "./ui/DicerRoller";
66

7+
const VIEW_TYPE = "react-view";
8+
79
class MyReactView extends ItemView {
8-
private reactComponent: any;
10+
private reactComponent: React.ReactElement;
911

1012
getViewType(): string {
11-
return "react-view";
13+
return VIEW_TYPE;
1214
}
1315

1416
getDisplayText(): string {
@@ -20,10 +22,10 @@ class MyReactView extends ItemView {
2022
}
2123

2224
async onOpen(): Promise<void> {
23-
this.reactComponent = ReactDOM.render(
24-
React.createElement(DiceRoller),
25-
(this as any).contentEl
26-
);
25+
this.reactComponent = React.createElement(DiceRoller);
26+
27+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
28+
ReactDOM.render(this.reactComponent, (this as any).contentEl);
2729
}
2830
}
2931

@@ -32,19 +34,19 @@ export default class ReactStarterPlugin extends Plugin {
3234

3335
async onload(): Promise<void> {
3436
this.registerView(
35-
"react-view",
37+
VIEW_TYPE,
3638
(leaf: WorkspaceLeaf) => (this.view = new MyReactView(leaf))
3739
);
3840

3941
this.app.workspace.onLayoutReady(this.onLayoutReady.bind(this));
4042
}
4143

4244
onLayoutReady(): void {
43-
if (this.app.workspace.getLeavesOfType("react-view").length) {
45+
if (this.app.workspace.getLeavesOfType(VIEW_TYPE).length) {
4446
return;
4547
}
4648
this.app.workspace.getRightLeaf(false).setViewState({
47-
type: "react-view",
49+
type: VIEW_TYPE,
4850
});
4951
}
5052
}

Diff for: src/ui/DicerRoller.js

-18
This file was deleted.

Diff for: src/ui/DicerRoller.tsx

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import * as React from "react";
2+
3+
function randomNumber(min: number, max: number): number {
4+
return Math.random() * (max - min) + min;
5+
}
6+
7+
function rollDice(
8+
numDice: number,
9+
diceSides: number,
10+
modifier: number
11+
): number {
12+
let result = 0;
13+
14+
for (let i = 0; i < numDice; i++) {
15+
result += randomNumber(1, diceSides);
16+
}
17+
18+
return result + modifier;
19+
}
20+
21+
export default function DiceRoller(): JSX.Element {
22+
const [numDice, setNumDice] = React.useState(1);
23+
const [diceSides, setDiceSides] = React.useState(20);
24+
const [modifier, setModifier] = React.useState(0);
25+
const [result, setResult] = React.useState(null);
26+
27+
return (
28+
<>
29+
<div>
30+
<input
31+
type="number"
32+
value={diceSides}
33+
onChange={(e) => setNumDice(parseInt(e.target.value, 10))}
34+
/>
35+
D
36+
<input
37+
type="number"
38+
value={numDice}
39+
onChange={(e) => setDiceSides(parseInt(e.target.value, 10))}
40+
/>
41+
+
42+
<input
43+
type="number"
44+
value={modifier}
45+
onChange={(e) => setModifier(parseInt(e.target.value, 10))}
46+
/>
47+
</div>
48+
<h4>{result}</h4>
49+
<button onClick={() => setResult(rollDice(numDice, diceSides, modifier))}>
50+
Roll!
51+
</button>
52+
</>
53+
);
54+
}

Diff for: tsconfig.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"allowSyntheticDefaultImports": true,
55
"inlineSourceMap": true,
66
"inlineSources": true,
7+
"jsx": "react",
78
"module": "ESNext",
89
"target": "esnext",
910
"allowJs": true,
@@ -12,5 +13,5 @@
1213
"importHelpers": true,
1314
"lib": ["dom", "esnext"]
1415
},
15-
"include": ["**/*.ts"]
16+
"include": ["**/*.ts", "**/*.tsx"]
1617
}

0 commit comments

Comments
 (0)