Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(devtools): preparation for integration of rsdoctor #5335

Merged
merged 8 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion packages/devtools/client/modern.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path';
import { appTools, defineConfig } from '@modern-js/app-tools';
import { nanoid } from '@modern-js/utils';
import { ROUTE_BASENAME } from '@modern-js/devtools-kit';
import { ROUTE_BASENAME } from '@modern-js/devtools-kit/runtime';
import { ServiceWorkerCompilerPlugin } from './plugins/ServiceWorkerCompilerPlugin';
import packageMeta from './package.json';

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SetupClientParams } from '@modern-js/devtools-kit';
import { SetupClientParams } from '@modern-js/devtools-kit/runtime';
import { Flex, Theme } from '@radix-ui/themes';
import React, { useEffect, useState } from 'react';
import { useEvent, useToggle } from 'react-use';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FileSystemRoutes } from '@modern-js/devtools-kit';
import { FileSystemRoutes } from '@modern-js/devtools-kit/runtime';
import type { RouteLegacy, ServerRoute } from '@modern-js/types';
import _ from 'lodash';
import React, { Suspense } from 'react';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
ServerFunctions,
ClientFunctions as ToServerFunctions,
WebSocketChannel,
} from '@modern-js/devtools-kit';
} from '@modern-js/devtools-kit/runtime';
import { createBirpc } from 'birpc';
import { createHooks } from 'hookable';
import {
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/src/entries/client/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type {
CustomTabView,
NameDefinition,
PackageDefinition,
} from '@modern-js/devtools-kit';
} from '@modern-js/devtools-kit/runtime';
import { ReactElement, ReactNode } from 'react';

export interface BreadcrumbItem {
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/src/entries/mount/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import './react-devtools-backend';
import './state';
import { createRoot } from 'react-dom/client';
import { SetupClientParams } from '@modern-js/devtools-kit';
import { SetupClientParams } from '@modern-js/devtools-kit/runtime';
import styles from './index.module.scss';
import { DevtoolsCapsule } from '@/components/Devtools/Capsule';

Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/client/src/entries/mount/state.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessagePortChannel } from '@modern-js/devtools-kit';
import { MessagePortChannel } from '@modern-js/devtools-kit/runtime';
import { createBirpc } from 'birpc';
import { createHooks } from 'hookable';
import createDeferred from 'p-defer';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
MessagePortChannel,
PostMessageListener,
PostMessageTarget,
} from '@modern-js/devtools-kit';
} from '@modern-js/devtools-kit/runtime';
import { WallAgent } from '../../src/utils/react-devtools';

