Skip to content

Commit

Permalink
1.初始化component流程
Browse files Browse the repository at this point in the history
2.配置rollup打包
  • Loading branch information
jindy committed Jun 7, 2022
1 parent b6a8f97 commit aa7a797
Show file tree
Hide file tree
Showing 15 changed files with 307 additions and 3 deletions.
12 changes: 12 additions & 0 deletions example/HelloWorld/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { h } from '../../lib/guide-mini-vue.esm.js'

export const App = {
render() {
return h('div', 'hi, ' + this.msg)
},
setup() {
return {
msg: 'mini-vue'
}
}
}
16 changes: 16 additions & 0 deletions example/HelloWorld/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<div id="app"></div>
<script src="./main.js" type="module"></script>
</body>

</html>
6 changes: 6 additions & 0 deletions example/HelloWorld/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

import { createApp } from '../../lib/guide-mini-vue.esm.js'
import { App } from './App.js'

const rootContainer = document.querySelector('#app')
createApp(App).mount(rootContainer)
80 changes: 80 additions & 0 deletions lib/guide-mini-vue.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

function createComponentInstance(vnode) {
const component = {
vnode,
type: vnode.type,
};
return component;
}
function setupComponent(instance) {
// initProps()
// initSlots()
setupStatefulComponent(instance);
}
function setupStatefulComponent(instance) {
const Component = instance.type;
const { setup } = Component;
if (setup) {
const setupResult = setup();
handleSetupResult(instance, setupResult);
}
}
function handleSetupResult(instance, setupResult) {
if (typeof setupResult === "object") {
instance.setupState = setupResult;
}
finishSetupComponent(instance);
}
function finishSetupComponent(instance) {
const Component = instance.type;
if (Component.render) {
instance.render = Component.render;
}
}

function render(vnode, container) {
patch(vnode);
}
function patch(vnode, container) {
processComponent(vnode);
}
function processComponent(vnode, container) {
mountComponent(vnode);
}
function mountComponent(vnode, container) {
const instance = createComponentInstance(vnode);
setupComponent(instance);
setupRenderEffect(instance);
}
function setupRenderEffect(instance, container) {
const subTree = instance.render();
patch(subTree);
}

function createVNode(type, props, children) {
const vnode = {
type,
props,
children,
};
return vnode;
}

function createApp(rootComponent) {
return {
mount(rootContainer) {
const vnode = createVNode(rootComponent);
render(vnode);
},
};
}

function h(type, props, children) {
return createVNode(type, props, children);
}

exports.createApp = createApp;
exports.h = h;
75 changes: 75 additions & 0 deletions lib/guide-mini-vue.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
function createComponentInstance(vnode) {
const component = {
vnode,
type: vnode.type,
};
return component;
}
function setupComponent(instance) {
// initProps()
// initSlots()
setupStatefulComponent(instance);
}
function setupStatefulComponent(instance) {
const Component = instance.type;
const { setup } = Component;
if (setup) {
const setupResult = setup();
handleSetupResult(instance, setupResult);
}
}
function handleSetupResult(instance, setupResult) {
if (typeof setupResult === "object") {
instance.setupState = setupResult;
}
finishSetupComponent(instance);
}
function finishSetupComponent(instance) {
const Component = instance.type;
if (Component.render) {
instance.render = Component.render;
}
}

function render(vnode, container) {
patch(vnode);
}
function patch(vnode, container) {
processComponent(vnode);
}
function processComponent(vnode, container) {
mountComponent(vnode);
}
function mountComponent(vnode, container) {
const instance = createComponentInstance(vnode);
setupComponent(instance);
setupRenderEffect(instance);
}
function setupRenderEffect(instance, container) {
const subTree = instance.render();
patch(subTree);
}

function createVNode(type, props, children) {
const vnode = {
type,
props,
children,
};
return vnode;
}

function createApp(rootComponent) {
return {
mount(rootContainer) {
const vnode = createVNode(rootComponent);
render(vnode);
},
};
}

