@hyper-fetch/plugin-devtools 10.2.0 → 10.3.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.
@@ -0,0 +1,52 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
3
+
4
+ const packageDir = path.join(__dirname, "../../..");
5
+ const srcDir = path.join(packageDir, "src");
6
+
7
+ // Read from disk rather than imported, so a package without `peerDependenciesMeta` still type checks.
8
+ const pkg: { peerDependenciesMeta?: Record<string, { optional?: boolean }> } = JSON.parse(
9
+ fs.readFileSync(path.join(packageDir, "package.json"), "utf8"),
10
+ );
11
+
12
+ const optionalPeers = Object.entries(pkg.peerDependenciesMeta ?? {})
13
+ .filter(([, meta]) => meta.optional)
14
+ .map(([name]) => name);
15
+
16
+ const listSourceFiles = (dir: string): string[] =>
17
+ fs.readdirSync(dir, { withFileTypes: true }).flatMap((entry) => {
18
+ const file = path.join(dir, entry.name);
19
+ if (entry.isDirectory()) return listSourceFiles(file);
20
+ return /\.tsx?$/.test(entry.name) ? [file] : [];
21
+ });
22
+
23
+ /** Names an `import` or `export ... from` statement brings in at runtime. Type-only names are left out. */
24
+ const getValueImports = (code: string, peer: string) => {
25
+ const statements = code.matchAll(
26
+ new RegExp(`(import|export)\\s+(type\\s+)?([^;]*?)\\s+from\\s+["']${peer}["']`, "g"),
27
+ );
28
+ return [...statements].flatMap(([, , typeOnly, clause]) => {
29
+ if (typeOnly) return [];
30
+ const named = clause.match(/\{([^}]*)\}/)?.[1];
31
+ if (!named) return [clause.trim()];
32
+ return named
33
+ .split(",")
34
+ .map((name) => name.trim())
35
+ .filter((name) => name && !name.startsWith("type "));
36
+ });
37
+ };
38
+
39
+ describe("Package [ Optional peers ]", () => {
40
+ it("should import optional peers only as types, so installs without them keep working", () => {
41
+ // npm does not install optional peers. A runtime import of one breaks every app that did not add it by hand.
42
+ const valueImports = listSourceFiles(srcDir).flatMap((file) =>
43
+ optionalPeers.flatMap((peer) =>
44
+ getValueImports(fs.readFileSync(file, "utf8"), peer).map(
45
+ (name) => `${path.relative(srcDir, file)}: ${name} from ${peer}`,
46
+ ),
47
+ ),
48
+ );
49
+
50
+ expect(valueImports).toStrictEqual([]);
51
+ });
52
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hyper-fetch/plugin-devtools",
3
- "version": "10.2.0",
3
+ "version": "10.3.0",
4
4
  "description": "Devtools plugin for HyperFetch: streams requests, cache and queues from your app to the HyperFlow desktop devtools",
5
5
  "author": "Maciej Pyrc <maciekpyrc@gmail.com>, Kacper Skawina <kacper.skawina@gmail.com>",
6
6
  "homepage": "https://hyperfetch.bettertyped.com/docs/integrations/plugin-devtools/",