-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathbuild-treemap.js
83 lines (73 loc) · 3.36 KB
/
build-treemap.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
/**
* @license Copyright 2020 The Lighthouse Authors. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
/** @typedef {import('../lighthouse-core/lib/i18n/locales').LhlMessages} LhlMessages */
const fs = require('fs');
const GhPagesApp = require('./gh-pages-app.js');
const {LH_ROOT} = require('../root.js');
/**
* Extract only the strings needed for lighthouse-treemap into
* a script that sets a global variable `strings`, whose keys
* are locale codes (en-US, es, etc.) and values are localized UIStrings.
*/
function buildStrings() {
const locales = require('../lighthouse-core/lib/i18n/locales.js');
// TODO(esmodules): use dynamic import when build/ is esm.
const utilCode = fs.readFileSync(LH_ROOT + '/lighthouse-treemap/app/src/util.js', 'utf-8');
const {UIStrings} = eval(utilCode.replace('export ', '') + '\nmodule.exports = TreemapUtil;');
const strings = /** @type {Record<LH.Locale, LhlMessages>} */ ({});
for (const [locale, lhlMessages] of Object.entries(locales)) {
const localizedStrings = Object.fromEntries(
Object.entries(lhlMessages).map(([icuMessageId, v]) => {
const [filename, varName] = icuMessageId.split(' | ');
if (!filename.endsWith('util.js') || !(varName in UIStrings)) {
return [];
}
return [varName, v];
})
);
strings[/** @type {LH.Locale} */ (locale)] = localizedStrings;
}
return 'const strings =' + JSON.stringify(strings, null, 2) + ';';
}
/**
* Build treemap app, optionally deploying to gh-pages if `--deploy` flag was set.
*/
async function run() {
const app = new GhPagesApp({
name: 'treemap',
appDir: `${LH_ROOT}/lighthouse-treemap/app`,
html: {path: 'index.html'},
stylesheets: [
fs.readFileSync(require.resolve('tabulator-tables/dist/css/tabulator.min.css'), 'utf8'),
{path: 'styles/*'},
],
javascripts: [
/* eslint-disable max-len */
fs.readFileSync(require.resolve('idb-keyval/dist/idb-keyval-min.js'), 'utf8'),
fs.readFileSync(require.resolve('event-target-shim/umd'), 'utf8'),
fs.readFileSync(require.resolve('webtreemap-cdt'), 'utf8'),
fs.readFileSync(require.resolve('tabulator-tables/dist/js/tabulator_core.js'), 'utf8'),
fs.readFileSync(require.resolve('tabulator-tables/dist/js/modules/sort.js'), 'utf8'),
fs.readFileSync(require.resolve('tabulator-tables/dist/js/modules/format.js'), 'utf8'),
fs.readFileSync(require.resolve('tabulator-tables/dist/js/modules/resize_columns.js'), 'utf8'),
fs.readFileSync(require.resolve('pako/dist/pako_inflate.js'), 'utf-8'),
/* eslint-enable max-len */
buildStrings(),
{path: 'src/main.js', rollup: true},
],
assets: [
{path: 'images/**/*'},
{path: 'debug.json'},
],
});
await app.build();
const argv = process.argv.slice(2);
if (argv.includes('--deploy')) {
await app.deploy();
}
}
run();