Skip to content

Commit 2f6f9dd

Browse files
committed
feat: liferay#538 Add webpack actions to report
This is so that we can tell the user what webpack and/or our plugins are doing on each file.
1 parent 9eb4980 commit 2f6f9dd

File tree

4 files changed

+246
-28
lines changed

4 files changed

+246
-28
lines changed

packages/liferay-npm-build-tools-common/src/project/misc.ts

+64-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55

66
import {Project} from '.';
77
import prop from 'dot-prop';
8+
import {print, warn} from 'liferay-npm-build-tools-common/lib/format';
89

910
import FilePath from '../file-path';
1011

12+
/** Valid log levels for console and report */
13+
export type LogLevel = 'off' | 'error' | 'warn' | 'info' | 'debug';
14+
1115
/**
1216
* Reflects miscellaneous project configuration values.
1317
*/
@@ -23,10 +27,28 @@ export default class Misc {
2327
/**
2428
* Whether or not to dump detailed information about what the tool is doing
2529
*/
26-
get logLevel(): 'off' | 'error' | 'warn' | 'info' | 'debug' {
30+
get logLevel(): LogLevel {
2731
const {npmbundlerrc} = this._project;
2832

29-
return prop.get(npmbundlerrc, 'log-level', 'warn');
33+
let logLevel = prop.get<string>(npmbundlerrc, 'log-level', 'warn');
34+
35+
switch (logLevel) {
36+
case 'off':
37+
case 'error':
38+
case 'warn':
39+
case 'info':
40+
case 'debug':
41+
break;
42+
43+
default:
44+
logLevel = 'off';
45+
print(
46+
warn`Configuration value {log-level} has invalid value: it will be ignored`
47+
);
48+
break;
49+
}
50+
51+
return logLevel as LogLevel;
3052
}
3153

3254
/**
@@ -54,7 +76,6 @@ export default class Misc {
5476
/**
5577
* Get the path to the report file or undefined if no report is configured.
5678
*/
57-
5879
get reportFile(): FilePath | undefined {
5980
const {_project} = this;
6081
const {npmbundlerrc} = _project;
@@ -66,5 +87,45 @@ export default class Misc {
6687
: undefined;
6788
}
6889

90+
/**
91+
* Get report log level
92+
*/
93+
get reportLevel(): LogLevel {
94+
const {_project} = this;
95+
const {npmbundlerrc} = _project;
96+
97+
let dumpReport = prop.get<string | boolean>(
98+
npmbundlerrc,
99+
'dump-report',
100+
false
101+
);
102+
103+
switch (dumpReport) {
104+
case 'off':
105+
case 'error':
106+
case 'warn':
107+
case 'info':
108+
case 'debug':
109+
break;
110+
111+
case true:
112+
dumpReport = 'info';
113+
break;
114+
115+
case false:
116+
dumpReport = 'off';
117+
break;
118+
119+
default:
120+
dumpReport = 'off';
121+
print(
122+
warn`Configuration value {dump-report} has invalid value: it will be ignored`
123+
);
124+
break;
125+
}
126+
127+
return dumpReport as LogLevel;
128+
}
129+
69130
private readonly _project: Project;
70131
}

packages/liferay-npm-bundler/src/report/html.ts

+57-5
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export function htmlDump(report: Report): string {
2121
_rules,
2222
_versionsInfo,
2323
_warnings,
24+
_webpack,
2425
} = report;
2526

2627
const title = 'Report of liferay-npm-bundler execution';
@@ -66,6 +67,39 @@ export function htmlDump(report: Report): string {
6667
)
6768
);
6869

70+
const webpack = htmlSection(
71+
'Details of webpack execution',
72+
htmlTable(
73+
'File',
74+
'',
75+
'Source',
76+
'Messages',
77+
Object.entries(_webpack.logs)
78+
.sort((a, b) => a[0].localeCompare(b[0]))
79+
.map(([prjRelPath, sources]) =>
80+
Object.entries(sources).map(([source, logger]) =>
81+
logger.messages
82+
.map(({logLevel, things}, index) =>
83+
htmlRow(
84+
`
85+
<td>${index == 0 ? prjRelPath : ''}</td>
86+
<td class="${logLevel}">
87+
${logLevel.toUpperCase()}
88+
</td>
89+
<td class="source">[${source}]</td>
90+
<td>
91+
${things.map(thing => `${thing}`).join(' ')}
92+
</td>
93+
`,
94+
logLevel
95+
)
96+
)
97+
.join('')
98+
)
99+
)
100+
)
101+
);
102+
69103
const rulesExecution = htmlIf(Object.keys(_rules.files).length > 0, () =>
70104
htmlSection(
71105
'Details of rule executions',
@@ -131,20 +165,28 @@ export function htmlDump(report: Report): string {
131165
}
132166
133167
th, td {
134-
padding: .1em 0;
168+
padding: .1em;
135169
vertical-align: top;
136170
}
137171
138-
td.info, td.warn, td.error {
139-
background: green;
172+
td.debug, td.info, td.warn, td.error {
140173
border-radius: 4px;
141174
color: white;
175+
padding: 0 2px;
142176
text-align: center;
143177
vertical-align: middle;
144178
width: 1px;
145179
white-space: nowrap;
146180
}
147181
182+
td.debug {
183+
background: gray;
184+
}
185+
186+
td.info {
187+
background: green;
188+
}
189+
148190
td.warn {
149191
background: orange;
150192
}
@@ -154,6 +196,7 @@ export function htmlDump(report: Report): string {
154196
}
155197
156198
td.source {
199+
color: grey;
157200
white-space: nowrap;
158201
}
159202
@@ -231,21 +274,28 @@ export function htmlDump(report: Report): string {
231274
232275
var select = document.getElementById('log-level-select');
233276
234-
select.value = 'info';
277+
select.value = 'debug';
235278
236279
select.onchange = function() {
237280
switch(select.value) {
238-
case 'info':
281+
case 'debug':
239282
style.innerHTML = '';
240283
break;
241284
285+
case 'info':
286+
style.innerHTML =
287+
'tr.debug {display: none;}';
288+
break;
289+
242290
case 'warn':
243291
style.innerHTML =
292+
'tr.debug {display: none;}' +
244293
'tr.info {display: none;}';
245294
break;
246295
247296
case 'error':
248297
style.innerHTML =
298+
'tr.debug {display: none;}' +
249299
'tr.info {display: none;} ' +
250300
'tr.warn {display: none;}';
251301
break;
@@ -258,6 +308,7 @@ export function htmlDump(report: Report): string {
258308
<div id='log-level-selector'>
259309
Log level filter:
260310
<select id='log-level-select'>
311+
<option>debug</option>
261312
<option>info</option>
262313
<option>warn</option>
263314
<option>error</option>
@@ -269,6 +320,7 @@ export function htmlDump(report: Report): string {
269320
${warnings}
270321
${projectInfo}
271322
${versionsInfo}
323+
${webpack}
272324
${rulesExecution}
273325
</body>
274326
</html>

packages/liferay-npm-bundler/src/report/index.ts

+43-20
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,13 @@ import PluginLogger from 'liferay-npm-build-tools-common/lib/plugin-logger';
88
import {VersionInfo} from 'liferay-npm-build-tools-common/lib/project/types';
99

1010
import {htmlDump} from './html';
11+
import ReportLogger from './logger';
1112

1213
/**
1314
* A Report holds data describing a execution of the liferay-npm-bundler so that
1415
* it can be dump as an HTML report.
1516
*/
1617
export class Report {
17-
_executionDate: Date;
18-
_executionTime: number[];
19-
_versionsInfo: {
20-
[key: string]: VersionInfo;
21-
};
22-
_rootPkg: {
23-
id: string;
24-
name: string;
25-
version: string;
26-
};
27-
_rules: {
28-
config: object;
29-
files: {
30-
[prjRelPath: string]: {
31-
logger: PluginLogger;
32-
};
33-
};
34-
};
35-
_warnings: string[];
36-
3718
constructor() {
3819
this._executionDate = new Date();
3920
this._executionTime = undefined;
@@ -45,6 +26,9 @@ export class Report {
4526
};
4627

4728
this._warnings = [];
29+
this._webpack = {
30+
logs: {},
31+
};
4832
}
4933

5034
/**
@@ -122,6 +106,45 @@ export class Report {
122106
rulesRun(prjRelPath: string, logger: PluginLogger): void {
123107
this._rules.files[prjRelPath] = {logger};
124108
}
109+
110+
getWebpackLogger(source: string, prjRelPath: string): ReportLogger {
111+
if (this._webpack.logs[prjRelPath] === undefined) {
112+
this._webpack.logs[prjRelPath] = {};
113+
}
114+
115+
if (this._webpack.logs[prjRelPath][source] === undefined) {
116+
this._webpack.logs[prjRelPath][source] = new ReportLogger();
117+
}
118+
119+
return this._webpack.logs[prjRelPath][source];
120+
}
121+
122+
readonly _executionDate: Date;
123+
_executionTime: number[];
124+
_versionsInfo: {
125+
[key: string]: VersionInfo;
126+
};
127+
_rootPkg: {
128+
id: string;
129+
name: string;
130+
version: string;
131+
};
132+
readonly _rules: {
133+
config: object;
134+
files: {
135+
[prjRelPath: string]: {
136+
logger: PluginLogger;
137+
};
138+
};
139+
};
140+
readonly _warnings: string[];
141+
readonly _webpack: {
142+
logs: {
143+
[prjRelPath: string]: {
144+
[source: string]: ReportLogger;
145+
};
146+
};
147+
};
125148
}
126149

127150
const report = new Report();

0 commit comments

Comments
 (0)