function h(type, props, children) {
return createVNode(type, props, children);
}

export { createApp, h };
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
{
"name": "guide-mini-vue",
"version": "1.0.0",
"main": "index.js",
"main": "lib/guide-mini-vue.cjs.js",
"module": "lib/guide-mini-vue.esm.js",
"license": "MIT",
"scripts": {
"test": "jest"
"test": "jest",
"build": "rollup -c rollup.config.js"
},
"devDependencies": {
"@babel/core": "^7.18.0",
"@babel/preset-env": "^7.18.0",
"@babel/preset-typescript": "^7.17.12",
"@rollup/plugin-typescript": "^8.3.2",
"@types/jest": "^27.5.1",
"babel-jest": "^28.1.0",
"jest": "^28.1.0",
"rollup": "^2.75.5",
"ts-jest": "^28.0.3",
"tslib": "^2.4.0",
"typescript": "^4.7.2"
}
}
19 changes: 19 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pkg from './package.json'
import typescript from "@rollup/plugin-typescript"

export default {
input: './src/index.ts',
output: [
// cjs属于commonjs规范
// esm属于es规范
{
format: 'cjs',
file: pkg.main
},
{
format: 'es',
file: pkg.module
}
],
plugins: [typescript()]
}
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// mini-vue的出口

export * from "./runtime-core/index"
35 changes: 35 additions & 0 deletions src/runtime-core/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export function createComponentInstance(vnode) {
const component = {
vnode,
type: vnode.type,
}
return component
}

export function setupComponent(instance) {
// initProps()
// initSlots()
setupStatefulComponent(instance)
}

function setupStatefulComponent(instance: any) {
const Component = instance.type

const { setup } = Component
if (setup) {
const setupResult = setup()
handleSetupResult(instance, setupResult)
}
}
function handleSetupResult(instance: any, setupResult: any) {
if (typeof setupResult === "object") {
instance.setupState = setupResult
}
finishSetupComponent(instance)
}
function finishSetupComponent(instance: any) {
const Component = instance.type
// if (Component.render) {
instance.render = Component.render
// }
}
11 changes: 11 additions & 0 deletions src/runtime-core/createApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { render } from "./renderer"
import { createVNode } from "./vnode"

export function createApp(rootComponent) {
return {
mount(rootContainer) {
const vnode = createVNode(rootComponent)
render(vnode, rootContainer)
},
}
}
5 changes: 5 additions & 0 deletions src/runtime-core/h.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { createVNode } from "./vnode"

export function h(type, props?, children?) {
return createVNode(type, props, children)
}
2 changes: 2 additions & 0 deletions src/runtime-core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { createApp } from "./createApp"
export { h } from "./h"
27 changes: 27 additions & 0 deletions src/runtime-core/renderer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { createComponentInstance, setupComponent } from "./component"

export function render(vnode, container) {
patch(vnode, container)
}

function patch(vnode, container) {
// 需要区分element和component
// 如果是function就认为是component?
// 如果是object就认为是element?
processComponent(vnode, container)
}

function processComponent(vnode: any, container: any) {
mountComponent(vnode, container)
}

function mountComponent(vnode: any, container) {
const instance = createComponentInstance(vnode)
setupComponent(instance)
setupRenderEffect(instance, container)
}

function setupRenderEffect(instance: any, container) {
const subTree = instance.render()
patch(subTree, container)
}
8 changes: 8 additions & 0 deletions src/runtime-core/vnode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function createVNode(type, props?, children?) {
const vnode = {
type,
props,
children,
}
return vnode
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */

/* Modules */
"module": "commonjs", /* Specify what module code is generated. */
"module": "esnext", /* Specify what module code is generated. */
// "rootDir": "./", /* Specify the root folder within your source files. */
// "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
// "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
Expand Down

0 comments on commit aa7a797

Please sign in to comment.