wxa-runner 0.10.0 → 0.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # wxa-runner
2
2
 
3
- 在普通浏览器里,用**微信官方基础库**运行小程序。为本地调试和 E2E 自动化测试而生。
3
+ 在普通浏览器里,用**微信官方基础库**运行小程序。用于本地调试和 E2E 自动化测试。
4
4
 
5
5
  ```bash
6
6
  npx wxa-runner dev ./dist/dev/mp-weixin # 原生小程序目录,或 uni-app / Taro 编译产物
package/bin/wxa-runner.js CHANGED
@@ -84,6 +84,120 @@ program
84
84
  }
85
85
  });
86
86
 
87
+ // 查看器:把小程序打成 .wxapack,不带源码在浏览器里运行。实现在 @open-wxa/wxa-viewer 里,执行时才加载:
88
+ // wxa-runner 不依赖它,只用 dev 的人不会因此多装 esbuild-wasm 等依赖
89
+ const viewer = program
90
+ .command('viewer')
91
+ .description('查看器:把小程序打成 .wxapack,不带源码在浏览器里运行(需要 @open-wxa/wxa-viewer)');
92
+
93
+ viewer
94
+ .command('pack <project-path>')
95
+ .description('把小程序编译成 .wxapack 包')
96
+ .option('-o, --out <file>', '输出文件(默认在当前目录按 appid 或项目目录名命名)')
97
+ .option('-b, --baselib <version>', '基础库版本(默认使用本机已安装的最新兼容版本)')
98
+ .option('--setup <file>', '打进包里的 setup 文件(默认使用项目目录下的 wxa.setup.ts / .js / .mjs)')
99
+ .option('--no-setup', '不打包 setup 文件')
100
+ .action(async (projectPath, options) => {
101
+ try {
102
+ const fs = require('fs');
103
+ const { createPack } = loadViewer();
104
+ const resolvedProjectPath = path.resolve(projectPath);
105
+ const { bytes, manifest, warnings } = await createPack({
106
+ projectPath: resolvedProjectPath,
107
+ baseLibVersion: options.baselib,
108
+ setup: options.setup,
109
+ logger: { warn() {} }
110
+ });
111
+ const configFile = path.join(resolvedProjectPath, 'project.config.json');
112
+ const config = fs.existsSync(configFile) ? JSON.parse(fs.readFileSync(configFile, 'utf8')) : {};
113
+ const output = path.resolve(options.out || `${config.appid || path.basename(resolvedProjectPath)}.wxapack`);
114
+ fs.writeFileSync(output, bytes);
115
+
116
+ console.log('');
117
+ console.log(chalk.green(' wxapack 已生成'));
118
+ console.log(` 包路径 ${chalk.cyan(output)}`);
119
+ console.log(` 大小 ${(bytes.byteLength / 1024).toFixed(1)} KB(${bytes.byteLength} 字节)`);
120
+ console.log(` 页面 ${manifest.build.pages.length}`);
121
+ console.log(` 基础库 ${manifest.baseLib.version}`);
122
+ warnings.forEach(warning => console.warn(chalk.yellow(` 警告 ${warning}`)));
123
+ console.log('');
124
+ } catch (error) {
125
+ console.error(chalk.red('\n打包失败:'), error.message);
126
+ process.exit(1);
127
+ }
128
+ });
129
+
130
+ viewer
131
+ .command('preview [packs...]')
132
+ .description('在本机打开查看器,打开时自动导入传入的包')
133
+ .option('-p, --port <port>', '端口号(默认 3000,被占用时换用后面空闲的端口)')
134
+ .option('--host [host]', '监听地址,不带值时监听所有地址,局域网也能访问', 'localhost')
135
+ .option('-b, --baselib <version>', '基础库版本(默认使用本机已安装的最新兼容版本)')
136
+ .option('-o, --open', '启动后打开浏览器')
137
+ .action(async (packs, options) => {
138
+ try {
139
+ const { ViewerServer } = loadViewer();
140
+ const server = new ViewerServer({
141
+ packs: packs.map(file => path.resolve(file)),
142
+ port: options.port === undefined ? undefined : parseInt(options.port, 10),
143
+ host: options.host === true ? '0.0.0.0' : options.host,
144
+ baseLibVersion: options.baselib
145
+ });
146
+ const url = await server.start();
147
+ console.log('');
148
+ console.log(chalk.green(' 查看器已启动'));
149
+ console.log(` 地址 ${chalk.cyan(url)}`);
150
+ const networkUrls = server.networkUrls;
151
+ if (!networkUrls.length) console.log(` 局域网 ${chalk.gray('加 --host 开放局域网访问')}`);
152
+ networkUrls.forEach((networkUrl, i) => console.log(` ${i === 0 ? '局域网' : ' '} ${chalk.cyan(networkUrl)}`));
153
+ console.log(` 基础库 ${server.baseLib.version}${server.baseLib.source === 'bundled' ? '(内置)' : ''}`);
154
+ console.log('');
155
+ if (options.open) openBrowser(url);
156
+ const shutdown = () => server.close().then(() => process.exit(0));
157
+ process.on('SIGINT', shutdown);
158
+ process.on('SIGTERM', shutdown);
159
+ } catch (error) {
160
+ console.error(chalk.red('\n启动失败:'), error.message);
161
+ process.exit(1);
162
+ }
163
+ });
164
+
165
+ viewer
166
+ .command('export <out-dir>')
167
+ .description('导出查看器:纯静态站点 site/,和转发 wx.request 的处理函数 proxy.mjs')
168
+ .option('-b, --baselib <version>', '基础库版本(默认使用本机已安装的最新兼容版本)')
169
+ .option('--force', '输出目录不为空时也写入,覆盖之前导出的文件')
170
+ .option('--pack <file>', '默认包,可以写多次:浏览器里还没有项目时自动导入', (file, packs) => packs.concat(file), [])
171
+ .action(async (outDir, options) => {
172
+ try {
173
+ const { exportViewer } = loadViewer();
174
+ const output = exportViewer(outDir, { baseLibVersion: options.baselib, force: options.force, packs: options.pack });
175
+ console.log('');
176
+ console.log(chalk.green(' 查看器已导出'));
177
+ console.log(` 静态站点 ${chalk.cyan(path.join(output, 'site'))}`);
178
+ options.pack.forEach((file, i) => console.log(` ${i === 0 ? '默认包' : ' '} ${path.basename(file)}`));
179
+ console.log(` 请求转发 ${chalk.cyan(path.join(output, 'proxy.mjs'))}`);
180
+ console.log(' 下一步 把 site/ 交给任意静态托管;要转发 wx.request 时,把 /__wxa__/proxy/ 开头的请求交给 proxy.mjs');
181
+ console.log('');
182
+ } catch (error) {
183
+ console.error(chalk.red('\n导出失败:'), error.message);
184
+ process.exit(1);
185
+ }
186
+ });
187
+
188
+ /** @throws 没装 @open-wxa/wxa-viewer 时,消息里说明怎么装 */
189
+ function loadViewer() {
190
+ try {
191
+ return require('@open-wxa/wxa-viewer');
192
+ } catch (error) {
193
+ // 只处理查看器本身找不到;它内部依赖缺失等别的错误照常抛出
194
+ if (error.code === 'MODULE_NOT_FOUND' && error.message.includes("'@open-wxa/wxa-viewer'")) {
195
+ throw new Error('查看器命令需要 @open-wxa/wxa-viewer,请先安装:npm i -D @open-wxa/wxa-viewer');
196
+ }
197
+ throw error;
198
+ }
199
+ }
200
+
87
201
  const baselib = program.command('baselib').description('管理官方基础库');
88
202
 
89
203
  baselib
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wxa-runner",
3
- "version": "0.10.0",
3
+ "version": "0.11.0",
4
4
  "description": "Run WeChat Mini Programs in the browser on the official base library, for debugging and E2E testing",
5
5
  "keywords": [
6
6
  "wechat",
@@ -36,8 +36,11 @@
36
36
  "@vercel/detect-agent": "^1.2.7",
37
37
  "chalk": "^4.1.2",
38
38
  "commander": "^11.1.0",
39
- "@open-wxa/wxa-runner-extractor": "0.10.0",
40
- "@open-wxa/wxa-runner-core": "0.10.0"
39
+ "@open-wxa/wxa-runner-core": "0.11.0",
40
+ "@open-wxa/wxa-runner-extractor": "0.11.0"
41
+ },
42
+ "devDependencies": {
43
+ "@open-wxa/wxa-viewer": "0.10.0"
41
44
  },
42
45
  "engines": {
43
46
  "node": ">=18"