-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
169 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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,5 +1,102 @@ | ||
import { capitalize } from '../utils' | ||
import { NodeTypes } from './ast' | ||
export function generate(ast) { | ||
return ` | ||
console.log('hello world') | ||
` | ||
const codegen = traverseNode(ast) | ||
const exeCode = ` | ||
with(_ctx){ | ||
const { | ||
createApp, | ||
render, | ||
h, | ||
Text, | ||
Fragment, | ||
nextTick, | ||
reactive, | ||
ref, | ||
computed, | ||
effect, | ||
compile | ||
} = MiniVue | ||
return ${codegen} | ||
} | ||
` | ||
return exeCode | ||
} | ||
|
||
export function traverseNode(node) { | ||
switch (node.type) { | ||
case NodeTypes.ROOT: | ||
if (node.children.length > 1) { | ||
return traverseChildren(node.children) | ||
} else if (node.children.length === 1) { | ||
return traverseNode(node.children[0]) | ||
} | ||
break | ||
case NodeTypes.ELEMENT: | ||
return createElement(node) | ||
case NodeTypes.TEXT: | ||
return createTextVNode(node) | ||
case NodeTypes.INTERPOLATION: | ||
return createTextVNode(node.content) | ||
default: | ||
break | ||
} | ||
} | ||
|
||
function createTextVNode(node) { | ||
const child = createText(node) | ||
return `h(Text,null,${child})` | ||
} | ||
|
||
function createText({ isStatic = true, content = '' } = {}) { | ||
return isStatic ? JSON.stringify(content) : content | ||
} | ||
|
||
function createElement(node) { | ||
let result = traverseChildren(node.children) | ||
const propsArr = createPropsArr(node) | ||
|
||
const propsStr = propsArr.length ? `{${propsArr.join(', ')}}` : `null` | ||
|
||
return result | ||
? `h("${node.tag}",${propsStr},${result})` | ||
: `h("${node.tag}",${propsStr})` | ||
} | ||
|
||
function createPropsArr({ props, directives }) { | ||
return [ | ||
...props.map(({ name, value }) => `${name}:${createText(value)}`), | ||
...directives.map(({ name, exp, arg }) => { | ||
switch (name) { | ||
case 'on': | ||
const value = createText(exp) | ||
if (/^\w+\(\w*\)/.test(value)) { | ||
return `on${capitalize(arg.content)}:($event) => ${value}` | ||
} else { | ||
return `on${capitalize(arg.content)}:${value}` | ||
} | ||
case 'bind': | ||
return `${arg.content}:${createText(exp)}` | ||
case 'html': | ||
return `innerHTML:${createText(exp)}` | ||
default: | ||
break | ||
} | ||
}) | ||
] | ||
} | ||
|
||
function traverseChildren(children) { | ||
if (!children.length) { | ||
return | ||
} | ||
if (children.length === 1) { | ||
const child = children[0] | ||
if (child.type === NodeTypes.TEXT) { | ||
return createText(child) | ||
} else if (child.type === NodeTypes.INTERPOLATION) { | ||
return child.content.content | ||
} | ||
} | ||
return `[${children.map(child => traverseNode(child)).join(', ')}]` | ||
} |
This file contains 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 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,46 @@ | ||
<!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="/dist/mini-vue.js"></script> | ||
<script type="text/template" id="template"> | ||
<div>{{counter.value}}</div> | ||
<button @click="add">add</button> | ||
</script> | ||
<script> | ||
const { | ||
createApp, | ||
render, | ||
h, | ||
Text, | ||
Fragment, | ||
nextTick, | ||
reactive, | ||
ref, | ||
computed, | ||
effect, | ||
compile | ||
} = MiniVue | ||
const instance = { | ||
template: '#template', | ||
setup() { | ||
const counter = ref(0) | ||
const add = () => { | ||
counter.value++ | ||
} | ||
return { | ||
counter, | ||
add | ||
} | ||
} | ||
} | ||
createApp(instance).mount('#app') | ||
</script> | ||
</body> | ||
</html> |
This file contains 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 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 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