First commit

Create basic render engine that only allows to add elements
This commit is contained in:
Alexander Navarro 2024-06-25 13:02:10 -04:00
parent 0d3eb3d40f
commit f411544fe9
Signed by untrusted user who does not match committer: anavarro
GPG key ID: 6426043E9FA3E3B5
14 changed files with 414 additions and 0 deletions

24
.gitignore vendored Normal file
View file

@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View file

@ -1,2 +1,15 @@
# yarjs
To install dependencies:
```bash
bun install
```
To run:
```bash
bun run index.ts
```
This project was created using `bun init` in bun v1.1.16. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.

BIN
bun.lockb Executable file

Binary file not shown.

10
eslint.config.js Normal file
View file

@ -0,0 +1,10 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";
export default [
{ files: ["**/*.{js,mjs,cjs,ts}"] },
{ languageOptions: { globals: globals.browser } },
pluginJs.configs.recommended,
...tseslint.configs.recommended,
];

13
index.html Normal file
View file

@ -0,0 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>

22
package.json Normal file
View file

@ -0,0 +1,22 @@
{
"name": "yarjs",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"devDependencies": {
"@eslint/js": "^9.5.0",
"@types/bun": "latest",
"@types/react": "^18.3.3",
"eslint": "9.x",
"globals": "^15.6.0",
"typescript": "^5.2.2",
"typescript-eslint": "^7.13.1",
"vite": "^5.3.1"
},
"module": "index.ts"
}

1
public/vite.svg Normal file
View file

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>

After

Width:  |  Height:  |  Size: 1.5 KiB

164
src/lib/YarJS.ts Normal file
View file

@ -0,0 +1,164 @@
import {
YarElement,
YarFiber,
YarHTMLTagName,
YarProps,
} from "./YarJs.interfaces";
export function createTextElement(text: string) {
return {
type: "TEXT",
props: {
nodeValue: text,
children: [],
},
};
}
export function createElement(
type: YarHTMLTagName,
props: YarProps,
...children: YarElement[]
) {
return {
type,
props: {
...props,
children: children.map((child) =>
typeof child === "object" ? child : createTextElement(child),
),
},
};
}
function createDom(fiber: YarFiber) {
const dom =
fiber.type === "TEXT"
? document.createTextNode("")
: document.createElement(fiber.type);
Object.keys(fiber.props)
.filter((key) => key !== "children")
.forEach((key) => {
// @ts-expect-error: I cannot figure it out how to properly type the dom props
dom[key] = fiber.props[key];
});
return dom;
}
let nextUnitOfWork: YarFiber | null | undefined = null;
let wipRoot: YarFiber | null | undefined = null;
function commitWork(fiber: YarFiber | null) {
if (!fiber) return;
const domParent = fiber.parent!.dom!;
domParent.appendChild(fiber.dom!);
commitWork(fiber.child);
commitWork(fiber.sibling);
}
function commitRoot() {
if (!wipRoot) return;
commitWork(wipRoot.child);
wipRoot = null;
}
function workLoop(deadline: IdleDeadline) {
let shouldYield = false;
while (nextUnitOfWork && !shouldYield) {
nextUnitOfWork = performUnitOfWork(nextUnitOfWork);
shouldYield = deadline.timeRemaining() < 1;
}
if (!nextUnitOfWork && wipRoot) {
commitRoot();
}
requestIdleCallback(workLoop);
}
requestIdleCallback(workLoop);
function performUnitOfWork(fiber: YarFiber) {
// Process the Fiber Tree, this is a representation of the DOM
// Since this this process could be interrupted before the whole
// tree is processed, we just do the representation and computation here,
// then in "commitWork" we add the representation to the actual DOM
if (!fiber.dom) {
// Create the actual dom element
fiber.dom = createDom(fiber);
}
// Create a new fiber for each child
const elements = fiber.props.children;
let index = 0;
let prevSibling: YarFiber | null = null;
while (index < elements.length) {
const element = elements[index];
const newFiber = {
parent: fiber,
type: element.type,
props: element.props,
child: null,
sibling: null,
dom: null,
};
// Each Fiber only holds a reference to it's first child (in the Fiber Tree representation),
// so if is the first new Fiber we add it as a child to the parent,
// if is not, we add as a sibling of the last child
// Nonetheless, each fiber has a reference to it's parent
if (index === 0) {
fiber.child = newFiber;
} else if (prevSibling !== null) {
prevSibling.sibling = newFiber;
}
prevSibling = newFiber;
index++;
}
// Search for the new fiber that needs to be processed
if (fiber.child) {
// return the first child of the current fiber
return fiber.child;
}
let nextFiber = fiber;
while (nextFiber) {
if (nextFiber.sibling) {
// return the next sibling of the current fiber
return nextFiber.sibling;
}
// Reference the parent so we look at the "uncle" (parent sibling)
// in the next itereation
nextFiber = nextFiber.parent!;
}
}
export function render(element: React.JSX.Element, container: HTMLElement) {
wipRoot = {
type: "",
dom: container,
parent: null,
child: null,
sibling: null,
props: {
children: [element],
},
};
nextUnitOfWork = wipRoot;
}
export function Fragment() {}
export default { render, createElement, Fragment };

