Skip to content
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

feat: homework #2

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ jobs:
with:
node-version: ${{ matrix.node-version }}
- run: npm i
- run: npm run tsc
# Just run tests once
- run: npm test
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@ coverage
*.njsproj
*.sln
*.sw?
mini-renderer2.js
src/mini-renderer.js
20 changes: 20 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"dev": "vite",
"build": "vite build",
"preview": "vite preview --port 4173",
"test": "vitest --environment jsdom"
"test": "vitest --environment jsdom",
"tsc": "tsc"
},
"dependencies": {
"vue": "^3.2.38"
Expand All @@ -15,6 +16,7 @@
"@vitejs/plugin-vue": "^3.0.3",
"@vue/test-utils": "^2.0.2",
"jsdom": "^20.0.0",
"typescript": "^4.8.4",
"vite": "^3.0.9",
"vitest": "^0.23.0"
}
Expand Down
4 changes: 2 additions & 2 deletions src/components/__tests__/HelloWorld.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { describe, it, expect } from 'vitest'
import { createApp } from '../../mini-renderer'
import { createApp } from '../../mini-renderer.js'
import App from '../../App.vue'

describe('createApp for dom', () => {
it('should render component', () => {
const app = createApp(App)
app.mount(document.createElement('div'))
expect(app._component).not.toBe(App)
expect(app._component).not.toBe(App);
})

it('should not mutate original root component options object', () => {
Expand Down
74 changes: 0 additions & 74 deletions src/mini-renderer.js

This file was deleted.

122 changes: 122 additions & 0 deletions src/mini-renderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import {
createRenderer,
RendererOptions,
ConcreteComponent,
createVNode,
AppContext,
} from '@vue/runtime-core';


const render = createRenderer({
forcePatchProp(el, key): boolean {
return false
},
patchProp(
el,
key,
prevValue,
nextValue,
isSVG = false,
prevChildren,
parentComponent,
parentSuspense,
unmountChildren
) {
if (key.startsWith('on')) {
el.addEventListener(key.substring(2).toLowerCase(), nextValue)
} else {
el.setAttribute(key, nextValue)
}
},
insert(child, parent, anchor) {
parent.insertBefore(child, anchor || null)
},
remove(child) {
const parent = child.parentNode
if (parent) {
parent.removeNode(child)
}
},
createElement(type, isSvg, isCustomizedBuiltIn) {
return document.createElement(type)
},
createText(text) {
return document.createTextNode(text)
},
createComment(text) {
return document.createComment(text)
},
setText(node, text) {
node.nodeValue = text
},
setElementText(el, text) {
el.textContent = text
},
parentNode(node) {
return node.parentNode
},
nextSibling(node) {
return node.nextSibling
},
querySelector(selector) {
return document.querySelector(selector)
},
setScopeId(el, id) {
el.setAttribute(id, '')
},
cloneNode(el) {
return el.cloneNode(true)
},
insertStaticContent(): any {
return []
}
} as RendererOptions & { forcePatchProp?: (el: any, key: string) => boolean })

function createAppContext(): AppContext {
return {
app: null as any,
config: {
isNativeTag: () => false,
performance: false,
globalProperties: {},
optionMergeStrategies: {},
errorHandler: undefined,
warnHandler: undefined,
compilerOptions: {}
},
mixins: [],
components: {},
directives: {},
provides: Object.create(null),
}
}

const createApp = (rootComponent, rootProps = null) => {
//TODO
let isMounted = false;
const context = createAppContext();
if (typeof rootComponent !== 'function') {
rootComponent = { ...rootComponent };
}
const app = {
_component: rootComponent as ConcreteComponent,
_container: null,
mount: (rootContainer: Element) => {
if (!isMounted) {
const vnode = createVNode(rootComponent, rootProps);
render.render(vnode, rootContainer, false);
isMounted = true;
app._container = rootContainer as any;
(rootContainer as any).__vue_app__ = app;
return vnode.component!.proxy;
} else {
console.warn('App has already been mounted');
}
}
};
return app;
}

export {
createApp
}
9 changes: 9 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"compilerOptions": {
"target": "ESNext",
"moduleResolution":"Node" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
},
"files": [
"src/mini-renderer.ts"
]
}