Skip to content

Commit 3aa15dc

Browse files
authored
report: convert to ES modules (#12702)
1 parent 579e3cb commit 3aa15dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+1248
-513
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ jobs:
4949
- run: yarn test-legacy-javascript
5050
- run: yarn i18n:checks
5151
- run: yarn dogfood-lhci
52+
- run: bash lighthouse-core/scripts/copy-util-commonjs.sh
5253

5354
# Fail if any changes were written to any source files or generated untracked files (ex, from: build/build-cdt-lib.js).
5455
- run: git add -A && git diff --cached --exit-code

build/build-lightrider-bundles.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const fs = require('fs');
1010
const path = require('path');
1111
const bundleBuilder = require('./build-bundle.js');
1212
const {minifyFileTransform} = require('./build-utils.js');
13+
const {buildPsiReport} = require('./build-report.js');
1314
const {LH_ROOT} = require('../root.js');
1415

1516
const distDir = path.join(LH_ROOT, 'dist', 'lightrider');
@@ -53,6 +54,7 @@ async function run() {
5354
await Promise.all([
5455
buildEntryPoint(),
5556
buildReportGenerator(),
57+
buildPsiReport(),
5658
]);
5759
}
5860

build/build-report.js

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,96 @@
55
*/
66
'use strict';
77

8-
const fs = require('fs');
9-
10-
function concatRendererCode() {
11-
return [
12-
fs.readFileSync(__dirname + '/../report/renderer/util.js', 'utf8'),
13-
fs.readFileSync(__dirname + '/../report/renderer/dom.js', 'utf8'),
14-
fs.readFileSync(__dirname + '/../report/renderer/details-renderer.js', 'utf8'),
15-
fs.readFileSync(__dirname + '/../report/renderer/crc-details-renderer.js', 'utf8'),
16-
fs.readFileSync(__dirname + '/../report/renderer/snippet-renderer.js', 'utf8'),
17-
fs.readFileSync(__dirname + '/../report/renderer/element-screenshot-renderer.js', 'utf8'),
18-
fs.readFileSync(__dirname + '/../lighthouse-core/lib/file-namer.js', 'utf8'),
19-
fs.readFileSync(__dirname + '/../report/renderer/logger.js', 'utf8'),
20-
fs.readFileSync(__dirname + '/../report/renderer/report-ui-features.js', 'utf8'),
21-
fs.readFileSync(__dirname + '/../report/renderer/category-renderer.js', 'utf8'),
22-
fs.readFileSync(__dirname + '/../report/renderer/performance-category-renderer.js', 'utf8'),
23-
fs.readFileSync(__dirname + '/../report/renderer/pwa-category-renderer.js', 'utf8'),
24-
fs.readFileSync(__dirname + '/../report/renderer/report-renderer.js', 'utf8'),
25-
fs.readFileSync(__dirname + '/../report/renderer/i18n.js', 'utf8'),
26-
fs.readFileSync(__dirname + '/../report/renderer/text-encoding.js', 'utf8'),
27-
].join(';\n');
28-
}
8+
const rollup = require('rollup');
9+
const {terser} = require('rollup-plugin-terser');
10+
// Only needed b/c getFilenamePrefix loads a commonjs module.
11+
const commonjs =
12+
// @ts-expect-error types are wrong.
13+
/** @type {import('rollup-plugin-commonjs').default} */ (require('rollup-plugin-commonjs'));
2914

3015
async function buildStandaloneReport() {
31-
const REPORT_JAVASCRIPT = [
32-
concatRendererCode(),
33-
fs.readFileSync(__dirname + '/../report/clients/standalone.js', 'utf8'),
34-
].join(';\n');
35-
fs.mkdirSync(__dirname + '/../dist/report', {recursive: true});
36-
fs.writeFileSync(__dirname + '/../dist/report/standalone.js', REPORT_JAVASCRIPT);
16+
const bundle = await rollup.rollup({
17+
input: 'report/clients/standalone.js',
18+
plugins: [
19+
commonjs(),
20+
terser(),
21+
],
22+
});
23+
24+
await bundle.write({
25+
file: 'dist/report/standalone.js',
26+
format: 'iife',
27+
});
28+
}
29+
30+
async function buildPsiReport() {
31+
const bundle = await rollup.rollup({
32+
input: 'report/clients/psi.js',
33+
plugins: [
34+
commonjs(),
35+
],
36+
});
37+
38+
await bundle.write({
39+
file: 'dist/report/psi.js',
40+
format: 'esm',
41+
});
42+
}
43+
44+
async function buildViewerReport() {
45+
const bundle = await rollup.rollup({
46+
input: 'report/clients/viewer.js',
47+
plugins: [
48+
commonjs(),
49+
],
50+
});
51+
52+
await bundle.write({
53+
file: 'dist/report/viewer.js',
54+
format: 'iife',
55+
});
56+
}
57+
58+
async function buildTreemapReport() {
59+
const bundle = await rollup.rollup({
60+
input: 'report/clients/treemap.js',
61+
plugins: [
62+
commonjs(),
63+
],
64+
});
65+
66+
await bundle.write({
67+
file: 'dist/report/treemap.js',
68+
format: 'iife',
69+
});
70+
}
71+
72+
async function buildEsModulesBundle() {
73+
const bundle = await rollup.rollup({
74+
input: 'report/clients/bundle.js',
75+
plugins: [
76+
commonjs(),
77+
],
78+
});
79+
80+
await bundle.write({
81+
file: 'dist/report/bundle.js',
82+
format: 'esm',
83+
});
3784
}
3885

3986
if (require.main === module) {
40-
buildStandaloneReport();
87+
if (process.argv[2] === '--only-standalone') {
88+
buildStandaloneReport();
89+
} else {
90+
buildStandaloneReport();
91+
buildEsModulesBundle();
92+
}
4193
}
4294

4395
module.exports = {
4496
buildStandaloneReport,
45-
concatRendererCode,
97+
buildPsiReport,
98+
buildViewerReport,
99+
buildTreemapReport,
46100
};

build/build-treemap.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
/** @typedef {import('../lighthouse-core/lib/i18n/locales').LhlMessages} LhlMessages */
99

1010
const fs = require('fs');
11+
const {buildTreemapReport} = require('./build-report.js');
1112
const GhPagesApp = require('./gh-pages-app.js');
1213
const {LH_ROOT} = require('../root.js');
1314

@@ -45,6 +46,8 @@ function buildStrings() {
4546
* Build treemap app, optionally deploying to gh-pages if `--deploy` flag was set.
4647
*/
4748
async function run() {
49+
await buildTreemapReport();
50+
4851
const app = new GhPagesApp({
4952
name: 'treemap',
5053
appDir: `${LH_ROOT}/lighthouse-treemap/app`,
@@ -65,9 +68,7 @@ async function run() {
6568
fs.readFileSync(require.resolve('pako/dist/pako_inflate.js'), 'utf-8'),
6669
/* eslint-enable max-len */
6770
buildStrings(),
68-
{path: '../../report/renderer/logger.js'},
69-
{path: '../../report/renderer/i18n.js'},
70-
{path: '../../report/renderer/text-encoding.js'},
71+
{path: '../../dist/report/treemap.js'},
7172
{path: '../../lighthouse-viewer/app/src/drag-and-drop.js'},
7273
{path: '../../lighthouse-viewer/app/src/github-api.js'},
7374
{path: '../../lighthouse-viewer/app/src/firebase-auth.js'},

build/build-viewer.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const fs = require('fs');
99
const browserify = require('browserify');
1010
const GhPagesApp = require('./gh-pages-app.js');
1111
const {minifyFileTransform} = require('./build-utils.js');
12-
const {concatRendererCode} = require('./build-report.js');
12+
const {buildViewerReport} = require('./build-report.js');
1313
const htmlReportAssets = require('../report/report-assets.js');
1414
const {LH_ROOT} = require('../root.js');
1515

@@ -32,6 +32,8 @@ async function run() {
3232
});
3333
});
3434

35+
await buildViewerReport();
36+
3537
const app = new GhPagesApp({
3638
name: 'viewer',
3739
appDir: `${LH_ROOT}/lighthouse-viewer/app`,
@@ -45,8 +47,8 @@ async function run() {
4547
],
4648
javascripts: [
4749
await generatorJsPromise,
48-
concatRendererCode(),
4950
fs.readFileSync(require.resolve('idb-keyval/dist/idb-keyval-min.js'), 'utf8'),
51+
{path: '../../dist/report/viewer.js'},
5052
{path: 'src/*'},
5153
],
5254
assets: [

lighthouse-core/audits/audit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
const {isUnderTest} = require('../lib/lh-env.js');
99
const statistics = require('../lib/statistics.js');
10-
const Util = require('../../report/renderer/util.js');
10+
const Util = require('../util-commonjs.js');
1111

1212
const DEFAULT_PASS = 'defaultPass';
1313

lighthouse-core/computed/resource-summary.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const NetworkRecords = require('./network-records.js');
1010
const URL = require('../lib/url-shim.js');
1111
const NetworkRequest = require('../lib/network-request.js');
1212
const Budget = require('../config/budget.js');
13-
const Util = require('../../report/renderer/util.js');
13+
const Util = require('../util-commonjs.js');
1414

1515
/** @typedef {{count: number, resourceSize: number, transferSize: number}} ResourceEntry */
1616

lighthouse-core/lib/file-namer.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,4 @@ function getFilenamePrefix(lhr) {
3434
return filenamePrefix.replace(/[/?<>\\:*|"]/g, '-');
3535
}
3636

37-
// don't attempt to export in the browser.
38-
if (typeof module !== 'undefined' && module.exports) {
39-
module.exports = {getFilenamePrefix};
40-
}
37+
module.exports = {getFilenamePrefix};

lighthouse-core/lib/url-shim.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* URL shim so we keep our code DRY
1010
*/
1111

12-
const Util = require('../../report/renderer/util.js');
12+
const Util = require('../util-commonjs.js');
1313

1414
/** @typedef {import('./network-request.js')} NetworkRequest */
1515

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/usr/bin/env bash
2+
3+
##
4+
# @license Copyright 2021 The Lighthouse Authors. All Rights Reserved.
5+
# 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
6+
# 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.
7+
##
8+
9+
# TODO(esmodules): delete when consumers of util.js are all esm.
10+
11+
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
12+
LH_ROOT_DIR="$SCRIPT_DIR/../.."
13+
14+
OUT_FILE="$LH_ROOT_DIR"/lighthouse-core/util-commonjs.js
15+
16+
echo '// @ts-nocheck' > "$OUT_FILE"
17+
echo '// Auto-generated by lighthouse-core/scripts/copy-util-commonjs.sh' >> "$OUT_FILE"
18+
echo '// Temporary solution until all our code uses esmodules' >> "$OUT_FILE"
19+
sed 's/export class Util/class Util/g; s/export const UIStrings = Util.UIStrings;//g' "$LH_ROOT_DIR"/report/renderer/util.js >> "$OUT_FILE"
20+
echo 'module.exports = Util;' >> "$OUT_FILE"

lighthouse-core/scripts/dogfood-lhci.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ if ! echo "$CHANGED_FILES" | grep -E 'report|lhci' > /dev/null; then
3636
fi
3737

3838
# Generate HTML reports in ./dist/now/
39+
yarn build-report
3940
yarn now-build
4041

4142
# Install LHCI

lighthouse-core/scripts/i18n/collect-strings.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const path = require('path');
1414
const expect = require('expect');
1515
const tsc = require('typescript');
1616
const MessageParser = require('intl-messageformat-parser').default;
17-
const Util = require('../../../report/renderer/util.js');
17+
const Util = require('../../../lighthouse-core/util-commonjs.js');
1818
const {collectAndBakeCtcStrings} = require('./bake-ctc-to-lhl.js');
1919
const {pruneObsoleteLhlMessages} = require('./prune-obsolete-lhl-messages.js');
2020
const {countTranslatedMessages} = require('./count-translated.js');
@@ -41,6 +41,7 @@ const ignoredPathComponents = [
4141
'**/test/**',
4242
'**/*-test.js',
4343
'**/*-renderer.js',
44+
'**/util-commonjs.js',
4445
'lighthouse-treemap/app/src/main.js',
4546
];
4647

lighthouse-core/scripts/roll-to-devtools.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,17 @@ lh_bg_js="dist/lighthouse-dt-bundle.js"
4242
yarn build-report
4343
yarn build-devtools
4444

45-
# copy lighthouse-dt-bundle (potentially stale)
45+
# copy lighthouse-dt-bundle
4646
cp -pPR "$lh_bg_js" "$fe_lh_dir/lighthouse-dt-bundle.js"
47-
echo -e "$check (Potentially stale) lighthouse-dt-bundle copied."
47+
echo -e "$check lighthouse-dt-bundle copied."
48+
49+
# generate bundle.d.ts
50+
npx tsc --allowJs --declaration --emitDeclarationOnly dist/report/bundle.js
51+
52+
# copy report code $fe_lh_dir
53+
fe_lh_report_dir="$fe_lh_dir/report/"
54+
cp dist/report/bundle.js dist/report/bundle.d.ts "$fe_lh_report_dir"
55+
echo -e "$check Report code copied."
4856

4957
# copy report generator + cached resources into $fe_lh_dir
5058
fe_lh_report_assets_dir="$fe_lh_dir/report-assets/"

lighthouse-core/test/lib/page-functions-test.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,31 @@
55
*/
66
'use strict';
77

8-
const assert = require('assert').strict;
9-
const jsdom = require('jsdom');
10-
const DOM = require('../../../report/renderer/dom.js');
11-
const pageFunctions = require('../../lib/page-functions.js');
8+
// TODO(esmodules): Node 14, 16 crash with `--experimental-vm-modules` if require and import
9+
// are used in the same test file.
10+
// See https://github.com/GoogleChrome/lighthouse/pull/12702#issuecomment-876832620
11+
12+
/** @type {import('assert').strict} */
13+
let assert;
14+
/** @type {import('jsdom').strict} */
15+
let jsdom;
16+
/** @type {import('../../lib/page-functions.js')} */
17+
let pageFunctions;
18+
/** @type {import('../../../report/renderer/dom.js').DOM} */
19+
let DOM;
1220

1321
/* eslint-env jest */
1422

1523
describe('Page Functions', () => {
1624
const url = 'http://www.example.com';
1725
let dom;
1826

19-
beforeAll(() => {
27+
beforeAll(async () => {
28+
assert = (await import('assert')).strict;
29+
jsdom = await import('jsdom');
30+
pageFunctions = (await import('../../lib/page-functions.js')).default;
31+
DOM = (await import('../../../report/renderer/dom.js')).DOM;
32+
2033
const {document, ShadowRoot, Node, HTMLElement} = new jsdom.JSDOM('', {url}).window;
2134
global.ShadowRoot = ShadowRoot;
2235
global.Node = Node;

0 commit comments

Comments
 (0)