@rspack/cli 0.2.11 → 0.2.12-canary-8aa969c-20230816005959

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -15,7 +15,9 @@ const core_1 = require("@rspack/core");
15
15
  const options_1 = require("./utils/options");
16
16
  const loadConfig_1 = require("./utils/loadConfig");
17
17
  const findConfig_1 = __importDefault(require("./utils/findConfig"));
18
+ const core_2 = require("@rspack/core");
18
19
  const path_1 = __importDefault(require("path"));
20
+ const profile_1 = require("./utils/profile");
19
21
  const defaultEntry = "src/index";
20
22
  class RspackCLI {
21
23
  constructor() {
@@ -115,6 +117,22 @@ class RspackCLI {
115
117
  }
116
118
  });
117
119
  }
120
+ if (process.env.RSPACK_PROFILE) {
121
+ const { default: exitHook } = await import("exit-hook");
122
+ Object.entries((0, profile_1.resolveProfile)(process.env.RSPACK_PROFILE)).forEach(([kind, value]) => {
123
+ var _a, _b;
124
+ if (kind === "TRACE" && "filter" in value) {
125
+ (0, core_2.experimental_registerGlobalTrace)(value.filter, value.layer, value.output);
126
+ exitHook(core_2.experimental_cleanupGlobalTrace);
127
+ }
128
+ else if (kind === "JSCPU") {
129
+ ((_a = item.plugins) !== null && _a !== void 0 ? _a : (item.plugins = [])).push(new profile_1.RspackProfileJSCPUProfilePlugin(value.output));
130
+ }
131
+ else if (kind === "LOGGING") {
132
+ ((_b = item.plugins) !== null && _b !== void 0 ? _b : (item.plugins = [])).push(new profile_1.RspackProfileLoggingPlugin(value.output));
133
+ }
134
+ });
135
+ }
118
136
  // cli --watch overrides the watch config
