@teardown/metro-config 2.0.52 → 2.0.56

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/dist/index.js CHANGED
@@ -128,11 +128,11 @@ function withTeardown(config, options = {}) {
128
128
  return finalConfig;
129
129
  }
130
130
  // Re-export Bun utilities
131
- const bun_2 = require("./bun");
131
+ var bun_2 = require("./bun");
132
132
  Object.defineProperty(exports, "BUN_BLOCK_PATTERN", { enumerable: true, get: function () { return bun_2.BUN_BLOCK_PATTERN; } });
133
133
  Object.defineProperty(exports, "getBlockList", { enumerable: true, get: function () { return bun_2.getBlockList; } });
134
134
  // Re-export workspace utilities for advanced use cases
135
- const workspace_2 = require("./workspace");
135
+ var workspace_2 = require("./workspace");
136
136
  Object.defineProperty(exports, "getMetroServerRoot", { enumerable: true, get: function () { return workspace_2.getMetroServerRoot; } });
137
137
  Object.defineProperty(exports, "getModulesPaths", { enumerable: true, get: function () { return workspace_2.getModulesPaths; } });
138
138
  Object.defineProperty(exports, "getRelativeProjectRoot", { enumerable: true, get: function () { return workspace_2.getRelativeProjectRoot; } });
package/package.json CHANGED
@@ -1,26 +1,31 @@
1
1
  {
2
2
  "name": "@teardown/metro-config",
3
- "version": "2.0.52",
3
+ "version": "2.0.56",
4
4
  "description": "Metro configuration for Teardown - Rust-powered transforms via Facetpack",
5
5
  "private": false,
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
+ "type": "commonjs",
9
10
  "main": "./dist/index.js",
10
- "types": "./dist/index.d.ts",
11
11
  "exports": {
12
12
  ".": {
13
13
  "types": "./dist/index.d.ts",
14
14
  "require": "./dist/index.js",
15
- "import": "./dist/index.js",
16
15
  "default": "./dist/index.js"
17
16
  },
18
17
  "./package.json": "./package.json"
19
18
  },
19
+ "files": [
20
+ "dist/**/*"
21
+ ],
20
22
  "scripts": {
21
- "typecheck": "bun x tsgo --noEmit --project ./tsconfig.json",
22
- "build": "bun x tsgo --project ./tsconfig.json",
23
- "dev": "bun x tsgo --watch --project ./tsconfig.json"
23
+ "build": "tsc",
24
+ "prepublishOnly": "bun run build",
25
+ "typecheck": "bun x tsgo --noEmit",
26
+ "lint": "bun x biome lint --write ./src",
27
+ "fmt": "bun x biome format --write ./src",
28
+ "check": "bun x biome check ./src"
24
29
  },
25
30
  "peerDependencies": {
26
31
  "@ecrindigital/facetpack": "^0.2.0",
@@ -40,7 +45,7 @@
40
45
  "resolve-workspace-root": "^2.0.0"
41
46
  },
42
47
  "devDependencies": {
43
- "@teardown/tsconfig": "2.0.52",
48
+ "@teardown/tsconfig": "2.0.56",
44
49
  "@types/bun": "1.3.5",
45
50
  "typescript": "5.9.3"
46
51
  },
package/src/bun.ts DELETED
@@ -1,95 +0,0 @@
1
- /**
2
- * Bun-specific Metro configuration utilities.
3
- *
4
- * Handles blocking of .bun directories which can cause Metro resolution issues.
5
- */
6
-
7
- import { existsSync, readFileSync } from "node:fs";
8
- import path from "node:path";
9
- import type { ResolveRequestFn } from "./types";
10
- import { getModulesPaths } from "./workspace";
11
-
12
- /**
13
- * Block pattern for .bun directories.
14
- * Metro's blockList prevents processing of matched paths.
15
- */
16
- export const BUN_BLOCK_PATTERN = /.*[/\\]\.bun[/\\].*/;
17
-
18
- /**
19
- * Create a custom resolver that filters out .bun paths during module resolution.
20
- *
21
- * Metro's blockList only prevents processing after resolution, not during.
22
- * This resolver intercepts resolution and redirects away from .bun paths.
23
- */
24
- export function createBunAwareResolver(projectRoot: string, defaultResolveRequest: ResolveRequestFn): ResolveRequestFn {
25
- const modulesPaths = getModulesPaths(projectRoot);
26
-
27
- return (context: { resolveRequest: ResolveRequestFn }, moduleName: string, platform: string | null) => {
28
- // Use Metro's default resolution
29
- const result = defaultResolveRequest(context, moduleName, platform);
30
-
31
- // If resolved path contains .bun, try to find alternative
32
- if (result?.filePath?.includes("/.bun/")) {
33
- for (const modulesPath of modulesPaths) {
34
- if (modulesPath.includes("/.bun/")) continue;
35
-
36
- const packagePath = path.join(modulesPath, moduleName);
37
- if (existsSync(packagePath)) {
38
- const mainFile = resolvePackageMain(packagePath);
39
- if (mainFile) {
40
- return { type: "sourceFile", filePath: mainFile };
41
- }
42
- }
43
- }
44
- }
45
-
46
- return result;
47
- };
48
- }
49
-
50
- /**
51
- * Resolve the main entry point of a package.
52
- */
53
- function resolvePackageMain(packagePath: string): string | null {
54
- const packageJsonPath = path.join(packagePath, "package.json");
55
- if (existsSync(packageJsonPath)) {
56
- try {
57
- const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8"));
58
- const main = pkg.main || pkg.module || "index.js";
59
- const mainPath = path.join(packagePath, main);
60
-
61
- if (existsSync(mainPath)) {
62
- return mainPath;
63
- }
64
- // Try with .js extension
65
- const mainPathJs = mainPath + ".js";
66
- if (existsSync(mainPathJs)) {
67
- return mainPathJs;
68
- }
69
- } catch {
70
- // Ignore parse errors
71
- }
72
- }
73
-
74
- // Fallback to index.js
75
- const indexPath = path.join(packagePath, "index.js");
76
- if (existsSync(indexPath)) {
77
- return indexPath;
78
- }
79
-
80
- return null;
81
- }
82
-
83
- /**
84
- * Get blockList patterns for Metro config.
85
- * Includes .bun directory blocking.
86
- */
87
- export function getBlockList(existingBlockList?: RegExp | RegExp[]): RegExp[] {
88
- const blockListArray = Array.isArray(existingBlockList)
89
- ? existingBlockList
90
- : existingBlockList
91
- ? [existingBlockList]
92
- : [];
93
-
94
- return [...blockListArray, BUN_BLOCK_PATTERN];
95
- }
package/src/index.ts DELETED
@@ -1,169 +0,0 @@
1
- /**
2
- * @teardown/metro-config
3
- *
4
- * Metro configuration wrapper that provides:
5
- * - Rust-powered transforms via Facetpack (36x faster than Babel)
6
- * - Automatic monorepo/workspace detection and configuration
7
- * - Bun-specific handling (blocks .bun directories)
8
- *
9
- * @example
10
- * ```js
11
- * // metro.config.js
12
- * const { withTeardown } = require('@teardown/metro-config')
13
- * const { getDefaultConfig } = require('@react-native/metro-config')
14
- *
15
- * module.exports = withTeardown(getDefaultConfig(__dirname))
16
- * ```
17
- *
18
- * @packageDocumentation
19
- */
20
-
21
- import { getStoredOptions, withFacetpack } from "@ecrindigital/facetpack";
22
- import { createBunAwareResolver, getBlockList } from "./bun";
23
- import type { MetroConfig, ResolveRequestFn, TeardownMetroOptions } from "./types";
24
- import {
25
- getMetroServerRoot,
26
- getModulesPaths,
27
- getRelativeProjectRoot,
28
- getWatchFolders,
29
- getWorkspaceRoot,
30
- isInMonorepo,
31
- } from "./workspace";
32
-
33
- /**
34
- * Wraps a Metro configuration with Teardown's enhancements.
35
- *
36
- * This function applies:
37
- * - Facetpack for Rust-powered transforms (36x faster)
38
- * - Automatic monorepo detection and watch folder configuration
39
- * - Bun-specific handling (.bun directory blocking)
40
- * - Proper nodeModulesPaths for monorepo resolution
41
- *
42
- * @param config - Base Metro configuration
43
- * @param options - Optional configuration options
44
- * @returns Enhanced Metro configuration
45
- *
46
- * @example
47
- * ```js
48
- * // Basic usage - all monorepo handling is automatic
49
- * const { withTeardown } = require('@teardown/metro-config')
50
- * const { getDefaultConfig } = require('@react-native/metro-config')
51
- *
52
- * module.exports = withTeardown(getDefaultConfig(__dirname))
53
- * ```
54
- *
55
- * @example
56
- * ```js
57
- * // With options
58
- * const { withTeardown } = require('@teardown/metro-config')
59
- * const { getDefaultConfig } = require('@react-native/metro-config')
60
- *
61
- * module.exports = withTeardown(getDefaultConfig(__dirname), {
62
- * verbose: true,
63
- * projectRoot: __dirname, // explicitly set project root
64
- * })
65
- * ```
66
- */
67
- export function withTeardown(config: MetroConfig, options: TeardownMetroOptions = {}): MetroConfig {
68
- const { verbose, projectRoot: explicitProjectRoot, ...facetpackOptions } = options;
69
-
70
- // Determine project root
71
- const projectRoot = explicitProjectRoot || (config.projectRoot as string | undefined) || process.cwd();
72
-
73
- if (verbose) {
74
- console.log("[teardown/metro-config] Applying Teardown configuration...");
75
- console.log(`[teardown/metro-config] Project root: ${projectRoot}`);
76
- }
77
-
78
- // Check if in monorepo
79
- const inMonorepo = isInMonorepo(projectRoot);
80
- if (verbose && inMonorepo) {
81
- const workspaceRoot = getWorkspaceRoot(projectRoot);
82
- console.log(`[teardown/metro-config] Monorepo detected: ${workspaceRoot}`);
83
- }
84
-
85
- // Get monorepo-aware watch folders
86
- const additionalWatchFolders = getWatchFolders(projectRoot);
87
- const existingWatchFolders = (config.watchFolders as string[] | undefined) || [];
88
-
89
- // Get monorepo-aware node modules paths
90
- const modulesPaths = getModulesPaths(projectRoot);
91
- const existingNodeModulesPaths = (config.resolver?.nodeModulesPaths as string[] | undefined) || [];
92
-
93
- // Get block list with .bun blocking
94
- const blockList = getBlockList(config.resolver?.blockList as RegExp | RegExp[] | undefined);
95
-
96
- // Create Bun-aware resolver
97
- const defaultResolveRequest: ResolveRequestFn =
98
- (config.resolver?.resolveRequest as ResolveRequestFn | undefined) ||
99
- ((context, moduleName, platform) => context.resolveRequest(context, moduleName, platform));
100
-
101
- const bunAwareResolver = createBunAwareResolver(projectRoot, defaultResolveRequest);
102
-
103
- // Build enhanced config
104
- const enhancedConfig: MetroConfig = {
105
- ...config,
106
- projectRoot,
107
- watchFolders: [...new Set([...existingWatchFolders, ...additionalWatchFolders])],
108
- resolver: {
109
- ...config.resolver,
110
- blockList,
111
- nodeModulesPaths: [...new Set([...modulesPaths, ...existingNodeModulesPaths])],
112
- resolveRequest: bunAwareResolver,
113
- },
114
- };
115
-
116
- // Set unstable_serverRoot for monorepo web support
117
- if (inMonorepo) {
118
- const serverRoot = getMetroServerRoot(projectRoot);
119
- (enhancedConfig as Record<string, unknown>).unstable_serverRoot = serverRoot;
120
-
121
- // Add relative project root to transformer cache key for cache collision prevention
122
- const relativeRoot = getRelativeProjectRoot(projectRoot);
123
- if (relativeRoot) {
124
- const existingCacheKey =
125
- (config.transformer as Record<string, unknown> | undefined)?.customTransformOptions || {};
126
- enhancedConfig.transformer = {
127
- ...config.transformer,
128
- customTransformOptions: {
129
- ...(existingCacheKey as Record<string, unknown>),
130
- _teardownRelativeProjectRoot: relativeRoot,
131
- },
132
- };
133
- }
134
- }
135
-
136
- if (verbose) {
137
- const watchFoldersCount = (enhancedConfig.watchFolders as string[] | undefined)?.length || 0;
138
- const nodeModulesPathsCount = (enhancedConfig.resolver?.nodeModulesPaths as string[] | undefined)?.length || 0;
139
- console.log(`[teardown/metro-config] Watch folders: ${watchFoldersCount}`);
140
- console.log(`[teardown/metro-config] Node modules paths: ${nodeModulesPathsCount}`);
141
- }
142
-
143
- // Apply Facetpack for Rust-powered transforms
144
- const finalConfig = withFacetpack(enhancedConfig, facetpackOptions);
145
-
146
- if (verbose) {
147
- console.log("[teardown/metro-config] Metro config enhanced successfully");
148
- }
149
-
150
- return finalConfig;
151
- }
152
-
153
- // Re-export useful utilities from Facetpack
154
- export { getStoredOptions };
155
-
156
- // Re-export Bun utilities
157
- export { BUN_BLOCK_PATTERN, getBlockList } from "./bun";
158
- // Re-export workspace utilities for advanced use cases
159
- export {
160
- getMetroServerRoot,
161
- getModulesPaths,
162
- getRelativeProjectRoot,
163
- getWatchFolders,
164
- getWorkspaceRoot,
165
- isInMonorepo,
166
- } from "./workspace";
167
-
168
- // Re-export types
169
- export type { TeardownMetroOptions, MetroConfig };
package/src/types.ts DELETED
@@ -1,50 +0,0 @@
1
- /**
2
- * Type definitions for @teardown/metro-config
3
- */
4
-
5
- import type { MetroConfig as FacetpackMetroConfig } from "@ecrindigital/facetpack";
6
-
7
- /**
8
- * Extended Metro configuration type with proper resolver and transformer types
9
- */
10
- export interface MetroConfig extends FacetpackMetroConfig {
11
- projectRoot?: string;
12
- watchFolders?: string[];
13
- resolver?: {
14
- blockList?: RegExp | RegExp[];
15
- nodeModulesPaths?: string[];
16
- resolveRequest?: ResolveRequestFn;
17
- [key: string]: unknown;
18
- };
19
- transformer?: {
20
- customTransformOptions?: Record<string, unknown>;
21
- [key: string]: unknown;
22
- };
23
- [key: string]: unknown;
24
- }
25
-
26
- /**
27
- * Metro resolve request function type
28
- */
29
- export type ResolveRequestFn = (
30
- context: { resolveRequest: ResolveRequestFn },
31
- moduleName: string,
32
- platform: string | null
33
- ) => { type: string; filePath: string } | null;
34
-
35
- /**
36
- * Teardown-specific Metro configuration options
37
- */
38
- export interface TeardownMetroOptions {
39
- /**
40
- * Enable verbose logging for debugging
41
- * @default false
42
- */
43
- verbose?: boolean;
44
-
45
- /**
46
- * Explicit project root path.
47
- * If not provided, uses config.projectRoot or process.cwd().
48
- */
49
- projectRoot?: string;
50
- }
package/src/workspace.ts DELETED
@@ -1,233 +0,0 @@
1
- /**
2
- * Workspace and monorepo detection utilities for Metro configuration.
3
- *
4
- * Uses `resolve-workspace-root` to detect workspace roots across:
5
- * - Yarn workspaces
6
- * - npm workspaces
7
- * - pnpm workspaces
8
- * - Bun workspaces
9
- */
10
-
11
- import { existsSync, readFileSync } from "node:fs";
12
- import path from "node:path";
13
- import { globSync } from "glob";
14
-
15
- /**
16
- * Read and parse a JSON file, returning null if invalid.
17
- */
18
- function readJsonFile(filePath: string): unknown {
19
- const file = readFileSync(filePath, "utf8");
20
- return JSON.parse(file);
21
- }
22
-
23
- /**
24
- * Check if a file is a valid JSON file.
25
- */
26
- function isValidJsonFile(filePath: string): boolean {
27
- try {
28
- readJsonFile(filePath);
29
- return true;
30
- } catch {
31
- return false;
32
- }
33
- }
34
-
35
- /**
36
- * Glob all package.json paths in a workspace.
37
- * Matches Expo's implementation pattern.
38
- *
39
- * @param workspaceRoot Root file path for the workspace
40
- * @param linkedPackages List of folders that contain linked node modules, ex: `['packages/*', 'apps/*']`
41
- * @returns List of valid package.json file paths
42
- */
43
- export function globAllPackageJsonPaths(workspaceRoot: string, linkedPackages: string[]): string[] {
44
- return linkedPackages
45
- .flatMap((pattern) => {
46
- // Globs should only contain `/` as separator, even on Windows.
47
- return globSync(path.posix.join(pattern, "package.json").replace(/\\/g, "/"), {
48
- cwd: workspaceRoot,
49
- absolute: true,
50
- ignore: ["**/@(Carthage|Pods|node_modules)/**"],
51
- }).filter((pkgPath) => isValidJsonFile(pkgPath));
52
- })
53
- .map((p) => path.join(p));
54
- }
55
-
56
- /**
57
- * Resolve all workspace package.json paths.
58
- *
59
- * @param workspaceRoot root file path for a workspace.
60
- * @returns list of package.json file paths that are linked to the workspace.
61
- */
62
- export function resolveAllWorkspacePackageJsonPaths(workspaceRoot: string): string[] {
63
- try {
64
- const workspaceGlobs = getWorkspaceGlobs(workspaceRoot);
65
- if (!workspaceGlobs?.length) return [];
66
- return globAllPackageJsonPaths(workspaceRoot, workspaceGlobs);
67
- } catch {
68
- return [];
69
- }
70
- }
71
-
72
- /**
73
- * Resolve the workspace root directory.
74
- * Returns the project root if not in a workspace.
75
- */
76
- export function getWorkspaceRoot(projectRoot: string): string {
77
- try {
78
- // Dynamic import to handle the package
79
- const { resolveWorkspaceRoot } = require("resolve-workspace-root") as {
80
- resolveWorkspaceRoot: (cwd: string) => string | null;
81
- };
82
- const workspaceRoot = resolveWorkspaceRoot(projectRoot);
83
- return workspaceRoot || projectRoot;
84
- } catch {
85
- // Fallback: check for turbo.jsonc or package.json with workspaces
86
- return findMonorepoRootFallback(projectRoot);
87
- }
88
- }
89
-
90
- /**
91
- * Fallback monorepo root detection for when resolve-workspace-root fails.
92
- * Checks for turbo.jsonc or package.json with workspaces field.
93
- */
94
- function findMonorepoRootFallback(startDir: string): string {
95
- let current = startDir;
96
- while (current !== path.dirname(current)) {
97
- const turboConfig = path.join(current, "turbo.jsonc");
98
- const turboJson = path.join(current, "turbo.json");
99
- const packageJson = path.join(current, "package.json");
100
-
101
- if (existsSync(turboConfig) || existsSync(turboJson)) {
102
- return current;
103
- }
104
-
105
- if (existsSync(packageJson)) {
106
- try {
107
- const pkg = JSON.parse(readFileSync(packageJson, "utf8"));
108
- if (pkg.workspaces) {
109
- return current;
110
- }
111
- } catch {
112
- // Ignore parse errors
113
- }
114
- }
115
-
116
- current = path.dirname(current);
117
- }
118
- return startDir;
119
- }
120
-
121
- /**
122
- * Get workspace glob patterns from the workspace root.
123
- */
124
- export function getWorkspaceGlobs(workspaceRoot: string): string[] {
125
- try {
126
- const { getWorkspaceGlobs: getGlobs } = require("resolve-workspace-root") as {
127
- getWorkspaceGlobs: (cwd: string) => string[] | null;
128
- };
129
- return getGlobs(workspaceRoot) ?? [];
130
- } catch {
131
- // Fallback: try to read from package.json
132
- const packageJsonPath = path.join(workspaceRoot, "package.json");
133
- if (existsSync(packageJsonPath)) {
134
- try {
135
- const pkg = JSON.parse(readFileSync(packageJsonPath, "utf8"));
136
- if (Array.isArray(pkg.workspaces)) {
137
- return pkg.workspaces;
138
- }
139
- if (pkg.workspaces?.packages) {
140
- return pkg.workspaces.packages;
141
- }
142
- } catch {
143
- // Ignore parse errors
144
- }
145
- }
146
- return [];
147
- }
148
- }
149
-
150
- /**
151
- * Check if the project is in a monorepo (workspace root differs from project root).
152
- */
153
- export function isInMonorepo(projectRoot: string): boolean {
154
- const workspaceRoot = getWorkspaceRoot(projectRoot);
155
- return workspaceRoot !== projectRoot;
156
- }
157
-
158
- /**
159
- * Get the Metro server root for monorepo support.
160
- * This should be set as `unstable_serverRoot` in Metro config.
161
- */
162
- export function getMetroServerRoot(projectRoot: string): string {
163
- // Can be disabled with environment variable
164
- if (process.env.TEARDOWN_NO_METRO_WORKSPACE_ROOT) {
165
- return projectRoot;
166
- }
167
- return getWorkspaceRoot(projectRoot);
168
- }
169
-
170
- /**
171
- * Helper to get unique items from an array.
172
- */
173
- function uniqueItems(items: string[]): string[] {
174
- return [...new Set(items)];
175
- }
176
-
177
- /**
178
- * Get watch folders for Metro in a monorepo setup.
179
- *
180
- * Returns:
181
- * - Empty array if not in a monorepo
182
- * - Workspace root node_modules + all workspace package directories if in monorepo
183
- */
184
- export function getWatchFolders(projectRoot: string): string[] {
185
- const resolvedProjectRoot = path.resolve(projectRoot);
186
- const workspaceRoot = getMetroServerRoot(resolvedProjectRoot);
187
-
188
- // Rely on default behavior in standard projects.
189
- if (workspaceRoot === resolvedProjectRoot) {
190
- return [];
191
- }
192
-
193
- const packages = resolveAllWorkspacePackageJsonPaths(workspaceRoot);
194
- if (!packages?.length) {
195
- return [];
196
- }
197
-
198
- return uniqueItems([path.join(workspaceRoot, "node_modules"), ...packages.map((pkg) => path.dirname(pkg))]);
199
- }
200
-
201
- /**
202
- * Get node module paths for Metro resolver in a monorepo setup.
203
- *
204
- * Returns paths in priority order:
205
- * 1. Project's local node_modules
206
- * 2. Workspace root node_modules (if in monorepo)
207
- */
208
- export function getModulesPaths(projectRoot: string): string[] {
209
- const paths: string[] = [];
210
-
211
- // Only add paths if in a monorepo - minimizes chance of Metro resolver breaking
212
- const resolvedProjectRoot = path.resolve(projectRoot);
213
- const workspaceRoot = getMetroServerRoot(resolvedProjectRoot);
214
-
215
- if (workspaceRoot !== resolvedProjectRoot) {
216
- paths.push(path.resolve(projectRoot, "node_modules"));
217
- paths.push(path.resolve(workspaceRoot, "node_modules"));
218
- }
219
-
220
- return paths;
221
- }
222
-
223
- /**
224
- * Get the relative project root from workspace root.
225
- * Used for cache key generation to prevent collisions in monorepos.
226
- */
227
- export function getRelativeProjectRoot(projectRoot: string): string {
228
- const workspaceRoot = getWorkspaceRoot(projectRoot);
229
- if (workspaceRoot === projectRoot) {
230
- return "";
231
- }
232
- return path.relative(workspaceRoot, projectRoot);
233
- }
package/tsconfig.json DELETED
@@ -1,17 +0,0 @@
1
- {
2
- "extends": "@teardown/tsconfig/tsconfig.bun.json",
3
- "compilerOptions": {
4
- "outDir": "./dist",
5
- "rootDir": "./src",
6
- "module": "CommonJS",
7
- "moduleResolution": "node",
8
- "esModuleInterop": true,
9
- "declaration": true,
10
- "declarationMap": true,
11
- "emitDeclarationOnly": false,
12
- "allowImportingTsExtensions": false,
13
- "verbatimModuleSyntax": false
14
- },
15
- "include": ["src/**/*"],
16
- "exclude": ["node_modules", "dist"]
17
- }