Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/weak-candles-dream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@callstack/repack-dev-server": patch
"@callstack/repack": patch
---

Drop dependency on `@react-native-community/cli-server-api` in the DevServer
11 changes: 2 additions & 9 deletions packages/dev-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,19 @@
"fastify": "^4.24.3",
"fastify-favicon": "^4.3.0",
"fastify-plugin": "^4.5.1",
"launch-editor": "^2.10.0",
"open": "^10.1.0",
"pretty-format": "^28.1.0",
"source-map": "^0.7.4",
"ws": "^8.7.0"
},
"devDependencies": {
"@babel/cli": "^7.23.9",
"@babel/core": "^7.23.9",
"@react-native-community/cli-server-api": "15.0.1",
"@types/babel__code-frame": "^7.0.3",
"@types/node": "catalog:",
"@types/ws": "^8.5.3",
"babel-plugin-add-import-extension": "^1.6.0",
"typescript": "catalog:"
},
"peerDependencies": {
"@react-native-community/cli-server-api": ">=13.6.4"
},
"peerDependenciesMeta": {
"@react-native-community/cli-server-api": {
"optional": true
}
}
}
58 changes: 43 additions & 15 deletions packages/dev-server/src/plugins/devtools/devtoolsPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
import {
openStackFrameInEditorMiddleware,
openURLMiddleware,
} from '@react-native-community/cli-server-api';
import type { FastifyInstance } from 'fastify';
import fastifyPlugin from 'fastify-plugin';
import launchEditor from 'launch-editor';
import open from 'open';

async function devtoolsPlugin(
instance: FastifyInstance,
{ rootDir }: { rootDir: string }
) {
instance.use('/open-url', openURLMiddleware);
interface OpenURLRequestBody {
url: string;
}

interface OpenStackFrameRequestBody {
file: string;
lineNumber: number;
}

function parseRequestBody<T>(body: unknown): T {
if (typeof body === 'object') return body as T;
if (typeof body === 'string') return JSON.parse(body) as T;
throw new Error(`Unsupported body type: ${typeof body}`);
}

async function devtoolsPlugin(instance: FastifyInstance) {
// reference implementation in `@react-native-community/cli-server-api`:
// https://github.com/react-native-community/cli/blob/46436a12478464752999d34ed86adf3212348007/packages/cli-server-api/src/openURLMiddleware.ts
instance.route({
method: ['POST'],
url: '/open-url',
handler: async (request, reply) => {
const { url } = parseRequestBody<OpenURLRequestBody>(request.body);
await open(url);
reply.send('OK');
},
});

instance.use(
'/open-stack-frame',
openStackFrameInEditorMiddleware({
watchFolders: [rootDir],
})
);
// reference implementation in `@react-native-community/cli-server-api`:
// https://github.com/react-native-community/cli/blob/46436a12478464752999d34ed86adf3212348007/packages/cli-server-api/src/openStackFrameMiddleware.ts
instance.route({
method: ['POST'],
url: '/open-stack-frame',
handler: async (request, reply) => {
const { file, lineNumber } = parseRequestBody<OpenStackFrameRequestBody>(
request.body
);
// TODO fix rewriting of `webpack://` to rootDir of the project
launchEditor(`${file}:${lineNumber}`, process.env.REACT_EDITOR);
reply.send('OK');
},
});

instance.route({
method: ['GET', 'POST', 'PUT'],
Expand Down
87 changes: 84 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading