Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
41 changes: 41 additions & 0 deletions .github/workflows/build-webview-bridge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: build-webview-bridge

on:
pull_request:
paths:
- 'packages/webview-bridge/src/**'
- 'packages/webview-bridge/build/**'
- 'packages/webview-bridge/package.json'
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
token: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
working-directory: packages/webview-bridge
run: npm install

- name: Build webview-bridge
working-directory: packages/webview-bridge
run: node build/build.js

- name: Commit if changed
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add packages/webview-bridge/dist/
git add examples/mpx-webview/H5/webviewbridge.min.js
if git diff --cached --quiet; then
echo "dist is already up-to-date, nothing to commit"
else
git commit -m "chore: auto-build webview-bridge dist"
git push origin HEAD:${{ github.head_ref }}
fi
34 changes: 34 additions & 0 deletions .github/workflows/gen-api-dynamic.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: gen-dynamic

on:
pull_request:
paths:
- 'packages/api-proxy/src/platform/**'
- 'packages/api-proxy/scripts/gen-dynamic.js'
workflow_dispatch:

jobs:
generate:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
token: ${{ secrets.GITHUB_TOKEN }}

- name: Generate dynamic.js
run: node packages/api-proxy/scripts/gen-dynamic.js

- name: Commit if changed
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add packages/api-proxy/src/common/js/dynamic.js
if git diff --cached --quiet; then
echo "dynamic.js is already up-to-date, nothing to commit"
else
git commit -m "chore: auto-generate dynamic.js"
git push origin HEAD:${{ github.head_ref }}
fi
3 changes: 2 additions & 1 deletion packages/api-proxy/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"*.styl"
],
"scripts": {
"test": "jest"
"test": "jest",
"gen:dynamic": "node scripts/gen-dynamic.js"
},
"files": [
"src",
Expand Down
106 changes: 106 additions & 0 deletions packages/api-proxy/scripts/gen-dynamic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
#!/usr/bin/env node
/**
* Scans all exported methods from src/platform/api and generates
* src/common/js/dynamic.js with lazy-loading require() wrappers.
*
* Run: node scripts/gen-dynamic.js
*/

const fs = require('fs')
const path = require('path')

const srcDir = path.resolve(__dirname, '../src')
const platformIndexPath = path.join(srcDir, 'platform/index.js')
const outputPath = path.join(srcDir, 'common/js/dynamic.js')
const commonJsDir = path.join(srcDir, 'common/js')

if (!fs.existsSync(platformIndexPath)) {
console.error('platform/index.js not found:', platformIndexPath)
process.exit(1)
}

const platformContent = fs.readFileSync(platformIndexPath, 'utf-8')

// Extract all "export * from './api/xxx'" paths
const exportMatches = [...platformContent.matchAll(/export \* from '(.+?)'/g)]

if (exportMatches.length === 0) {
console.error('No export * from entries found in platform/index.js')
process.exit(1)
}

// name -> requirePath (relative to common/js/)
const apiEntries = []
const seenNames = new Set()

for (const [, relPath] of exportMatches) {
// relPath is like './api/system' or './api/device/network'
const apiDir = path.join(srcDir, 'platform', relPath)

let indexFilePath = path.join(apiDir, 'index.js')
if (!fs.existsSync(indexFilePath)) {
// fallback: relPath might point directly to a .js file
indexFilePath = apiDir + '.js'
if (!fs.existsSync(indexFilePath)) {
console.warn(' [skip] index.js not found for:', relPath)
continue
}
}

const content = fs.readFileSync(indexFilePath, 'utf-8')

// Match all export { name1, name2, ... } blocks (may span multiple lines)
const exportBlockRegex = /export\s*\{([^}]+)\}/g
let blockMatch
while ((blockMatch = exportBlockRegex.exec(content)) !== null) {
const names = blockMatch[1]
.split(',')
.map(n => {
// Handle "localName as exportedName" — take the exported name
const parts = n.trim().split(/\s+as\s+/)
return (parts[1] || parts[0]).trim()
})
.filter(n => n && /^[a-zA-Z_$][a-zA-Z0-9_$]*$/.test(n))

// require path: relative from common/js/ to the api dir's index (no .js extension)
const requireTarget = path.join(apiDir, 'index')
let requirePath = path.relative(commonJsDir, requireTarget).replace(/\\/g, '/')
if (!requirePath.startsWith('.')) requirePath = './' + requirePath

for (const name of names) {
if (seenNames.has(name)) {
console.warn(' [dup] skipping duplicate export:', name)
continue
}
seenNames.add(name)
apiEntries.push({ name, requirePath })
}
}
}

if (apiEntries.length === 0) {
console.error('No exported names found — aborting to avoid writing an empty file')
process.exit(1)
}

// Build dynamic.js content
const methodLines = apiEntries.map(({ name, requirePath }, i) => {
const comma = i < apiEntries.length - 1 ? ',' : ''
return [
` ${name} (...args) {`,
` const ${name} = require('${requirePath}').${name}`,
` return ${name}(...args)`,
` }${comma}`
].join('\n')
})

const output = [
'// This file is auto-generated by scripts/gen-dynamic.js — do not edit manually',
'export default {',
methodLines.join('\n'),
'}',
''
].join('\n')

fs.writeFileSync(outputPath, output, 'utf-8')
console.log(`Generated ${path.relative(process.cwd(), outputPath)} with ${apiEntries.length} methods`)
Loading