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

refactor: runtime structure #63

Merged
merged 28 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3482953
refactor: runtime structure
SoloJiang Apr 25, 2022
42aa51b
chore: lint
SoloJiang Apr 25, 2022
1949423
feat: support element template diff
SoloJiang Apr 26, 2022
b38fee5
chore: lint
SoloJiang Apr 26, 2022
9c687a5
feat(compiler): update structure in get template method (#62)
ChrisCindy Apr 26, 2022
7c7a6af
chore: resolve conflict
SoloJiang Apr 26, 2022
271da66
feat: html method support new structure
SoloJiang Apr 26, 2022
5dae056
feat: html method support new structure
SoloJiang Apr 26, 2022
57a434b
fix(compiler): inject wrong value in runtime compiler
ChrisCindy Apr 26, 2022
739ce9e
refactor: reactive node
SoloJiang May 6, 2022
83eb345
chore: test case
SoloJiang May 6, 2022
00d3192
fix(compiler): replace values with templateData in template result
ChrisCindy May 6, 2022
c30711b
chore: test case
SoloJiang May 6, 2022
3c04a3d
chore: add test case
SoloJiang May 6, 2022
3d77430
refactor: base render
SoloJiang May 7, 2022
a7a4316
chore: revert code
SoloJiang May 7, 2022
086d62f
chore: test case
SoloJiang May 9, 2022
32bec4f
chore: resolve conflict
SoloJiang May 9, 2022
32cabcd
chore: lint
SoloJiang May 9, 2022
24bb23b
chore: ts type
SoloJiang May 9, 2022
7d78381
chore: test case
SoloJiang May 9, 2022
56614ee
chore: add comment
SoloJiang May 9, 2022
39398dc
chore: remove useless comment
SoloJiang May 9, 2022
e7e4c36
chore: add test case
SoloJiang May 9, 2022
13cea0b
chore: optimize code
SoloJiang May 9, 2022
b2be016
chore: optimize code
SoloJiang May 10, 2022
44f9f5b
chore: link
SoloJiang May 10, 2022
87202f1
chore: optimize code
SoloJiang May 10, 2022
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
10 changes: 5 additions & 5 deletions examples/basic/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class Child extends HTMLElement {
connectedCallback() {
super.connectedCallback();
console.log('connected');
console.log('checked => ', this.checked);
console.log(this.className)
}

get template() {
return html`<div>
return html`<div id="container">
Child ${this.name}
<div>parent class name is ${this.className}</div>
</div>`;
Expand Down Expand Up @@ -49,13 +49,13 @@ class CustomElement extends HTMLElement {
onClick() {
this.#data.name += '!';
this.#text += '?';
this.className = this.className === 'green' ? 'red' : 'green';
this.#className = this.#className === 'green' ? 'red' : 'green';
};

get template() {
return html`<div class=${this.className} @click=${this.onClick}>
return html`<div class=${this.#className} @click=${this.onClick}>
${this.#text} - ${this.#data.name}
<child-element name=${this.#data.name} checked=${true} data-class-name=${this.className} />
<child-element name=${this.#data.name} checked=${true} data-class-name=${this.#className} />
</div>`;
}
}
21 changes: 21 additions & 0 deletions examples/list/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Basic</title>
<style>
.red {
color: red;
}
.green {
color: green;
}
</style>
</head>
<body>
<custom-element>
</custom-element>
<script type="module" src="/main.js"></script>
</body>
</html>
34 changes: 34 additions & 0 deletions examples/list/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { reactive, customElement, html } from 'pwc';

@customElement('custom-element')
class CustomElement extends HTMLElement {
@reactive
accessor #title = 'default title';

@reactive
accessor #list = ['item 1', 'item 2', 'item 3', 'item 4'];

onClick() {
this.#list.push('item 5');
}

handleItemClick(index) {
this.#list = [...this.#list.slice(0, index), ...this.#list.slice(index + 1)];
}

get template() {
return html`<div @click=${this.onClick}>
${html`${this.#list.map((item, index) => {
if (item === 'item 2') {
return null;
}
if (item === 'item 3') {
return [1, 2, 3].map((insideItem) => {
return html`<div @click=${() => this.handleItemClick(index)}>inside list: ${insideItem}</div>`;
});
}
return html`<div @click=${() => this.handleItemClick(index)}>${item}</div>`;
})}`}
</div>`;
}
}
18 changes: 18 additions & 0 deletions examples/list/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "basic",
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"devDependencies": {
"rollup": "^2.0.0",
"vite": "^2.7.2",
"vite-plugin-babel": "^1.0.0"
},
"dependencies": {
"pwc": "workspace:*"
},
"browserslist": "chrome > 60"
}
33 changes: 33 additions & 0 deletions examples/list/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { defineConfig } from 'vite';
import babel from 'vite-plugin-babel';

