Skip to content

Commit 7c1337b

Browse files
committed
feat: initial coding
0 parents  commit 7c1337b

15 files changed

+8254
-0
lines changed

.eslintrc.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const path = require('path')
2+
3+
module.exports = {
4+
root: true,
5+
parserOptions: {
6+
ecmaVersion: 9,
7+
sourceType: 'module',
8+
},
9+
parser: '@typescript-eslint/parser',
10+
plugins: ['@typescript-eslint', 'import', 'promise', 'prettier'],
11+
overrides: [
12+
{
13+
files: ['*.ts'],
14+
extends: [
15+
'plugin:@typescript-eslint/recommended',
16+
'plugin:@typescript-eslint/recommended-requiring-type-checking',
17+
'eslint-config-prettier',
18+
],
19+
parserOptions: {
20+
project: path.join(__dirname, 'tsconfig.json'),
21+
},
22+
rules: {},
23+
},
24+
],
25+
extends: [
26+
'eslint:recommended',
27+
'plugin:promise/recommended',
28+
'eslint-config-prettier',
29+
],
30+
env: {
31+
es6: true,
32+
jest: true,
33+
},
34+
rules: {},
35+
}

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.DS_Store
2+
node_modules
3+
/dist

.prettierrc.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module.exports = {
2+
printWidth: 100,
3+
tabWidth: 2,
4+
useTabs: false,
5+
semi: false,
6+
singleQuote: true,
7+
quoteProps: 'as-needed',
8+
trailingComma: 'all',
9+
bracketSpacing: true,
10+
arrowParens: 'always',
11+
requirePragma: false,
12+
insertPragma: false,
13+
proseWrap: 'preserve',
14+
endOfLine: 'lf',
15+
embeddedLanguageFormatting: 'auto',
16+
}

LICENSE

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Copyright 2024 wechat-miniprogram
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4+
5+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6+
7+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Chaining API Polyfill
2+
3+
为在非 glass-easel 组件框架下运行的小程序提供 Chaining API 支持。
4+
5+
6+
## 为什么要使用 Chaining API ?
7+
8+
小程序使用传统的 `Page``Component` 构造器时,在复杂情况下会很不好用。
9+
10+
例如,如果页面或组件有个复杂的私有变量,只能选择将它写入到 `this` 的某个字段上,或者 setData 到 `this.data` 上。无论哪种处理方式,对 TypeScript 都不太友好,而且可能带来较大的性能开销。
11+
12+
为了解决这类问题,小程序的 [glass-easel 组件框架](https://github.com/wechat-miniprogram/glass-easel)提供了 [Chaining API](https://developers.weixin.qq.com/miniprogram/dev/framework/custom-component/glass-easel/chaining-api.html) 。其中最重要的是提供了 `init` 函数。使用 Chaining API 可以编写出更易于维护、对 TypeScript 更有好的组件代码。
13+
14+
然而,在未激活 glass-easel 组件框架时,或在旧版本的小程序基础库中, Chaining API 不被支持。这个 polyfill 可以在这些时候补上 Chaining API 支持。
15+
16+
17+
## 这个 Polyfill 对 Chaining API 的支持度
18+
19+
这个 Polyfill 支持提供绝大多数 Chaining API 系列接口。
20+
21+
但受限于旧版小程序框架,仍有少量接口无法支持,主要包括:
22+
23+
* `.data(...)` 中不能使用函数形式,只能使用对象形式。
24+
* `.init(ctx)` 中不支持 `ctx.relation`
25+
26+
最好在 TypeScript 下使用这个模块。(这样可以准确知道可用的接口。)
27+
28+
29+
## 基础用法
30+
31+
首先在 npm 中引入:
32+
33+
```shell
34+
npm install --save miniprogram-chaining-api-polyfill
35+
```
36+
37+
npm install 之后,需要点一下小程序开发者工具菜单中的“工具”——“构建 npm ”。
38+
39+
在想要使用 Chaining API 的页面或组件文件中,引入 polyfill 过的 `Component` 构造器:
40+
41+
```ts
42+
import { Component } from 'miniprogram-chaining-api-polyfill'
43+
```
44+
45+
然后就可以使用 Chaining API 了。
46+
47+
上述方式引入时,会自动判断当前环境是否具有原生的 Chaining API 支持。如果有,就会使用原生的;反之会激活 polyfill 。
48+
49+
如果需要强制激活 polyfill ,可以这样引入:
50+
51+
```ts
52+
import { ComponentForcePolyfill as Component } from 'miniprogram-chaining-api-polyfill'
53+
```
54+
55+
56+
## LICENSE
57+
58+
MIT

jest.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
bail: 1,
4+
verbose: true,
5+
testEnvironment: 'jsdom',
6+
moduleFileExtensions: ['js', 'ts'],
7+
setupFiles: ['<rootDir>/test/setup.ts'],
8+
testMatch: ['<rootDir>/test/**/*.test.ts'],
9+
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!**/__test__/**'],
10+
}

0 commit comments

Comments
 (0)