transone-cli 0.5.0 → 0.6.1

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
@@ -87,6 +87,7 @@ export default defineConfig({
87
87
  directoryPages: false,
88
88
  },
89
89
  mp: {
90
+ host: 'https://api.example.com', // default request baseURL
90
91
  navigationBarTitleText: 'TransOne',
91
92
  lengthUnit: 'px', // or 'rpx' (values converted at 1px = 2rpx)
92
93
  // pages, window, tabBar, appExtra, pageExtra, globalData, publicDir ...
@@ -147,3 +148,29 @@ bun run lint
147
148
  ## License
148
149
 
149
150
  MIT
151
+
152
+ ### API host for mini-programs and native apps
153
+
154
+ Set `mp.host` or `app.host` in `transone.config.ts` (an absolute HTTP/HTTPS URL).
155
+ `mp['mp-weixin'].host` and `app['app-ios'].host` override their common host.
156
+ For mini-programs, `transone/request` uses this host as its default `baseURL`;
157
+ instance and per-request `baseURL` take priority, and absolute request URLs keep their own address.
158
+ With no host configured, request behavior remains unchanged.
159
+
160
+ ```ts
161
+ export default defineConfig({
162
+ mp: {
163
+ host: 'https://api.example.com',
164
+ 'mp-weixin': { host: 'https://wx-api.example.com' },
165
+ },
166
+ app: {
167
+ host: 'https://api.example.com',
168
+ 'app-ios': { host: 'https://ios-api.example.com' },
169
+ },
170
+ });
171
+ ```
172
+
173
+ Native app builds write `{ host }` to `transone.config.json` in the platform resource directory.
174
+ Native request compilation is not yet supported; this file is configuration for future integration.
175
+
176
+ Weixin builds default to `lazyCodeLoading: "requiredComponents"` in `app.json` (component injection on demand). Override it through `mp.appExtra.lazyCodeLoading` or the platform-specific `appExtra`.
@@ -7,6 +7,7 @@ import"./index-yb2wsc7f.js";
7
7
  // src/mp/build-mp.ts
8
8
  import { cp, mkdir, readdir, rm, writeFile } from "fs/promises";
9
9
  import { existsSync as existsSync2 } from "fs";
10
+ import { fileURLToPath } from "url";
10
11
  import { basename, dirname as dirname3, join as join3, relative, resolve as resolve2 } from "path";
11
12
 
12
13
  // src/mp/analyze.ts
@@ -2268,6 +2269,13 @@ function visit(context, classSource, node, state) {
2268
2269
  }
2269
2270
  }
2270
2271
  if (ts.isIdentifier(node) && !state.locals.has(node.text) && state.consts.has(node.text)) {
2272
+ const initializer = state.consts.get(node.text);
2273
+ if (ts.isCallExpression(initializer) && ts.isIdentifier(initializer.expression)) {
2274
+ const factoryName = initializer.expression.text;
2275
+ const isRequestFactory = classSource.source.statements.some((statement) => ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier) && statement.moduleSpecifier.text === "transone/request" && statement.importClause?.namedBindings && ts.isNamedImports(statement.importClause.namedBindings) && statement.importClause.namedBindings.elements.some((item) => item.name.text === factoryName && (item.propertyName?.text ?? item.name.text) === "createRequest"));
2276
+ if (isRequestFactory)
2277
+ return node;
2278
+ }
2271
2279
  const folded = foldExpression(ts, state.consts.get(node.text), state.consts);
2272
2280
  if (folded === undefined) {
2273
2281
  throw mpError(classSource.filePath, `\u65B9\u6CD5\u4E2D\u5F15\u7528\u4E86\u65E0\u6CD5\u9759\u6001\u6C42\u503C\u7684\u6A21\u5757\u5E38\u91CF "${node.text}"`);
@@ -2475,10 +2483,34 @@ function isComponentFile(context, filePath) {
2475
2483
  function compileInlineModule(context, source) {
2476
2484
  const { ts } = context;
2477
2485
  const kept = [];
2486
+ const requestImports = [];
2478
2487
  for (const statement of source.statements) {
2488
+ if (ts.isImportDeclaration(statement) && ts.isStringLiteral(statement.moduleSpecifier) && statement.moduleSpecifier.text === "transone/request" && !statement.importClause?.isTypeOnly) {
2489
+ const clause = statement.importClause;
2490
+ if (clause?.name)
2491
+ requestImports.push(`const ${clause.name.text} = getApp().__transoneRequest.default;`);
2492
+ const bindings = clause?.namedBindings;
2493
+ if (bindings && ts.isNamespaceImport(bindings)) {
2494
+ requestImports.push(`const ${bindings.name.text} = getApp().__transoneRequest;`);
2495
+ } else if (bindings && ts.isNamedImports(bindings)) {
2496
+ for (const item of bindings.elements) {
2497
+ if (!item.isTypeOnly)
2498
+ requestImports.push(`const ${item.name.text} = getApp().__transoneRequest[${JSON.stringify(item.propertyName?.text ?? item.name.text)}];`);
2499
+ }
2500
+ }
2501
+ }
2479
2502
  if (ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement) || ts.isExportAssignment(statement) || ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement)) {
2480
2503
  continue;
2481
2504
  }
2505
+ if (ts.isClassDeclaration(statement) && statement.heritageClauses?.some((clause) => clause.types.some((type) => ts.isIdentifier(type.expression) && (type.expression.text === "Component" || type.expression.text.endsWith("Component")))))
2506
+ continue;
2507
+ if (ts.isVariableStatement(statement)) {
2508
+ const declarations = statement.declarationList.declarations.filter((declaration) => !(declaration.initializer && ts.isCallExpression(declaration.initializer) && ts.isIdentifier(declaration.initializer.expression) && declaration.initializer.expression.text === "createApp"));
2509
+ if (declarations.length === 0)
2510
+ continue;
2511
+ kept.push(ts.factory.updateVariableStatement(statement, statement.modifiers, ts.factory.updateVariableDeclarationList(statement.declarationList, declarations)));
2512
+ continue;
2513
+ }
2482
2514
  kept.push(statement);
2483
2515
  }
2484
2516
  const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
@@ -2490,7 +2522,8 @@ function compileInlineModule(context, source) {
2490
2522
  },
2491
2523
  reportDiagnostics: false
2492
2524
  });
