Skip to content

Commit 63136c7

Browse files
committed
feat(scripts): add compile time message to output
It was complicated than I thought it would be. Firstly because, webpack multi compiler always report for all instances. So I had to store last stat (.toJson, not the actual object, because it would produce the same result corresponding the current one) and compare with current one against `builtAt`. If that value changes then webpack has compiled the instance. See #444
1 parent c1885e2 commit 63136c7

File tree

4 files changed

+101
-8
lines changed

4 files changed

+101
-8
lines changed

examples/plugin/src/ts/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ console.log(strRepeat('foo', 10));
88
console.log(strRepeat('foo', 10));
99
console.log(strRepeat('foo', 10));
1010
console.log(strRepeat('10', 20));
11-
console.log(strRepeat('[]', 20));
11+
console.log(strRepeat('10', 20));
1212

1313
if (module.hot) {
1414
module.hot.accept('./module.ts', () => {

packages/scripts/src/bin/serve.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import {
2121
printCompiledWithWarnMessage,
2222
printFailedCompileMEssage,
2323
printGeneralInfoMessage,
24+
printCompileTimeMessages,
25+
webpackStatToJsonOptions,
2426
} from './utils';
2527

2628
/**
@@ -69,27 +71,31 @@ export function serve(options: ProgramOptions | undefined): void {
6971

7072
spinner.start();
7173

74+
let lastWebpackStat: any | null = null;
75+
7276
// Start the webpack/browserSync server
7377
const server: Server = new Server(projectConfig, serverConfig, cwd, {
7478
// tslint:disable:no-empty
7579
invalid: () => {
7680
printCompilingMessage();
7781
},
78-
done: () => {
82+
done: stat => {
7983
printSuccessfullyCompiledMessage();
80-
printWatchingMessage();
8184
},
8285
onError: msg => {
8386
console.log(`${chalk.bgRed.black(' ERROR ')} please review`);
8487
console.log('');
8588
msg.errors.forEach(e => console.log(e));
8689
console.log('');
8790
printFailedCompileMEssage();
88-
printWatchingMessage();
8991
},
9092
onWarn: msg => {
9193
printCompiledWithWarnMessage();
9294
msg.warnings.forEach(e => console.log(e));
95+
},
96+
onEmit: stats => {
97+
printCompileTimeMessages(stats, lastWebpackStat);
98+
lastWebpackStat = stats.toJson(webpackStatToJsonOptions);
9399
printWatchingMessage();
94100
},
95101
firstCompile: (stats: webpack.Stats) => {
@@ -113,7 +119,9 @@ export function serve(options: ProgramOptions | undefined): void {
113119
} else {
114120
printSuccessfullyCompiledMessage();
115121
}
122+
printCompileTimeMessages(stats, lastWebpackStat);
116123
printWatchingMessage();
124+
lastWebpackStat = stats.toJson(webpackStatToJsonOptions);
117125
},
118126
onBsChange(file) {
119127
printGeneralInfoMessage(`changed: ${chalk.bold(file)}`);

packages/scripts/src/bin/utils.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import gradient from 'gradient-string';
77
import logSymbols from 'log-symbols';
88
import path from 'path';
99
import PrettyError from 'pretty-error';
10+
import webpack from 'webpack';
1011
import { WpackioError } from '../errors/WpackioError';
1112
import { ArchiveResolve } from '../scripts/Pack';
1213

@@ -51,6 +52,90 @@ export function printCompilingMessage() {
5152
);
5253
}
5354

55+
export function printCompileTimeMessage(stat: any, lastStat: any | null) {
56+
// safety
57+
if (!stat || typeof stat !== 'object' || !stat.builtAt) {
58+
return;
59+
}
60+
61+
if (
62+
lastStat === null ||
63+
typeof lastStat !== 'object' ||
64+
!lastStat.builtAt ||
65+
stat.builtAt !== lastStat.builtAt
66+
) {
67+
const entryName = chalk.bold(stat.name);
68+
let name = chalk.green(entryName);
69+
let symbol = logSymbols.success;
70+
if (stat.errors.length) {
71+
name = chalk.red(entryName);
72+
symbol = logSymbols.error;
73+
} else if (stat.warnings.length) {
74+
name = chalk.yellow(entryName);
75+
symbol = logSymbols.warning;
76+
}
77+
console.log(
78+
addTimeStampToLog(
79+
`${symbol} entry ${name} in ${chalk.magenta(`${stat.time}ms`)}.`
80+
)
81+
);
82+
}
83+
}
84+
85+
export const webpackStatToJsonOptions: webpack.Stats.ToJsonOptions = {
86+
timings: true,
87+
assets: false,
88+
builtAt: true,
89+
cached: false,
90+
cachedAssets: false,
91+
children: true,
92+
chunkModules: false,
93+
chunkOrigins: false,
94+
chunks: false,
95+
entrypoints: false,
96+
env: false,
97+
errors: true,
98+
warnings: true,
99+
modules: false,
100+
};
101+
102+
export function printCompileTimeMessages(
103+
stats: webpack.Stats,
104+
lastStatsJson: any | null
105+
) {
106+
let statsJson: any | null = null;
107+
if (stats) {
108+
statsJson = stats.toJson(webpackStatToJsonOptions);
109+
}
110+
111+
if (
112+
statsJson !== null &&
113+
!statsJson.time &&
114+
statsJson.children &&
115+
Array.isArray(statsJson.children)
116+
) {
117+
// if it is multicompiler
118+
statsJson.children.forEach((sj: any, index: number) => {
119+
if (lastStatsJson === null) {
120+
printCompileTimeMessage(sj, null);
121+
} else if (
122+
lastStatsJson !== null &&
123+
lastStatsJson.children &&
124+
Array.isArray(lastStatsJson.children)
125+
) {
126+
printCompileTimeMessage(sj, lastStatsJson.children[index]);
127+
}
128+
});
129+
} else if (statsJson.time && statsJson.name) {
130+
// if it is single compiler
131+
if (lastStatsJson === null) {
132+
printCompileTimeMessage(statsJson, null);
133+
} else {
134+
printCompileTimeMessage(statsJson, lastStatsJson);
135+
}
136+
}
137+
}
138+
54139
export function printSuccessfullyCompiledMessage() {
55140
console.log(
56141
addTimeStampToLog(`${logSymbols.success} compiled successfully`)

packages/scripts/src/scripts/Server.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ interface Callbacks {
1717
onError(err: { errors: string[]; warnings: string[] }): void;
1818
onWarn(warn: { errors: string[]; warnings: string[] }): void;
1919
onBsChange(file: string): void;
20+
onEmit(stats: webpack.Stats): void;
2021
}
2122

2223
/**
@@ -248,12 +249,11 @@ export class Server {
248249
}
249250
if (messages.errors.length) {
250251
this.callbacks.onError(messages);
251-
252-
return;
253-
}
254-
if (messages.warnings.length) {
252+
} else if (messages.warnings.length) {
255253
this.callbacks.onWarn(messages);
256254
}
255+
256+
this.callbacks.onEmit(stats);
257257
});
258258

259259
// On compile start

0 commit comments

Comments
 (0)