export default defineConfig({
plugins: [
babel({
babelConfig: {
presets: [
[
'@babel/preset-env',
{
targets: {
chrome: 99,
},
modules: false,
},
],
],
plugins: [
[
'@babel/plugin-proposal-decorators',
{
version: '2021-12',
},
],
'@babel/plugin-proposal-class-properties',
'@babel/plugin-proposal-class-static-block',
'@babel/plugin-proposal-private-methods'
],
},
}),
],
});
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"@types/jest": "^27.4.1",
"@types/node": "^17.0.21",
"chalk": "^4.1.2",
"codecov": "^3.8.3",
"esbuild": "^0.14.27",
"eslint": "^7.32.0",
"esno": "^0.14.1",
Expand All @@ -47,6 +48,7 @@
"prettier": "^2.6.0",
"stylelint": "^14.6.0",
"typescript": "^4.6.2",
"codecov": "^3.8.3"
"driver-dom": "^2.2.2",
"rax": "^1.2.2"
}
}
56 changes: 34 additions & 22 deletions packages/pwc-compiler/__tests__/compileScript.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const multipleKindsOfUsingBindingsComponent = `
attr3="{{this.data3.arr1[0]}}"
attr4="{{this['data-five']}}"
@click="{{this.#fn}}"
onevent="{{this.#fn}}"
@input="{{this.methods.fn2}}"
>
<div>{{ this.#data1 }}</div>
Expand Down Expand Up @@ -63,7 +64,11 @@ describe('compileScript', () => {
const result = compileScript(descriptor);

expect(result.content).toContain(`get template() {
return [\"\\n <p><!--?pwc_t--></p>\\n\", [this.#text]];
return {
templateString: \"\\n <p><!--?pwc_t--></p>\\n\",
templateData: [this.#text],
template: true
};
}`);
});

Expand Down Expand Up @@ -105,27 +110,34 @@ export default class CustomElement extends HTMLElement {
};

get template() {
return ["\\n <!--?pwc_p--><div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n </div>\\n", [[{
name: "attr1",
value: this.#data1
}, {
name: "attr2",
value: this.#data2.name1
}, {
name: "attr3",
value: this.data3.arr1[0]
}, {
name: "attr4",
value: this['data-five']
}, {
name: "onclick",
value: this.#fn,
capture: false
}, {
name: "oninput",
value: this.methods.fn2,
capture: false
}], this.#data1, this.#data2.name1, this.data3.arr1[0], this.data4.obj1.name2, this['data-five']]];
return {
templateString: "\\n <!--?pwc_p--><div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n <div><!--?pwc_t--></div>\\n </div>\\n",
templateData: [[{
name: "attr1",
value: this.#data1
}, {
name: "attr2",
value: this.#data2.name1
}, {
name: "attr3",
value: this.data3.arr1[0]
}, {
name: "attr4",
value: this['data-five']
}, {
name: "onclick",
handler: this.#fn,
capture: false
}, {
name: "onevent",
value: this.#fn
}, {
name: "oninput",
handler: this.methods.fn2,
capture: false
}], this.#data1, this.#data2.name1, this.data3.arr1[0], this.data4.obj1.name2, this['data-five']],
template: true
};
}

}`);
Expand Down
Loading