119
137
  if (options.watch) {
120
138
  item.watch = options.watch;
@@ -0,0 +1,35 @@
1
+ import { experimental_registerGlobalTrace as registerGlobalTrace } from "@rspack/core";
2
+ type JSCPUProfileOptionsOutput = string;
3
+ export type JSCPUProfileOptions = {
4
+ output: JSCPUProfileOptionsOutput;
5
+ };
6
+ type ParametersOfRegisterGlobalTrace = Parameters<typeof registerGlobalTrace>;
7
+ type RustTraceOptionsFilter = ParametersOfRegisterGlobalTrace[0];
8
+ type RustTraceOptionsLayer = ParametersOfRegisterGlobalTrace[1];
9
+ type RustTraceOptionsOutput = ParametersOfRegisterGlobalTrace[2];
10
+ export type RustTraceOptions = {
11
+ filter: RustTraceOptionsFilter;
12
+ layer: RustTraceOptionsLayer;
13
+ output: RustTraceOptionsOutput;
14
+ };
15
+ type LoggingOutputOptions = string;
16
+ export type LoggingOptions = {
17
+ output: LoggingOutputOptions;
18
+ };
19
+ export type ProfileOptions = {
20
+ TRACE?: RustTraceOptions;
21
+ JSCPU?: JSCPUProfileOptions;
22
+ LOGGING?: LoggingOptions;
23
+ };
24
+ export declare function resolveProfile(value: string): ProfileOptions;
25
+ export declare class RspackProfileJSCPUProfilePlugin {
26
+ private output;
27
+ constructor(output: string);
28
+ apply(compiler: any): void;
29
+ }
30
+ export declare class RspackProfileLoggingPlugin {
31
+ private output;
32
+ constructor(output: string);
33
+ apply(compiler: any): void;
34
+ }
35
+ export {};
@@ -0,0 +1,137 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.RspackProfileLoggingPlugin = exports.RspackProfileJSCPUProfilePlugin = exports.resolveProfile = void 0;
7
+ const inspector_1 = __importDefault(require("inspector"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ const url_1 = require("url");
10
+ const defaultJSCPUProfileOutput = `./rspack.jscpuprofile`;
11
+ const defaultRustTraceChromeOutput = `./rspack.trace`;
12
+ const defaultRustTraceLoggerOutput = `stdout`;
13
+ const defaultRustTraceFilter = "trace";
14
+ const defaultRustTraceLayer = "chrome";
15
+ const defaultLoggingOutput = `./rspack.logging`;
16
+ function resolveProfile(value) {
17
+ if (value.toUpperCase() === "ALL") {
18
+ return {
19
+ TRACE: {
20
+ filter: defaultRustTraceFilter,
21
+ layer: defaultRustTraceLayer,
22
+ output: defaultRustTraceChromeOutput
23
+ },
24
+ JSCPU: { output: defaultJSCPUProfileOutput },
25
+ LOGGING: { output: defaultLoggingOutput }
26
+ };
27
+ }
28
+ if (value.startsWith("[") && value.endsWith("]")) {
29
+ return {
30
+ TRACE: resolveRustTraceOptions(value.slice(1, value.length - 1)),
31
+ JSCPU: { output: defaultJSCPUProfileOutput },
32
+ LOGGING: { output: defaultLoggingOutput }
33
+ };
34
+ }
35
+ return value.split("|").reduce((acc, cur) => {
36
+ if (cur.toUpperCase().startsWith("TRACE")) {
37
+ acc.TRACE = resolveRustTraceOptions(cur.slice(6));
38
+ }
39
+ if (cur.toUpperCase().startsWith("JSCPU")) {
40
+ acc.JSCPU = resolveJSCPUProfileOptions(cur.slice(6));
41
+ }
42
+ if (cur.toUpperCase().startsWith("LOGGING")) {
43
+ acc.LOGGING = resolveLoggingOptions(cur.slice(8));
44
+ }
45
+ return acc;
46
+ }, {});
47
+ }
48
+ exports.resolveProfile = resolveProfile;
49
+ // JSCPU=value
50
+ function resolveJSCPUProfileOptions(value) {
51
+ // output=filepath
52
+ if (value.includes("=")) {
53
+ const parsed = new url_1.URLSearchParams(value);
54
+ return { output: parsed.get("output") || defaultJSCPUProfileOutput };
55
+ }
56
+ // filepath
57
+ return { output: value || defaultJSCPUProfileOutput };
58
+ }
59
+ // TRACE=value
60
+ function resolveRustTraceOptions(value) {
61
+ // filter=trace&output=stdout&layer=logger
62
+ if (value.includes("=")) {
63
+ const parsed = new url_1.URLSearchParams(value);
64
+ const filter = parsed.get("filter") || defaultRustTraceFilter;
65
+ const layer = parsed.get("layer") || defaultRustTraceLayer;
66
+ const output = layer === "chrome"
67
+ ? parsed.get("output") || defaultRustTraceChromeOutput
68
+ : parsed.get("output") || defaultRustTraceLoggerOutput;
69
+ if (layer !== "chrome" && layer !== "logger") {
70
+ throw new Error(`${layer} is not a valid layer, should be chrome or logger`);
71
+ }
72
+ return {
73
+ filter,
74
+ layer,
75
+ output
76
+ };
77
+ }
78
+ // trace
79
+ return {
80
+ filter: value || defaultRustTraceFilter,
81
+ layer: defaultRustTraceLayer,
82
+ output: defaultRustTraceChromeOutput
83
+ };
84
+ }
85
+ // LOGGING=value
86
+ function resolveLoggingOptions(value) {
87
+ // output=filepath
88
+ if (value.includes("=")) {
89
+ const parsed = new url_1.URLSearchParams(value);
90
+ return { output: parsed.get("output") || defaultLoggingOutput };
91
+ }
92
+ // filepath
93
+ return { output: value || defaultLoggingOutput };
94
+ }
95
+ class RspackProfileJSCPUProfilePlugin {
96
+ constructor(output) {
97
+ this.output = output;
98
+ }
99
+ apply(compiler) {
100
+ const session = new inspector_1.default.Session();
101
+ session.connect();
102
+ session.post("Profiler.enable");
103
+ session.post("Profiler.start");
104
+ compiler.hooks.done.tapAsync(RspackProfileJSCPUProfilePlugin.name, (stats, callback) => {
105
+ if (compiler.watchMode)
106
+ return callback();
107
+ session.post("Profiler.stop", (error, param) => {
108
+ if (error) {
109
+ console.error("Failed to generate JS CPU profile:", error);
110
+ return;
111
+ }
112
+ fs_1.default.writeFileSync(this.output, JSON.stringify(param.profile));
113
+ });
114
+ return callback();
115
+ });
116
+ }
117
+ }
118
+ exports.RspackProfileJSCPUProfilePlugin = RspackProfileJSCPUProfilePlugin;
119
+ class RspackProfileLoggingPlugin {
120
+ constructor(output) {
121
+ this.output = output;
122
+ }
123
+ apply(compiler) {
124
+ compiler.hooks.done.tapAsync(RspackProfileLoggingPlugin.name, (stats, callback) => {
125
+ if (compiler.watchMode)
126
+ return callback();
127
+ const logging = stats.toJson({
128
+ all: false,
129
+ logging: "verbose",
130
+ loggingTrace: true
131
+ });
132
+ fs_1.default.writeFileSync(this.output, JSON.stringify(logging));
133
+ return callback();
134
+ });
135
+ }
136
+ }
137
+ exports.RspackProfileLoggingPlugin = RspackProfileLoggingPlugin;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rspack/cli",
3
- "version": "0.2.11",
3
+ "version": "0.2.12-canary-8aa969c-20230816005959",
4
4
  "license": "MIT",
5
5
  "description": "CLI for rspack",
6
6
  "bin": {
@@ -31,13 +31,14 @@
31
31
  "dependencies": {
32
32
  "@discoveryjs/json-ext": "^0.5.7",
33
33
  "colorette": "2.0.19",
34
+ "exit-hook": "^3.2.0",
34
35
  "interpret": "^3.1.1",
35
36
  "rechoir": "^0.8.0",
36
37
  "semver": "6.3.0",
37
38
  "webpack-bundle-analyzer": "4.6.1",
38
39
  "yargs": "17.6.2",
39
- "@rspack/core": "0.2.11",
40
- "@rspack/dev-server": "0.2.11"
40
+ "@rspack/core": "0.2.12-canary-8aa969c-20230816005959",
41
+ "@rspack/dev-server": "0.2.12-canary-8aa969c-20230816005959"
41
42
  },
42
43
  "scripts": {
43
44
  "build": "rimraf dist/ && tsc -b ./tsconfig.build.json --force",