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
5 changes: 5 additions & 0 deletions .changeset/shy-worlds-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@callstack/repack": minor
---

Support `RSPACK_PROFILE` env var for obtaining traces from Rspack
1 change: 1 addition & 0 deletions packages/repack/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
"estree-util-is-identifier-name": "^1.1.0",
"events": "^3.3.0",
"execa": "^5.0.0",
"exit-hook": "^4.0.0",
"flow-remove-types": "^2.268.0",
"gradient-string": "^2.0.2",
"image-size": "^1.1.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ describe('setupInteractions', () => {
} as unknown as Logger;

mockProcess = {
on: (event: string) => {
if (event === 'SIGINT') {
mockProcess.exit();
}
},
stdin: {
setRawMode: jest.fn(),
on: jest.fn(),
Expand Down
7 changes: 6 additions & 1 deletion packages/repack/src/commands/common/setupInteractions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,17 @@ export function setupInteractions(
readline.emitKeypressEvents(process.stdin);
process.stdin.setRawMode(true);

// graceful shutdown
process.on('SIGINT', () => {
process.exit();
});

process.stdin.on('keypress', (_key, data) => {
const { ctrl, name } = data;
if (ctrl === true) {
switch (name) {
case 'c':
process.exit();
process.emit('SIGINT', 'SIGINT');
break;

case 'z':
Expand Down
75 changes: 75 additions & 0 deletions packages/repack/src/commands/rspack/profile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* Reference: https://github.com/web-infra-dev/rspack/blob/bad70431988d77d8a95320066f72c77b6cc4c120/packages/rspack-cli/src/utils/profile.ts
*/

/**
* `RSPACK_PROFILE=ALL` // all trace events
* `RSPACK_PROFILE=OVERVIEW` // overview trace events
* `RSPACK_PROFILE=warn,tokio::net=info` // trace filter from https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#example-syntax
*/
import fs from 'node:fs';
import path from 'node:path';
import { rspack } from '@rspack/core';

const overviewTraceFilter = 'info';
const allTraceFilter = 'trace';
const defaultRustTraceLayer = 'chrome';

function resolveLayer(value: string) {
if (value === 'OVERVIEW') {
return overviewTraceFilter;
}
if (value === 'ALL') {
return allTraceFilter;
}
return value;
}

export async function applyProfile(
filterValue: string,
traceLayer: string = defaultRustTraceLayer,
traceOutput?: string
) {
const { asyncExitHook } = await import('exit-hook');

if (traceLayer !== 'chrome' && traceLayer !== 'logger') {
throw new Error(`unsupported trace layer: ${traceLayer}`);
}

if (!traceOutput) {
const timestamp = Date.now();
const defaultOutputDir = path.resolve(
`.rspack-profile-${timestamp}-${process.pid}`
);
const defaultRustTraceChromeOutput = path.join(
defaultOutputDir,
'trace.json'
);
const defaultRustTraceLoggerOutput = 'stdout';

const defaultTraceOutput =
traceLayer === 'chrome'
? defaultRustTraceChromeOutput
: defaultRustTraceLoggerOutput;

// biome-ignore lint/style/noParameterAssign: setting default value makes sense
traceOutput = defaultTraceOutput;
}

const filter = resolveLayer(filterValue);

await ensureFileDir(traceOutput);
await rspack.experiments.globalTrace.register(
filter,
traceLayer,
traceOutput
);
asyncExitHook(rspack.experiments.globalTrace.cleanup, {
wait: 500,
});
}

async function ensureFileDir(outputFilePath: string) {
const dir = path.dirname(outputFilePath);
await fs.promises.mkdir(dir, { recursive: true });
}
9 changes: 9 additions & 0 deletions packages/repack/src/commands/rspack/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ export async function start(
});
}

if (process.env.RSPACK_PROFILE) {
const { applyProfile } = await import('./profile.js');
await applyProfile(
process.env.RSPACK_PROFILE,
process.env.RSPACK_TRACE_LAYER,
process.env.RSPACK_TRACE_OUTPUT
);
}

const compiler = new Compiler(configs, reporter, cliConfig.root);

const { createServer } = await import('@callstack/repack-dev-server');
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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