@backtickjs/core 0.2.0 → 0.2.2

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
@@ -58,6 +58,56 @@ import { greet } from "my-native-module" with { backtick: "client" };
58
58
 
59
59
  The package must be installed where the server's file finds it, for its types and its version. The client must provide it, and the server must list it in the bundler's `modules` at that version or a later one of the same major. The compiler writes each name as what `createImport({ name, from, version })` makes, which a hand-written import can call too.
60
60
 
61
+ To draw for clients on two majors of a package, install both, one under an alias: `"acme-ui-2": "npm:acme-ui@2"`. An import from either is of `acme-ui`, the module clients provide, typed against its own version; `supports`, in a server component, picks the one the client can run:
62
+
63
+ ```ts
64
+ import { Button } from "acme-ui" with { backtick: "client" };
65
+ import { Button as Button2 } from "acme-ui-2" with { backtick: "client" };
66
+
67
+ function Save() {
68
+ return supports("acme-ui", "^2.0.0")
69
+ ? cs`<$Button2 tone="primary">Save</$Button2>`
70
+ : cs`<$Button variant="primary">Save</$Button>`;
71
+ }
72
+ ```
73
+
74
+ ## Every build in users' hands
75
+
76
+ A store build carries the modules it was built with, at their versions, for as long as it's installed, while your server moves on. Each build sends the modules it carries, and the server bundles its screens for exactly those: there's nothing to record before a build ships, and any version of your server can draw for any build.
77
+
78
+ 1. **The app's module map** offers what screens may require, each from a package in `package.json`'s dependencies. A platform that differs gets its own file, as Metro picks files: `client/moduleMap.web.ts` for the web.
79
+
80
+ 2. **The app's build records its installed packages.** `installedPackages`, from `@backtickjs/core/node`, reads the installed version of each of the app's dependencies from `node_modules`, and the build embeds them in the app. In an Expo app, the config plugin from [`@backtickjs/expo`](https://github.com/backtickjs/backtick/tree/main/packages/expo) does it:
81
+
82
+ ```json
83
+ // app.json
84
+ { "expo": { "plugins": ["@backtickjs/expo"] } }
85
+ ```
86
+
87
+ 3. **The app sends its modules** however it reaches its server: a header, a GraphQL variable. `modulesOf` gives each module in the map its package's version, and throws as the app starts for a module from a package that isn't a dependency:
88
+
89
+ ```ts
90
+ import { modulesOf } from "@backtickjs/core";
91
+ import { getBacktickExtra } from "@backtickjs/expo";
92
+
93
+ const { installedPackages } = getBacktickExtra(Constants.expoConfig);
94
+ const modules = JSON.stringify(modulesOf(moduleMap, installedPackages));
95
+ fetch(url, { headers: { "x-backtick-modules": modules } });
96
+ ```
97
+
98
+ 4. **The server bundles for them:**
99
+
100
+ ```ts
101
+ const modules = JSON.parse(context.req.header("x-backtick-modules") ?? "{}");
102
+ const bundle = await bundler.build({ input, modules });
103
+ ```
104
+
105
+ The bundler refuses modules that aren't `{ "specifier": "version" }`, and a screen that imports a module the build doesn't carry, or at a version it can't run. To draw differently for builds that lack a module, or to ask old ones to update, check with `supports` from [`@backtickjs/bundler`](https://github.com/backtickjs/backtick/tree/main/packages/bundler), in a server component: `supports("react-native", ">=0.86")`.
106
+
107
+ In development, a reload keeps the app config the app was opened with, and its versions with it: after upgrading a package, close the app and open it again.
108
+
109
+ A test draws a screen with a module map of its own, stand-ins included; its modules are that map's, at the versions the app installs: `modulesOf(moduleMap, installedPackages(projectRoot))`.
110
+
61
111
  ## API
62
112
 
63
113
  - `cs`: the tag for client scripts. Compiled at build time; throws if called uncompiled.
@@ -70,6 +120,8 @@ The package must be installed where the server's file finds it, for its types an
70
120
  - `Bundle<T>`: a bundle's code, branded with what it runs to. `bundler.build(…).generate(…)` writes one.
71
121
  - `execRoot(bundle, moduleMap)`, from `@backtickjs/core/cjs`: in a React Native app, runs the CommonJS bundle it starts from to its value, as a promise, with `require` answered from `moduleMap`, a `ModuleMap`: the modules the app provides, by specifier.
72
122
  - `execRoot(bundle)`, from `@backtickjs/core/es`: in a web page, imports the ES bundle it starts from and resolves to its value, the page's import map answering its imports. Each format has its own entry, so a React Native app's bundler never reads the ES one's `import()`.
123
+ - `modulesOf(moduleMap, installedPackages)`: the modules `moduleMap`, a `ModuleMap`, offers, each at its package's version in `installedPackages`, for the app to send its server.
124
+ - `installedPackages(projectRoot)`, from `@backtickjs/core/node`: the installed version of each of the app's dependencies, read from `node_modules` as Metro finds packages by default. A `metro.config.js` that aliases or redirects packages needs its own versions. Node only.
73
125
  - `exec`: in a script, `$exec(bundle)` runs another bundle as the script's own was run, with the same modules, to a promise of its value.
74
126
  - `isClientExec`, `ClientExec`: its check and type.
75
127
  - `SplicesAs<T>`: `T` as a splice checks it, member by member. Used by the bundler to check its input.
package/dist/cjs.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { Bundle } from "./Bundle.js";
2
- import type { ModuleMap } from "./ModuleMap.js";
3
- export type { ModuleMap };
2
+ import type { ModuleMap } from "./modulesOf.js";
4
3
  /**
5
4
  * Executes a CommonJS bundle an app starts from, the one nothing else runs,
6
5
  * to its value, with `require` answered from `moduleMap`: how a React Native
package/dist/cjs.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"cjs.js","sourceRoot":"","sources":["../src/cjs.ts"],"names":[],"mappings":"AAKA;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,MAAiB,EACjB,SAAoB;IAEpB,MAAM,OAAO,GAAG,CAAC,SAAiB,EAAW,EAAE;QAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,KAAK,CACb,wBAAwB,SAAS,oCAAoC,CACtE,CAAC;QACJ,CAAC;QACD,OAAO,SAAS,CAAC,SAAS,CAAC,CAAC;IAC9B,CAAC,CAAC;IACF,MAAM,MAAM,GAAG,EAAE,OAAO,EAAE,EAAa,EAAE,CAAC;IAC1C,IAAI,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,CAAC,CAClD,MAAM,EACN,MAAM,CAAC,OAAO,EACd,OAAO,CACR,CAAC;IACF,OAAO,MAAM,CAAC,OAAqB,CAAC;AACtC,CAAC"}
1
+ {"version":3,"file":"cjs.js","sourceRoot":"","sources":["../src/cjs.ts"],"names":[],"mappings":"AAGA;;;;;;;;;GASG;AACH,MAAM,CAAC,KAAK,UAAU,QAAQ,CAC5B,MAAiB,EACjB,SAAoB;IAEpB,MAAM,OAAO,GAAG,CAAC,SAAiB,EAAW,EAAE;QAC7C,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,EAAE,CAAC;YACzC,MAAM,IAAI,KAAK,CACb,wBAAwB,SAAS,oCAAoC,CACtE,CAAC;QACJ,CAAC;QACD,OAAO,SAAS,CAAC,SAAS,CAAC,CAAC;IAC9B,CAAC,CAAC;IACF,MAAM,MAAM,GAAG,EAAE,OAAO,EAAE,EAAa,EAAE,CAAC;IAC1C,IAAI,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,SAAS,EAAE,MAAM,CAAC,CAClD,MAAM,EACN,MAAM,CAAC,OAAO,EACd,OAAO,CACR,CAAC;IACF,OAAO,MAAM,CAAC,OAAqB,CAAC;AACtC,CAAC"}
package/dist/index.d.ts CHANGED
@@ -12,3 +12,4 @@ export type { Bundle } from "./Bundle.js";
12
12
  export { exec, isClientExec, type ClientExec } from "./exec.js";
13
13
  export { createJsxElement, isJsxElement, type JsxElement, type JsxElementOf, type JsxElementTypeOf, } from "./JsxElement.js";
14
14
  export { create, type ScriptModule, type ClientScript, isClientScript, type Param, } from "./ClientScript.js";
15
+ export { modulesOf, type ModuleMap } from "./modulesOf.js";
package/dist/index.js CHANGED
@@ -14,4 +14,7 @@ export { createJsxElement, isJsxElement, } from "./JsxElement.js";
14
14
  // What a template compiles to: the compiler writes one and the bundler reads
15
15
  // it, so the shape is neither end's.
16
16
  export { create, isClientScript, } from "./ClientScript.js";
17
+ // What an app provides bundles, and what it sends its server for them: the
18
+ // modules it offers, at its packages' versions.
19
+ export { modulesOf } from "./modulesOf.js";
17
20
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,4EAA4E;AAC5E,wCAAwC;AACxC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAKjC,OAAO,EACL,YAAY,EACZ,cAAc,GAEf,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAAE,IAAI,EAAE,YAAY,EAAmB,MAAM,WAAW,CAAC;AAEhE,0EAA0E;AAC1E,4BAA4B;AAC5B,OAAO,EACL,gBAAgB,EAChB,YAAY,GAIb,MAAM,iBAAiB,CAAC;AAEzB,6EAA6E;AAC7E,qCAAqC;AACrC,OAAO,EACL,MAAM,EAGN,cAAc,GAEf,MAAM,mBAAmB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,4EAA4E;AAC5E,wCAAwC;AACxC,OAAO,EAAE,EAAE,EAAE,MAAM,SAAS,CAAC;AAE7B,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAKjC,OAAO,EACL,YAAY,EACZ,cAAc,GAEf,MAAM,mBAAmB,CAAC;AAM3B,OAAO,EAAE,IAAI,EAAE,YAAY,EAAmB,MAAM,WAAW,CAAC;AAEhE,0EAA0E;AAC1E,4BAA4B;AAC5B,OAAO,EACL,gBAAgB,EAChB,YAAY,GAIb,MAAM,iBAAiB,CAAC;AAEzB,6EAA6E;AAC7E,qCAAqC;AACrC,OAAO,EACL,MAAM,EAGN,cAAc,GAEf,MAAM,mBAAmB,CAAC;AAE3B,2EAA2E;AAC3E,gDAAgD;AAChD,OAAO,EAAE,SAAS,EAAkB,MAAM,gBAAgB,CAAC"}
@@ -0,0 +1,27 @@
1
+ /**
2
+ * What a bundle may import, by specifier: each the module the app imported
3
+ * for it. What a web page's import map does, for an app, with modules for
4
+ * values instead of URLs:
5
+ *
6
+ * import * as React from "react";
7
+ * import * as JSXRuntime from "react/jsx-runtime";
8
+ * import * as ReactNative from "react-native";
9
+ *
10
+ * export const moduleMap = {
11
+ * react: React,
12
+ * "react/jsx-runtime": JSXRuntime,
13
+ * "react-native": ReactNative,
14
+ * };
15
+ */
16
+ export type ModuleMap = Readonly<Record<string, unknown>>;
17
+ /**
18
+ * The modules `moduleMap` offers, each at the version in `installedPackages`
19
+ * of the package it comes from: what the app sends however it reaches its
20
+ * server, a header, a GraphQL variable, for the server to bundle screens it
21
+ * can run. The app's build records them, with `installedPackages` from
22
+ * `@backtickjs/core/node`. Throws as the app starts for a module from a
23
+ * package that isn't a dependency.
24
+ *
25
+ * const modules = modulesOf(moduleMap, installedPackages);
26
+ */
27
+ export declare function modulesOf(moduleMap: ModuleMap, installedPackages: Readonly<Record<string, string>>): Record<string, string>;
@@ -0,0 +1,25 @@
1
+ // The package a module specifier is in: `react` for `react/jsx-runtime`.
2
+ function packageOf(specifier) {
3
+ const segments = specifier.split("/");
4
+ return segments.slice(0, specifier.startsWith("@") ? 2 : 1).join("/");
5
+ }
6
+ /**
7
+ * The modules `moduleMap` offers, each at the version in `installedPackages`
8
+ * of the package it comes from: what the app sends however it reaches its
9
+ * server, a header, a GraphQL variable, for the server to bundle screens it
10
+ * can run. The app's build records them, with `installedPackages` from
11
+ * `@backtickjs/core/node`. Throws as the app starts for a module from a
12
+ * package that isn't a dependency.
13
+ *
14
+ * const modules = modulesOf(moduleMap, installedPackages);
15
+ */
16
+ export function modulesOf(moduleMap, installedPackages) {
17
+ return Object.fromEntries(Object.keys(moduleMap).map((specifier) => {
18
+ const version = installedPackages[packageOf(specifier)];
19
+ if (version === undefined) {
20
+ throw new Error(`The module map offers ${JSON.stringify(specifier)}, but ${packageOf(specifier)} isn't in package.json's dependencies.`);
21
+ }
22
+ return [specifier, version];
23
+ }));
24
+ }
25
+ //# sourceMappingURL=modulesOf.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"modulesOf.js","sourceRoot":"","sources":["../src/modulesOf.ts"],"names":[],"mappings":"AAiBA,yEAAyE;AACzE,SAAS,SAAS,CAAC,SAAiB;IAClC,MAAM,QAAQ,GAAG,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACtC,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,SAAS,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACxE,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,SAAS,CACvB,SAAoB,EACpB,iBAAmD;IAEnD,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE;QACvC,MAAM,OAAO,GAAG,iBAAiB,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC,CAAC;QACxD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;YAC1B,MAAM,IAAI,KAAK,CACb,yBAAyB,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,SAAS,SAAS,CAAC,SAAS,CAAC,wCAAwC,CACxH,CAAC;QACJ,CAAC;QACD,OAAO,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAC9B,CAAC,CAAC,CACH,CAAC;AACJ,CAAC"}
package/dist/node.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ /**
2
+ * The installed version of each of the app's dependencies, at `projectRoot`:
3
+ * what a build made from it now carries there, for `modulesOf`. Node only:
4
+ * the app's build calls it, and embeds what it answers in the app.
5
+ * Packages are found as Metro finds them by default, in `node_modules` here
6
+ * or above: a `metro.config.js` that aliases or redirects them needs its own
7
+ * versions.
8
+ */
9
+ export declare function installedPackages(projectRoot: string): Record<string, string>;
package/dist/node.js ADDED
@@ -0,0 +1,30 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+ // Where a package is installed for the app at `projectRoot`, as Node finds
4
+ // it: in its node_modules, or the nearest above, as a workspace hoists it.
5
+ function installed(projectRoot, name) {
6
+ for (let directory = projectRoot;; directory = dirname(directory)) {
7
+ const file = join(directory, "node_modules", name, "package.json");
8
+ if (existsSync(file))
9
+ return file;
10
+ if (dirname(directory) === directory) {
11
+ throw new Error(`${projectRoot}: ${name} isn't installed`);
12
+ }
13
+ }
14
+ }
15
+ /**
16
+ * The installed version of each of the app's dependencies, at `projectRoot`:
17
+ * what a build made from it now carries there, for `modulesOf`. Node only:
18
+ * the app's build calls it, and embeds what it answers in the app.
19
+ * Packages are found as Metro finds them by default, in `node_modules` here
20
+ * or above: a `metro.config.js` that aliases or redirects them needs its own
21
+ * versions.
22
+ */
23
+ export function installedPackages(projectRoot) {
24
+ const { dependencies = {} } = JSON.parse(readFileSync(join(projectRoot, "package.json"), "utf8"));
25
+ return Object.fromEntries(Object.keys(dependencies).map((name) => [
26
+ name,
27
+ JSON.parse(readFileSync(installed(projectRoot, name), "utf8")).version,
28
+ ]));
29
+ }
30
+ //# sourceMappingURL=node.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"node.js","sourceRoot":"","sources":["../src/node.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AACnD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AAE1C,2EAA2E;AAC3E,2EAA2E;AAC3E,SAAS,SAAS,CAAC,WAAmB,EAAE,IAAY;IAClD,KAAK,IAAI,SAAS,GAAG,WAAW,GAAI,SAAS,GAAG,OAAO,CAAC,SAAS,CAAC,EAAE,CAAC;QACnE,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,EAAE,cAAc,EAAE,IAAI,EAAE,cAAc,CAAC,CAAC;QACnE,IAAI,UAAU,CAAC,IAAI,CAAC;YAAE,OAAO,IAAI,CAAC;QAClC,IAAI,OAAO,CAAC,SAAS,CAAC,KAAK,SAAS,EAAE,CAAC;YACrC,MAAM,IAAI,KAAK,CAAC,GAAG,WAAW,KAAK,IAAI,kBAAkB,CAAC,CAAC;QAC7D,CAAC;IACH,CAAC;AACH,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAAC,WAAmB;IACnD,MAAM,EAAE,YAAY,GAAG,EAAE,EAAE,GAAG,IAAI,CAAC,KAAK,CACtC,YAAY,CAAC,IAAI,CAAC,WAAW,EAAE,cAAc,CAAC,EAAE,MAAM,CAAC,CACX,CAAC;IAC/C,OAAO,MAAM,CAAC,WAAW,CACvB,MAAM,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC;QACtC,IAAI;QAEF,IAAI,CAAC,KAAK,CAAC,YAAY,CAAC,SAAS,CAAC,WAAW,EAAE,IAAI,CAAC,EAAE,MAAM,CAAC,CAG9D,CAAC,OAAO;KACV,CAAC,CACH,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@backtickjs/core",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "The cs tag for Backtick client scripts, and the types for the values that cross from server to client",
5
5
  "keywords": [
6
6
  "backtick",
@@ -36,6 +36,10 @@
36
36
  "./es": {
37
37
  "types": "./dist/es.d.ts",
38
38
  "default": "./dist/es.js"
39
+ },
40
+ "./node": {
41
+ "types": "./dist/node.d.ts",
42
+ "default": "./dist/node.js"
39
43
  }
40
44
  },
41
45
  "files": [
package/src/cjs.ts CHANGED
@@ -1,7 +1,5 @@
1
1
  import type { Bundle } from "./Bundle.js";
2
- import type { ModuleMap } from "./ModuleMap.js";
3
-
4
- export type { ModuleMap };
2
+ import type { ModuleMap } from "./modulesOf.js";
5
3
 
6
4
  /**
7
5
  * Executes a CommonJS bundle an app starts from, the one nothing else runs,
package/src/index.ts CHANGED
@@ -43,3 +43,7 @@ export {
43
43
  isClientScript,
44
44
  type Param,
45
45
  } from "./ClientScript.js";
46
+
47
+ // What an app provides bundles, and what it sends its server for them: the
48
+ // modules it offers, at its packages' versions.
49
+ export { modulesOf, type ModuleMap } from "./modulesOf.js";
@@ -0,0 +1,49 @@
1
+ /**
2
+ * What a bundle may import, by specifier: each the module the app imported
3
+ * for it. What a web page's import map does, for an app, with modules for
4
+ * values instead of URLs:
5
+ *
6
+ * import * as React from "react";
7
+ * import * as JSXRuntime from "react/jsx-runtime";
8
+ * import * as ReactNative from "react-native";
9
+ *
10
+ * export const moduleMap = {
11
+ * react: React,
12
+ * "react/jsx-runtime": JSXRuntime,
13
+ * "react-native": ReactNative,
14
+ * };
15
+ */
16
+ export type ModuleMap = Readonly<Record<string, unknown>>;
17
+
18
+ // The package a module specifier is in: `react` for `react/jsx-runtime`.
19
+ function packageOf(specifier: string): string {
20
+ const segments = specifier.split("/");
21
+ return segments.slice(0, specifier.startsWith("@") ? 2 : 1).join("/");
22
+ }
23
+
24
+ /**
25
+ * The modules `moduleMap` offers, each at the version in `installedPackages`
26
+ * of the package it comes from: what the app sends however it reaches its
27
+ * server, a header, a GraphQL variable, for the server to bundle screens it
28
+ * can run. The app's build records them, with `installedPackages` from
29
+ * `@backtickjs/core/node`. Throws as the app starts for a module from a
30
+ * package that isn't a dependency.
31
+ *
32
+ * const modules = modulesOf(moduleMap, installedPackages);
33
+ */
34
+ export function modulesOf(
35
+ moduleMap: ModuleMap,
36
+ installedPackages: Readonly<Record<string, string>>,
37
+ ): Record<string, string> {
38
+ return Object.fromEntries(
39
+ Object.keys(moduleMap).map((specifier) => {
40
+ const version = installedPackages[packageOf(specifier)];
41
+ if (version === undefined) {
42
+ throw new Error(
43
+ `The module map offers ${JSON.stringify(specifier)}, but ${packageOf(specifier)} isn't in package.json's dependencies.`,
44
+ );
45
+ }
46
+ return [specifier, version];
47
+ }),
48
+ );
49
+ }
package/src/node.ts ADDED
@@ -0,0 +1,38 @@
1
+ import { existsSync, readFileSync } from "node:fs";
2
+ import { dirname, join } from "node:path";
3
+
4
+ // Where a package is installed for the app at `projectRoot`, as Node finds
5
+ // it: in its node_modules, or the nearest above, as a workspace hoists it.
6
+ function installed(projectRoot: string, name: string): string {
7
+ for (let directory = projectRoot; ; directory = dirname(directory)) {
8
+ const file = join(directory, "node_modules", name, "package.json");
9
+ if (existsSync(file)) return file;
10
+ if (dirname(directory) === directory) {
11
+ throw new Error(`${projectRoot}: ${name} isn't installed`);
12
+ }
13
+ }
14
+ }
15
+
16
+ /**
17
+ * The installed version of each of the app's dependencies, at `projectRoot`:
18
+ * what a build made from it now carries there, for `modulesOf`. Node only:
19
+ * the app's build calls it, and embeds what it answers in the app.
20
+ * Packages are found as Metro finds them by default, in `node_modules` here
21
+ * or above: a `metro.config.js` that aliases or redirects them needs its own
22
+ * versions.
23
+ */
24
+ export function installedPackages(projectRoot: string): Record<string, string> {
25
+ const { dependencies = {} } = JSON.parse(
26
+ readFileSync(join(projectRoot, "package.json"), "utf8"),
27
+ ) as { dependencies?: Record<string, string> };
28
+ return Object.fromEntries(
29
+ Object.keys(dependencies).map((name) => [
30
+ name,
31
+ (
32
+ JSON.parse(readFileSync(installed(projectRoot, name), "utf8")) as {
33
+ version: string;
34
+ }
35
+ ).version,
36
+ ]),
37
+ );
38
+ }
@@ -1,16 +0,0 @@
1
- /**
2
- * What a bundle may import, by specifier: each the module the app imported
3
- * for it. What a web page's import map does, for an app, with modules for
4
- * values instead of URLs:
5
- *
6
- * import * as React from "react";
7
- * import * as JSXRuntime from "react/jsx-runtime";
8
- * import * as ReactNative from "react-native";
9
- *
10
- * export const moduleMap = {
11
- * react: React,
12
- * "react/jsx-runtime": JSXRuntime,
13
- * "react-native": ReactNative,
14
- * };
15
- */
16
- export type ModuleMap = Readonly<Record<string, unknown>>;
package/dist/ModuleMap.js DELETED
@@ -1,2 +0,0 @@
1
- export {};
2
- //# sourceMappingURL=ModuleMap.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"ModuleMap.js","sourceRoot":"","sources":["../src/ModuleMap.ts"],"names":[],"mappings":""}
package/src/ModuleMap.ts DELETED
@@ -1,16 +0,0 @@
1
- /**
2
- * What a bundle may import, by specifier: each the module the app imported
3
- * for it. What a web page's import map does, for an app, with modules for
4
- * values instead of URLs:
5
- *
6
- * import * as React from "react";
7
- * import * as JSXRuntime from "react/jsx-runtime";
8
- * import * as ReactNative from "react-native";
9
- *
10
- * export const moduleMap = {
11
- * react: React,
12
- * "react/jsx-runtime": JSXRuntime,
13
- * "react-native": ReactNative,
14
- * };
15
- */
16
- export type ModuleMap = Readonly<Record<string, unknown>>;