forked from GoogleChrome/lighthouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-report.js
184 lines (166 loc) · 5.02 KB
/
build-report.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* @license
* Copyright 2021 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import esbuild from 'esbuild';
import esMain from 'es-main';
import * as plugins from './esbuild-plugins.js';
import {LH_ROOT} from '../shared/root.js';
import {getIcuMessageIdParts} from '../shared/localization/format.js';
import {locales} from '../shared/localization/locales.js';
import {UIStrings as FlowUIStrings} from '../flow-report/src/i18n/ui-strings.js';
/**
* Extract only the strings needed for the flow report. Code generated is
* an object whose keys are locale codes (en-US, es, etc.) and values are localized UIStrings.
* For flow-report/src/i18n/localized-strings.js
*/
function buildFlowStrings() {
const strings = /** @type {Record<LH.Locale, string>} */ ({});
for (const [locale, lhlMessages] of Object.entries(locales)) {
const localizedStrings = Object.fromEntries(
Object.entries(lhlMessages).map(([icuMessageId, v]) => {
const {filename, key} = getIcuMessageIdParts(icuMessageId);
if (!filename.endsWith('ui-strings.js') || !(key in FlowUIStrings)) {
return [];
}
return [key, v.message];
})
);
strings[/** @type {LH.Locale} */ (locale)] = localizedStrings;
}
return 'export default ' + JSON.stringify(strings, null, 2) + ';';
}
function buildStandaloneReport() {
return esbuild.build({
entryPoints: ['report/clients/standalone.js'],
outfile: 'dist/report/standalone.js',
format: 'iife',
bundle: true,
minify: true,
});
}
const buildReportBulkLoader = plugins.bulkLoader([
plugins.partialLoaders.inlineFs({verbose: Boolean(process.env.DEBUG)}),
plugins.partialLoaders.rmGetModuleDirectory,
]);
async function buildFlowReport() {
return esbuild.build({
entryPoints: ['flow-report/clients/standalone.ts'],
outfile: 'dist/report/flow.js',
format: 'iife',
charset: 'utf8',
bundle: true,
minify: true,
plugins: [
plugins.replaceModules({
[`${LH_ROOT}/flow-report/src/i18n/localized-strings.js`]: buildFlowStrings(),
[`${LH_ROOT}/shared/localization/locales.js`]: 'export const locales = {}',
}),
plugins.ignoreBuiltins(),
buildReportBulkLoader,
],
});
}
async function buildEsModulesBundle() {
// Include the type detail for bundle.esm.d.ts generation
const i18nModuleShim = `
/**
* Returns a new LHR with all strings changed to the new requestedLocale.
* @param {LH.Result} lhr
* @param {LH.Locale} requestedLocale
* @return {{lhr: LH.Result, missingIcuMessageIds: string[]}}
*/
export function swapLocale(lhr, requestedLocale) {
// Stub function only included for types
return {
lhr,
missingIcuMessageIds: [],
};
}
/**
* Populate the i18n string lookup dict with locale data
* Used when the host environment selects the locale and serves lighthouse the intended locale file
* @see https://docs.google.com/document/d/1jnt3BqKB-4q3AE94UWFA0Gqspx8Sd_jivlB7gQMlmfk/edit
* @param {LH.Locale} locale
* @param {Record<string, {message: string}>} lhlMessages
*/
function registerLocaleData(locale, lhlMessages) {
// Stub function only included for types
}
/**
* Returns whether the requestedLocale is registered and available for use
* @param {LH.Locale} requestedLocale
* @return {boolean}
*/
function hasLocale(requestedLocale) {
// Stub function only included for types
return false;
}
export const format = {registerLocaleData, hasLocale};
`;
return esbuild.build({
entryPoints: ['report/clients/bundle.js'],
outfile: 'dist/report/bundle.esm.js',
format: 'esm',
bundle: true,
minify: true,
plugins: [
plugins.replaceModules({
// Exclude this 30kb from the devtools bundle for now.
[`${LH_ROOT}/shared/localization/i18n-module.js`]: i18nModuleShim,
}),
],
});
}
async function buildUmdBundle() {
await esbuild.build({
entryPoints: ['report/clients/bundle.js'],
outfile: 'dist/report/bundle.umd.js',
bundle: true,
// We do not minify, because this is pulled into google3 and minified there anyhow.
minify: false,
plugins: [
plugins.umd('report'),
plugins.replaceModules({
[`${LH_ROOT}/shared/localization/locales.js`]: 'export const locales = {}',
}),
plugins.ignoreBuiltins(),
buildReportBulkLoader,
],
});
}
async function main() {
if (process.argv.length <= 2) {
await Promise.all([
buildStandaloneReport(),
buildFlowReport(),
buildEsModulesBundle(),
buildUmdBundle(),
]);
}
if (process.argv.includes('--psi')) {
console.error('--psi build removed. use --umd instead.');
process.exit(1);
}
if (process.argv.includes('--standalone')) {
await buildStandaloneReport();
}
if (process.argv.includes('--flow')) {
await buildFlowReport();
}
if (process.argv.includes('--esm')) {
await buildEsModulesBundle();
}
if (process.argv.includes('--umd')) {
await buildUmdBundle();
}
}
if (esMain(import.meta)) {
await main();
}
export {
buildStandaloneReport,
buildFlowReport,
buildUmdBundle,
};