@solidnative-dev/metro 0.0.0-stage → 0.1.0-beta.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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Solid Native contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,57 @@
1
- # Temporary Holding Version
1
+ # `@solidnative-dev/metro`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ This package uses the shared `@solidnative-dev/compiler` boundary to compile
4
+ application TSX with the AST-native `@solidjs/compiler` OXC backend
5
+ before Metro performs React Native's platform and module lowering. It composes
6
+ the compiler map with Metro's module map without mutating Babel AST locations.
7
+
8
+ The Solid transform has no Babel fallback. The OXC compiler is pinned exactly
9
+ while its package remains on an experimental prerelease line, and unsupported
10
+ compiler input fails the build instead of silently switching semantics.
11
+
12
+ Use the exported worker path, Solid runtime resolver, and safe reload pattern in
13
+ the application Metro config:
14
+
15
+ ```js
16
+ const {
17
+ createSolidNativeFullReloadPattern,
18
+ createSolidNativeMetroResolver,
19
+ solidNativeTransformWorkerPath,
20
+ } = require("@solidnative-dev/metro");
21
+ const { getDefaultConfig, mergeConfig } = require("@react-native/metro-config");
22
+
23
+ module.exports = mergeConfig(getDefaultConfig(__dirname), {
24
+ transformerPath: solidNativeTransformWorkerPath,
25
+ resolver: {
26
+ resolveRequest: createSolidNativeMetroResolver(__dirname),
27
+ unstable_forceFullRefreshPatterns: [
28
+ createSolidNativeFullReloadPattern(__dirname),
29
+ ],
30
+ },
31
+ });
32
+ ```
33
+
34
+ Metro distinguishes ESM `import` from CommonJS `require` when evaluating
35
+ package exports. Without the resolver, dependencies can pull `solid.js` and
36
+ `solid.cjs` into the same application, creating two independent reactive
37
+ graphs. The resolver pins every edge to one browser ESM runtime while retaining
38
+ Solid's development build for development bundles and production build for
39
+ release bundles.
40
+
41
+ Metro's built-in React Refresh considers exported component-shaped functions
42
+ to be React refresh boundaries. Solid component exports can satisfy that
43
+ heuristic even though React cannot replace their mounted Solid owners. The
44
+ full-reload pattern makes application JavaScript and TypeScript edits restart
45
+ the complete JavaScript/native surface lifecycle while excluding every
46
+ `node_modules` subtree. This is a correctness and cleanup boundary, not
47
+ owner-preserving Solid HMR; that requires an explicit compiler/runtime owner
48
+ replacement protocol.
49
+
50
+ React Native's Babel preset remains in the application config for its own
51
+ lowering after Solid JSX is gone. `babel-preset-solid` is not installed or
52
+ applied.
53
+
54
+ Both the resolver and transform-worker subpaths ship explicit TypeScript
55
+ declarations. The repository artifact gate packs this package, installs it into
56
+ an isolated NodeNext consumer, invokes the resolver through those declarations,
57
+ and rejects any JavaScript export that lacks a packed `types` condition.
package/index.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ export interface SolidNativeMetroResolverContext {
2
+ readonly dev: boolean;
3
+ readonly resolveRequest: (
4
+ context: SolidNativeMetroResolverContext,
5
+ moduleName: string,
6
+ platform: string | null,
7
+ ) => unknown;
8
+ }
9
+
10
+ export type SolidNativeMetroResolver = (
11
+ context: SolidNativeMetroResolverContext,
12
+ moduleName: string,
13
+ platform: string | null,
14
+ ) => unknown;
15
+
16
+ /** Absolute path to Solid Native's package-owned Metro transform worker. */
17
+ export declare const solidNativeTransformWorkerPath: string;
18
+
19
+ /**
20
+ * Matches application JavaScript/TypeScript modules that must trigger a full
21
+ * runtime reload instead of entering React Refresh's component heuristics.
22
+ */
23
+ export declare function createSolidNativeFullReloadPattern(
24
+ projectRoot: string,
25
+ ): RegExp;
26
+
27
+ /**
28
+ * Creates a Metro resolver that pins Solid and its signal graph to one browser
29
+ * runtime selected by Metro's development mode.
30
+ */
31
+ export declare function createSolidNativeMetroResolver(
32
+ projectRoot: string,
33
+ ): SolidNativeMetroResolver;
package/index.js ADDED
@@ -0,0 +1,166 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const path = require("node:path");
5
+
6
+ const solidNativeTransformWorkerPath = require.resolve("./transform-worker");
7
+ const SOLID_NATIVE_DEVELOPMENT_ENTRY_POINTS = new Map([
8
+ [
9
+ "@solidnative-dev/devtools/network-service",
10
+ {
11
+ productionFile: "network-service.js",
12
+ developmentFile: "network-service-development.js",
13
+ },
14
+ ],
15
+ [
16
+ "@solidnative-dev/devtools/root",
17
+ {
18
+ productionFile: "root.js",
19
+ developmentFile: "root-development.js",
20
+ },
21
+ ],
22
+ [
23
+ "@solidnative-dev/devtools/solid-diagnostics",
24
+ {
25
+ productionFile: "solid-diagnostics.js",
26
+ developmentFile: "solid-diagnostics-development.js",
27
+ },
28
+ ],
29
+ ]);
30
+
31
+ function escapeRegularExpression(value) {
32
+ return value.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
33
+ }
34
+
35
+ /**
36
+ * Prevents Metro's React Refresh heuristics from treating exported Solid
37
+ * component functions as React refresh boundaries. Until Solid Native owns a
38
+ * renderer-aware HMR protocol, application source changes must restart the
39
+ * complete JavaScript/native surface lifecycle instead of retaining stale
40
+ * Solid owners.
41
+ */
42
+ function createSolidNativeFullReloadPattern(projectRoot) {
43
+ if (typeof projectRoot !== "string" || !path.isAbsolute(projectRoot)) {
44
+ throw new TypeError(
45
+ "The Solid Native Metro project root must be an absolute path.",
46
+ );
47
+ }
48
+ const escapedRoot = path
49
+ .normalize(projectRoot)
50
+ .split(path.sep)
51
+ .map(escapeRegularExpression)
52
+ .join("[\\\\/]");
53
+ return new RegExp(
54
+ `^${escapedRoot}[\\\\/](?!node_modules[\\\\/]|.*[\\\\/]node_modules[\\\\/]).+\\.[cm]?[jt]sx?$`,
55
+ );
56
+ }
57
+
58
+ function resolvePackageRoot(packageName, projectRoot) {
59
+ return path.dirname(
60
+ require.resolve(`${packageName}/package.json`, { paths: [projectRoot] }),
61
+ );
62
+ }
63
+
64
+ function tryResolvePackageRoot(packageName, searchPaths) {
65
+ try {
66
+ let directory = path.dirname(
67
+ require.resolve(packageName, { paths: searchPaths }),
68
+ );
69
+ for (;;) {
70
+ if (fs.existsSync(path.join(directory, "package.json"))) {
71
+ return directory;
72
+ }
73
+ const parent = path.dirname(directory);
74
+ if (parent === directory) return undefined;
75
+ directory = parent;
76
+ }
77
+ } catch {
78
+ return undefined;
79
+ }
80
+ }
81
+
82
+ /**
83
+ * Pins every Metro import/require edge to one browser Solid runtime. Without
84
+ * this resolver, Metro can honor both halves of Solid's conditional exports
85
+ * and bundle ESM plus CommonJS signal graphs into the same application.
86
+ */
87
+ function createSolidNativeMetroResolver(projectRoot) {
88
+ const solidRoot = resolvePackageRoot("solid-js", projectRoot);
89
+ const signalsRoot = resolvePackageRoot("@solidjs/signals", solidRoot);
90
+ const rendererRoot = tryResolvePackageRoot("@solidnative-dev/renderer", [
91
+ projectRoot,
92
+ ]);
93
+ const universalRoot = tryResolvePackageRoot(
94
+ "@solidjs/universal",
95
+ rendererRoot === undefined ? [projectRoot] : [projectRoot, rendererRoot],
96
+ );
97
+ return (context, moduleName, platform) => {
98
+ let canonicalModule = moduleName;
99
+ if (moduleName === "solid-js") {
100
+ canonicalModule = path.join(
101
+ solidRoot,
102
+ "dist",
103
+ context.dev ? "solid.dev.js" : "solid.js",
104
+ );
105
+ } else if (moduleName === "solid-js/attribution") {
106
+ canonicalModule = path.join(solidRoot, "dist", "attribution.js");
107
+ } else if (moduleName === "@solidjs/signals") {
108
+ canonicalModule = path.join(
109
+ signalsRoot,
110
+ "dist",
111
+ context.dev ? "dev.js" : path.join("prod", "index.js"),
112
+ );
113
+ } else if (moduleName === "@solidjs/signals/attribution") {
114
+ canonicalModule = path.join(
115
+ signalsRoot,
116
+ "dist",
117
+ context.dev
118
+ ? "dev.attribution.js"
119
+ : path.join("prod", "attribution.js"),
120
+ );
121
+ } else if (
122
+ moduleName === "@solidjs/universal" &&
123
+ universalRoot !== undefined
124
+ ) {
125
+ canonicalModule = path.join(
126
+ universalRoot,
127
+ "dist",
128
+ context.dev ? "universal.dev.js" : "universal.js",
129
+ );
130
+ }
131
+ const resolution = context.resolveRequest(
132
+ context,
133
+ canonicalModule,
134
+ platform,
135
+ );
136
+ const developmentEntry =
137
+ SOLID_NATIVE_DEVELOPMENT_ENTRY_POINTS.get(moduleName);
138
+ if (developmentEntry === undefined || !context.dev) {
139
+ return resolution;
140
+ }
141
+ if (
142
+ resolution === null ||
143
+ typeof resolution !== "object" ||
144
+ resolution.type !== "sourceFile" ||
145
+ typeof resolution.filePath !== "string" ||
146
+ path.basename(resolution.filePath) !== developmentEntry.productionFile
147
+ ) {
148
+ throw new Error(
149
+ `${moduleName} did not resolve to its reviewed production shim.`,
150
+ );
151
+ }
152
+ return {
153
+ ...resolution,
154
+ filePath: path.join(
155
+ path.dirname(resolution.filePath),
156
+ developmentEntry.developmentFile,
157
+ ),
158
+ };
159
+ };
160
+ }
161
+
162
+ module.exports = {
163
+ createSolidNativeFullReloadPattern,
164
+ createSolidNativeMetroResolver,
165
+ solidNativeTransformWorkerPath,
166
+ };
package/package.json CHANGED
@@ -1,6 +1,45 @@
1
1
  {
2
2
  "name": "@solidnative-dev/metro",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0-beta.0",
4
+ "description": "OXC Solid universal compilation and source-map composition for Metro.",
5
+ "license": "MIT",
6
+ "homepage": "https://solidnative.dev",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/solid-native/solid-native.git",
10
+ "directory": "packages/metro"
11
+ },
12
+ "publishConfig": {
13
+ "access": "public"
14
+ },
15
+ "engines": {
16
+ "node": "^22.13.0 || ^24.3.0 || >=26.0.0"
17
+ },
18
+ "exports": {
19
+ ".": {
20
+ "types": "./index.d.ts",
21
+ "default": "./index.js"
22
+ },
23
+ "./transform-worker": {
24
+ "types": "./transform-worker.d.ts",
25
+ "default": "./transform-worker.js"
26
+ }
27
+ },
28
+ "files": [
29
+ "index.d.ts",
30
+ "index.js",
31
+ "transform-worker.d.ts",
32
+ "transform-worker.js"
33
+ ],
34
+ "dependencies": {
35
+ "@jridgewell/remapping": "2.3.5",
36
+ "@jridgewell/trace-mapping": "0.3.31",
37
+ "@solidnative-dev/compiler": "^0.1.0-beta.0",
38
+ "metro-transform-worker": "0.87.3",
39
+ "vlq": "1.0.1"
40
+ },
41
+ "scripts": {
42
+ "check": "node --check index.js && node --check transform-worker.js",
43
+ "test": "node --test test/*.test.mjs"
44
+ }
6
45
  }
