Skip to content

Commit 2226ca7

Browse files
committed
feat: parse projectRoot paths
1 parent f684761 commit 2226ca7

13 files changed

Lines changed: 130 additions & 96 deletions

File tree

apps/tester-federation-v2/ios/Podfile.lock

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
PODS:
22
- boost (1.84.0)
3-
- callstack-repack (5.0.6):
3+
- callstack-repack (5.1.1):
44
- DoubleConversion
55
- glog
66
- hermes-engine
@@ -2109,12 +2109,12 @@ EXTERNAL SOURCES:
21092109

21102110
SPEC CHECKSUMS:
21112111
boost: 7e761d76ca2ce687f7cc98e698152abd03a18f90
2112-
callstack-repack: 0f50dfdbd14d23303ce2d7a1740219910e97e1de
2112+
callstack-repack: dea0d9a2ed51c0d32c0008893224c224a82de370
21132113
DoubleConversion: cb417026b2400c8f53ae97020b2be961b59470cb
21142114
fast_float: 06eeec4fe712a76acc9376682e4808b05ce978b6
21152115
FBLazyVector: abbac80c6f89e71a8c55c7e92ec015c8a9496753
21162116
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
2117-
glog: eb93e2f488219332457c3c4eafd2738ddc7e80b8
2117+
glog: 5683914934d5b6e4240e497e0f4a3b42d1854183
21182118
hermes-engine: c32f2e405098bc1ebe30630a051ddce6f21d3c2e
21192119
JWTDecode: 2eed97c2fa46ccaf3049a787004eedf0be474a87
21202120
RCT-Folly: 36fe2295e44b10d831836cc0d1daec5f8abcf809

packages/dev-server/src/createServer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ export async function createServer(config: Server.Config) {
126126
delegate,
127127
});
128128
await instance.register(devtoolsPlugin, {
129-
rootDir: options.rootDir,
129+
delegate,
130130
});
131131
await instance.register(symbolicatePlugin, {
132132
delegate,

packages/dev-server/src/plugins/compiler/compilerPlugin.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ async function compilerPlugin(
2121
},
2222
},
2323
handler: async (request, reply) => {
24-
const filename = (request.params as { '*'?: string })['*'];
24+
const filepath = (request.params as { '*'?: string })['*'];
2525
let { platform } = request.query as { platform?: string };
2626

27-
if (!filename) {
27+
if (!filepath) {
2828
// This technically should never happen - this route should not be called if file is missing.
2929
request.log.debug('File was not provided');
3030
return reply.notFound('File was not provided');
@@ -49,12 +49,12 @@ async function compilerPlugin(
4949

5050
try {
5151
const asset = await delegate.compiler.getAsset(
52-
filename,
52+
filepath,
5353
platform,
5454
sendProgress
5555
);
5656
const mimeType = delegate.compiler.getMimeType(
57-
filename,
57+
filepath,
5858
platform,
5959
asset
6060
);

packages/dev-server/src/plugins/devtools/devtoolsPlugin.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import type { FastifyInstance } from 'fastify';
22
import fastifyPlugin from 'fastify-plugin';
33
import launchEditor from 'launch-editor';
44
import open from 'open';
5-
import { parseSourceFilename } from '../../utils/parseSourceFilename.js';
5+
import type { Server } from '../../types.js';
66

77
interface OpenURLRequestBody {
88
url: string;
@@ -21,7 +21,7 @@ function parseRequestBody<T>(body: unknown): T {
2121

2222
async function devtoolsPlugin(
2323
instance: FastifyInstance,
24-
{ rootDir }: { rootDir: string }
24+
{ delegate }: { delegate: Server.Delegate }
2525
) {
2626
// reference implementation in `@react-native-community/cli-server-api`:
2727
// https://github.com/react-native-community/cli/blob/46436a12478464752999d34ed86adf3212348007/packages/cli-server-api/src/openURLMiddleware.ts
@@ -42,7 +42,8 @@ async function devtoolsPlugin(
4242
url: '/open-stack-frame',
4343
handler: async (request, reply) => {
4444
const body = parseRequestBody<OpenStackFrameRequestBody>(request.body);
45-
const filepath = parseSourceFilename(body.file, rootDir);
45+
const filepath =
46+
delegate.devTools?.resolveProjectPath(body.file) ?? body.file;
4647
launchEditor(`${filepath}:${body.lineNumber}`, process.env.REACT_EDITOR);
4748
reply.send('OK');
4849
},

packages/dev-server/src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ export namespace Server {
7777
/** A compiler delegate. */
7878
compiler: CompilerDelegate;
7979

80+
/** A DevTools delegate. */
81+
devTools?: DevToolsDelegate;
82+
8083
/** A symbolicator delegate. */
8184
symbolicator: SymbolicatorDelegate;
8285

@@ -139,6 +142,19 @@ export namespace Server {
139142
onMessage: (log: any) => void;
140143
}
141144

145+
/**
146+
* Delegate with implementation for dev tools functions.
147+
*/
148+
export interface DevToolsDelegate {
149+
/**
150+
* Resolve the project filepath with [projectRoot] prefix.
151+
*
152+
* @param filepath The filepath to resolve.
153+
* @returns The resolved project path.
154+
*/
155+
resolveProjectPath: (filepath: string) => string;
156+
}
157+
142158
/**
143159
* Delegate with implementation for messages used in route handlers.
144160
*/

packages/dev-server/src/utils/parseSourceFilename.ts

Lines changed: 0 additions & 22 deletions
This file was deleted.

packages/repack/src/commands/common/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
export * from './adaptFilenameToPlatform.js';
22
export * from './getMimeType.js';
3+
export * from './resolveProjectPath.js';
34
export * from './runAdbReverse.js';
4-
export * from './parseFileUrl.js';
5+
export * from './parseUrl.js';
56
export * from './resetPersistentCache.js';
67
export * from './setupInteractions.js';
78
export * from './setupStatsWriter.js';

packages/repack/src/commands/common/parseFileUrl.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export function parseUrl(url: string, platforms: string[], base = 'file:///') {
2+
const { pathname, searchParams } = new URL(url, base);
3+
4+
let path = pathname;
5+
let platform = searchParams.get('platform');
6+
7+
if (!platform) {
8+
const pathArray = pathname.split('/');
9+
const platformFromPath = pathArray[1];
10+
11+
if (platforms.includes(platformFromPath)) {
12+
platform = platformFromPath;
13+
path = pathArray.slice(2).join('/');
14+
}
15+
}
16+
17+
if (!platform) {
18+
const [, platformOrName, name] = path.split('.').reverse();
19+
if (name !== undefined) {
20+
platform = platformOrName;
21+
}
22+
}
23+
24+
return {
25+
resourcePath: path.replace(/^\//, ''),
26+
platform: platform || undefined,
27+
};
28+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import path from 'node:path';
2+
3+
const projectRootPattern = /^\[projectRoot(?:\^(\d+))?\]$/;
4+
5+
function isProjectPath(filepath: string) {
6+
const root = filepath.split('/')[0];
7+
return root.match(projectRootPattern);
8+
}
9+
10+
// Resolve [projectRoot] and [projectRoot^N] prefixes
11+
export function resolveProjectPath(filepath: string, rootDir: string) {
12+
const match = isProjectPath(filepath);
13+
if (!match) return filepath;
14+
15+
const [prefix, upLevels] = match;
16+
const upPath = '../'.repeat(Number(upLevels ?? 0));
17+
const rootPath = path.join(rootDir, upPath);
18+
return path.resolve(filepath.replace(prefix, rootPath));
19+
}

0 commit comments

Comments
 (0)