Skip to content

Commit

Permalink
refactor(shared): move formatStats helper to core (#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan authored Jan 12, 2024
1 parent f883a5f commit 48b43a3
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 51 deletions.
3 changes: 1 addition & 2 deletions packages/compat/webpack/src/core/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ import {
isDev,
logger,
debug,
formatStats,
type Stats,
type Rspack,
type RspackConfig,
} from '@rsbuild/shared';
import type { InternalContext } from '@rsbuild/core/provider';
import { formatStats, type InternalContext } from '@rsbuild/core/provider';
import type { WebpackConfig } from '../types';
import { initConfigs, type InitConfigsOptions } from './initConfigs';
import type { Compiler } from 'webpack';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/modern.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineConfig({
buildConfig: [
{
...baseBuildConfig.buildConfig,
input: ['src', '!src/client'],
input: ['src', '!src/client/hmr'],
},
{
buildType: 'bundle',
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion packages/core/src/client/hmr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Tips: this package will be bundled and running in the browser, do not import from the entry of @rsbuild/core.
*/
import type { StatsError } from '@rsbuild/shared';
import { formatStatsMessages } from '@rsbuild/shared/format-stats';
import { formatStatsMessages } from '../formatStats';
import { createSocketUrl } from './createSocketUrl';

// declare any to fix the type of `module.hot`
Expand Down
7 changes: 5 additions & 2 deletions packages/core/src/provider/core/createCompiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
color,
logger,
prettyTime,
formatStats,
TARGET_ID_MAP,
type RspackConfig,
type RspackCompiler,
Expand All @@ -15,7 +14,11 @@ import {
import { initConfigs, type InitConfigsOptions } from './initConfigs';
import type { InternalContext } from '../../types';
import type { Stats, MultiStats, StatsCompilation } from '@rspack/core';
import { isSatisfyRspackVersion, rspackMinVersion } from '../shared';
import {
formatStats,
rspackMinVersion,
isSatisfyRspackVersion,
} from '../shared';

export async function createCompiler({
context,
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/provider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export { getPluginAPI } from './core/initPlugins';
export { applyBaseCSSRule, applyCSSModuleRule } from './plugins/css';
export type { InternalContext } from '../types';
export { setHTMLPlugin, getHTMLPlugin } from './htmlPluginUtil';
export { formatStats } from './shared';
41 changes: 41 additions & 0 deletions packages/core/src/provider/shared.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { join } from 'path';
import {
color,
getSharedPkgCompiledPath,
type Stats,
type MultiStats,
type SharedCompiledPkgNames,
} from '@rsbuild/shared';
import { fse } from '@rsbuild/shared';
import type { RsbuildPlugin } from '../types';
import { awaitableGetter, type Plugins } from '@rsbuild/shared';
import { formatStatsMessages } from '../client/formatStats';

export const applyDefaultPlugins = (plugins: Plugins) =>
awaitableGetter<RsbuildPlugin>([
Expand Down Expand Up @@ -90,3 +94,40 @@ export const getCompiledPath = (packageName: string) => {
};

export const BUILTIN_LOADER = 'builtin:';

export function formatStats(stats: Stats | MultiStats, showWarnings = true) {
const statsData = stats.toJson({
preset: 'errors-warnings',
});

const { errors, warnings } = formatStatsMessages(statsData);

if (errors.length) {
const errorMsgs = `${errors.join('\n\n')}\n`;
const isTerserError = errorMsgs.includes('from Terser');
const title = color.bold(
color.red(isTerserError ? 'Minify error: ' : 'Compile error: '),
);
const tip = color.yellow(
isTerserError
? 'Failed to minify with terser, check for syntax errors.'
: 'Failed to compile, check the errors for troubleshooting.',
);

return {
message: `${title}\n${tip}\n${errorMsgs}`,
level: 'error',
};
}

// always show warnings in tty mode
if (warnings.length && (showWarnings || process.stdout.isTTY)) {
const title = color.bold(color.yellow('Compile Warning: \n'));
return {
message: `${title}${`${warnings.join('\n\n')}\n`}`,
level: 'warning',
};
}

return {};
}
4 changes: 0 additions & 4 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./format-stats": {
"types": "./dist/formatStats.d.ts",
"default": "./dist/formatStats.js"
},
"./jiti": {
"types": "./compiled/jiti/types/jiti.d.ts",
"default": "./compiled/jiti/index.js"
Expand Down
40 changes: 0 additions & 40 deletions packages/shared/src/format.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/shared/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from './mergeChainedOptions';
export * from './mergeRsbuildConfig';
export * from './types';
export * from './config';
export * from './format';
export * from './chain';
export * from './getLoaderOptions';
export * from './patch';
Expand Down

0 comments on commit 48b43a3

Please sign in to comment.