beforeAll(() => {
Expand Down
33 changes: 27 additions & 6 deletions packages/devtools/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@
"default": "./dist/index.js"
},
"./runtime": {
"jsnext:source": "./src/runtime/index.ts",
"types": "./dist/runtime/index.d.ts",
"default": "./dist/runtime/index.js"
"jsnext:source": "./src/runtime.ts",
"types": "./dist/runtime.d.ts",
"default": "./dist/runtime.js"
},
"./node": {
"jsnext:source": "./src/node.ts",
"types": "./dist/node.d.ts",
"default": "./dist/node.js"
},
"./package.json": {
"default": "./package.json"
Expand All @@ -43,7 +48,10 @@
"typesVersions": {
"*": {
"runtime": [
"./dist/runtime/index.d.ts"
"./dist/runtime.d.ts"
],
"node": [
"./dist/node.d.ts"
]
}
},
Expand All @@ -60,6 +68,7 @@
"birpc": "0.2.13",
"hookable": "^5.5.3",
"ufo": "^1.3.0",
"flatted": "^3.2.9",
"ws": "^8.13.0"
},
"devDependencies": {
Expand All @@ -81,10 +90,22 @@
"nanoid": "3.3.4",
"react": "~18.2.0",
"type-fest": "^4.1.0",
"typescript": "^5"
"typescript": "^5",
"@rsdoctor/types": "^0.1.0",
"@rsdoctor/utils": "^0.1.0"
},
"peerDependencies": {
"react": "~18.2.0"
"react": "~18.2.0",
"@rsdoctor/types": "^0.1.0",
"@rsdoctor/utils": "^0.1.0"
},
"peerDependenciesMeta": {
"@rsdoctor/types": {
"optional": true
},
"@rsdoctor/utils": {
"optional": true
}
},
"sideEffects": false,
"publishConfig": {
Expand Down
5 changes: 3 additions & 2 deletions packages/devtools/kit/src/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { nanoid } from 'nanoid';
import * as flatted from 'flatted';
import type { ChannelOptions } from 'birpc';

export class WebSocketChannel implements ChannelOptions {
Expand Down Expand Up @@ -53,11 +54,11 @@ export class WebSocketChannel implements ChannelOptions {
}

serialize(data: unknown): string {
return JSON.stringify(data);
return flatted.stringify([data]);
}

deserialize(e: MessageEvent): unknown {
return JSON.parse(e.data.toString());
return flatted.parse(e.data.toString())[0];
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/kit/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable max-classes-per-file */
import { Entrypoint } from '@modern-js/types/cli';
import type { Entrypoint } from '@modern-js/types';
import logo from './logo';
import { FileSystemRoutes } from './server';

Expand Down
7 changes: 1 addition & 6 deletions packages/devtools/kit/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
export * from './server';
export * from './client';
export * from './mount-point';
export * from './utils';
export * from './constants';
export * from './channel';
export * from './node';
7 changes: 7 additions & 0 deletions packages/devtools/kit/src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export * from './server';
export * from './client';
export * from './mount-point';
export * from './utils';
export * from './constants';
export * from './channel';
export * from './rsdoctor';
37 changes: 37 additions & 0 deletions packages/devtools/kit/src/rsdoctor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Manifest } from '@rsdoctor/types';

export async function fetchShardingFile(url: string): Promise<string> {
const { Url } = await import('@rsdoctor/utils/common');
if (Url.isUrl(url)) {
const resp = await fetch(url);
return resp.text();
}
// json string
return url;
}

export async function parseManifest(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where do we use this method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is used for parsing manifest stats of rsdoctor.
Pasted from rsdoctor and will be used in the next PR.
For now it just a placeholder.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Maybe we should discuss with Rsdoctor to get a public API for this.
  2. So why this placeholder is added to this PR not next PR.

Copy link
Contributor Author

@Asuka109 Asuka109 Jan 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I removed it from this PR.

json: Manifest.RsdoctorManifestWithShardingFiles,
) {
const utils = await import('@rsdoctor/utils/common');
let transformedData: Manifest.RsdoctorManifestData;

// try to load cloud data first
if (json.data) {
try {
transformedData = await utils.Manifest.fetchShardingFiles(
json.data,
fetchShardingFile,
);
} catch (error) {
console.log('cloudData load error: ', error);
}
} else {
throw new Error('fallback to load json.data');
}

return {
...json,
data: transformedData!,
};
}
7 changes: 7 additions & 0 deletions packages/devtools/kit/src/runtime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export type * from './server';
export * from './client';
export * from './mount-point';
export * from './utils';
export * from './constants';
export * from './channel';
export type * from './rsdoctor';
6 changes: 1 addition & 5 deletions packages/devtools/kit/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ import type {
RspackConfig,
} from '@modern-js/uni-builder';
import { NormalizedConfig } from '@modern-js/core';
import {
RouteLegacy,
NestedRouteForCli,
PageRoute,
} from '@modern-js/types/cli';
import { RouteLegacy, NestedRouteForCli, PageRoute } from '@modern-js/types';
import type { ClientDefinition } from './client';

export type BuilderContext = RsbuildContext;
Expand Down
2 changes: 2 additions & 0 deletions packages/devtools/kit/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
"extends": "@modern-js/tsconfig/base",
"compilerOptions": {
"target": "ES2019",
"module": "ES2020",
"declaration": true,
"outDir": "./dist",
"jsx": "preserve",
"baseUrl": "./",
"isolatedModules": true,
"moduleResolution": "bundler",
"paths": {},
"skipLibCheck": true
},
Expand Down
1 change: 1 addition & 0 deletions packages/devtools/plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"birpc": "0.2.13",
"@modern-js/devtools-kit": "workspace:*",
"@modern-js/devtools-client": "workspace:*",
"flatted": "^3.2.9",
"@modern-js/utils": "workspace:*",
"serve-static": "^1.14.1",
"p-defer": "^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/plugin/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { ProxyDetail } from '@modern-js/types';
import { getPort, logger } from '@modern-js/utils';
import createServeMiddleware from 'serve-static';
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
import { ROUTE_BASENAME } from '@modern-js/devtools-kit';
import { ROUTE_BASENAME } from '@modern-js/devtools-kit/node';
import {
DevtoolsPluginOptions,
DevtoolsPluginInlineOptions,
Expand Down
2 changes: 1 addition & 1 deletion packages/devtools/plugin/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
ClientDefinition,
ROUTE_BASENAME,
SetupClientParams,
} from '@modern-js/devtools-kit';
} from '@modern-js/devtools-kit/node';
import type { RequiredDeep } from 'type-fest';

export type DevtoolsPluginOptions = Partial<SetupClientParams>;
Expand Down
7 changes: 4 additions & 3 deletions packages/devtools/plugin/src/rpc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import {
type FileSystemRoutes,
type NormalizedBuilderConfig,
type ServerFunctions,
} from '@modern-js/devtools-kit';
} from '@modern-js/devtools-kit/node';
import type { JsonValue, PartialDeep } from 'type-fest';
import { createBirpc, BirpcOptions } from 'birpc';
import * as flatted from 'flatted';
import createDeferPromise from 'p-defer';
import { RawData } from 'ws';
import type { RsbuildContext, RsbuildPlugin } from '@modern-js/uni-builder';
Expand Down Expand Up @@ -150,8 +151,8 @@ export const setupClientConnection = async (
post: data =>
onceConnection.then(() => server.clients.forEach(ws => ws.send(data))),
on: cb => (handleMessage = cb),
serialize: v => JSON.stringify(v),
deserialize: v => JSON.parse(v.toString()),
serialize: v => flatted.stringify([v]),
deserialize: v => flatted.parse(v.toString())[0],
};

const clientConn = createBirpc<ClientFunctions, ServerFunctions>(
Expand Down
Loading
Loading