@@ -0,0 +1,13 @@
1
+ import type { Buffer } from "node:buffer";
2
+
3
+ /** Delegates to Metro after applying Solid Native's OXC transform when needed. */
4
+ export declare function transform(
5
+ config: unknown,
6
+ projectRoot: string,
7
+ filename: string,
8
+ data: Buffer,
9
+ options: unknown,
10
+ ): Promise<unknown>;
11
+
12
+ /** Returns the cache identity for the worker, compiler, and Metro options. */
13
+ export declare function getCacheKey(config: unknown, options: unknown): string;
@@ -0,0 +1,165 @@
1
+ "use strict";
2
+
3
+ const crypto = require("node:crypto");
4
+ const fs = require("node:fs");
5
+ const path = require("node:path");
6
+ const remapping = require("@jridgewell/remapping");
7
+ const {
8
+ GREATEST_LOWER_BOUND,
9
+ TraceMap,
10
+ originalPositionFor,
11
+ } = require("@jridgewell/trace-mapping");
12
+ const metroWorker = require("metro-transform-worker");
13
+ const {
14
+ solidNativeCompilerCacheKey,
15
+ transformSolidNativeJsx,
16
+ } = require("@solidnative-dev/compiler");
17
+ const vlq = require("vlq");
18
+
19
+ const workerSource = fs.readFileSync(__filename);
20
+
21
+ function usesReactControl() {
22
+ return process.env.SOLID_NATIVE_REACT_CONTROL === "1";
23
+ }
24
+
25
+ function shouldCompile(filename) {
26
+ return (
27
+ !usesReactControl() &&
28
+ !filename.includes(`${path.sep}node_modules${path.sep}`) &&
29
+ /\.[jt]sx$/.test(filename)
30
+ );
31
+ }
32
+
33
+ function decodeFunctionMap(functionMap) {
34
+ const mappings = [];
35
+ let line = 1;
36
+ let nameIndex = 0;
37
+ for (const lineMappings of functionMap.mappings.split(";")) {
38
+ let column = 0;
39
+ for (const mapping of lineMappings.split(",")) {
40
+ if (mapping === "") continue;
41
+ const [columnDelta, nameDelta, lineDelta = 0] = vlq.decode(mapping);
42
+ line += lineDelta;
43
+ nameIndex += nameDelta;
44
+ column += columnDelta;
45
+ mappings.push({ line, column, name: functionMap.names[nameIndex] });
46
+ }
47
+ }
48
+ return mappings;
49
+ }
50
+
51
+ function encodeFunctionMap(mappings) {
52
+ const names = [];
53
+ const nameIndexes = new Map();
54
+ let encoded = "";
55
+ let previousLine = 1;
56
+ let previousColumn = 0;
57
+ let previousNameIndex = 0;
58
+ for (const [index, mapping] of mappings.entries()) {
59
+ let nameIndex = nameIndexes.get(mapping.name);
60
+ if (nameIndex === undefined) {
61
+ nameIndex = names.length;
62
+ nameIndexes.set(mapping.name, nameIndex);
63
+ names.push(mapping.name);
64
+ }
65
+ const lineDelta = mapping.line - previousLine;
66
+ const firstOnLine = index === 0 || lineDelta > 0;
67
+ if (index > 0) encoded += lineDelta > 0 ? ";" : ",";
68
+ if (firstOnLine) previousColumn = 0;
69
+ const segment = [
70
+ mapping.column - previousColumn,
71
+ nameIndex - previousNameIndex,
72
+ ];
73
+ if (firstOnLine) segment.push(lineDelta);
74
+ encoded += vlq.encode(segment);
75
+ previousLine = mapping.line;
76
+ previousColumn = mapping.column;
77
+ previousNameIndex = nameIndex;
78
+ }
79
+ return { names, mappings: encoded };
80
+ }
81
+
82
+ function remapFunctionMap(functionMap, compilerMap) {
83
+ if (functionMap === null || functionMap === undefined) return functionMap;
84
+ const mappings = decodeFunctionMap(functionMap)
85
+ .map((mapping) => {
86
+ if (mapping.name === "<global>") {
87
+ return { ...mapping, line: 1, column: 0 };
88
+ }
89
+ const original = originalPositionFor(compilerMap, {
90
+ line: mapping.line,
91
+ column: mapping.column,
92
+ bias: GREATEST_LOWER_BOUND,
93
+ });
94
+ return original.line === null || original.column === null
95
+ ? null
96
+ : { ...mapping, line: original.line, column: original.column };
97
+ })
98
+ .filter((mapping) => mapping !== null)
99
+ .sort((left, right) =>
100
+ left.line === right.line
101
+ ? left.column - right.column
102
+ : left.line - right.line,
103
+ )
104
+ .filter(
105
+ (mapping, index, all) =>
106
+ index === all.length - 1 ||
107
+ mapping.line !== all[index + 1].line ||
108
+ mapping.column !== all[index + 1].column,
109
+ );
110
+ return encodeFunctionMap(mappings);
111
+ }
112
+
113
+ function composeOutputMap(output, filename, compiledSource, compilerMap) {
114
+ if (!output.type.startsWith("js/")) return output;
115
+ const metroMap = {
116
+ version: 3,
117
+ sources: [filename],
118
+ sourcesContent: [compiledSource],
119
+ names: output.data.map.names,
120
+ mappings: output.data.map.mappings,
121
+ };
122
+ const map = remapping([metroMap, compilerMap], () => null);
123
+ return {
124
+ ...output,
125
+ data: {
126
+ ...output.data,
127
+ map: { mappings: map.mappings, names: map.names },
128
+ functionMap: remapFunctionMap(output.data.functionMap, compilerMap),
129
+ },
130
+ };
131
+ }
132
+
133
+ async function transform(config, projectRoot, filename, data, options) {
134
+ if (!shouldCompile(filename)) {
135
+ return metroWorker.transform(config, projectRoot, filename, data, options);
136
+ }
137
+
138
+ const result = transformSolidNativeJsx(data.toString("utf8"), filename);
139
+ const transformed = await metroWorker.transform(
140
+ config,
141
+ projectRoot,
142
+ filename,
143
+ Buffer.from(result.code),
144
+ options,
145
+ );
146
+ const compilerTraceMap = new TraceMap(result.map);
147
+ return {
148
+ ...transformed,
149
+ output: transformed.output.map((output) =>
150
+ composeOutputMap(output, filename, result.code, compilerTraceMap),
151
+ ),
152
+ };
153
+ }
154
+
155
+ function getCacheKey(config, options) {
156
+ return crypto
157
+ .createHash("sha256")
158
+ .update(workerSource)
159
+ .update(solidNativeCompilerCacheKey)
160
+ .update(usesReactControl() ? "react-control" : "solid-oxc")
161
+ .update(metroWorker.getCacheKey(config, options))
162
+ .digest("hex");
163
+ }
164
+
165
+ module.exports = { getCacheKey, transform };