Skip to content

Commit cf2cddb

Browse files
committed
Setup for 1.0
1 parent 6c970a8 commit cf2cddb

15 files changed

+241
-19
lines changed

README.md

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
1-
# typescript-project-template
2-
Template repo for new TypeScript projects.
1+
# simple-tsx
32

4-
This repo assumes the following:
3+
A simple way to use `.tsx` files without Babel or React!
54

6-
* Source code lives in the `src` directory.
7-
* The main export is located in `src/index.ts`.
8-
* Tests live in the `test` directory.
9-
* Each test is name `<name>.test.ts`.
10-
* The `npm_token`, `app_id`, and `app_private_key` secrets are present in the repo.
11-
* The code will be a CommonJS module.
5+
## Installation
6+
7+
npm: `npm install --save simple-tsx`
8+
yarn: `yarn add simple-tsx`
9+
10+
## Usage
11+
12+
In your `tsconfig.json` file, add these following values to the `compilerOptions` section:
13+
14+
```json
15+
"jsx": "react",
16+
"jsxFactory": "SimpleTSX.createElement",
17+
"jsxFragmentFactory": "SimpleTSX.createFragment",
18+
```
19+
20+
In each of your `.tsx` files, put the following import line on top:
21+
22+
```tsx
23+
import * as SimpleTSX from "simple-tsx";
24+
```

package-lock.json

Lines changed: 15 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "<package-name>",
2+
"name": "simple-tsx",
33
"version": "1.0.0",
4-
"description": "<package-description>",
4+
"description": "A simple way to write tsx files. No babel required!",
55
"main": "dist/index.js",
66
"type": "commonjs",
77
"types": "dist/index.d.ts",
@@ -13,17 +13,15 @@
1313
},
1414
"repository": {
1515
"type": "git",
16-
"url": "git+https://github.com/PythonCoderAS/<package-name>.git"
16+
"url": "git+https://github.com/PythonCoderAS/simple-tsx.git"
1717
},
1818
"keywords": [],
1919
"author": "PythonCoderAS",
2020
"license": "MIT",
2121
"bugs": {
22-
"url": "https://github.com/PythonCoderAS/<package-name>/issues"
23-
},
24-
"homepage": "https://github.com/PythonCoderAS/<package-name>#readme",
25-
"dependencies": {
22+
"url": "https://github.com/PythonCoderAS/simple-tsx/issues"
2623
},
24+
"homepage": "https://github.com/PythonCoderAS/simple-tsx#readme",
2725
"devDependencies": {
2826
"@types/chai": "^4.3.2",
2927
"@types/chai-as-promised": "^7.1.5",
@@ -36,6 +34,7 @@
3634
"eslint-config-pythoncoderas-combo": "^1.1.3",
3735
"mocha": "^10.0.0",
3836
"prettier": "^2.7.1",
37+
"subprocess-test-utils": "^1.0.0",
3938
"ts-node": "^10.9.1",
4039
"typescript": "^4.7.4"
4140
}

src/createElement.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Element, { ComponentFunction } from "./element";
2+
3+
export default function createElement<
4+
T extends Record<string, string | ((event: Event) => unknown)>
5+
>(tag: string | ComponentFunction<T>, props: T, ...children: Element[]) {
6+
if (typeof tag === "string") {
7+
return new Element(tag, props, ...children);
8+
}
9+
10+
return tag(props, ...children);
11+
}

src/createFragment.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default function createFragment(
2+
props: unknown,
3+
...children: Element[]
4+
): Element[] {
5+
return children;
6+
}

src/element.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { AttributesType } from "./types";
2+
3+
export default class Element {
4+
element: HTMLElement;
5+
6+
constructor(
7+
name: string,
8+
attributes: AttributesType | null,
9+
...children: Element[]
10+
) {
11+
this.element = document.createElement(name);
12+
const trueAttributes = attributes || {};
13+
Object.entries(trueAttributes).forEach(([key, value]) => {
14+
if (key.startsWith("on") && key.toLowerCase() in window) {
15+
this.element.addEventListener(
16+
key.toLowerCase().substring(2),
17+
value as EventListener
18+
);
19+
} else {
20+
this.element.setAttribute(name, value.toString());
21+
}
22+
});
23+
children.forEach((child) => this.element.appendChild(child.element));
24+
}
25+
}
26+
27+
export type ComponentFunction<T extends Record<string, unknown>> = (
28+
props: T,
29+
...children: Element[]
30+
) => Element;

src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { default as createElement } from "./createElement";
2+
export { default as createFragment } from "./createFragment";
3+
export { default as Element, ComponentFunction } from "./element";
4+
export * from "./utils";
5+
export * from "./types";

src/types.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type AttributesType = {
2+
[key: string]: string | ((event: Event) => unknown);
3+
};

src/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import Element from "./element";
2+
3+
export function setElement(container: HTMLElement, element: Element): void {
4+
container.replaceChildren(element.element);
5+
}
6+
7+
export function appendElement(container: HTMLElement, element: Element): void {
8+
container.appendChild(element.element);
9+
}
10+
11+
export function appendElementLeft(
12+
container: HTMLElement,
13+
element: Element
14+
): void {
15+
container.insertBefore(element.element, container.firstChild);
16+
}

tests/index.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { expect, use } from "chai";
2+
import * as chaiAsPromised from "chai-as-promised";
3+
import { runScript } from "subprocess-test-utils";
4+
5+
use(chaiAsPromised);
6+
7+
describe("simple-tsx tests", () => {
8+
it("compiles correctly", async () => {
9+
expect(
10+
runScript("bash", ["-c", "cd tests/test-project && npm run build"])
11+
).to.eventually.equal(true);
12+
});
13+
});

0 commit comments

Comments
 (0)