@assistant-ui/metro 0.0.8 → 0.0.11

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.
@@ -1 +1 @@
1
- {"version":3,"file":"transformer.d.cts","names":[],"sources":["../src/transformer.ts"],"mappings":";;KAeK;EACH,YAAY;IACV;IACA;IACA;MACM;QAA2B;;;KAEhC;;EAEH;GACC;;iBA2Ca,UACd,OAAO,WAAW;iBA0CJ"}
1
+ {"version":3,"file":"transformer.d.cts","names":[],"sources":["../src/transformer.ts"],"mappings":";;KAYK;EACH,YAAY;IACV;IACA;IACA;MACM;QAA2B;;;KAEhC;;EAEH;GACC;;iBA2Ca,UACd,OAAO,WAAW;iBA2CJ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assistant-ui/metro",
3
- "version": "0.0.8",
3
+ "version": "0.0.11",
4
4
  "description": "Metro / Expo integration for assistant-ui: the \"use generative\" directive compiler that colocates a tool's schema, server execute, and client render in one file. Works with Expo and bare React Native.",
5
5
  "keywords": [
6
6
  "assistant-ui",
@@ -37,13 +37,12 @@
37
37
  "@babel/types": "^8.0.4"
38
38
  },
39
39
  "devDependencies": {
40
- "@types/node": "^26.1.2",
41
- "tsdown": "^0.22.14",
40
+ "@assistant-ui/x-buildutils": "0.0.24",
41
+ "@assistant-ui/x-generative-compiler": "^0.0.15",
42
+ "@types/node": "^26.2.0",
42
43
  "typescript": "npm:@typescript/typescript6@^6.0.2",
43
44
  "typescript-7": "npm:typescript@^7.0.2",
44
- "vitest": "^4.1.10",
45
- "@assistant-ui/x-buildutils": "0.0.22",
46
- "@assistant-ui/x-generative-compiler": "^0.0.12"
45
+ "vitest": "^4.1.11"
47
46
  },
48
47
  "publishConfig": {
49
48
  "access": "public",
@@ -59,7 +58,7 @@
59
58
  "url": "https://github.com/assistant-ui/assistant-ui/issues"
60
59
  },
61
60
  "scripts": {
62
- "build": "tsdown",
61
+ "build": "aui-build",
63
62
  "test": "vitest run",
64
63
  "test:watch": "vitest"
65
64
  }
package/src/index.ts CHANGED
@@ -8,6 +8,8 @@ const require = createRequire(import.meta.url);
8
8
  * preserved and only `transformer.babelTransformerPath` is augmented.
9
9
  */
10
10
  export type MetroConfigLike = {
11
+ /** assistant-ui options, stripped from the config handed back to Metro. */
12
+ aui?: WithAuiOptions | undefined;
11
13
  transformer?:
12
14
  | {
13
15
  babelTransformerPath?: string | undefined;
@@ -24,6 +26,21 @@ export type MetroConfigLike = {
24
26
  */
25
27
  export const UPSTREAM_TRANSFORMER_ENV = "AUI_METRO_UPSTREAM_TRANSFORMER";
26
28
 
29
+ /**
30
+ * Env var carrying the `backendless` option to the transformer. Metro forks its
31
+ * transform workers after `metro.config` is evaluated, so they inherit it.
32
+ */
33
+ export const BACKENDLESS_ENV = "AUI_METRO_BACKENDLESS";
34
+
35
+ export interface WithAuiOptions {
36
+ /**
37
+ * The app has no backend importing the server builds of its `"use generative"`
38
+ * modules (e.g. cloud-hosted runs). Frontend/human tool schemas then stay
39
+ * uploadable from the client instead of being assumed backend-known.
40
+ */
41
+ backendless?: boolean;
42
+ }
43
+
27
44
  /**
28
45
  * Wraps a Metro (or Expo) config so assistant-ui `"use generative"` modules are
29
46
  * compiled. Such a file colocates a tool's schema, its `execute`, and its
@@ -40,7 +57,10 @@ export const UPSTREAM_TRANSFORMER_ENV = "AUI_METRO_UPSTREAM_TRANSFORMER";
40
57
  * const { getDefaultConfig } = require("expo/metro-config");
41
58
  * const { withAui } = require("@assistant-ui/metro");
42
59
  *
43
- * module.exports = withAui(getDefaultConfig(__dirname));
60
+ * module.exports = withAui({
61
+ * ...getDefaultConfig(__dirname),
62
+ * aui: { backendless: true },
63
+ * });
44
64
  * ```
45
65
  *
46
66
  * Authoring is identical to the web: a `"use generative"` file whose default
@@ -48,8 +68,13 @@ export const UPSTREAM_TRANSFORMER_ENV = "AUI_METRO_UPSTREAM_TRANSFORMER";
48
68
  */
49
69
  export function withAui<T extends MetroConfigLike>(config: T): T {
50
70
  const self = require.resolve("@assistant-ui/metro/transformer");
71
+ // Metro validates config keys it does not recognize, so `aui` must not
72
+ // survive into the returned config.
73
+ const { aui, ...baseConfig } = config;
51
74
  const upstream = config.transformer?.babelTransformerPath;
52
75
 
76
+ if (aui?.backendless) process.env[BACKENDLESS_ENV] = "1";
77
+
53
78
  // Guard against a double-wrap (`withAui(withAui(config))`, or a shared config
54
79
  // already wrapped): if it already points at our transformer, keep the real
55
80
  // upstream captured earlier rather than overwriting it with our own path,
@@ -59,10 +84,10 @@ export function withAui<T extends MetroConfigLike>(config: T): T {
59
84
  }
60
85
 
61
86
  return {
62
- ...config,
87
+ ...baseConfig,
63
88
  transformer: {
64
89
  ...config.transformer,
65
90
  babelTransformerPath: self,
66
91
  },
67
- };
92
+ } as T;
68
93
  }
@@ -3,15 +3,12 @@ import { statSync } from "node:fs";
3
3
  import { fileURLToPath } from "node:url";
4
4
  import {
5
5
  compileGenerative,
6
- isGenerativeModule,
6
+ isGenerativeSource,
7
7
  } from "@assistant-ui/x-generative-compiler";
8
- import { UPSTREAM_TRANSFORMER_ENV } from "./index";
8
+ import { BACKENDLESS_ENV, UPSTREAM_TRANSFORMER_ENV } from "./index";
9
9
 
10
10
  const require = createRequire(import.meta.url);
11
11
 
12
- /** Source modules a `"use generative"` directive can appear in. */
13
- const SOURCE_RE = /\.[cm]?[jt]sx?$/;
14
-
15
12
  /** Minimal shape of a Metro babel transformer. */
16
13
  type BabelTransformer = {
17
14
  transform: (props: {
@@ -72,7 +69,7 @@ export function transform(
72
69
  const upstream = upstreamTransformer();
73
70
  const { filename, src, options } = props;
74
71
 
75
- if (SOURCE_RE.test(filename) && isGenerativeModule(src)) {
72
+ if (isGenerativeSource(filename, src)) {
76
73
  const target = isServerEnvironment(
77
74
  options?.customTransformOptions?.environment,
78
75
  )
@@ -86,6 +83,7 @@ export function transform(
86
83
  // resolve; the environment split already keeps a server `execute` out of
87
84
  // the client bundle.
88
85
  injectServerOnly: false,
86
+ backendless: process.env[BACKENDLESS_ENV] === "1",
89
87
  });
90
88
 
91
89
  return upstream.transform({ ...props, src: code });
@@ -112,5 +110,7 @@ function selfCacheToken(): string {
112
110
  export function getCacheKey(): string {
113
111
  const upstream = upstreamTransformer();
114
112
  const base = upstream.getCacheKey ? upstream.getCacheKey() : "";
115
- return `${base}$aui-metro:${selfCacheToken()}`;
113
+ const backendless =
114
+ process.env[BACKENDLESS_ENV] === "1" ? ":backendless" : "";
115
+ return `${base}$aui-metro:${selfCacheToken()}${backendless}`;
116
116
  }