View file

@ -0,0 +1,18 @@
export type YarHTMLTagName = keyof React.JSX.IntrinsicElements | string;
export type YarProps = {
children: YarElement[];
[key: string]: unknown;
};
export interface YarElement {
type: YarHTMLTagName;
props: YarProps;
}
export interface YarFiber extends YarElement {
parent: null | YarFiber;
dom: null | HTMLElement | Text;
child: null | YarFiber;
sibling: null | YarFiber;
}

13
src/main.tsx Normal file
View file

@ -0,0 +1,13 @@
import "./style.css";
import { render } from "./lib/YarJS";
const root = document.querySelector<HTMLDivElement>("#app")!;
const element = (
<div id="foo">
<a>bar</a>
<b />
</div>
);
render(element, root);

96
src/style.css Normal file
View file

@ -0,0 +1,96 @@
:root {
font-family: Inter, system-ui, Avenir, Helvetica, Arial, sans-serif;
line-height: 1.5;
font-weight: 400;
color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;
font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
a {
font-weight: 500;
color: #646cff;
text-decoration: inherit;
}
a:hover {
color: #535bf2;
}
body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
h1 {
font-size: 3.2em;
line-height: 1.1;
}
#app {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}
.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
transition: filter 300ms;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.vanilla:hover {
filter: drop-shadow(0 0 2em #3178c6aa);
}
.card {
padding: 2em;
}
.read-the-docs {
color: #888;
}
button {
border-radius: 8px;
border: 1px solid transparent;
padding: 0.6em 1.2em;
font-size: 1em;
font-weight: 500;
font-family: inherit;
background-color: #1a1a1a;
cursor: pointer;
transition: border-color 0.25s;
}
button:hover {
border-color: #646cff;
}
button:focus,
button:focus-visible {
outline: 4px auto -webkit-focus-ring-color;
}
@media (prefers-color-scheme: light) {
:root {
color: #213547;
background-color: #ffffff;
}
a:hover {
color: #747bff;
}
button {
background-color: #f9f9f9;
}
}

1
src/vite-env.d.ts vendored Normal file
View file

@ -0,0 +1 @@
/// <reference types="vite/client" />

29
tsconfig.json Normal file
View file

@ -0,0 +1,29 @@
{
"compilerOptions": {
"jsx": "preserve",
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,
"paths": {
"@/*": ["./*"]
},
/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"moduleDetection": "force",
"noEmit": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}

10
vite.config.ts Normal file
View file

@ -0,0 +1,10 @@
// vite.config.js
import { defineConfig } from "vite";
export default defineConfig({
esbuild: {
jsxFactory: "createElement",
jsxFragment: "Fragment",
jsxInject: `import {createElement, Fragment} from './lib/YarJS'`,
},
});