1 | 'use strict';
|
2 |
|
3 | let reactopt = require('./src/index.js');
|
4 | reactopt = reactopt.whyDidYouUpdate;
|
5 | export {reactopt};
|
6 |
|
7 |
|
8 | const chalk = require('./node_modules/chalk');
|
9 | const chalkAnimation = require('./node_modules/chalk-animation');
|
10 | const log = console.log;
|
11 |
|
12 | const chromeLauncher = require('./node_modules/chrome-launcher');
|
13 | const readline = require('readline');
|
14 | const rl = readline.createInterface({
|
15 | input: process.stdin,
|
16 | output: process.stdout
|
17 | });
|
18 |
|
19 |
|
20 | let data = require('./data').data;
|
21 |
|
22 |
|
23 | chromeLauncher.launch({
|
24 | startingUrl: 'http://localhost:3000',
|
25 | }).then((chrome) => {
|
26 | rl.on('line', (line) => {
|
27 | if (line === 'exit') {
|
28 | chrome.kill();
|
29 | endUserInteraction();
|
30 | }
|
31 | });
|
32 | });
|
33 |
|
34 |
|
35 | function startReactopt() {
|
36 | log(chalk.bgCyan.bold('Reactopt is running - Interact with your app and then type/enter "end"'));
|
37 | log('');
|
38 | const loading = chalkAnimation.radar('----------------------------------------------------------------------');
|
39 | loading.start();
|
40 | }
|
41 |
|
42 | startReactopt();
|
43 |
|
44 |
|
45 | function endUserInteraction() {
|
46 |
|
47 | printLine();
|
48 | componentRerenders();
|
49 | printLine();
|
50 | versionOfReact();
|
51 | printLine();
|
52 | productionMode();
|
53 | }
|
54 |
|
55 |
|
56 | function printHeading(string) {
|
57 | log(chalk.black.bgWhite.dim(string));
|
58 | log('');
|
59 | }
|
60 |
|
61 | function printPass(string) {
|
62 | log(chalk.cyan.bold(string));
|
63 | }
|
64 |
|
65 | function printFail(string) {
|
66 | log(chalk.magenta.bold(string));
|
67 | }
|
68 |
|
69 | function printSuggestion(string) {
|
70 | log(chalk.gray(string));
|
71 | }
|
72 |
|
73 | function printLine() {
|
74 | log('');
|
75 | log(chalk.gray('------------------------------------------------------'));
|
76 | log('');
|
77 | }
|
78 |
|
79 |
|
80 | function componentRerenders() {
|
81 | let events = Object.keys(data);
|
82 |
|
83 | if (events.length !== 0) {
|
84 | let components;
|
85 |
|
86 | printHeading('Unnecessary Component Re-rendering');
|
87 | printFail('There are components that unnecessarily re-rendered, and the events that triggered them:');
|
88 | log('');
|
89 |
|
90 |
|
91 | for (let x = 0; x < events.length; x += 1) {
|
92 | components = Object.keys(data[events[x]]);
|
93 | log(chalk.underline(events[x]), chalk.reset.white(' : ' + components) );
|
94 | }
|
95 | printSuggestion("Consider implementing 'shouldComponentUpdate' to prevent re-rendering when \nthe states or props of a component don't change.");
|
96 | } else {
|
97 | printPass('Your version of React is the most current and quickest.');
|
98 | }
|
99 | }
|
100 |
|
101 | function versionOfReact() {
|
102 |
|
103 | let version = '16';
|
104 | printHeading('Version of React');
|
105 | if (version === '16') {
|
106 | printPass('Your version of React is the most current and quickest.');
|
107 | } else {
|
108 | printFail('Your version of React is out of date and slower than newer versions');
|
109 | printSuggestion('Consider upgrading to React v 16, which has the fastest production speed.');
|
110 | }
|
111 | }
|
112 |
|
113 | function productionMode() {
|
114 |
|
115 | let processChecks = true;
|
116 | printHeading('Rendering in Production/Development Mode');
|
117 | if (processChecks === false) {
|
118 | printPass('Your version of React is the most current and quickest.');
|
119 | } else {
|
120 | printFail('Your code contains unneccesary process.env.NODE_ENV checks.');
|
121 | printSuggestion('These checks are useful but can slow down your application. \n Be sure these are removed when application is put into production.');
|
122 | }
|
123 | }
|
124 |
|
125 | module.export = reactopt;
|
126 |
|