Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

report: convert to ES modules #12702

Merged
merged 86 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from 75 commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
2b9cec9
first pass
connorjclark Jun 4, 2021
e89d095
rollup yum
connorjclark Jun 4, 2021
4e3800b
Merge remote-tracking branch 'origin/master' into esmodules
connorjclark Jun 21, 2021
9b5e303
copy and transform util.js to commonjs
connorjclark Jun 21, 2021
f32562d
tweak
connorjclark Jun 22, 2021
e6bb972
update for cdt
connorjclark Jun 22, 2021
937c710
import
connorjclark Jun 22, 2021
c745cbd
remove some changes
connorjclark Jun 23, 2021
aeea6a8
use render.js for standalone
connorjclark Jun 23, 2021
f34a012
tweak
connorjclark Jun 23, 2021
d24381f
fix tests. defer common entry point.
connorjclark Jun 24, 2021
5c66cfe
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jun 25, 2021
0107c03
report/clients
connorjclark Jun 25, 2021
cc3b98c
terser
connorjclark Jun 25, 2021
18eb5a4
tsignore
connorjclark Jun 25, 2021
0ac3a82
comments
connorjclark Jun 25, 2021
ad0974c
fix viewer
connorjclark Jun 25, 2021
593906b
fix devtools action
connorjclark Jun 25, 2021
3a38115
build: add build step for report
connorjclark Jun 25, 2021
53033c8
mkdir
connorjclark Jun 25, 2021
9b425a0
build that shiz
connorjclark Jun 25, 2021
19e11bb
rm
connorjclark Jun 25, 2021
844d5bf
ohhhh windows
connorjclark Jun 25, 2021
cafef7c
ugh
connorjclark Jun 25, 2021
9073d97
fix viewer
connorjclark Jun 25, 2021
f574fe2
fix collect strings
connorjclark Jun 25, 2021
a66a626
update psi.js
connorjclark Jun 29, 2021
f36cbab
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jun 29, 2021
008c3b0
rm autobuild
connorjclark Jun 29, 2021
aee255b
convert tests
connorjclark Jun 29, 2021
489e1a8
convert report tests
connorjclark Jun 29, 2021
cf5480a
remove global declaring stuff
connorjclark Jun 30, 2021
0ea11ab
fix treemap
connorjclark Jun 30, 2021
6579cce
--experimental-vm-modules
connorjclark Jun 30, 2021
214eb77
yarn jest
connorjclark Jun 30, 2021
91b6926
build report
connorjclark Jun 30, 2021
353c772
dist
connorjclark Jun 30, 2021
fd5128b
Merge remote-tracking branch 'origin/master' into build-report
connorjclark Jun 30, 2021
060eced
fix yarn jest
connorjclark Jun 30, 2021
e1627a8
Merge branch 'build-report' into report-esmodules
connorjclark Jun 30, 2021
bef23c1
yarn build-report
connorjclark Jun 30, 2021
bafdc7e
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jun 30, 2021
5805f34
lol
connorjclark Jun 30, 2021
1340b49
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jun 30, 2021
f1b4351
tweak
connorjclark Jun 30, 2021
7c9d522
only
connorjclark Jun 30, 2021
be94766
tweak roll script
connorjclark Jun 30, 2021
c419c30
tweak
connorjclark Jun 30, 2021
9ec4934
fix dt build
connorjclark Jun 30, 2021
329f09b
fix test
connorjclark Jun 30, 2021
a3bb85a
psi
connorjclark Jun 30, 2021
a8bb6f0
fix yarn test-clients
connorjclark Jul 1, 2021
d78111a
scripts(i18n): support es modules in collect-strings
connorjclark Jul 1, 2021
cd3e92a
more root
connorjclark Jul 2, 2021
258dcaf
fix script tests
connorjclark Jul 2, 2021
7329b3c
Merge branch 'esmodules-collect' into report-esmodules
connorjclark Jul 2, 2021
2e01344
ci
connorjclark Jul 2, 2021
8307eaf
just import
connorjclark Jul 2, 2021
1f386b7
rm
connorjclark Jul 2, 2021
9f50d9d
update
connorjclark Jul 2, 2021
642289b
revertests
connorjclark Jul 2, 2021
3e9f32e
more revert
connorjclark Jul 2, 2021
2a43904
Merge commit '2e013444f7eaf7d3b1249f5166a607274aa7c2c9' into report-e…
connorjclark Jul 2, 2021
a570796
update
connorjclark Jul 2, 2021
6a2bda5
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jul 8, 2021
a4488b4
fix async test
connorjclark Jul 8, 2021
fa1c6dd
yarn jest
connorjclark Jul 8, 2021
f9c508e
copy util again
connorjclark Jul 8, 2021
ebee371
fix for windows
connorjclark Jul 8, 2021
c6a6ef8
tmp no fail fast
connorjclark Jul 9, 2021
0f0fcee
tmp: debuging with runInBand
connorjclark Jul 9, 2021
a983e75
lets get that stacktrace
connorjclark Jul 9, 2021
7f5839e
node crash workaround
connorjclark Jul 9, 2021
3b8f9bb
comment
connorjclark Jul 9, 2021
b73d2c6
update
connorjclark Jul 13, 2021
292d3a8
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jul 13, 2021
46ca260
comment
connorjclark Jul 13, 2021
baf5e55
build: run build-report for vercel deployment
connorjclark Jul 13, 2021
d6d93b9
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jul 13, 2021
b406a6c
Merge branch 'fixnowbuildreport' into report-esmodules
connorjclark Jul 13, 2021
3679313
fix
connorjclark Jul 13, 2021
f9ff110
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jul 13, 2021
c7dfe92
fix standalone bundle
connorjclark Jul 14, 2021
860cb29
Merge remote-tracking branch 'origin/master' into report-esmodules
connorjclark Jul 14, 2021
547afdf
add comment for cdt
connorjclark Jul 15, 2021
88a86fe
fix un upstreamed psi fix
connorjclark Jul 15, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ jobs:
- run: yarn test-legacy-javascript
- run: yarn i18n:checks
- run: yarn dogfood-lhci
- run: bash lighthouse-core/scripts/copy-util-commonjs.sh

