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: stats for timings and builtAt #2425

Merged
merged 5 commits into from
Mar 22, 2023
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/stale-ways-drum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@rspack/core": patch
"@rspack/cli": patch
---

feat: stats for timings and builtAt
3 changes: 1 addition & 2 deletions packages/rspack-cli/src/commands/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,11 @@ export class BuildCommand implements RspackCommand {
}
}
};
console.time("build");

let rspackOptions = { ...options, argv: { ...options } };

const errorHandler = (err, Stats) => {
callback(err, Stats);
console.timeEnd("build");
};

const compiler = await cli.createCompiler(
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack-cli/src/rspack-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ export class RspackCLI {
}

if (typeof item.stats === "undefined") {
item.stats = { preset: "errors-warnings" };
item.stats = { preset: "errors-warnings", timings: true };
} else if (typeof item.stats === "boolean") {
item.stats = item.stats ? { preset: "normal" } : { preset: "none" };
} else if (typeof item.stats === "string") {
Expand Down
35 changes: 20 additions & 15 deletions packages/rspack/scripts/precompile-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,23 @@ ajv.addKeyword({

const validate = ajv.compile(require(configSchema));
const code = standaloneCode(ajv, validate);
terser
.minify(code, {
compress: {
passes: 3
},
mangle: true,
ecma: 2015,
toplevel: true
})
.then(minified => {
const code =
"/** This file was automatically generated, Run `pnpm precompile-schema` to update */\n" +
minified.code;
fs.promises.writeFile(configCheck, code);
});
const generated =
"/** This file was automatically generated, Run `pnpm precompile-schema` to update */\n" +
code;
fs.writeFileSync(configCheck, generated);

// terser
// .minify(code, {
// compress: {
// passes: 3
// },
// mangle: true,
// ecma: 2015,
// toplevel: true
// })
// .then(minified => {
// const code =
// "/** This file was automatically generated, Run `pnpm precompile-schema` to update */\n" +
// minified.code;
// fs.promises.writeFile(configCheck, code);
// });
9 changes: 9 additions & 0 deletions packages/rspack/src/compilation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,14 @@ export class Compilation {
inputFileSystem: any;
logging: Map<string, LogEntry[]>;
name?: string;
startTime?: number;
endTime?: number;
normalModuleFactory?: NormalModuleFactory;

constructor(compiler: Compiler, inner: JsCompilation) {
this.name = undefined;
this.startTime = undefined;
this.endTime = undefined;
let processAssetsHooks = createFakeProcessAssetsHook(this);
this.hooks = {
processAssets: processAssetsHooks,
Expand Down Expand Up @@ -227,6 +231,11 @@ export class Compilation {
options.outputPath,
!context.forToString
);
options.timings = optionOrLocalFallback(options.timings, true);
options.builtAt = optionOrLocalFallback(
options.builtAt,
!context.forToString
);

return options;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/rspack/src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ class Compiler {
if (this.running) {
return callback(new ConcurrentCompilationError());
}
const startTime = Date.now();
this.running = true;
const doRun = () => {
// @ts-expect-error
Expand All @@ -407,6 +408,8 @@ class Compiler {
if (err) {
return finalCallback(err);
}
this.compilation.startTime = startTime;
this.compilation.endTime = Date.now();
const stats = new Stats(this.compilation);
this.hooks.done.callAsync(stats, err => {
if (err) {
Expand Down
2 changes: 1 addition & 1 deletion packages/rspack/src/config/adapter-rule-use.ts
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ function composeJsUse(
typeof sourceMap === "string"
? sourceMap
: JSON.stringify(sourceMap)
)
)
: undefined,
additionalData: additionalData
? toBuffer(JSON.stringify(additionalData))
Expand Down
21 changes: 21 additions & 0 deletions packages/rspack/src/config/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -1584,6 +1584,27 @@ module.exports = {
warningsCount: {
description: "Add warnings count.",
type: "boolean"
},
outputPath: {
description: "Add output path information.",
type: "boolean"
},
chunkModules: {
description: "Add built modules information to chunk information.",
type: "boolean"
},
chunkRelations: {
description:
"Add information about parent, children and sibling chunks to chunk information.",
type: "boolean"
},
timings: {
description: "Add timing information.",
type: "boolean"
},
builtAt: {
description: "Add built at time information.",
type: "boolean"
}
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/rspack/src/config/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,8 @@ export interface StatsOptions {
outputPath?: boolean;
chunkModules?: boolean;
chunkRelations?: boolean;
timings?: boolean;
builtAt?: boolean;
}

///// Optimization /////
Expand Down
8 changes: 8 additions & 0 deletions packages/rspack/src/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import { LogType } from "./logging/Logger";
export type StatsCompilation = {
name?: string;
hash?: string;
time?: number;
builtAt?: number;
publicPath?: string;
outputPath?: string;
assets?: binding.JsStatsAsset[];
Expand Down Expand Up @@ -62,6 +64,12 @@ export class Stats {
if (options.hash) {
obj.hash = this.#inner.getHash();
}
if (options.timings) {
obj.time = this.compilation.endTime! - this.compilation.startTime!;
}
if (options.builtAt) {
obj.builtAt = this.compilation.endTime;
}
if (options.publicPath) {
obj.publicPath = this.compilation.outputOptions.publicPath;
}
Expand Down
20 changes: 6 additions & 14 deletions packages/rspack/src/watching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ class Watching {
onChange?: () => void;
onInvalid?: () => void;
invalid: boolean;
startTime?: number;
#invalidReported: boolean;
#closeCallbacks?: ((err?: Error) => void)[];
#initial: boolean;
Expand Down Expand Up @@ -206,8 +207,8 @@ class Watching {
}

#go(changedFiles?: ReadonlySet<string>, removedFiles?: ReadonlySet<string>) {
if (this.startTime === undefined) this.startTime = Date.now();
this.running = true;
const logger = this.compiler.getInfrastructureLogger("watcher");
if (this.watcher) {
this.pausedWatcher = this.watcher;
this.lastWatcherStartTime = Date.now();
Expand All @@ -223,27 +224,18 @@ class Watching {
this.#collectedRemovedFiles);
this.#collectedChangedFiles = undefined;
this.#collectedRemovedFiles = undefined;
const begin = Date.now();
this.invalid = false;
this.#invalidReported = false;
this.compiler.hooks.watchRun.callAsync(this.compiler, err => {
if (err) return this._done(err);

const isRebuild = this.compiler.options.devServer && !this.#initial;
const print = isRebuild
? () =>
console.log("rebuild success, time cost", Date.now() - begin, "ms")
: () =>
console.log("build success, time cost", Date.now() - begin, "ms");

const onBuild = (err?: Error) => {
if (err) return this._done(err);
// if (this.invalid) return this._done(null);
// @ts-expect-error
this._done(null);
if (!err && !this.#closed && !this.invalid) {
print();
}
};

if (isRebuild) {
Expand All @@ -260,13 +252,11 @@ class Watching {
*/
private _done(error?: Error) {
this.running = false;
let stats: Stats | null = null;
const handleError = (err?: Error, cbs?: Callback<Error, void>[]) => {
// @ts-expect-error
this.compiler.hooks.failed.call(err);
// this.compiler.cache.beginIdle();
// this.compiler.idle = true;
// @ts-expect-error
this.handler(err, stats);
if (!cbs) {
cbs = this.callbacks;
Expand All @@ -282,7 +272,10 @@ class Watching {
const cbs = this.callbacks;
this.callbacks = [];

stats = new Stats(this.compiler.compilation);
this.compiler.compilation.startTime = this.startTime;
this.compiler.compilation.endTime = Date.now();
const stats = new Stats(this.compiler.compilation);
this.startTime = undefined;
this.compiler.hooks.done.callAsync(stats, err => {
if (err) return handleError(err, cbs);
// @ts-expect-error
Expand All @@ -298,7 +291,6 @@ class Watching {
}
});
for (const cb of cbs) cb(null);
// @ts-expect-error
this.compiler.hooks.afterDone.call(stats);
});
}
Expand Down
4 changes: 2 additions & 2 deletions packages/rspack/tests/Stats.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe("Stats", () => {
main: "./fixtures/a"
}
});
const statsOptions = { all: true };
const statsOptions = { all: true, timings: false, builtAt: false };
expect(typeof stats?.hash).toBe("string");
expect(stats?.toJson(statsOptions)).toMatchInlineSnapshot(`
{
Expand Down Expand Up @@ -157,7 +157,7 @@ describe("Stats", () => {
context: __dirname,
entry: "./fixtures/abc"
});
expect(stats?.toString()).toMatchInlineSnapshot(`
expect(stats?.toString({ timings: false })).toMatchInlineSnapshot(`
"Hash: 2168fece27972fed
PublicPath: auto
Asset Size Chunks Chunk Names
Expand Down
6 changes: 5 additions & 1 deletion packages/rspack/tests/StatsTestCases.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ describe("StatsTestCases", () => {
};
const stats = await util.promisify(rspack)(options);
if (!stats) return expect(false);
const statsOptions = options.stats ?? { all: true };
const statsOptions = options.stats ?? {
all: true,
timings: false,
builtAt: false
};
const statsJson = stats.toJson(statsOptions);
// case ends with error should generate errors
if (/error$/.test(testName)) {
Expand Down