Skip to content

Commit

Permalink
feat: add innerClientName for inner-rsdoctor
Browse files Browse the repository at this point in the history
feat: add innerClientName for inner-rsdoctor
  • Loading branch information
easy1090 committed Mar 20, 2024
1 parent 5986abf commit adacfe0
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/inner-plugins/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function normalizeUserConfig<Rules extends Linter.ExtendRuleData[]>(
sdkInstance,
reportCodeType = { noModuleSource: false, noAssetsAndModuleSource: false },
disableTOSUpload = false,
innerClientName = '',
innerClientPath = '',
} = config;

assert(linter && typeof linter === 'object');
Expand Down Expand Up @@ -90,7 +90,7 @@ export function normalizeUserConfig<Rules extends Linter.ExtendRuleData[]>(
? SDK.ToDataType.Lite
: SDK.ToDataType.Normal,
disableTOSUpload,
innerClientName,
innerClientPath,
};

return res;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface RsdoctorWebpackPluginOptions<
* The name of inner rsdoctor's client package, used by inner-rsdoctor.
* @default false
*/
innerClientName?: string;
innerClientPath?: string;
}

export interface RsdoctorWebpackMultiplePluginOptions<
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class RsdoctorRspackPlugin<Rules extends Linter.ExtendRuleData[]>
root: process.cwd(),
type: SDK.ToDataType.Normal,
config: { disableTOSUpload: this.options.disableTOSUpload },
innerClientName: this.options.innerClientName,
innerClientPath: this.options.innerClientPath,
});
this.modulesGraph = new ModuleGraph();
}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/sdk/sdk/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface RsdoctorBuilderSDK extends RsdoctorSDKOptions {
port?: number;
noServer?: boolean;
config?: SDK.SDKOptionsType;
innerClientName?: string;
innerClientPath?: string;
}

export interface RsdoctorWebpackSDKOptions extends RsdoctorBuilderSDK {}
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/sdk/sdk/webpack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export class RsdoctorWebpackSDK<
super(options);
this.server = options.noServer
? new RsdoctorFakeServer(this, undefined)
: new RsdoctorServer(this, options.port, options.innerClientName);
: new RsdoctorServer(this, options.port, options.innerClientPath);
this.type = options.type || SDK.ToDataType.Normal;
this.extraConfig = options.config;
}
Expand Down
8 changes: 3 additions & 5 deletions packages/sdk/src/sdk/server/apis/renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import path from 'path';
import { File } from '@rsdoctor/utils/build';
import { BaseAPI } from './base';
import { Router } from '../router';
import { Algorithm } from '@rsdoctor/utils/common';

export class RendererAPI extends BaseAPI {
private isClientServed = false;
Expand All @@ -14,13 +13,12 @@ export class RendererAPI extends BaseAPI {
public async entryHtml(): Promise<
SDK.ServerAPI.InferResponseType<SDK.ServerAPI.API.EntryHtml>
> {
const { server, res, req } = this.ctx;
const name = req.url && req.url.match(/innerClientName=([\w-|\=]+)/)?.[1];
const { server, res } = this.ctx;

// dynamic serve client:
// require.resolve will failed due to the dist will remove when execute "npm run build" of client.
const clientHtmlPath = name
? require.resolve(`${Algorithm.decompressText(name)}/react-client`)
const clientHtmlPath = server.innerClientPath
? server.innerClientPath
: require.resolve('@rsdoctor/client');
if (!this.isClientServed) {
this.isClientServed = true;
Expand Down
25 changes: 10 additions & 15 deletions packages/sdk/src/sdk/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,31 @@ import { Router } from './router';
import * as APIs from './apis';
import { chalk, logger } from '@rsdoctor/utils/logger';
import { openBrowser } from '@/sdk/utils/openBrowser';
import { Algorithm } from '@rsdoctor/utils/common';
export * from './utils';

export class RsdoctorServer implements SDK.RsdoctorServerInstance {
private _server!: Common.PromiseReturnType<typeof Server.createServer>;

public port: number;

public innerClientName: string;

private _socket?: Socket;

private disposed = true;

private _router: Router;

private _innerClientPath: string;

constructor(
protected sdk: SDK.RsdoctorBuilderSDKInstance,
port = Server.defaultPort,
innerClientName = '',
innerClientPath = '',
) {
assert(typeof port === 'number');
// maybe the port will be rewrite in bootstrap()
this.port = port;
this._router = new Router({ sdk, server: this, apis: Object.values(APIs) });
this.innerClientName = innerClientName;
this._innerClientPath = innerClientPath;
}

public get app(): SDK.RsdoctorServerInstance['app'] {
Expand All @@ -56,6 +55,10 @@ export class RsdoctorServer implements SDK.RsdoctorServerInstance {
return `ws://localhost:${this.port}`;
}

public get innerClientPath(): string {
return this._innerClientPath;
}

async bootstrap() {
if (!this.disposed) {
return;
Expand Down Expand Up @@ -166,21 +169,13 @@ export class RsdoctorServer implements SDK.RsdoctorServerInstance {
public getClientUrl(route?: 'homepage'): string;

public getClientUrl(route = 'homepage', ...args: unknown[]) {
const relativeUrl = this.innerClientName
? `${
SDK.ServerAPI.API.EntryHtml
}?innerClientName=${Algorithm.compressText(this.innerClientName)}`
: SDK.ServerAPI.API.EntryHtml;
const relativeUrl = SDK.ServerAPI.API.EntryHtml;

switch (route) {
case Client.RsdoctorClientRoutes.BundleDiff: {
const [baseline, current] = args as string[];
const qs = Bundle.getBundleDiffPageQueryString([baseline, current]);
return this.innerClientName
? `${relativeUrl}${qs.replace('?', '&')}#${
Client.RsdoctorClientRoutes.BundleDiff
}`
: `${relativeUrl}${qs}#${Client.RsdoctorClientRoutes.BundleDiff}`;
return `${relativeUrl}${qs}#${Client.RsdoctorClientRoutes.BundleDiff}`;
}
default:
return relativeUrl;
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/sdk/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export interface RsdoctorServerInstance {
readonly port: number;
readonly origin: string;

innerClientPath?: string;

get(
route: string,
cb: (
Expand Down
2 changes: 1 addition & 1 deletion packages/webpack-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class RsdoctorWebpackPlugin<Rules extends Linter.ExtendRuleData[]>
root: process.cwd(),
type: this.options.reportCodeType,
config: { disableTOSUpload: this.options.disableTOSUpload },
innerClientName: this.options.innerClientName,
innerClientPath: this.options.innerClientPath,
});
this.outsideInstance = Boolean(this.options.sdkInstance);
this.modulesGraph = new ModuleGraph();
Expand Down

0 comments on commit adacfe0

Please sign in to comment.