# Fail if any changes were written to any source files or generated untracked files (ex, from: build/build-cdt-lib.js).
- run: git add -A && git diff --cached --exit-code
Expand Down
4 changes: 2 additions & 2 deletions build/build-dt-report-resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ writeFile('report-generator.d.ts', 'export {}');

const pathToReportAssets = require.resolve('../clients/devtools-report-assets.js');
browserify(generatorFilename, {standalone: 'Lighthouse.ReportGenerator'})
// Shims './html/html-report-assets.js' to resolve to devtools-report-assets.js
.require(pathToReportAssets, {expose: './html/html-report-assets.js'})
// Shims './report/report-assets.js' to resolve to devtools-report-assets.js
.require(pathToReportAssets, {expose: './report-assets.js'})
.bundle((err, src) => {
if (err) throw err;
fs.writeFileSync(bundleOutFile, src.toString());
Expand Down
2 changes: 2 additions & 0 deletions build/build-lightrider-bundles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const fs = require('fs');
const path = require('path');
const bundleBuilder = require('./build-bundle.js');
const {minifyFileTransform} = require('./build-utils.js');
const {buildPsiReport} = require('./build-report.js');
const {LH_ROOT} = require('../root.js');

const distDir = path.join(LH_ROOT, 'dist', 'lightrider');
Expand Down Expand Up @@ -53,6 +54,7 @@ async function run() {
await Promise.all([
buildEntryPoint(),
buildReportGenerator(),
buildPsiReport(),
]);
}

Expand Down
112 changes: 83 additions & 29 deletions build/build-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,96 @@
*/
'use strict';

const fs = require('fs');

function concatRendererCode() {
return [
fs.readFileSync(__dirname + '/../report/renderer/util.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/dom.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/details-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/crc-details-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/snippet-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/element-screenshot-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../lighthouse-core/lib/file-namer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/logger.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/report-ui-features.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/category-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/performance-category-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/pwa-category-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/report-renderer.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/i18n.js', 'utf8'),
fs.readFileSync(__dirname + '/../report/renderer/text-encoding.js', 'utf8'),
].join(';\n');
}
const rollup = require('rollup');
const {terser} = require('rollup-plugin-terser');
// Only needed b/c getFilenamePrefix loads a commonjs module.
const commonjs =
// @ts-expect-error types are wrong.
/** @type {import('rollup-plugin-commonjs').default} */ (require('rollup-plugin-commonjs'));

async function buildStandaloneReport() {
const REPORT_JAVASCRIPT = [
concatRendererCode(),
fs.readFileSync(__dirname + '/../report/clients/standalone.js', 'utf8'),
].join(';\n');
fs.mkdirSync(__dirname + '/../dist/report', {recursive: true});
fs.writeFileSync(__dirname + '/../dist/report/standalone.js', REPORT_JAVASCRIPT);
const bundle = await rollup.rollup({
input: 'report/clients/standalone.js',
plugins: [
commonjs(),
terser(),
],
});

await bundle.write({
file: 'dist/report/standalone.js',
format: 'iife',
});
}

async function buildPsiReport() {
const bundle = await rollup.rollup({
input: 'report/clients/psi.js',
plugins: [
commonjs(),
],
});

await bundle.write({
file: 'dist/report/psi.js',
format: 'esm',
});
}

async function buildViewerReport() {
const bundle = await rollup.rollup({
input: 'report/clients/viewer.js',
plugins: [
commonjs(),
],
});

await bundle.write({
file: 'dist/report/viewer.js',
format: 'iife',
});
}

async function buildTreemapReport() {
const bundle = await rollup.rollup({
input: 'report/clients/treemap.js',
plugins: [
commonjs(),
],
});

await bundle.write({
file: 'dist/report/treemap.js',
format: 'iife',
});
}

async function buildEsModulesBundle() {
const bundle = await rollup.rollup({
input: 'report/clients/bundle.js',
plugins: [
commonjs(),
],
});

await bundle.write({
file: 'dist/report/bundle.js',
format: 'esm',
});
}

if (require.main === module) {
buildStandaloneReport();
if (process.argv[2] === '--only-standalone') {
buildStandaloneReport();
} else {
buildStandaloneReport();
buildEsModulesBundle();
}
}

module.exports = {
buildStandaloneReport,
concatRendererCode,
buildPsiReport,
buildViewerReport,
buildTreemapReport,
};
7 changes: 4 additions & 3 deletions build/build-treemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/** @typedef {import('../lighthouse-core/lib/i18n/locales').LhlMessages} LhlMessages */

const fs = require('fs');
const {buildTreemapReport} = require('./build-report.js');
const GhPagesApp = require('./gh-pages-app.js');
const {LH_ROOT} = require('../root.js');

Expand Down Expand Up @@ -45,6 +46,8 @@ function buildStrings() {
* Build treemap app, optionally deploying to gh-pages if `--deploy` flag was set.
*/
async function run() {
await buildTreemapReport();

const app = new GhPagesApp({
name: 'treemap',
appDir: `${LH_ROOT}/lighthouse-treemap/app`,
Expand All @@ -65,9 +68,7 @@ async function run() {
fs.readFileSync(require.resolve('pako/dist/pako_inflate.js'), 'utf-8'),
/* eslint-enable max-len */
buildStrings(),
{path: '../../report/renderer/logger.js'},
{path: '../../report/renderer/i18n.js'},
{path: '../../report/renderer/text-encoding.js'},
{path: '../../dist/report/treemap.js'},
{path: '../../lighthouse-viewer/app/src/drag-and-drop.js'},
{path: '../../lighthouse-viewer/app/src/github-api.js'},
{path: '../../lighthouse-viewer/app/src/firebase-auth.js'},
Expand Down
6 changes: 4 additions & 2 deletions build/build-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fs = require('fs');
const browserify = require('browserify');
const GhPagesApp = require('./gh-pages-app.js');
const {minifyFileTransform} = require('./build-utils.js');
const {concatRendererCode} = require('./build-report.js');
const {buildViewerReport} = require('./build-report.js');
const htmlReportAssets = require('../report/report-assets.js');
const {LH_ROOT} = require('../root.js');

Expand All @@ -32,6 +32,8 @@ async function run() {
});
});

await buildViewerReport();

const app = new GhPagesApp({
name: 'viewer',
appDir: `${LH_ROOT}/lighthouse-viewer/app`,
Expand All @@ -45,8 +47,8 @@ async function run() {
],
javascripts: [
await generatorJsPromise,
concatRendererCode(),
fs.readFileSync(require.resolve('idb-keyval/dist/idb-keyval-min.js'), 'utf8'),
{path: '../../dist/report/viewer.js'},
{path: 'src/*'},
],
assets: [
Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/audits/audit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

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

const DEFAULT_PASS = 'defaultPass';

Expand Down
2 changes: 1 addition & 1 deletion lighthouse-core/computed/resource-summary.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const URL = require('../lib/url-shim.js');
const NetworkRequest = require('../lib/network-request.js');
const MainResource = require('./main-resource.js');
const Budget = require('../config/budget.js');
const Util = require('../../report/renderer/util.js');
const Util = require('../util-commonjs.js');

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

Expand Down
5 changes: 1 addition & 4 deletions lighthouse-core/lib/file-namer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,4 @@ function getFilenamePrefix(lhr) {
return filenamePrefix.replace(/[/?<>\\:*|"]/g, '-');
}

// don't attempt to export in the browser.
if (typeof module !== 'undefined' && module.exports) {
module.exports = {getFilenamePrefix};
}
module.exports = {getFilenamePrefix};
2 changes: 1 addition & 1 deletion lighthouse-core/lib/url-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* URL shim so we keep our code DRY
*/

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

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

Expand Down
20 changes: 20 additions & 0 deletions lighthouse-core/scripts/copy-util-commonjs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

##
# @license Copyright 2021 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.
##

# TODO(esmodules): delete when consumers of util.js are all esm.

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
LH_ROOT_DIR="$SCRIPT_DIR/../.."

OUT_FILE="$LH_ROOT_DIR"/lighthouse-core/util-commonjs.js

echo '// @ts-nocheck' > "$OUT_FILE"
echo '// Auto-generated by lighthouse-core/scripts/copy-util-commonjs.sh' >> "$OUT_FILE"
echo '// Temporary solution until all our code uses esmodules' >> "$OUT_FILE"
sed 's/export class Util/class Util/g; s/export const UIStrings = Util.UIStrings;//g' "$LH_ROOT_DIR"/report/renderer/util.js >> "$OUT_FILE"
echo 'module.exports = Util;' >> "$OUT_FILE"
3 changes: 2 additions & 1 deletion lighthouse-core/scripts/i18n/collect-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const path = require('path');
const expect = require('expect');
const tsc = require('typescript');
const MessageParser = require('intl-messageformat-parser').default;
const Util = require('../../../report/renderer/util.js');
const Util = require('../../../lighthouse-core/util-commonjs.js');
const {collectAndBakeCtcStrings} = require('./bake-ctc-to-lhl.js');
const {pruneObsoleteLhlMessages} = require('./prune-obsolete-lhl-messages.js');
const {countTranslatedMessages} = require('./count-translated.js');
Expand All @@ -41,6 +41,7 @@ const ignoredPathComponents = [
'**/test/**',
'**/*-test.js',
'**/*-renderer.js',
'**/util-commonjs.js',
'lighthouse-treemap/app/src/main.js',
];

Expand Down
12 changes: 10 additions & 2 deletions lighthouse-core/scripts/roll-to-devtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,17 @@ lh_bg_js="dist/lighthouse-dt-bundle.js"
yarn build-report
yarn build-devtools

# copy lighthouse-dt-bundle (potentially stale)
# copy lighthouse-dt-bundle
cp -pPR "$lh_bg_js" "$fe_lh_dir/lighthouse-dt-bundle.js"
echo -e "$check (Potentially stale) lighthouse-dt-bundle copied."
echo -e "$check lighthouse-dt-bundle copied."

# generate bundle.d.ts
npx tsc --allowJs --declaration --emitDeclarationOnly dist/report/bundle.js

# copy report code $fe_lh_dir
fe_lh_report_dir="$fe_lh_dir/report/"
cp dist/report/bundle.js dist/report/bundle.d.ts "$fe_lh_report_dir"
echo -e "$check Report code copied."

# copy report generator + cached resources into $fe_lh_dir
fe_lh_report_assets_dir="$fe_lh_dir/report-assets/"
Expand Down
23 changes: 18 additions & 5 deletions lighthouse-core/test/lib/page-functions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,31 @@
*/
'use strict';

const assert = require('assert').strict;
const jsdom = require('jsdom');
const DOM = require('../../../report/renderer/dom.js');
const pageFunctions = require('../../lib/page-functions.js');
// TODO(esmodules): Node 14, 16 crash with `--experimental-vm-modules` if require and import
// are used in the same test file.
// See https://github.com/GoogleChrome/lighthouse/pull/12702#issuecomment-876832620

/** @type {import('assert').strict} */
let assert;
/** @type {import('jsdom').strict} */
let jsdom;
/** @type {import('../../lib/page-functions.js')} */
let pageFunctions;
/** @type {import('../../../report/renderer/dom.js').DOM} */
let DOM;

/* eslint-env jest */

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

beforeAll(() => {
beforeAll(async () => {
assert = (await import('assert')).strict;
jsdom = await import('jsdom');
pageFunctions = (await import('../../lib/page-functions.js')).default;
DOM = (await import('../../../report/renderer/dom.js')).DOM;

const {document, ShadowRoot, Node, HTMLElement} = new jsdom.JSDOM('', {url}).window;
global.ShadowRoot = ShadowRoot;
global.Node = Node;
Expand Down
Loading