@open-wxa/wxa-runner-core 0.8.1 → 0.9.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/dist/index.d.mts CHANGED
@@ -1,3 +1,4 @@
1
+ import { IncomingMessage, ServerResponse } from 'http';
1
2
  import { ResolvedBaseLib } from '@open-wxa/wxa-runner-extractor';
2
3
  import { D as DeviceProfile, F as ForwardConsole } from './setup-Bac8XdhR.mjs';
3
4
  export { A as ApiContext, a as ApiHandler, b as ApiResult, c as AppInfo, M as MockConfig, d as MockRequest, e as MockResponse, f as MockValue, N as NetworkConfig, R as RouteHandler, S as SetupApi, g as SetupConfig, h as SetupContext, i as SetupDefinition } from './setup-Bac8XdhR.mjs';
@@ -124,6 +125,10 @@ interface BuildResult {
124
125
  components: string[];
125
126
  durationMs: number;
126
127
  }
128
+ interface AppBuilderOptions {
129
+ /** wxml 交给 wcc 前的转换,见 DevServerOptions.wxmlTransform */
130
+ wxmlTransform?: (code: string, file: string) => string;
131
+ }
127
132
  declare class AppBuilder {
128
133
  readonly project: MiniProgram;
129
134
  private version;
@@ -134,7 +139,13 @@ declare class AppBuilder {
134
139
  private dirty;
135
140
  private wxmlCache;
136
141
  private wxssCache;
137
- constructor(project: MiniProgram);
142
+ /** 设置了 wxmlTransform 时才有 */
143
+ private readonly wxmlMirror;
144
+ constructor(project: MiniProgram, options?: AppBuilderOptions);
145
+ /** wxmlTransform 的镜像目录;没有设置转换或还没编译过时为 null */
146
+ get wxmlMirrorDir(): string | null;
147
+ /** 删除 wxmlTransform 的镜像目录,开发服务器关闭时调用 */
148
+ dispose(): void;
138
149
  /** 根据变化的文件标记需要重新构建的部分;返回 true 表示需要刷新 */
139
150
  invalidate(changedFiles?: string[]): boolean;
140
151
  /**
@@ -158,6 +169,41 @@ declare class AppBuilder {
158
169
  private createServiceBootstrap;
159
170
  }
160
171
 
172
+ /**
173
+ * 开发服务器扩展:检查器这类开发工具把要用的扩展点打包成一个扩展,
174
+ * 由 DevServer 的 extensions 选项或命令行的 --extension 加载。runner 只认这个约定,不认识具体的工具。
175
+ *
176
+ * (「插件」在 runner 里指小程序插件,见 plugins.ts,所以这里叫扩展)
177
+ */
178
+
179
+ interface DevServerExtension {
180
+ /** 出现在报错和启动信息里 */
181
+ name: string;
182
+ /** 开发服务器构造时调用一次 */
183
+ setup(context: ExtensionContext): ExtensionSetup;
184
+ }
185
+ interface ExtensionContext {
186
+ /** 项目目录(DevServer 的 projectPath,绝对路径) */
187
+ projectPath: string;
188
+ /** 小程序代码根目录,已考虑 project.config.json 的 miniprogramRoot */
189
+ root: string;
190
+ }
191
+ interface ExtensionSetup {
192
+ webviewScripts?: WebviewScript[];
193
+ middlewares?: DevServerMiddleware[];
194
+ wxmlTransform?: (code: string, file: string) => string;
195
+ /** 扩展自己的页面:启动信息里打印,命令行 --open 时打开 */
196
+ entry?: {
197
+ label: string;
198
+ path: string;
199
+ };
200
+ }
201
+ interface ExtensionEntry {
202
+ name: string;
203
+ label: string;
204
+ path: string;
205
+ }
206
+
161
207
  /**
162
208
  * 开发服务器
163
209
  *
@@ -179,9 +225,31 @@ declare class AppBuilder {
179
225
  * /__wxa__/region/<kind> 地区选择器用的行政区划数据(districts / streets),取自本机开发者工具
180
226
  * /__wxa__/api/* --devtools-login:wx.login 的 code、手机号,服务市场、插件的请求转发,只允许本机访问
181
227
  * /__wxa__/ws 文件变化通知:热更新(hot)或整页刷新(reload);开启 forwardConsole 时浏览器从这里发来报错
228
+ * middlewares / 扩展挂载的路径 排在 runner 自己的路由之前
182
229
  * 其余路径 小程序目录下的静态资源
183
230
  */
184
231
 
232
+ /** 注入每个渲染层页面的脚本,src 与 content 二选一 */
233
+ type WebviewScript = ({
234
+ src: string;
235
+ content?: never;
236
+ } | {
237
+ content: string;
238
+ src?: never;
239
+ }) & {
240
+ /**
241
+ * 'head':在基础库(WAWebview.js)之前执行,用于要赶在基础库之前注册事件监听的脚本,执行时还没有 document.body;
242
+ * 'body'(默认):在 <body> 开头、页面开始渲染之前执行。不能放在 <body> 末尾:基础库渲染时会换掉 body,
243
+ * 之后才解析到的脚本落在脱离文档的旧 body 里,不会执行
244
+ */
245
+ position?: 'head' | 'body';
246
+ };
247
+ /** 挂在开发服务器上的中间件:按前缀匹配,handler 收到的 req.url 已去掉 path 这段前缀 */
248
+ interface DevServerMiddleware {
249
+ /** 以 / 开头的字面量路径前缀,如 /__tool__;不能是 /,不能含通配符和参数,也不能落在 runner 保留的前缀下(见 RESERVED_PREFIXES) */
250
+ path: string;
251
+ handler: (req: IncomingMessage, res: ServerResponse, next: (error?: unknown) => void) => void;
252
+ }
185
253
  interface DevServerOptions {
186
254
  projectPath: string;
187
255
  /** 不传时从 3000 起找空闲的端口;指定的端口被占用时启动失败;传 0 使用随机端口 */
@@ -212,6 +280,17 @@ interface DevServerOptions {
212
280
  * 默认不转发;命令行默认 'errors',检测到编程助手时 'all'
213
281
  */
214
282
  forwardConsole?: ForwardConsole;
283
+ /** 注入每个渲染层页面(含插件页面、跳转后新建的页面)的脚本,供检查器等开发工具使用 */
284
+ webviewScripts?: WebviewScript[];
285
+ /** 挂在开发服务器上的中间件,供开发工具提供自己的页面和接口 */
286
+ middlewares?: DevServerMiddleware[];
287
+ /**
288
+ * wxml 交给 wcc 编译前的转换,file 是相对小程序根目录的路径(如 pages/index/index.wxml)。
289
+ * 不能改变行数,编译错误和热更新才能对应到源文件;相同输入要返回相同输出。源文件不会被修改
290
+ */
291
+ wxmlTransform?: (code: string, file: string) => string;
292
+ /** 开发服务器扩展:把上面几个扩展点打包在一起的开发工具,按顺序合并在直接给出的选项之后,见 extensions.ts */
293
+ extensions?: DevServerExtension[];
215
294
  logger?: Pick<Console, 'log' | 'warn' | 'error'>;
216
295
  }
217
296
  declare const DEFAULT_DEVICE: DeviceProfile;
@@ -222,6 +301,8 @@ declare class DevServer {
222
301
  readonly baseLib: ResolvedBaseLib;
223
302
  /** 生效的 setup 文件绝对路径,没有时为 null */
224
303
  readonly setupFile: string | null;
304
+ /** 扩展提供的页面,按扩展的顺序 */
305
+ readonly extensionEntries: ExtensionEntry[];
225
306
  private readonly setupEnv;
226
307
  private readonly device;
227
308
  private readonly logger;
@@ -237,6 +318,9 @@ declare class DevServer {
237
318
  /** 开启 --devtools-login 时才有 */
238
319
  private readonly ticketSource;
239
320
  private readonly plugins;
321
+ /** 选项里直接给出的扩展点与各个扩展合并后的结果 */
322
+ private readonly webviewScripts;
323
+ private readonly middlewares;
240
324
  constructor(options: DevServerOptions);
241
325
  /** 本机访问的地址 */
242
326
  get url(): string;
@@ -271,4 +355,4 @@ interface WxConfigInput {
271
355
  }
272
356
  declare function createWxConfig(input: WxConfigInput): Record<string, any>;
273
357
 
274
- export { AppBuilder, type AppJson, type BuildResult, DEFAULT_DEVICE, DevServer, type DevServerOptions, DeviceProfile, ForwardConsole, MiniProgram, type UnitJson, createWxConfig };
358
+ export { AppBuilder, type AppBuilderOptions, type AppJson, type BuildResult, DEFAULT_DEVICE, DevServer, type DevServerExtension, type DevServerMiddleware, type DevServerOptions, DeviceProfile, type ExtensionContext, type ExtensionEntry, type ExtensionSetup, ForwardConsole, MiniProgram, type UnitJson, type WebviewScript, createWxConfig };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { IncomingMessage, ServerResponse } from 'http';
1
2
  import { ResolvedBaseLib } from '@open-wxa/wxa-runner-extractor';
2
3
  import { D as DeviceProfile, F as ForwardConsole } from './setup-Bac8XdhR.js';
3
4
  export { A as ApiContext, a as ApiHandler, b as ApiResult, c as AppInfo, M as MockConfig, d as MockRequest, e as MockResponse, f as MockValue, N as NetworkConfig, R as RouteHandler, S as SetupApi, g as SetupConfig, h as SetupContext, i as SetupDefinition } from './setup-Bac8XdhR.js';
@@ -124,6 +125,10 @@ interface BuildResult {
124
125
  components: string[];
125
126
  durationMs: number;
126
127
  }
128
+ interface AppBuilderOptions {
129
+ /** wxml 交给 wcc 前的转换,见 DevServerOptions.wxmlTransform */
130
+ wxmlTransform?: (code: string, file: string) => string;
131
+ }
127
132
  declare class AppBuilder {
128
133
  readonly project: MiniProgram;
129
134
  private version;
@@ -134,7 +139,13 @@ declare class AppBuilder {
134
139
  private dirty;
135
140
  private wxmlCache;
136
141
  private wxssCache;
137
- constructor(project: MiniProgram);
142
+ /** 设置了 wxmlTransform 时才有 */
143
+ private readonly wxmlMirror;
144
+ constructor(project: MiniProgram, options?: AppBuilderOptions);
145
+ /** wxmlTransform 的镜像目录;没有设置转换或还没编译过时为 null */
146
+ get wxmlMirrorDir(): string | null;
147
+ /** 删除 wxmlTransform 的镜像目录,开发服务器关闭时调用 */
148
+ dispose(): void;
138
149
  /** 根据变化的文件标记需要重新构建的部分;返回 true 表示需要刷新 */
139
150
  invalidate(changedFiles?: string[]): boolean;
140
151
  /**
@@ -158,6 +169,41 @@ declare class AppBuilder {
158
169
  private createServiceBootstrap;
159
170
  }
160
171
 
172
+ /**
173
+ * 开发服务器扩展:检查器这类开发工具把要用的扩展点打包成一个扩展,
174
+ * 由 DevServer 的 extensions 选项或命令行的 --extension 加载。runner 只认这个约定,不认识具体的工具。
175
+ *
176
+ * (「插件」在 runner 里指小程序插件,见 plugins.ts,所以这里叫扩展)
177
+ */
178
+
179
+ interface DevServerExtension {
180
+ /** 出现在报错和启动信息里 */
181
+ name: string;
182
+ /** 开发服务器构造时调用一次 */
183
+ setup(context: ExtensionContext): ExtensionSetup;
184
+ }
185
+ interface ExtensionContext {
186
+ /** 项目目录(DevServer 的 projectPath,绝对路径) */
187
+ projectPath: string;
188
+ /** 小程序代码根目录,已考虑 project.config.json 的 miniprogramRoot */
189
+ root: string;
190
+ }
191
+ interface ExtensionSetup {
192
+ webviewScripts?: WebviewScript[];
193
+ middlewares?: DevServerMiddleware[];
194
+ wxmlTransform?: (code: string, file: string) => string;
195
+ /** 扩展自己的页面:启动信息里打印,命令行 --open 时打开 */
196
+ entry?: {
197
+ label: string;
198
+ path: string;
199
+ };
200
+ }
201
+ interface ExtensionEntry {
202
+ name: string;
203
+ label: string;
204
+ path: string;
205
+ }
206
+
161
207
  /**
162
208
  * 开发服务器
163
209
  *
@@ -179,9 +225,31 @@ declare class AppBuilder {
179
225
  * /__wxa__/region/<kind> 地区选择器用的行政区划数据(districts / streets),取自本机开发者工具
180
226
  * /__wxa__/api/* --devtools-login:wx.login 的 code、手机号,服务市场、插件的请求转发,只允许本机访问
181
227
  * /__wxa__/ws 文件变化通知:热更新(hot)或整页刷新(reload);开启 forwardConsole 时浏览器从这里发来报错
228
+ * middlewares / 扩展挂载的路径 排在 runner 自己的路由之前
182
229
  * 其余路径 小程序目录下的静态资源
183
230
  */
184
231
 
232
+ /** 注入每个渲染层页面的脚本,src 与 content 二选一 */
233
+ type WebviewScript = ({
234
+ src: string;
235
+ content?: never;
236
+ } | {
237
+ content: string;
238
+ src?: never;
239
+ }) & {
240
+ /**
241
+ * 'head':在基础库(WAWebview.js)之前执行,用于要赶在基础库之前注册事件监听的脚本,执行时还没有 document.body;
242
+ * 'body'(默认):在 <body> 开头、页面开始渲染之前执行。不能放在 <body> 末尾:基础库渲染时会换掉 body,
243
+ * 之后才解析到的脚本落在脱离文档的旧 body 里,不会执行
244
+ */
245
+ position?: 'head' | 'body';
246
+ };
247
+ /** 挂在开发服务器上的中间件:按前缀匹配,handler 收到的 req.url 已去掉 path 这段前缀 */
248
+ interface DevServerMiddleware {
249
+ /** 以 / 开头的字面量路径前缀,如 /__tool__;不能是 /,不能含通配符和参数,也不能落在 runner 保留的前缀下(见 RESERVED_PREFIXES) */
250
+ path: string;
251
+ handler: (req: IncomingMessage, res: ServerResponse, next: (error?: unknown) => void) => void;
252
+ }
185
253
  interface DevServerOptions {
186
254
  projectPath: string;
187
255
  /** 不传时从 3000 起找空闲的端口;指定的端口被占用时启动失败;传 0 使用随机端口 */
@@ -212,6 +280,17 @@ interface DevServerOptions {
212
280
  * 默认不转发;命令行默认 'errors',检测到编程助手时 'all'
213
281
  */
214
282
  forwardConsole?: ForwardConsole;
283
+ /** 注入每个渲染层页面(含插件页面、跳转后新建的页面)的脚本,供检查器等开发工具使用 */
284
+ webviewScripts?: WebviewScript[];
285
+ /** 挂在开发服务器上的中间件,供开发工具提供自己的页面和接口 */
286
+ middlewares?: DevServerMiddleware[];
287
+ /**
288
+ * wxml 交给 wcc 编译前的转换,file 是相对小程序根目录的路径(如 pages/index/index.wxml)。
289
+ * 不能改变行数,编译错误和热更新才能对应到源文件;相同输入要返回相同输出。源文件不会被修改
290
+ */
291
+ wxmlTransform?: (code: string, file: string) => string;
292
+ /** 开发服务器扩展:把上面几个扩展点打包在一起的开发工具,按顺序合并在直接给出的选项之后,见 extensions.ts */
293
+ extensions?: DevServerExtension[];
215
294
  logger?: Pick<Console, 'log' | 'warn' | 'error'>;
216
295
  }
217
296
  declare const DEFAULT_DEVICE: DeviceProfile;
@@ -222,6 +301,8 @@ declare class DevServer {
222
301
  readonly baseLib: ResolvedBaseLib;
223
302
  /** 生效的 setup 文件绝对路径,没有时为 null */
224
303
  readonly setupFile: string | null;
304
+ /** 扩展提供的页面,按扩展的顺序 */
305
+ readonly extensionEntries: ExtensionEntry[];
225
306
  private readonly setupEnv;
226
307
  private readonly device;
227
308
  private readonly logger;
@@ -237,6 +318,9 @@ declare class DevServer {
237
318
  /** 开启 --devtools-login 时才有 */
238
319
  private readonly ticketSource;
239
320
  private readonly plugins;
321
+ /** 选项里直接给出的扩展点与各个扩展合并后的结果 */
322
+ private readonly webviewScripts;
323
+ private readonly middlewares;
240
324
  constructor(options: DevServerOptions);
241
325
  /** 本机访问的地址 */
242
326
  get url(): string;
@@ -271,4 +355,4 @@ interface WxConfigInput {
271
355
  }
272
356
  declare function createWxConfig(input: WxConfigInput): Record<string, any>;
273
357
 
274
- export { AppBuilder, type AppJson, type BuildResult, DEFAULT_DEVICE, DevServer, type DevServerOptions, DeviceProfile, ForwardConsole, MiniProgram, type UnitJson, createWxConfig };
358
+ export { AppBuilder, type AppBuilderOptions, type AppJson, type BuildResult, DEFAULT_DEVICE, DevServer, type DevServerExtension, type DevServerMiddleware, type DevServerOptions, DeviceProfile, type ExtensionContext, type ExtensionEntry, type ExtensionSetup, ForwardConsole, MiniProgram, type UnitJson, type WebviewScript, createWxConfig };