Skip to content

Commit 3a3bad2

Browse files
authored
Merge pull request #257 from wechat-miniprogram/feat-checker
Add typescript & WXML interaction support
2 parents da591a9 + 73c653c commit 3a3bad2

File tree

34 files changed

+3033
-405
lines changed

34 files changed

+3033
-405
lines changed

glass-easel-miniprogram-adapter/src/component.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import { MediaQueryObserver } from './media_query'
88
type DataList = typeUtils.DataList
99
type PropertyList = typeUtils.PropertyList
1010
type MethodList = typeUtils.MethodList
11+
export type Empty = typeUtils.Empty
12+
export type PropertyValues<TProperty extends PropertyList> = typeUtils.PropertyValues<TProperty>
1113
export type AllData<
1214
TData extends DataList,
1315
TProperty extends PropertyList,

glass-easel-miniprogram-adapter/src/space.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,13 @@ export interface PageConstructor {
3131
/* TExtraThisFields */ Empty
3232
>
3333
>,
34-
): void
34+
): ComponentType<
35+
/* TData */ TData,
36+
/* TProperty */ Empty,
37+
/* TMethod */ TNewExtraFields,
38+
/* TComponentExport */ never,
39+
/* TExtraThisFields */ Empty
40+
>
3541
}
3642

3743
// The component constructor

glass-easel-miniprogram-adapter/tests/space.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,4 +1194,26 @@ describe('define', () => {
11941194
root.getComponent().triggerPageLifetime('show', [])
11951195
expect(domHtml(root.getComponent())).toBe('<comp1>2</comp1>')
11961196
})
1197+
1198+
test('overall behavior', () => {
1199+
const env = new MiniProgramEnv()
1200+
const codeSpace = env.createCodeSpace('', true)
1201+
1202+
const componentSpace = codeSpace.getComponentSpace()
1203+
const overall = componentSpace.defineBehavior({
1204+
is: 'overall',
1205+
data: {
1206+
num: 1,
1207+
},
1208+
})
1209+
codeSpace.setOverallBehavior(overall)
1210+
1211+
codeSpace.addCompiledTemplate('comp', tmpl(`<div>{{ num }}</div>`))
1212+
1213+
codeSpace.component('comp').register()
1214+
1215+
const ab = env.associateBackend()
1216+
const root = ab.createRoot('body', codeSpace, 'comp')
1217+
expect(domHtml(root.getComponent())).toBe('<div>1</div>')
1218+
})
11971219
})

glass-easel-miniprogram-template/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717
"eslint": "^7.32.0",
1818
"eslint-plugin-import": "^2.22.1",
1919
"eslint-plugin-promise": "^4.2.1",
20+
"glass-easel-miniprogram-typescript": "workspace:*",
2021
"glass-easel-miniprogram-webpack-plugin": "workspace:*",
2122
"less": "^4.1.3",
2223
"less-loader": "^11.0.0",
2324
"mini-css-extract-plugin": "^2.6.1",
2425
"ts-loader": "^9.4.2",
2526
"typescript": "~5.2.2",
26-
"webpack": "^5.85.0",
27-
"webpack-cli": "^5.0.1"
27+
"webpack": "^5.101.3",
28+
"webpack-cli": "^6.0.1"
2829
}
2930
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
Component({
1+
export default Component({
22
properties: {
33
src: String,
44
},
5+
data: {},
56
})

glass-easel-miniprogram-template/src/components/view/view.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { StyleSegmentIndex } from 'glass-easel'
22

3-
Component({
3+
export default Component({
44
properties: {
55
hidden: Boolean,
66
},
7+
data: {},
78
observers: {
89
hidden(hidden: boolean) {
910
// `this._$` is the underlying glass-easel element

glass-easel-miniprogram-template/src/pages/index/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Page({
1+
export default Page({
22
data: {
33
showAgain: false,
44
},
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/package-lock.json
2+
/node_modules
3+
/dist
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# glass-easel-miniprogram-typescript
2+
3+
An extra TypeScript checker for mini-program code running in glass-easel.
4+
5+
Refer to the [glass-easel](https://github.com/wechat-miniprogram/glass-easel) project for further details.
6+
7+
## Features
8+
9+
If the component is written in TypeScript, this tool can do some type-checking on expressions inside WXML.
10+
11+
Firstly, the component `.ts` must export the component as default. For example:
12+
13+
```ts
14+
export default Component({
15+
data: {
16+
hello: 'world',
17+
},
18+
})
19+
```
20+
21+
Then in `.wxml` :
22+
23+
```xml
24+
<div>{{ hello }}</div>
25+
26+
<!-- This tool will report type errors like -->
27+
<div>{{ heloo }}</div> <!-- Error: 'heloo' does not exist -->
28+
```
29+
30+
This tool is based on TypeScript (a.k.a. `tsserver` ) for type-checking.
31+
32+
Note that it only report errors in component WXML files. For type errors in TS files, you should run standard TypeScript commands.
33+
34+
## Usage
35+
36+
After install this module, the `miniprogram-typescript-check` tool should be available.
37+
38+
See all options: `npx miniprogram-typescript-check --help`
39+
40+
In most cases, this command is suggested: `npx miniprogram-typescript-check -p [PATH_TO_SRC]`
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
{
2+
"name": "glass-easel-miniprogram-typescript",
3+
"description": "A checker for the glass-easel project in MiniProgram file structure",
4+
"version": "0.13.0",
5+
"engines": {
6+
"node": ">=20.0.0"
7+
},
8+
"bin": {
9+
"miniprogram-typescript-check": "dist/cli.js"
10+
},
11+
"repository": {
12+
"type": "git",
13+
"url": "https://github.com/wechat-miniprogram/glass-easel.git"
14+
},
15+
"keywords": [
16+
"glass-easel"
17+
],
18+
"author": "wechat-miniprogram",
19+
"license": "MIT",
20+
"bugs": {
21+
"url": "https://github.com/wechat-miniprogram/glass-easel/issues"
22+
},
23+
"homepage": "https://github.com/wechat-miniprogram/glass-easel",
24+
"main": "dist/index.js",
25+
"types": "src/index.ts",
26+
"scripts": {
27+
"build": "tsc -p .",
28+
"dev": "tsc -w -p .",
29+
"lint": "eslint src/**/*.ts",
30+
"start": "node dist/cli.js",
31+
"build-and-start": "npm run build && node dist/cli.js"
32+
},
33+
"peerDependencies": {
34+
"glass-easel-miniprogram-adapter": "workspace:*",
35+
"typescript": "^5.2.2"
36+
},
37+
"dependencies": {
38+
"chalk": "4",
39+
"chokidar": "^4.0.3",
40+
"glass-easel-stylesheet-compiler": "workspace:*",
41+
"glass-easel-template-compiler": "workspace:*"
42+
}
43+
}

0 commit comments

Comments
 (0)