From 616ec7f7fd062c6f4aa0d4473283cd207ada0a4f Mon Sep 17 00:00:00 2001 From: Cheng Wang Date: Mon, 30 Oct 2023 10:47:39 +0800 Subject: [PATCH] add example-mina --- package.json | 3 +- packages/example-mina/.gitignore | 2 + packages/example-mina/miniprogram/app.js | 2 + packages/example-mina/miniprogram/app.json | 19 ++ packages/example-mina/miniprogram/app.wxss | 10 + .../miniprogram/pages/index/index.js | 7 + .../miniprogram/pages/index/index.json | 1 + .../miniprogram/pages/index/index.wxml | 6 + .../miniprogram/pages/index/index.wxss | 10 + .../miniprogram/project.config.json | 29 +++ packages/example-mina/package.json | 17 ++ packages/example-mina/src/connect-client.js | 52 ++++ packages/example-mina/src/polyfill.js | 11 + yarn.lock | 242 ++++++++++++++++++ 14 files changed, 410 insertions(+), 1 deletion(-) create mode 100644 packages/example-mina/.gitignore create mode 100644 packages/example-mina/miniprogram/app.js create mode 100644 packages/example-mina/miniprogram/app.json create mode 100644 packages/example-mina/miniprogram/app.wxss create mode 100644 packages/example-mina/miniprogram/pages/index/index.js create mode 100644 packages/example-mina/miniprogram/pages/index/index.json create mode 100644 packages/example-mina/miniprogram/pages/index/index.wxml create mode 100644 packages/example-mina/miniprogram/pages/index/index.wxss create mode 100644 packages/example-mina/miniprogram/project.config.json create mode 100644 packages/example-mina/package.json create mode 100644 packages/example-mina/src/connect-client.js create mode 100644 packages/example-mina/src/polyfill.js diff --git a/package.json b/package.json index ba89299..4dd3c92 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,8 @@ "private": true, "workspaces": [ "packages/connect-miniprogram", - "packages/example-taro" + "packages/example-taro", + "packages/example-mina" ], "prettier": { "arrowParens": "always", diff --git a/packages/example-mina/.gitignore b/packages/example-mina/.gitignore new file mode 100644 index 0000000..6dc7426 --- /dev/null +++ b/packages/example-mina/.gitignore @@ -0,0 +1,2 @@ +miniprogram/project.private.config.json +miniprogram/generated_modules diff --git a/packages/example-mina/miniprogram/app.js b/packages/example-mina/miniprogram/app.js new file mode 100644 index 0000000..8f91a05 --- /dev/null +++ b/packages/example-mina/miniprogram/app.js @@ -0,0 +1,2 @@ +// app.js +App({}); diff --git a/packages/example-mina/miniprogram/app.json b/packages/example-mina/miniprogram/app.json new file mode 100644 index 0000000..89618f5 --- /dev/null +++ b/packages/example-mina/miniprogram/app.json @@ -0,0 +1,19 @@ +{ + "pages": ["pages/index/index"], + "window": { + "navigationBarTextStyle": "black", + "navigationStyle": "custom" + }, + "style": "v2", + "renderer": "skyline", + "rendererOptions": { + "skyline": { + "defaultDisplayBlock": true, + "disableABTest": true, + "sdkVersionBegin": "3.0.0", + "sdkVersionEnd": "15.255.255" + } + }, + "componentFramework": "glass-easel", + "lazyCodeLoading": "requiredComponents" +} diff --git a/packages/example-mina/miniprogram/app.wxss b/packages/example-mina/miniprogram/app.wxss new file mode 100644 index 0000000..06c6fc9 --- /dev/null +++ b/packages/example-mina/miniprogram/app.wxss @@ -0,0 +1,10 @@ +/**app.wxss**/ +.container { + height: 100%; + display: flex; + flex-direction: column; + align-items: center; + justify-content: space-between; + padding: 200rpx 0; + box-sizing: border-box; +} diff --git a/packages/example-mina/miniprogram/pages/index/index.js b/packages/example-mina/miniprogram/pages/index/index.js new file mode 100644 index 0000000..e285c8d --- /dev/null +++ b/packages/example-mina/miniprogram/pages/index/index.js @@ -0,0 +1,7 @@ +Page({ + onReady: () => { + require.async('../../generated_modules/connect-client').then(({ main }) => { + main(); + }); + }, +}); diff --git a/packages/example-mina/miniprogram/pages/index/index.json b/packages/example-mina/miniprogram/pages/index/index.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/packages/example-mina/miniprogram/pages/index/index.json @@ -0,0 +1 @@ +{} diff --git a/packages/example-mina/miniprogram/pages/index/index.wxml b/packages/example-mina/miniprogram/pages/index/index.wxml new file mode 100644 index 0000000..de5e1c0 --- /dev/null +++ b/packages/example-mina/miniprogram/pages/index/index.wxml @@ -0,0 +1,6 @@ + + + + Weixin + + diff --git a/packages/example-mina/miniprogram/pages/index/index.wxss b/packages/example-mina/miniprogram/pages/index/index.wxss new file mode 100644 index 0000000..8c2b75a --- /dev/null +++ b/packages/example-mina/miniprogram/pages/index/index.wxss @@ -0,0 +1,10 @@ +/**index.wxss**/ +page { + height: 100vh; + display: flex; + flex-direction: column; +} +.scrollarea { + flex: 1; + overflow-y: hidden; +} diff --git a/packages/example-mina/miniprogram/project.config.json b/packages/example-mina/miniprogram/project.config.json new file mode 100644 index 0000000..fdcd674 --- /dev/null +++ b/packages/example-mina/miniprogram/project.config.json @@ -0,0 +1,29 @@ +{ + "appid": "wx043e0b542b3515b8", + "compileType": "miniprogram", + "libVersion": "3.1.5", + "packOptions": { + "ignore": [], + "include": [] + }, + "setting": { + "urlCheck": false, + "coverView": true, + "es6": true, + "postcss": true, + "minified": true, + "enhance": true, + "showShadowRootInWxmlPanel": true, + "packNpmRelationList": [], + "babelSetting": { + "ignore": [], + "disablePlugins": [], + "outputPath": "" + } + }, + "condition": {}, + "editorSetting": { + "tabIndent": "insertSpaces", + "tabSize": 2 + } +} \ No newline at end of file diff --git a/packages/example-mina/package.json b/packages/example-mina/package.json new file mode 100644 index 0000000..3a630d7 --- /dev/null +++ b/packages/example-mina/package.json @@ -0,0 +1,17 @@ +{ + "name": "example-mina", + "version": "1.0.0", + "private": true, + "scripts": { + "build": "esbuild src/connect-client.js --bundle --outfile=miniprogram/generated_modules/connect-client.js --format=cjs --target=es2015 --inject:src/polyfill.js" + }, + "dependencies": { + "@buf/connectrpc_eliza.connectrpc_es": "^1.1.3-20230913231627-233fca715f49.1", + "@bufbuild/protobuf": "^1.4.1", + "@connectrpc/connect": "^1.1.3", + "connect-miniprogram": "workspace:packages/connect-miniprogram" + }, + "devDependencies": { + "esbuild": "^0.19.5" + } +} diff --git a/packages/example-mina/src/connect-client.js b/packages/example-mina/src/connect-client.js new file mode 100644 index 0000000..c803e41 --- /dev/null +++ b/packages/example-mina/src/connect-client.js @@ -0,0 +1,52 @@ +import { createPromiseClient } from '@connectrpc/connect'; +import { + createConnectTransport, + createGrpcWebTransport, +} from 'connect-miniprogram/src'; +import { ElizaService } from '@buf/connectrpc_eliza.connectrpc_es/connectrpc/eliza/v1/eliza_connect'; + +export const isDevTool = wx.getSystemInfoSync().platform === 'devtools'; + +export const baseUrl = 'https://demo.connectrpc.com'; + +const grpcTransport = createGrpcWebTransport({ + baseUrl, + request: wx.request, + isDevTool, +}); + +const grpcClient = createPromiseClient(ElizaService, grpcTransport); + +const connectTransport = createConnectTransport({ + baseUrl, + request: wx.request, + isDevTool, +}); + +const connectClient = createPromiseClient(ElizaService, connectTransport); + +export async function main() { + connectClient + .say({ + sentence: 'I feel happy.', + }) + .then((res) => { + console.log('[connect.say]', res); + }); + + for await (const res of connectClient.introduce({ name: 'Joseph' })) { + console.log('[connect.introduce]', res); + } + + grpcClient + .say({ + sentence: 'I feel happy.', + }) + .then((res) => { + console.log('[grpc.say]', res); + }); + + for await (const res of grpcClient.introduce({ name: 'Joseph' })) { + console.log('[grpc.introduce]', res); + } +} diff --git a/packages/example-mina/src/polyfill.js b/packages/example-mina/src/polyfill.js new file mode 100644 index 0000000..bb62357 --- /dev/null +++ b/packages/example-mina/src/polyfill.js @@ -0,0 +1,11 @@ +import { + HeadersPolyfill, + FastTextDecoder, + FastTextEncoder, +} from 'connect-miniprogram/polyfill'; + +export { + HeadersPolyfill as Headers, + FastTextDecoder as TextDecoder, + FastTextEncoder as TextEncoder, +}; diff --git a/yarn.lock b/yarn.lock index 09eb749..60eae78 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1720,6 +1720,76 @@ __metadata: languageName: node linkType: hard +"@esbuild/android-arm64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/android-arm64@npm:0.19.5" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/android-arm@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/android-arm@npm:0.19.5" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + +"@esbuild/android-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/android-x64@npm:0.19.5" + conditions: os=android & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/darwin-arm64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/darwin-arm64@npm:0.19.5" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/darwin-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/darwin-x64@npm:0.19.5" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/freebsd-arm64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/freebsd-arm64@npm:0.19.5" + conditions: os=freebsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/freebsd-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/freebsd-x64@npm:0.19.5" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/linux-arm64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-arm64@npm:0.19.5" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/linux-arm@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-arm@npm:0.19.5" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@esbuild/linux-ia32@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-ia32@npm:0.19.5" + conditions: os=linux & cpu=ia32 + languageName: node + linkType: hard + "@esbuild/linux-loong64@npm:0.14.54": version: 0.14.54 resolution: "@esbuild/linux-loong64@npm:0.14.54" @@ -1727,6 +1797,90 @@ __metadata: languageName: node linkType: hard +"@esbuild/linux-loong64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-loong64@npm:0.19.5" + conditions: os=linux & cpu=loong64 + languageName: node + linkType: hard + +"@esbuild/linux-mips64el@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-mips64el@npm:0.19.5" + conditions: os=linux & cpu=mips64el + languageName: node + linkType: hard + +"@esbuild/linux-ppc64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-ppc64@npm:0.19.5" + conditions: os=linux & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/linux-riscv64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-riscv64@npm:0.19.5" + conditions: os=linux & cpu=riscv64 + languageName: node + linkType: hard + +"@esbuild/linux-s390x@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-s390x@npm:0.19.5" + conditions: os=linux & cpu=s390x + languageName: node + linkType: hard + +"@esbuild/linux-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/linux-x64@npm:0.19.5" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/netbsd-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/netbsd-x64@npm:0.19.5" + conditions: os=netbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/openbsd-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/openbsd-x64@npm:0.19.5" + conditions: os=openbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/sunos-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/sunos-x64@npm:0.19.5" + conditions: os=sunos & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/win32-arm64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/win32-arm64@npm:0.19.5" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/win32-ia32@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/win32-ia32@npm:0.19.5" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/win32-x64@npm:0.19.5": + version: 0.19.5 + resolution: "@esbuild/win32-x64@npm:0.19.5" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -7236,6 +7390,83 @@ __metadata: languageName: node linkType: hard +"esbuild@npm:^0.19.5": + version: 0.19.5 + resolution: "esbuild@npm:0.19.5" + dependencies: + "@esbuild/android-arm": "npm:0.19.5" + "@esbuild/android-arm64": "npm:0.19.5" + "@esbuild/android-x64": "npm:0.19.5" + "@esbuild/darwin-arm64": "npm:0.19.5" + "@esbuild/darwin-x64": "npm:0.19.5" + "@esbuild/freebsd-arm64": "npm:0.19.5" + "@esbuild/freebsd-x64": "npm:0.19.5" + "@esbuild/linux-arm": "npm:0.19.5" + "@esbuild/linux-arm64": "npm:0.19.5" + "@esbuild/linux-ia32": "npm:0.19.5" + "@esbuild/linux-loong64": "npm:0.19.5" + "@esbuild/linux-mips64el": "npm:0.19.5" + "@esbuild/linux-ppc64": "npm:0.19.5" + "@esbuild/linux-riscv64": "npm:0.19.5" + "@esbuild/linux-s390x": "npm:0.19.5" + "@esbuild/linux-x64": "npm:0.19.5" + "@esbuild/netbsd-x64": "npm:0.19.5" + "@esbuild/openbsd-x64": "npm:0.19.5" + "@esbuild/sunos-x64": "npm:0.19.5" + "@esbuild/win32-arm64": "npm:0.19.5" + "@esbuild/win32-ia32": "npm:0.19.5" + "@esbuild/win32-x64": "npm:0.19.5" + dependenciesMeta: + "@esbuild/android-arm": + optional: true + "@esbuild/android-arm64": + optional: true + "@esbuild/android-x64": + optional: true + "@esbuild/darwin-arm64": + optional: true + "@esbuild/darwin-x64": + optional: true + "@esbuild/freebsd-arm64": + optional: true + "@esbuild/freebsd-x64": + optional: true + "@esbuild/linux-arm": + optional: true + "@esbuild/linux-arm64": + optional: true + "@esbuild/linux-ia32": + optional: true + "@esbuild/linux-loong64": + optional: true + "@esbuild/linux-mips64el": + optional: true + "@esbuild/linux-ppc64": + optional: true + "@esbuild/linux-riscv64": + optional: true + "@esbuild/linux-s390x": + optional: true + "@esbuild/linux-x64": + optional: true + "@esbuild/netbsd-x64": + optional: true + "@esbuild/openbsd-x64": + optional: true + "@esbuild/sunos-x64": + optional: true + "@esbuild/win32-arm64": + optional: true + "@esbuild/win32-ia32": + optional: true + "@esbuild/win32-x64": + optional: true + bin: + esbuild: bin/esbuild + checksum: 401e6da33bf6f2c4bbfa0aa8f37ddc6eb41c9d8ddf6b32c9922aabeef3f1886ed792eb03e778859e7e61467c765c78245f88216bc1a59050413ce7a513dd675f + languageName: node + linkType: hard + "escalade@npm:^3.1.1": version: 3.1.1 resolution: "escalade@npm:3.1.1" @@ -7621,6 +7852,17 @@ __metadata: languageName: node linkType: hard +"example-mina@workspace:packages/example-mina": + version: 0.0.0-use.local + resolution: "example-mina@workspace:packages/example-mina" + dependencies: + "@bufbuild/protobuf": "npm:^1.4.1" + "@connectrpc/connect": "npm:^1.1.3" + connect-miniprogram: "workspace:packages/connect-miniprogram" + esbuild: "npm:^0.19.5" + languageName: unknown + linkType: soft + "example-taro@workspace:packages/example-taro": version: 0.0.0-use.local resolution: "example-taro@workspace:packages/example-taro"