@multiplatform.one/config 10.0.0 → 10.1.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.
@@ -1,59 +1,10 @@
1
- import fs from "node:fs";
2
1
  import path from "node:path";
3
2
  import { describe, expect, it } from "vitest";
4
- import { movedDepAliases } from "./movedDepAliases";
5
3
  import { createStorybookViteConfig } from "./storybook";
6
4
  import { createViteConfig } from "./vite";
7
5
 
8
6
  const workspaceRoot = path.resolve(__dirname, "../../..");
9
7
 
10
- describe("movedDepAliases", () => {
11
- it("points react-native-svg's dead registry import at react-native-web's (FZ-10849)", () => {
12
- // react-native-svg 15.16.0 and 15.16.1 both ship
13
- // lib/module/lib/resolveAssetUri.web.js importing getAssetByID from
14
- // @react-native/assets-registry/registry. React Native 0.88 renamed that
15
- // package, so the specifier resolves nowhere and `storybook build` dies
16
- // with "Rolldown failed to resolve import". Upstream fixed only the native
17
- // twin, whose own comment says react-native no longer ships it.
18
- const alias = movedDepAliases(workspaceRoot)["@react-native/assets-registry/registry"];
19
- expect(alias, "the moved registry must be aliased").toBeTruthy();
20
- expect(path.isAbsolute(alias)).toBe(true);
21
- expect(fs.existsSync(alias)).toBe(true);
22
- });
23
-
24
- it("aims at the web registry, which is the one web assets are registered in", () => {
25
- // Both react-native-web's and React Native's registries expose
26
- // registerAsset and getAssetByID, but on web it is react-native-web's
27
- // Image that registers, and it keeps the store on a
28
- // Symbol.for("react-native-web.AssetRegistry") global so its CJS and ESM
29
- // copies share one array. RN's registry would be a second, empty one and
30
- // every numeric source would throw "asset with ID could not be found".
31
- const alias = movedDepAliases(workspaceRoot)["@react-native/assets-registry/registry"];
32
- expect(alias).toContain(path.join("react-native-web", "dist", "modules", "AssetRegistry"));
33
- const source = fs.readFileSync(alias, "utf8");
34
- expect(source).toContain("export function getAssetByID");
35
- expect(source).toContain("react-native-web.AssetRegistry");
36
- });
37
-
38
- it("drops the entry while the package it names is still resolvable", () => {
39
- // The alias is measured, not asserted: a tree that carries
40
- // @react-native/assets-registry needs no alias, and shadowing a real
41
- // package would be worse than the resolve error.
42
- expect(() =>
43
- require.resolve("@react-native/assets-registry/package.json", { paths: [workspaceRoot] }),
44
- ).toThrow();
45
- });
46
-
47
- it("omits everything when nothing is installed", () => {
48
- expect(movedDepAliases(path.join(workspaceRoot, "public", "config"))).toEqual(
49
- expect.objectContaining({}),
50
- );
51
- for (const target of Object.values(movedDepAliases(path.join(workspaceRoot, "public")))) {
52
- expect(fs.existsSync(target)).toBe(true);
53
- }
54
- });
55
- });
56
-
57
8
  describe("the factories that build a web bundle carry it", () => {
58
9
  it("createStorybookViteConfig aliases it, which is where the build failed", () => {
59
10
  const alias = (createStorybookViteConfig({ workspaceRoot }).resolve?.alias ?? {}) as Record<
package/src/storybook.ts CHANGED
@@ -1,9 +1,12 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
+ import {
4
+ movedDepAliases,
5
+ nativeOnlyDepStubOptimizerPlugin,
6
+ nativeOnlyDepStubPlugin,
7
+ } from "@multiplatform.one/utils/dev";
3
8
  import type { Plugin, UserConfig } from "vite";
4
9
  import { phosphorTokenColorPlugin } from "./phosphorTokenColor.js";
5
- import { movedDepAliases } from "./movedDepAliases.js";
6
- import { nativeOnlyDepStubOptimizerPlugin, nativeOnlyDepStubPlugin } from "./nativeOnlyDepStubs.js";
7
10
  import { singletonResolvePlugin, withSingletonDepAliases } from "./singletonDepAliases.js";
8
11
  import { unexportedDepAliases } from "./unexportedDepAliases.js";
9
12
  import { discoverPublicPackageRoots, resolvePackageMainSource } from "./workspacePublicPackages.js";
package/src/vite.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  import fs from "node:fs";
2
2
  import path from "node:path";
3
- import { lookupTamaguiModules } from "@multiplatform.one/utils/dev";
3
+ import { lookupTamaguiModules, movedDepAliases } from "@multiplatform.one/utils/dev";
4
4
  import {
5
5
  discoverPublicPackageRoots,
6
6
  resolveWorkspaceNativeEntry,
7
7
  } from "./workspacePublicPackages.js";
8
8
  import { nativeBlockScopingPlugin } from "./nativeBlockScoping.js";
9
9
  import { phosphorTokenColorPlugin } from "./phosphorTokenColor.js";
10
- import { movedDepAliases } from "./movedDepAliases.js";
11
10
  import { singletonResolvePlugin, withSingletonDepAliases } from "./singletonDepAliases.js";
12
11
  import { ensureTamaguiWorkspacePaths } from "./tamaguiWorkspacePaths.js";
13
12
  import {
@@ -1 +1 @@
1
- {"version":3,"file":"storybook.d.ts","sourceRoot":"","sources":["../src/storybook.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAU,UAAU,EAAE,MAAM,MAAM,CAAC;AAS/C,MAAM,WAAW,gCAAgC;IAC/C;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC;;OAEG;IACH,OAAO,CAAC,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC;IAEhC;;OAEG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;CAC/B;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,yBAAyB,CACvC,OAAO,GAAE,gCAAqC,GAC7C,UAAU,CA8LZ"}
1
+ {"version":3,"file":"storybook.d.ts","sourceRoot":"","sources":["../src/storybook.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAU,UAAU,EAAE,MAAM,MAAM,CAAC;AAO/C,MAAM,WAAW,gCAAgC;IAC/C;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC;;OAEG;IACH,OAAO,CAAC,EAAE,UAAU,CAAC,SAAS,CAAC,CAAC;IAEhC;;OAEG;IACH,MAAM,CAAC,EAAE,UAAU,CAAC,QAAQ,CAAC,CAAC;CAC/B;AAED;;;;;;;;;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BAA8B,CAAC;AACtC,OAAO,EACL,kBAAkB,EAClB,oBAAoB,EACpB,KAAK,yBAAyB,GAC/B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,wBAAwB,EAAE,MAAM,yBAAyB,CAAC;AAuFnE;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAA;CAAE,EAAE,MAAM,EAAE,CAAC,GAAG,MAAM,CAE9E;AA6cD,eAAO,MAAM,wBAAwB,mCAAmC,CAAC;AACzE,+FAA+F;AAC/F,eAAO,MAAM,uBAAuB,kCAAkC,CAAC;AAEvE;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,WAAW,EAAE,MAAM,GAClB,CAAC,CAAC,EAAE,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM,KAAK,MAAM,GAAG,IAAI,CAAC,GAAG,IAAI,CAiB3D"}
@@ -1,91 +0,0 @@
1
- import fs from "node:fs";
2
- import { createRequire } from "node:module";
3
- import path from "node:path";
4
-
5
- /**
6
- * Modules a publisher still imports from a package that no longer exists.
7
- *
8
- * Different from `unexportedDepAliases`, which maps a subpath of a package
9
- * that IS installed and simply never declared it. Here the whole package is
10
- * gone from the tree, so there is nothing to deep-import: the build fails on
11
- * resolution and no exports map or `--frozen-lockfile` install can help.
12
- *
13
- * FZ-10849. `react-native-svg` 15.16.0 and 15.16.1 both ship
14
- * `lib/module/lib/resolveAssetUri.web.js` importing `getAssetByID` from
15
- * `@react-native/assets-registry/registry`. React Native 0.88 renamed that
16
- * package: the tree carries `@react-native/asset-utils` and RN re-exports the
17
- * registry's two functions from its own `react-native/asset-registry`
18
- * subpath. The old name resolves nowhere, and `storybook build` dies with
19
- * `[vite]: Rolldown failed to resolve import "@react-native/assets-registry/
20
- * registry"`. Upstream fixed only the native twin, whose own comment says
21
- * "react-native no longer ships (0.87+)" while the web file next to it still
22
- * imports it.
23
- *
24
- * The target is react-native-web's registry, not React Native's. Both expose
25
- * `registerAsset` and `getAssetByID`, but on web it is react-native-web's
26
- * `Image` that registers assets, and it keeps the store on a
27
- * `Symbol.for("react-native-web.AssetRegistry")` global so its CJS and ESM
28
- * copies share one array. Pointing at RN's registry instead would hand
29
- * react-native-svg a second, permanently empty one, and every numeric
30
- * `source` would throw "asset with ID could not be found".
31
- *
32
- * Each entry is measured rather than asserted: the alias applies only while
33
- * the package it names is genuinely unresolvable and the replacement file is
34
- * on disk. A react-native-svg release that fixes its own import, or a tree
35
- * that still carries the old package, drops the entry instead of shadowing
36
- * something real.
37
- */
38
- interface MovedModule {
39
- /** The specifier the publisher still imports. */
40
- specifier: string;
41
- /** Package whose absence is what makes the move real. */
42
- goneWith: string;
43
- /** Package that holds the replacement, and the file inside it. */
44
- nowIn: string;
45
- file: string;
46
- }
47
-
48
- const MOVED_MODULES: readonly MovedModule[] = [
49
- {
50
- specifier: "@react-native/assets-registry/registry",
51
- goneWith: "@react-native/assets-registry",
52
- nowIn: "react-native-web",
53
- file: "dist/modules/AssetRegistry/index.js",
54
- },
55
- ];
56
-
57
- /**
58
- * Vite `resolve.alias` entries for modules that moved out from under a
59
- * publisher's import.
60
- *
61
- * @param root Directory whose `node_modules` the packages resolve from.
62
- */
63
- export function movedDepAliases(root: string = process.cwd()): Record<string, string> {
64
- const requireFrom = createRequire(path.join(root, "package.json"));
65
- const aliases: Record<string, string> = {};
66
- for (const moved of MOVED_MODULES) {
67
- if (resolvesPackage(moved.goneWith, root, requireFrom)) continue;
68
- const host = resolvePackageDir(moved.nowIn, root, requireFrom);
69
- if (!host) continue;
70
- const target = path.join(host, moved.file);
71
- if (fs.existsSync(target)) aliases[moved.specifier] = target;
72
- }
73
- return aliases;
74
- }
75
-
76
- function resolvesPackage(pkgName: string, root: string, requireFrom: NodeRequire): boolean {
77
- return resolvePackageDir(pkgName, root, requireFrom) !== undefined;
78
- }
79
-
80
- function resolvePackageDir(
81
- pkgName: string,
82
- root: string,
83
- requireFrom: NodeRequire,
84
- ): string | undefined {
85
- try {
86
- return path.dirname(requireFrom.resolve(`${pkgName}/package.json`));
87
- } catch {
88
- const hoisted = path.join(root, "node_modules", ...pkgName.split("/"));
89
- return fs.existsSync(path.join(hoisted, "package.json")) ? hoisted : undefined;
90
- }
91
- }
@@ -1,178 +0,0 @@
1
- import fs from "node:fs";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { afterAll, beforeAll, describe, expect, it } from "vitest";
5
- import {
6
- nativeOnlyDepResolver,
7
- nativeOnlyDepStubOptimizerPlugin,
8
- nativeOnlyDepStubs,
9
- } from "./nativeOnlyDepStubs";
10
-
11
- const soundModule =
12
- "import { TurboModuleRegistry } from 'react-native';\n" +
13
- "export default TurboModuleRegistry.getEnforcing('RNSVGSoundModule');\n";
14
-
15
- /**
16
- * Builds a throwaway tree carrying react-native-svg 15.16.0's fabric sound
17
- * module and a react-native-web entry in its published shape. Asserting
18
- * against the real tree would pass for the wrong reason the moment either
19
- * package changes under us.
20
- */
21
- function writeFixture(
22
- root: string,
23
- { svg = true, webTurboModules = false }: { svg?: boolean; webTurboModules?: boolean } = {},
24
- ) {
25
- fs.mkdirSync(root, { recursive: true });
26
- fs.writeFileSync(path.join(root, "package.json"), JSON.stringify({ name: "fixture-root" }));
27
-
28
- const webDir = path.join(root, "node_modules", "react-native-web");
29
- fs.mkdirSync(path.join(webDir, "dist"), { recursive: true });
30
- fs.writeFileSync(
31
- path.join(webDir, "package.json"),
32
- JSON.stringify({ name: "react-native-web", version: "0.21.2", main: "dist/index.js" }),
33
- );
34
- fs.writeFileSync(
35
- path.join(webDir, "dist", "index.js"),
36
- "export { default as View } from './exports/View';\n" +
37
- "export { default as NativeModules } from './exports/NativeModules';\n" +
38
- (webTurboModules
39
- ? "export { default as TurboModuleRegistry } from './exports/TurboModuleRegistry';\n"
40
- : ""),
41
- );
42
- if (!svg) return;
43
-
44
- const svgDir = path.join(root, "node_modules", "react-native-svg");
45
- fs.mkdirSync(path.join(svgDir, "lib", "module", "fabric"), { recursive: true });
46
- fs.mkdirSync(path.join(svgDir, "lib", "module", "lib"), { recursive: true });
47
- fs.writeFileSync(
48
- path.join(svgDir, "package.json"),
49
- JSON.stringify({
50
- name: "react-native-svg",
51
- version: "15.16.0",
52
- main: "lib/commonjs/index.js",
53
- module: "lib/module/index.js",
54
- }),
55
- );
56
- fs.writeFileSync(
57
- path.join(svgDir, "lib", "module", "fabric", "NativeSvgSoundModule.js"),
58
- soundModule,
59
- );
60
- fs.writeFileSync(
61
- path.join(svgDir, "lib", "module", "lib", "SvgTouchableMixin.js"),
62
- "export const SvgTouchableMixin = {};\n",
63
- );
64
- }
65
-
66
- function importerIn(root: string) {
67
- return path.join(
68
- root,
69
- "node_modules",
70
- "react-native-svg",
71
- "lib",
72
- "module",
73
- "lib",
74
- "SvgTouchableMixin.js",
75
- );
76
- }
77
-
78
- describe("nativeOnlyDepStubs", () => {
79
- // realpath: macOS /var is a symlink to /private/var and require.resolve
80
- // reports the resolved path.
81
- const root = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "native-only-dep-stubs-")));
82
-
83
- beforeAll(() => writeFixture(root));
84
- afterAll(() => fs.rmSync(root, { recursive: true, force: true }));
85
-
86
- it("positive control: the module needs a binding the web runtime has not got", () => {
87
- // Without this the whole suite could be green around a stub that guards
88
- // nothing — the shape it exists for is this import against those exports.
89
- const shipped = fs.readFileSync(
90
- path.join(
91
- root,
92
- "node_modules",
93
- "react-native-svg",
94
- "lib",
95
- "module",
96
- "fabric",
97
- "NativeSvgSoundModule.js",
98
- ),
99
- "utf8",
100
- );
101
- const web = fs.readFileSync(
102
- path.join(root, "node_modules", "react-native-web", "dist", "index.js"),
103
- "utf8",
104
- );
105
- expect(shipped).toMatch(/import \{ TurboModuleRegistry \} from 'react-native'/);
106
- expect(web).not.toMatch(/TurboModuleRegistry/);
107
- });
108
-
109
- it("resolves the publisher's relative specifier to a stub that exists", () => {
110
- const resolve = nativeOnlyDepResolver(root);
111
- const stub = resolve?.("../fabric/NativeSvgSoundModule", importerIn(root));
112
-
113
- expect(stub).toMatch(/shims[\\/]svgTouchSound\.web\.js$/);
114
- expect(fs.existsSync(stub!)).toBe(true);
115
- // SvgTouchableMixin calls `.playTouchSound()` off the default export, so a
116
- // stub without it would crash instead of staying silent.
117
- expect(fs.readFileSync(stub!, "utf8")).toMatch(/playTouchSound\(\)\s*\{\}/);
118
- });
119
-
120
- it("matches the extension spellings a bundler can hand the resolver", () => {
121
- const resolve = nativeOnlyDepResolver(root);
122
- const importer = importerIn(root);
123
-
124
- expect(resolve?.("../fabric/NativeSvgSoundModule.js", importer)).toBeTruthy();
125
- expect(resolve?.("../fabric/NativeSvgSoundModule.ts", importer)).toBeTruthy();
126
- });
127
-
128
- it("declines the same specifier from outside the owning package", () => {
129
- const resolve = nativeOnlyDepResolver(root);
130
-
131
- // The specifier is relative, so only the importer says whose it is.
132
- expect(resolve?.("../fabric/NativeSvgSoundModule", path.join(root, "app", "Screen.tsx"))).toBe(
133
- null,
134
- );
135
- expect(resolve?.("../fabric/NativeSvgSoundModule", undefined)).toBe(null);
136
- });
137
-
138
- it("declines the package's other modules", () => {
139
- const resolve = nativeOnlyDepResolver(root);
140
-
141
- expect(resolve?.("../fabric/SvgViewNativeComponent", importerIn(root))).toBe(null);
142
- expect(resolve?.("react-native", importerIn(root))).toBe(null);
143
- });
144
-
145
- it("emits nothing once the web runtime exports the binding", () => {
146
- const healed = fs.realpathSync(
147
- fs.mkdtempSync(path.join(os.tmpdir(), "native-only-dep-stubs-healed-")),
148
- );
149
- writeFixture(healed, { webTurboModules: true });
150
- try {
151
- expect(nativeOnlyDepStubs(healed)).toEqual([]);
152
- expect(nativeOnlyDepResolver(healed)).toBeUndefined();
153
- } finally {
154
- fs.rmSync(healed, { recursive: true, force: true });
155
- }
156
- });
157
-
158
- it("emits nothing when the package is not installed", () => {
159
- const bare = fs.realpathSync(
160
- fs.mkdtempSync(path.join(os.tmpdir(), "native-only-dep-stubs-bare-")),
161
- );
162
- writeFixture(bare, { svg: false });
163
- try {
164
- expect(nativeOnlyDepStubs(bare)).toEqual([]);
165
- } finally {
166
- fs.rmSync(bare, { recursive: true, force: true });
167
- }
168
- });
169
-
170
- it("hands the dep optimizer a plugin with no vite-only fields", () => {
171
- // rolldown receives this object directly, not through vite's plugin
172
- // normalization, so `enforce` and friends have no meaning there.
173
- const plugin = nativeOnlyDepStubOptimizerPlugin(root);
174
-
175
- expect(Object.keys(plugin!).sort()).toEqual(["name", "resolveId"]);
176
- expect(plugin!.resolveId("../fabric/NativeSvgSoundModule", importerIn(root))).toBeTruthy();
177
- });
178
- });
@@ -1,215 +0,0 @@
1
- import fs from "node:fs";
2
- import { createRequire } from "node:module";
3
- import path from "node:path";
4
- import { fileURLToPath } from "node:url";
5
- import type { Plugin } from "vite";
6
-
7
- /**
8
- * Modules a package ships for a native runtime only, which its own web graph
9
- * still imports.
10
- *
11
- * Different from `movedDepAliases` (the package the publisher imports is gone
12
- * from the tree) and from `unexportedDepAliases` (the subpath is there but
13
- * undeclared). Here the file resolves perfectly well; what it imports cannot
14
- * exist on web, because `react-native` aliases to `react-native-web` and
15
- * react-native-web implements no TurboModules. Repointing that import would be
16
- * a lie. The module itself is dead code behind a `Platform.OS` branch, so the
17
- * honest move is to drop it and say why.
18
- *
19
- * FZ-10849. `react-native-svg` 15.16.0 added
20
- * `fabric/NativeSvgSoundModule` for the Android touch sound and reaches
21
- * it from `SvgTouchableMixin` through a `require()` nested in
22
- * `if (Platform.OS === "android")`. Rolldown hoists that require into a static
23
- * import, so `TurboModuleRegistry` becomes a hard requirement of the web
24
- * graph. `storybook build` survives it (Rollup shims the missing export and
25
- * prints `[SHIMMED_EXPORT]`); the dev dep-optimizer does not, and fails the
26
- * whole optimization, so no dev server starts at all.
27
- *
28
- * Each entry is measured, not asserted. A stub is wired up only while the
29
- * package really ships the module, the module really takes the binding off
30
- * `react-native`, and the web runtime really does not export it. A
31
- * react-native-svg release that adds a web twin, or a react-native-web that
32
- * grows the binding, drops the entry instead of shadowing something live.
33
- */
34
- interface NativeOnlyModule {
35
- /** Package whose web graph reaches the module. */
36
- pkg: string;
37
- /** Paths inside that package where the module ships. */
38
- flavours: readonly string[];
39
- /** The specifier its sibling imports it by, extension optional. */
40
- specifier: string;
41
- /** The `react-native` binding it needs and web cannot supply. */
42
- binding: string;
43
- /** Replacement, in `src/shims`. */
44
- stub: string;
45
- }
46
-
47
- const NATIVE_ONLY_MODULES: readonly NativeOnlyModule[] = [
48
- {
49
- pkg: "react-native-svg",
50
- flavours: [
51
- "lib/module/fabric/NativeSvgSoundModule.js",
52
- "lib/commonjs/fabric/NativeSvgSoundModule.js",
53
- "src/fabric/NativeSvgSoundModule.ts",
54
- ],
55
- specifier: "../fabric/NativeSvgSoundModule",
56
- binding: "TurboModuleRegistry",
57
- stub: "svgTouchSound.web.js",
58
- },
59
- ];
60
-
61
- /** The web stand-in for `react-native`, whose exports decide every entry. */
62
- const webRuntime = { pkg: "react-native-web", entry: "dist/index.js" };
63
-
64
- export interface NativeOnlyDepStub {
65
- /** Path fragment that proves an importer belongs to the owning package. */
66
- marker: string;
67
- /** Every spelling of the specifier a bundler can hand the resolver. */
68
- specifiers: readonly string[];
69
- /** Absolute path of the replacement module. */
70
- stub: string;
71
- }
72
-
73
- /** Resolves a package's native-only module to its web stub, or null. */
74
- export type NativeOnlyDepResolver = (id: string, importer?: string) => string | null;
75
-
76
- /**
77
- * Entries whose breakage is real in this tree, with their stub resolved.
78
- *
79
- * @param root Directory whose `node_modules` the packages resolve from.
80
- */
81
- export function nativeOnlyDepStubs(root: string = process.cwd()): NativeOnlyDepStub[] {
82
- const requireFrom = createRequire(path.join(root, "package.json"));
83
- const webExports = runtimeExports(root, requireFrom);
84
- const stubs: NativeOnlyDepStub[] = [];
85
- for (const entry of NATIVE_ONLY_MODULES) {
86
- if (!webExports || webExports.has(entry.binding)) continue;
87
- const pkgDir = resolvePackageDir(entry.pkg, root, requireFrom);
88
- if (!pkgDir) continue;
89
- const shipped = entry.flavours
90
- .map((flavour) => path.join(pkgDir, flavour))
91
- .filter((file) => fs.existsSync(file));
92
- if (!shipped.some((file) => importsBinding(file, entry.binding))) continue;
93
- const stub = stubFile(entry.stub);
94
- if (!stub) continue;
95
- stubs.push({
96
- marker: `${path.sep}${entry.pkg}${path.sep}`,
97
- specifiers: [entry.specifier, `${entry.specifier}.js`, `${entry.specifier}.ts`],
98
- stub,
99
- });
100
- }
101
- return stubs;
102
- }
103
-
104
- /**
105
- * The resolver behind both plugins below, or undefined when nothing in this
106
- * tree measures as broken.
107
- */
108
- export function nativeOnlyDepResolver(
109
- root: string = process.cwd(),
110
- ): NativeOnlyDepResolver | undefined {
111
- const stubs = nativeOnlyDepStubs(root);
112
- if (!stubs.length) return undefined;
113
- return (id, importer) => {
114
- if (!importer) return null;
115
- for (const stub of stubs) {
116
- if (!importer.includes(stub.marker)) continue;
117
- if (stub.specifiers.includes(id)) return stub.stub;
118
- }
119
- return null;
120
- };
121
- }
122
-
123
- /**
124
- * Vite plugin for the transform pipeline (`vite build`, `storybook build`).
125
- *
126
- * `enforce: "pre"` is load-bearing: a normal-order plugin resolves after
127
- * `vite:resolve`, which has already turned the relative specifier into a real
128
- * file by then.
129
- */
130
- export function nativeOnlyDepStubPlugin(root: string = process.cwd()): Plugin | undefined {
131
- const resolver = nativeOnlyDepResolver(root);
132
- if (!resolver) return undefined;
133
- return {
134
- name: "multiplatform-native-only-dep-stubs",
135
- enforce: "pre",
136
- resolveId(id, importer) {
137
- return resolver(id, importer);
138
- },
139
- };
140
- }
141
-
142
- /**
143
- * The same resolver for the dev dep-optimizer, which runs its own rolldown
144
- * build that sees neither user plugins nor `resolve.alias` for a relative
145
- * specifier — only `optimizeDeps.rolldownOptions.plugins`. Carries no vite-only
146
- * fields, since rolldown receives this object directly.
147
- */
148
- export function nativeOnlyDepStubOptimizerPlugin(
149
- root: string = process.cwd(),
150
- ): { name: string; resolveId: NativeOnlyDepResolver } | undefined {
151
- const resolver = nativeOnlyDepResolver(root);
152
- if (!resolver) return undefined;
153
- return { name: "multiplatform-native-only-dep-stubs", resolveId: resolver };
154
- }
155
-
156
- /** Names the web runtime re-exports, or undefined when it cannot be read. */
157
- function runtimeExports(root: string, requireFrom: NodeRequire): Set<string> | undefined {
158
- const pkgDir = resolvePackageDir(webRuntime.pkg, root, requireFrom);
159
- if (!pkgDir) return undefined;
160
- let source: string;
161
- try {
162
- source = fs.readFileSync(path.join(pkgDir, webRuntime.entry), "utf8");
163
- } catch {
164
- return undefined;
165
- }
166
- const names = new Set<string>();
167
- for (const match of source.matchAll(/^export\s*\{([^}]*)\}/gm)) {
168
- for (const clause of match[1]!.split(",")) {
169
- const name = clause
170
- .trim()
171
- .split(/\s+as\s+/)
172
- .pop()
173
- ?.trim();
174
- if (name) names.add(name);
175
- }
176
- }
177
- for (const match of source.matchAll(/^export\s+(?:const|let|var|function|class)\s+(\w+)/gm)) {
178
- names.add(match[1]!);
179
- }
180
- return names.size ? names : undefined;
181
- }
182
-
183
- /** Whether the module takes the binding off `react-native` by name. */
184
- function importsBinding(file: string, binding: string): boolean {
185
- let source: string;
186
- try {
187
- source = fs.readFileSync(file, "utf8");
188
- } catch {
189
- return false;
190
- }
191
- return new RegExp(`(?:import|require)[^;]*\\b${binding}\\b[^;]*['"]react-native['"]`, "s").test(
192
- source,
193
- );
194
- }
195
-
196
- /** `src/shims/<name>` from source, `../src/shims/<name>` from the built `lib`. */
197
- function stubFile(name: string): string | undefined {
198
- const here = path.dirname(fileURLToPath(import.meta.url));
199
- return [path.resolve(here, "shims", name), path.resolve(here, "../src/shims", name)].find(
200
- (candidate) => fs.existsSync(candidate),
201
- );
202
- }
203
-
204
- function resolvePackageDir(
205
- pkgName: string,
206
- root: string,
207
- requireFrom: NodeRequire,
208
- ): string | undefined {
209
- try {
210
- return path.dirname(requireFrom.resolve(`${pkgName}/package.json`));
211
- } catch {
212
- const hoisted = path.join(root, "node_modules", ...pkgName.split("/"));
213
- return fs.existsSync(path.join(hoisted, "package.json")) ? hoisted : undefined;
214
- }
215
- }
@@ -1,23 +0,0 @@
1
- /**
2
- * Web replacement for react-native-svg's `fabric/NativeSvgSoundModule`.
3
- *
4
- * FZ-10849. The real module is one line — `TurboModuleRegistry.getEnforcing(
5
- * "RNSVGSoundModule")` — and `react-native-svg` 15.16.0 added it for the
6
- * Android touch sound. `SvgTouchableMixin` reaches it through a `require()`
7
- * inside `if (Platform.OS === "android" && !touchSoundDisabled)`, so on web
8
- * the branch is dead, but rolldown hoists the require into a static import
9
- * and the web graph ends up importing `TurboModuleRegistry` from
10
- * `react-native` — which aliases to `react-native-web`, where no such export
11
- * exists. `storybook build` only warns (Rollup shims the export); the dev
12
- * dep-optimizer treats it as fatal and no dev server starts at all.
13
- *
14
- * `playTouchSound` stays a no-op rather than throwing: a browser has no touch
15
- * sound to play, and react-native-web's own Touchable ignores the same prop,
16
- * so a platform mis-detection degrades to silence instead of a crash.
17
- *
18
- * Plain JS, like the other shims here: it is resolved by absolute path from
19
- * build configs that never run the TypeScript pipeline over `src`.
20
- */
21
- export default {
22
- playTouchSound() {},
23
- };
@@ -1,8 +0,0 @@
1
- /**
2
- * Vite `resolve.alias` entries for modules that moved out from under a
3
- * publisher's import.
4
- *
5
- * @param root Directory whose `node_modules` the packages resolve from.
6
- */
7
- export declare function movedDepAliases(root?: string): Record<string, string>;
8
- //# sourceMappingURL=movedDepAliases.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"movedDepAliases.d.ts","sourceRoot":"","sources":["../src/movedDepAliases.ts"],"names":[],"mappings":"AAwDA;;;;;GAKG;AACH,wBAAgB,eAAe,CAAC,IAAI,GAAE,MAAsB,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAWpF"}
@@ -1,41 +0,0 @@
1
- import type { Plugin } from "vite";
2
- export interface NativeOnlyDepStub {
3
- /** Path fragment that proves an importer belongs to the owning package. */
4
- marker: string;
5
- /** Every spelling of the specifier a bundler can hand the resolver. */
6
- specifiers: readonly string[];
7
- /** Absolute path of the replacement module. */
8
- stub: string;
9
- }
10
- /** Resolves a package's native-only module to its web stub, or null. */
11
- export type NativeOnlyDepResolver = (id: string, importer?: string) => string | null;
12
- /**
13
- * Entries whose breakage is real in this tree, with their stub resolved.
14
- *
15
- * @param root Directory whose `node_modules` the packages resolve from.
16
- */
17
- export declare function nativeOnlyDepStubs(root?: string): NativeOnlyDepStub[];
18
- /**
19
- * The resolver behind both plugins below, or undefined when nothing in this
20
- * tree measures as broken.
21
- */
22
- export declare function nativeOnlyDepResolver(root?: string): NativeOnlyDepResolver | undefined;
23
- /**
24
- * Vite plugin for the transform pipeline (`vite build`, `storybook build`).
25
- *
26
- * `enforce: "pre"` is load-bearing: a normal-order plugin resolves after
27
- * `vite:resolve`, which has already turned the relative specifier into a real
28
- * file by then.
29
- */
30
- export declare function nativeOnlyDepStubPlugin(root?: string): Plugin | undefined;
31
- /**
32
- * The same resolver for the dev dep-optimizer, which runs its own rolldown
33
- * build that sees neither user plugins nor `resolve.alias` for a relative
34
- * specifier — only `optimizeDeps.rolldownOptions.plugins`. Carries no vite-only
35
- * fields, since rolldown receives this object directly.
36
- */
37
- export declare function nativeOnlyDepStubOptimizerPlugin(root?: string): {
38
- name: string;
39
- resolveId: NativeOnlyDepResolver;
40
- } | undefined;
41
- //# sourceMappingURL=nativeOnlyDepStubs.d.ts.map