-
Notifications
You must be signed in to change notification settings - Fork 0
[SeoIn] Virtual-DOM #2
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
Open
Seoin02
wants to merge
11
commits into
main
Choose a base branch
from
seoin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4eab1c7
chore: jsx 사용을 위한 babel 설치
Seoin02 5f8326a
feat: jsx로 가상돔 구현
Seoin02 6c0e8f0
chore: 깃허브에 올린 pnpm-lock.yaml 파일 삭제
Seoin02 3bc7a30
refactor: main.tsx type 추가
Seoin02 758892e
Merge branch 'seoin' of https://github.com/React-Core-Learn/Virtual-D…
Seoin02 9ce1629
feat: state 추가
Seoin02 090dbc1
refactor: VDOM -> RealDOM으로 변환
Seoin02 1b1d2a4
fix: 병합 충돌 해결
Seoin02 fed4b87
chore: 불필요한 코드 제거
Seoin02 6ac0b62
feat: diff 알고리즘 적용
Seoin02 bf8b472
refactor: RealDOM에 Diff 알고리즘 적용
Seoin02 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "presets": ["@babel/preset-env", "@babel/preset-typescript"], | ||
| "plugins": [ | ||
| [ | ||
| "@babel/plugin-transform-react-jsx", | ||
| { | ||
| "pragma": "h" | ||
| } | ||
| ] | ||
| ] | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| declare namespace JSX { | ||
| interface IntrinsicElements { | ||
| [elementName: string]: any; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| type RecordType = Record<string, any>; | ||
|
|
||
| interface VirtualNode { | ||
| type: string; | ||
| props: RecordType; | ||
| children: (VirtualNode | string)[]; | ||
| } | ||
|
|
||
| function h(type: string, props: RecordType | null, ...children: any[]): VirtualNode { | ||
| return { | ||
| type, | ||
| props: props || {}, | ||
| children: children.flat().filter((child) => child != null), | ||
| }; | ||
| } | ||
|
|
||
| function updateElement(parent: Node, oldNode: Node | null, newNode: Node | null) { | ||
| if (!newNode && oldNode && oldNode instanceof HTMLElement) { | ||
| oldNode.remove(); | ||
| return; | ||
| } | ||
|
|
||
| if (newNode && !oldNode) { | ||
| parent.appendChild(newNode); | ||
| return; | ||
| } | ||
|
|
||
| if (!oldNode || !newNode) return; | ||
|
|
||
| if (newNode instanceof Text && oldNode instanceof Text) { | ||
| if (newNode.nodeValue !== oldNode.nodeValue) { | ||
| oldNode.nodeValue = newNode.nodeValue; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (oldNode instanceof Element && newNode instanceof Element) { | ||
| if (newNode.nodeName !== oldNode.nodeName) { | ||
| oldNode.replaceWith(newNode); | ||
| return; | ||
| } | ||
|
|
||
| updateAttributes(oldNode, newNode); | ||
|
|
||
| const newChildren = Array.from(newNode.childNodes); | ||
| const oldChildren = Array.from(oldNode.childNodes); | ||
| const maxLength = Math.max(newChildren.length, oldChildren.length); | ||
|
|
||
| for (let i = 0; i < maxLength; i++) { | ||
| updateElement(oldNode, oldChildren[i] || null, newChildren[i] || null); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function updateAttributes(oldNode: Element, newNode: Element) { | ||
| const oldProps = Array.from(oldNode.attributes); | ||
| const newProps = Array.from(newNode.attributes); | ||
|
|
||
| for (const { name, value } of newProps) { | ||
| if (oldNode.getAttribute(name) !== value) { | ||
| oldNode.setAttribute(name, value); | ||
| } | ||
| } | ||
|
|
||
| for (const { name } of oldProps) { | ||
| if (!newNode.hasAttribute(name)) { | ||
| oldNode.removeAttribute(name); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const render = (state: RecordType[]) => { | ||
| const element = document.createElement('div'); | ||
| element.innerHTML = ` | ||
| <div id="app"> | ||
| <ul> | ||
| ${state | ||
| .map( | ||
| ({ completed, content }) => ` | ||
| <li class="${completed ? 'completed' : ''}"> | ||
| <input type="checkbox" class="toggle" ${completed ? 'checked' : ''} /> | ||
| ${content} | ||
| <button class="remove">삭제</button> | ||
| </li> | ||
| `, | ||
| ) | ||
| .join('')} | ||
| </ul> | ||
| <form> | ||
| <input type="text" /> | ||
| <button type="submit">추가</button> | ||
| </form> | ||
| </div> | ||
| `.trim(); | ||
|
|
||
| return element.firstElementChild; | ||
| }; | ||
|
|
||
| const oldState = [ | ||
| { id: 1, completed: false, content: 'todo list item 1' }, | ||
| { id: 2, completed: true, content: 'todo list item 2' }, | ||
| ]; | ||
|
|
||
| const newState = [ | ||
| { id: 1, completed: true, content: 'todo list item 1 updated' }, | ||
| { id: 2, completed: true, content: 'todo list item 2' }, | ||
| { id: 3, completed: false, content: 'todo list item 3' }, | ||
| ]; | ||
|
|
||
| const $root = document.createElement('div'); | ||
| document.body.appendChild($root); | ||
|
|
||
| const oldNode = render(oldState); | ||
| if (oldNode) { | ||
| $root.appendChild(oldNode); | ||
| } | ||
|
|
||
| setTimeout(() => { | ||
| const newNode = render(newState); | ||
| if (oldNode && newNode) { | ||
| updateElement($root, oldNode, newNode); | ||
| } | ||
| }, 1000); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,24 +1,30 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "target": "ES2020", | ||
| "target": "ES5", | ||
| "useDefineForClassFields": true, | ||
| "module": "ESNext", | ||
| "module": "CommonJS", | ||
| "lib": ["ES2020", "DOM", "DOM.Iterable"], | ||
| "skipLibCheck": true, | ||
|
|
||
| /* Bundler mode */ | ||
| "moduleResolution": "bundler", | ||
| "allowImportingTsExtensions": true, | ||
| "isolatedModules": true, | ||
| "moduleDetection": "force", | ||
| "noEmit": true, | ||
| "esModuleInterop": true, | ||
|
|
||
| /* Linting */ | ||
| "strict": true, | ||
| "noUnusedLocals": true, | ||
| "noUnusedParameters": true, | ||
| "noFallthroughCasesInSwitch": true, | ||
| "noUncheckedSideEffectImports": true | ||
| "jsx": "preserve", | ||
| "jsxFactory": "h", | ||
| "jsxImportSource": "@/lib/jsx", | ||
| "baseUrl": ".", | ||
| "paths": { | ||
| "@/*": ["./src/*"] | ||
| } | ||
| }, | ||
| "include": ["src"] | ||
| "include": ["src", "jsx.d.ts"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { defineConfig } from 'vite'; | ||
|
|
||
| export default defineConfig({ | ||
| server: { | ||
| port: 3000, | ||
| }, | ||
| build: { | ||
| outDir: 'dist', | ||
| }, | ||
| }); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
저는
vite.config.ts에서 세팅했는데, 이렇게 해도 잘 동작하나보네요!이 방법이 원초적인것 같아 좋은것 같네요. 참고해서 다시 해봐야겠어요 👍