UNPKG

1.76 kBJavaScriptView Raw
1const chalk = require('chalk');
2const webpack = require('webpack');
3
4function SimpleProgressPlugin() {
5 process.stdout.write(chalk.yellow(chalk.bold('Webpack: Starting ...')));
6 const startTime = Date.now();
7
8 let nextStep = 1;
9 let shown = new Set();
10
11 return new webpack.ProgressPlugin((progress) => {
12 const rounded = Math.ceil(progress * 100);
13 const showRegular = () => {
14 if (rounded % 5 === 0 && !shown.has(rounded)) {
15 shown.add(rounded);
16 process.stdout.write(` ${chalk.gray(`${rounded}%`)}`);
17 }
18 };
19
20 // STEP 1: COMPILATION
21 if (progress >= 0 && progress < 0.1) {
22 if (nextStep === 1) {
23 process.stdout.write(chalk.blue.bold('\n ▶ Compile modules '));
24 nextStep = 2;
25 }
26 else {
27 showRegular();
28 }
29 }
30
31 // STEP 2: BUILDING
32 if (progress >= 0.1 && progress < 0.7) {
33 if (nextStep === 2) {
34 process.stdout.write(chalk.blue.bold('\n ▶ Build modules '));
35 nextStep = 3;
36 }
37 else {
38 showRegular();
39 }
40 }
41
42 // STEP 3: OPTIMIZATION
43 if (progress >= 0.7 && progress < 0.95) {
44 if (nextStep === 3) {
45 process.stdout.write(chalk.blue.bold('\n ▶ Optimize modules'));
46 nextStep = 4;
47 }
48 else {
49 showRegular();
50 }
51 }
52
53 // STEP 4: EMIT
54 if (progress >= 0.95 && progress < 1) {
55 if (nextStep === 4) {
56 process.stdout.write(chalk.blue.bold('\n ▶ Emit files '));
57 nextStep = 5;
58 }
59 else {
60 showRegular();
61 }
62 }
63
64 // STEP 5: FOOTER
65 if (progress >= 1) {
66 if (nextStep === 5) {
67 const timeTaken = ((Date.now() - startTime) / 1000).toFixed(2);
68 process.stdout.write(chalk.green(chalk.bold(`▶ Webpack: Finished after ${timeTaken} seconds.\n\n`)));
69 }
70
71 nextStep = 1;
72 shown = new Set();
73 }
74 });
75}
76
77module.exports = SimpleProgressPlugin;