Skip to content

Commit 44281b4

Browse files
author
lishiwen
committed
feat: added customrenderer
1 parent 017d74c commit 44281b4

File tree

7 files changed

+331
-269
lines changed

7 files changed

+331
-269
lines changed

lib/m-vue.cjs.js

Lines changed: 108 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -298,100 +298,92 @@ function clearCurrentInstance() {
298298
currentInstance = null;
299299
}
300300

301-
function render(vnode, container, parentComponent) {
302-
patch(vnode, container, parentComponent);
303-
}
304-
function patch(vnode, container, parentComponent) {
305-
switch (vnode === null || vnode === void 0 ? void 0 : vnode.type) {
306-
case Fragment:
307-
processFragment(vnode, container, parentComponent);
308-
break;
309-
case Text:
310-
processText(vnode, container);
311-
break;
312-
default:
313-
if (vnode.shapeFlag & 1 /* ShapeFlags.ELEMENT */) {
314-
processElement(vnode, container, parentComponent);
301+
function createAppAPI(render) {
302+
return function createApp(rootComponent) {
303+
return {
304+
mount(rootContainer) {
305+
// 转vNode, 基于vNode工作
306+
const vnode = createVNode(rootComponent);
307+
render(vnode, rootContainer, undefined);
315308
}
316-
if (vnode.shapeFlag & 2 /* ShapeFlags.STATEFUL_COMPONENT */) {
317-
processComponent(vnode, container, parentComponent);
318-
}
319-
break;
320-
}
321-
}
322-
function processElement(vnode, container, parentComponent) {
323-
mountElement(vnode, container, parentComponent);
324-
}
325-
function processComponent(vnode, container, parentComponent) {
326-
mountComponent(vnode, container, parentComponent);
327-
}
328-
function processFragment(vnode, container, parentComponent) {
329-
mountChildren(vnode === null || vnode === void 0 ? void 0 : vnode.children, container, parentComponent);
330-
}
331-
function mountComponent(initialVNode, container, parentComponent) {
332-
const instance = createComponentInstance(initialVNode, parentComponent);
333-
setupComponent(instance);
334-
setupRenderEffect(instance, container, initialVNode);
309+
};
310+
};
335311
}
336-
function mountElement(vnode, container, parentComponent) {
337-
// vnode type -> div/span
338-
// vnode.el -> element.el
339-
const el = (vnode.el = document.createElement(vnode.type));
340-
// children
341-
if (vnode.shapeFlag & 4 /* ShapeFlags.TEXT_CHILDREN */) {
342-
el.textContent = vnode.children;
312+
313+
function createRenderer(customRenderOptions) {
314+
const { createElement, patchProp, insert } = customRenderOptions;
315+
function render(vnode, container, parentComponent) {
316+
patch(vnode, container, parentComponent);
343317
}
344-
else if (vnode.shapeFlag & 8 /* ShapeFlags.ARRAY_CHILDREN */) {
345-
mountChildren(vnode.children, el, parentComponent);
318+
function patch(vnode, container, parentComponent) {
319+
switch (vnode === null || vnode === void 0 ? void 0 : vnode.type) {
320+
case Fragment:
321+
processFragment(vnode, container, parentComponent);
322+
break;
323+
case Text:
324+
processText(vnode, container);
325+
break;
326+
default:
327+
if (vnode.shapeFlag & 1 /* ShapeFlags.ELEMENT */) {
328+
processElement(vnode, container, parentComponent);
329+
}
330+
if (vnode.shapeFlag & 2 /* ShapeFlags.STATEFUL_COMPONENT */) {
331+
processComponent(vnode, container, parentComponent);
332+
}
333+
break;
334+
}
346335
}
347-
// props
348-
addAttrs(vnode, el);
349-
// append to container
350-
container.appendChild(el);
351-
}
352-
function mountChildren(children = [], container, parentComponent) {
353-
children.forEach(child => {
354-
patch(child, container, parentComponent);
355-
});
356-
}
357-
function isOnEvent(propertyName) {
358-
return /^on[A-Z]/.test(propertyName);
359-
}
360-
isOnEvent.getEventName = function onEventName(propertyName) {
361-
return propertyName.slice(2).toLowerCase();
362-
};
363-
function addAttrs(vnode, container) {
364-
const props = vnode.props || {};
365-
for (const key in props) {
366-
if (Object.prototype.hasOwnProperty.call(props, key)) {
367-
const value = props[key];
368-
if (isOnEvent(key)) {
369-
container.addEventListener(isOnEvent.getEventName(key), value, false);
370-
}
371-
else {
372-
container.setAttribute(key, value);
336+
function processElement(vnode, container, parentComponent) {
337+
mountElement(vnode, container, parentComponent);
338+
}
339+
function processComponent(vnode, container, parentComponent) {
340+
mountComponent(vnode, container, parentComponent);
341+
}
342+
function processFragment(vnode, container, parentComponent) {
343+
mountChildren(vnode === null || vnode === void 0 ? void 0 : vnode.children, container, parentComponent);
344+
}
345+
function mountComponent(initialVNode, container, parentComponent) {
346+
const instance = createComponentInstance(initialVNode, parentComponent);
347+
setupComponent(instance);
348+
setupRenderEffect(instance, container, initialVNode);
349+
}
350+
function mountElement(vnode, container, parentComponent) {
351+
const el = (vnode.el = createElement(vnode.type));
352+
if (vnode.shapeFlag & 4 /* ShapeFlags.TEXT_CHILDREN */) {
353+
el.textContent = vnode.children;
354+
}
355+
else if (vnode.shapeFlag & 8 /* ShapeFlags.ARRAY_CHILDREN */) {
356+
mountChildren(vnode.children, el, parentComponent);
357+
}
358+
addAttrs(vnode, el);
359+
insert(el, container);
360+
}
361+
function mountChildren(children = [], container, parentComponent) {
362+
children.forEach(child => {
363+
patch(child, container, parentComponent);
364+
});
365+
}
366+
function addAttrs(vnode, container) {
367+
const props = vnode.props || {};
368+
for (const key in props) {
369+
if (Object.prototype.hasOwnProperty.call(props, key)) {
370+
const value = props[key];
371+
patchProp(container, key, value);
373372
}
374373
}
375374
}
376-
}
377-
function setupRenderEffect(instance, container, initialVNode) {
378-
var _a;
379-
const subTree = (_a = instance === null || instance === void 0 ? void 0 : instance.render) === null || _a === void 0 ? void 0 : _a.call(instance.proxy);
380-
patch(subTree, container, instance);
381-
initialVNode.el = subTree.el;
382-
}
383-
function processText(vnode, container) {
384-
const el = (vnode.el = document.createTextNode(vnode.children));
385-
container.append(el);
386-
}
387-
388-
function createApp(rootComponent) {
375+
function setupRenderEffect(instance, container, initialVNode) {
376+
var _a;
377+
const subTree = (_a = instance === null || instance === void 0 ? void 0 : instance.render) === null || _a === void 0 ? void 0 : _a.call(instance.proxy);
378+
patch(subTree, container, instance);
379+
initialVNode.el = subTree.el;
380+
}
381+
function processText(vnode, container) {
382+
const el = (vnode.el = document.createTextNode(vnode.children));
383+
container.append(el);
384+
}
389385
return {
390-
mount(rootContainer) {
391-
// 转vNode, 基于vNode工作
392-
const vnode = createVNode(rootComponent);
393-
render(vnode, rootContainer, undefined);
394-
}
386+
createApp: createAppAPI(render),
395387
};
396388
}
397389

@@ -462,17 +454,45 @@ function composeInjectValues(provides = {}, keys = [], defaultValues = {}) {
462454
}, {});
463455
}
464456

457+
function isOnEvent(propertyName) {
458+
return /^on[A-Z]/.test(propertyName);
459+
}
460+
isOnEvent.getEventName = function onEventName(propertyName) {
461+
return propertyName.slice(2).toLowerCase();
462+
};
463+
function createElement(type) {
464+
return document.createElement(type);
465+
}
466+
function patchProp(el, key, value) {
467+
if (isOnEvent(key)) {
468+
el.addEventListener(isOnEvent.getEventName(key), value, false);
469+
}
470+
else {
471+
el.setAttribute(key, value);
472+
}
473+
}
474+
function insert(el, parentContainer) {
475+
parentContainer.append(el);
476+
}
477+
const renderer = createRenderer({
478+
createElement,
479+
patchProp,
480+
insert
481+
});
482+
function createApp(...args) {
483+
return renderer.createApp(...args);
484+
}
485+
465486
exports.clearCurrentInstance = clearCurrentInstance;
466487
exports.createApp = createApp;
467488
exports.createComponentInstance = createComponentInstance;
489+
exports.createRenderer = createRenderer;
468490
exports.createTextVNode = createTextVNode;
469491
exports.createVNode = createVNode;
470492
exports.getCurrentInstance = getCurrentInstance;
471493
exports.h = h;
472494
exports.inject = inject;
473-
exports.patch = patch;
474495
exports.provide = provide;
475-
exports.render = render;
476496
exports.renderSlots = renderSlots;
477497
exports.setCurrentInstance = setCurrentInstance;
478498
exports.setupComponent = setupComponent;

0 commit comments

Comments
 (0)