Merge remote-tracking branch 'gitee/api2/for' into dev-04-15

# Conflicts:
#	demos/benchmark/src/main.jsx
#	demos/v2/src/App.view.tsx
#	packages/transpiler/babel-preset-inula-next/CHANGELOG.md
#	packages/transpiler/babel-preset-inula-next/package.json
#	packages/transpiler/class-transformer/package.json
#	packages/transpiler/class-transformer/src/pluginProvider.ts
#	packages/transpiler/vite-plugin-inula-next/CHANGELOG.md
#	packages/transpiler/vite-plugin-inula-next/package.json
This commit is contained in:
Hoikan 2024-04-15 11:51:44 +08:00
commit 37d6ba1033
9 changed files with 113 additions and 13 deletions

View File

@ -7,6 +7,6 @@
</head>
<body>
<div id="main"></div>
<script type="module" src="/src/App.view.tsx"></script>
<script type="module" src="/src/App.tsx"></script>
</body>
</html>

View File

@ -16,9 +16,7 @@ import {
} from '@openinula/next';
// @ts-ignore
function Button({ children: content, onClick }) {
console.log(content);
function Button({ children, onClick }) {
return (
<button
onClick={onClick}
@ -31,10 +29,11 @@ function Button({ children: content, onClick }) {
borderRadius: '4px',
}}
>
{content}
{children}
</button>
);
}
function ArrayModification() {
const arr = [];
willMount(() => {});
@ -47,9 +46,69 @@ function ArrayModification() {
);
}
function Counter() {
let count = 0;
const doubleCount = count * 2; // 当count变化时doubleCount自动更新
// 当count变化时watch会自动执行
watch(() => {
uploadToServer(count);
console.log(`count has changed: ${count}`);
});
// 只有在init的时候执行一次
console.log(`Counter willMount with count ${count}`);
// 在elements被挂载到DOM之后执行
didMount(() => {
console.log(`Counter didMount with count ${count}`);
});
return (
<section>
count: {count}, double is: {doubleCount}
<button onClick={() => count++}>Add</button>
</section>
);
}
function Counter() {
let count = 0;
const doubleCount = count * 2; // 当count变化时doubleCount自动更新
uploadToServer(count); // 当count变化时uploadToServer会自动执行
console.log(`count has changed: ${count}`); // 当count变化时console.log会自动执行
// 只有在init的时候执行一次
willMount(() => {
console.log(`Counter willMount with count ${count}`);
});
// 在elements被挂载到DOM之后执行
didMount(() => {
console.log(`Counter didMount with count ${count}`);
});
return (
<section>
count: {count}, double is: {doubleCount}
<button onClick={() => count++}>Add</button>
</section>
);
}
function MyComp() {
let count = 0;
const db = count * 2;
{
console.log(count);
const i = count * 2;
console.log(i);
}
console.log(count);
const i = count * 2;
console.log(i);
const XX = () => {};
return (
<>
@ -58,7 +117,7 @@ function MyComp() {
count: {count}, double is: {db}
<button onClick={() => (count += 1)}>Add</button>
</section>
<Button onClick={() => alert(count)}>{count}</Button>
<Button onClick={() => alert(count)}>Alter count</Button>
<ConditionalRendering count={count} />
<ArrayModification />
</>

View File

@ -1,5 +1,12 @@
# babel-preset-inula-next
## 0.0.3
### Patch Changes
- Updated dependencies
- @openinula/class-transformer@0.0.2
## 0.0.2
### Patch Changes

View File

@ -1,6 +1,6 @@
{
"name": "babel-preset-inula-next",
"version": "0.0.2",
"version": "0.0.3",
"author": {
"name": "IanDx",
"email": "iandxssxx@gmail.com"

View File

@ -0,0 +1,7 @@
# @openinula/class-transformer
## 0.0.2
### Patch Changes
- feat: add lifecycles and watch

View File

@ -1,6 +1,6 @@
{
"name": "@openinula/class-transformer",
"version": "0.0.1",
"version": "0.0.2",
"description": "Inula view generator",
"keywords": [
"inula"

View File

@ -53,6 +53,14 @@ export class PluginProvider {
return;
}
// ---- handle lifecycles
const lifecycles = ['willMount', 'didMount', 'willUnmount', 'didUnmount'];
if (this.t.isLabeledStatement(node) && lifecycles.includes(node.label.name)) {
// transform the lifecycle statement to lifecycle method
classTransformer.transformLifeCycle(node);
return;
}
// handle watch
if (this.t.isLabeledStatement(node) && node.label.name === 'watch') {
// transform the watch statement to watch method
@ -172,7 +180,19 @@ class ClassComponentTransformer {
this.addProperty(body, 'Body');
}
transformLifeCycle() {}
transformLifeCycle(node: t.LabeledStatement) {
// transform the lifecycle statement to lifecycle method
const methodName = node.label.name;
const method = this.t.classMethod(
'method',
this.t.identifier(methodName),
[],
this.t.blockStatement(node.body.body),
false,
false
);
this.addProperty(method, methodName);
}
transformComputed() {}
@ -218,7 +238,7 @@ class ClassComponentTransformer {
*/
transformWatch(node: t.LabeledStatement) {
const id = this.functionScope.generateUidIdentifier(DECORATOR_WATCH.toLowerCase());
const method = this.t.classMethod('method', id, [], this.t.blockStatement([node]), false, false);
const method = this.t.classMethod('method', id, [], this.t.blockStatement([node.body]), false, false);
method.decorators = [this.t.decorator(this.t.identifier(DECORATOR_WATCH))];
this.addProperty(method);
}

View File

@ -1,5 +1,11 @@
# vite-plugin-inula-next
## 0.0.3
### Patch Changes
- babel-preset-inula-next@0.0.3
## 0.0.2
### Patch Changes

View File

@ -1,6 +1,6 @@
{
"name": "vite-plugin-inula-next",
"version": "0.0.2",
"version": "0.0.3",
"author": {
"name": "IanDx",
"email": "iandxssxx@gmail.com"
@ -39,6 +39,7 @@
"esm"
],
"clean": true,
"dts": true
"dts": true,
"minify": true
}
}