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: use environment name as progress id #2670

Merged
merged 1 commit into from
Jun 22, 2024
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
2 changes: 1 addition & 1 deletion e2e/cases/javascript-api/custom-logger/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ rspackOnlyTest('should allow to customize logger', async () => {
});

expect(
logs.find((item) => item.includes('[READY] Client compiled in')),
logs.find((item) => item.includes('[READY] Compiled in')),
).toBeTruthy();

restore();
Expand Down
4 changes: 2 additions & 2 deletions e2e/cases/progress/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ webpackOnlyTest('should emit progress log in non-TTY environment', async () => {
});

expect(
infoMsgs.some((message) => message.includes('Client compile progress')),
infoMsgs.some((message) => message.includes('Compile progress')),
).toBeTruthy();
expect(
readyMsgs.some((message) => message.includes('Client compiled')),
readyMsgs.some((message) => message.includes('Compiled')),
).toBeTruthy();

process.stdout.isTTY = true;
Expand Down
3 changes: 1 addition & 2 deletions packages/compat/webpack/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
RspackChain,
} from '@rsbuild/core';
import { type CopyPluginOptions, castArray } from '@rsbuild/shared';
import { TARGET_ID_MAP } from './shared';

async function applyTsConfigPathsPlugin({
chain,
Expand Down Expand Up @@ -83,7 +82,7 @@ export const pluginAdaptor = (): RsbuildPlugin => ({
const { ProgressPlugin } = await import('./progress/ProgressPlugin');
chain.plugin(CHAIN_ID.PLUGIN.PROGRESS).use(ProgressPlugin, [
{
id: TARGET_ID_MAP[target],
id: environment,
...(progress === true ? {} : progress),
},
]);
Expand Down
4 changes: 3 additions & 1 deletion packages/compat/webpack/src/progress/ProgressPlugin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '@rsbuild/core';
import { color } from '@rsbuild/shared';
import webpack from 'webpack';
import { prettyTime } from '../shared';
import { bus, createFriendlyPercentage } from './helpers';
Expand Down Expand Up @@ -81,7 +82,8 @@ export class ProgressPlugin extends webpack.ProgressPlugin {
startTime = null;

if (!this.hasCompileErrors) {
logger.ready(`${this.id} compiled in ${this.compileTime}`);
const suffix = this.id ? color.gray(` (${this.id})`) : '';
logger.ready(`Compiled in ${this.compileTime} ${suffix}`);
}
}
});
Expand Down
9 changes: 6 additions & 3 deletions packages/compat/webpack/src/progress/helpers/nonTty.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { logger } from '@rsbuild/core';
import { color } from '@rsbuild/shared';

export function createNonTTYLogger() {
let prevPercentage = 0;
Expand All @@ -16,6 +17,8 @@ export function createNonTTYLogger() {
hasErrors: boolean;
compileTime: string | null;
}) => {
const suffix = color.gray(`(${id})`);

if (done) {
// avoid printing done twice
if (prevPercentage === 100) {
Expand All @@ -24,16 +27,16 @@ export function createNonTTYLogger() {

prevPercentage = 100;
if (hasErrors) {
logger.error(`${id} compile failed in ${compileTime}`);
logger.error(`Compile failed in ${compileTime} ${suffix}`);
} else {
logger.ready(`${id} compiled in ${compileTime}`);
logger.ready(`Compiled in ${compileTime} ${suffix}`);
}
}
// print progress when percentage increased by more than 10%
// because we don't want to spam the console
else if (current - prevPercentage > 10) {
prevPercentage = current;
logger.info(`${id} compile progress: ${current.toFixed(0)}%`);
logger.info(`Compile progress: ${current.toFixed(0)}% ${suffix}`);
}
};

Expand Down
2 changes: 0 additions & 2 deletions packages/compat/webpack/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
modifyBundlerChain,
onCompileDone,
prettyTime,
TARGET_ID_MAP,
} = __internalHelper;

export {
Expand All @@ -29,7 +28,6 @@ export {
modifyBundlerChain,
onCompileDone,
prettyTime,
TARGET_ID_MAP,
};

export type InternalContext = __internalHelper.InternalContext;
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly 1`] = `
"dependenciesCount": 10000,
"handler": [Function],
"hasCompileErrors": false,
"id": "Client",
"id": "web",
"modulesCount": 5000,
"name": "ProgressPlugin",
"percentBy": null,
Expand Down Expand Up @@ -779,7 +779,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when produ
"dependenciesCount": 10000,
"handler": [Function],
"hasCompileErrors": false,
"id": "Client",
"id": "web",
"modulesCount": 5000,
"name": "ProgressPlugin",
"percentBy": null,
Expand Down Expand Up @@ -1059,7 +1059,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"dependenciesCount": 10000,
"handler": [Function],
"hasCompileErrors": false,
"id": "Server",
"id": "node",
"modulesCount": 5000,
"name": "ProgressPlugin",
"percentBy": null,
Expand Down Expand Up @@ -1322,7 +1322,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when targe
"dependenciesCount": 10000,
"handler": [Function],
"hasCompileErrors": false,
"id": "Web Worker",
"id": "webWorker",
"modulesCount": 5000,
"name": "ProgressPlugin",
"percentBy": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ exports[`webpackConfig > should allow to append and prepend plugins 1`] = `
"dependenciesCount": 10000,
"handler": [Function],
"hasCompileErrors": false,
"id": "Client",
"id": "web",
"modulesCount": 5000,
"name": "ProgressPlugin",
"percentBy": null,
Expand Down
7 changes: 0 additions & 7 deletions packages/core/src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { join } from 'node:path';
import type { RsbuildTarget } from '@rsbuild/shared';

// Paths
// loaders will be emitted to the same folder of the main bundle
Expand Down Expand Up @@ -53,9 +52,3 @@ export const IMAGE_EXTENSIONS = [
];
export const VIDEO_EXTENSIONS = ['mp4', 'webm', 'ogg', 'mov'];
export const AUDIO_EXTENSIONS = ['mp3', 'wav', 'flac', 'aac', 'm4a', 'opus'];

export const TARGET_ID_MAP: Record<RsbuildTarget, string> = {
web: 'Client',
node: 'Server',
'web-worker': 'Web Worker',
};
1 change: 0 additions & 1 deletion packages/core/src/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,3 @@ export { applySwcDecoratorConfig } from './plugins/swc';
export { getSwcMinimizerOptions } from './plugins/minimize';
export { getDevMiddleware } from './server/devMiddleware';
export { createDevServer } from './server/devServer';
export { TARGET_ID_MAP } from './constants';
7 changes: 2 additions & 5 deletions packages/core/src/plugins/progress.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { rspack } from '@rspack/core';
import { TARGET_ID_MAP } from '../constants';
import { isProd } from '../helpers';
import type { RsbuildPlugin } from '../types';

Expand All @@ -12,7 +11,7 @@ export const pluginProgress = (): RsbuildPlugin => ({
return;
}

api.modifyBundlerChain(async (chain, { target, CHAIN_ID }) => {
api.modifyBundlerChain(async (chain, { CHAIN_ID, environment }) => {
const config = api.getNormalizedConfig();
const options =
config.dev.progressBar ??
Expand All @@ -24,9 +23,7 @@ export const pluginProgress = (): RsbuildPlugin => ({
}

const prefix =
options !== true && options.id !== undefined
? options.id
: TARGET_ID_MAP[target];
options !== true && options.id !== undefined ? options.id : environment;

chain.plugin(CHAIN_ID.PLUGIN.PROGRESS).use(rspack.ProgressPlugin, [
{
Expand Down
15 changes: 7 additions & 8 deletions packages/core/src/provider/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
} from '@rsbuild/shared';
import { rspack } from '@rspack/core';
import type { StatsCompilation } from '@rspack/core';
import { TARGET_ID_MAP } from '../constants';
import {
formatStats,
getStatsOptions,
Expand Down Expand Up @@ -72,27 +71,27 @@ export async function createCompiler({
}

const done = async (stats: Stats | MultiStats) => {
const obj = stats.toJson({
const statsJson = stats.toJson({
all: false,
timings: true,
});

const printTime = (c: StatsCompilation, index: number) => {
if (c.time) {
const time = prettyTime(c.time / 1000);
const target = context.targets[index];
const name = TARGET_ID_MAP[target || 'web'];
logger.ready(`${name} compiled in ${time}`);
const { name } = rspackConfigs[index];
const suffix = name ? color.gray(` (${name})`) : '';
logger.ready(`Compiled in ${time}${suffix}`);
}
};

if (!stats.hasErrors()) {
if (obj.children) {
obj.children.forEach((c, index) => {
if (statsJson.children) {
statsJson.children.forEach((c, index) => {
printTime(c, index);
});
} else {
printTime(obj, 0);
printTime(statsJson, 0);
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/__snapshots__/default.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ exports[`applyDefaultPlugins > should apply default plugins correctly when prod
},
ProgressPlugin {
"_options": {
"prefix": "Client",
"prefix": "web",
"profile": false,
"progressChars": "━━",
"template": "● {prefix:.bold} {bar:25.green/white.dim} ({percent}%) {wide_msg:.dim}",
Expand Down
Loading