2493
- return transpiled.outputText.replace(/^export\s+/gm, "").trim();
2525
+ return [...requestImports, transpiled.outputText.replace(/^export\s+/gm, "").trim()].join(`
2526
+ `);
2494
2527
  }
2495
2528
 
2496
2529
  // src/mp/compile.ts
@@ -2702,11 +2735,24 @@ async function buildMiniProgram(config, dialect) {
2702
2735
  ...mp.window
2703
2736
  },
2704
2737
  ...mp.tabBar !== undefined ? { tabBar: mp.tabBar } : {},
2705
- ...mp.appExtra,
2706
- ...dialect.appJsonExtras()
2738
+ ...dialect.appJsonExtras(),
2739
+ ...mp.appExtra
2707
2740
  }, null, 2)}
2708
2741
  `);
2709
- await write("app.js", `App(${JSON.stringify(mp.globalData !== undefined ? { globalData: mp.globalData } : {})});
2742
+ const requestBundle = await Bun.build({
2743
+ entrypoints: [fileURLToPath(import.meta.resolve("transone/request"))],
2744
+ target: "browser",
2745
+ format: "cjs",
2746
+ minify: false
2747
+ });
2748
+ if (!requestBundle.success)
2749
+ throw new Error(`Mini program request runtime build failed: ${requestBundle.logs.join(`
2750
+ `)}`);
2751
+ await write("transone-request.js", await requestBundle.outputs[0].text());
2752
+ await write("app.js", `App({__transoneRequest: require('./transone-request.js'), ...${JSON.stringify({
2753
+ ...mp.host !== undefined ? { __transoneConfig: { host: mp.host } } : {},
2754
+ ...mp.globalData !== undefined ? { globalData: mp.globalData } : {}
2755
+ })}});
2710
2756
  `);
2711
2757
  if (dialect.sitemap) {
2712
2758
  await write("sitemap.json", `${JSON.stringify({
@@ -2796,5 +2842,5 @@ export {
2796
2842
  buildMiniProgram
2797
2843
  };
2798
2844
 
2799
- //# debugId=B563DD7B11FB4E5E64756E2164756E21
2800
- //# sourceMappingURL=build-mp-6j38wf4r.js.map
2845
+ //# debugId=AE3147F44337807364756E2164756E21
2846
+ //# sourceMappingURL=build-mp-vk5ap2cw.js.map