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

misc: convert lighthouse-core/scripts to ES modules #13121

Merged
merged 12 commits into from
Sep 28, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
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
8 changes: 4 additions & 4 deletions lighthouse-core/lib/page-functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -530,17 +530,17 @@ module.exports = {
getElementsInDocument,
getElementsInDocumentString: getElementsInDocument.toString(),
getOuterHTMLSnippetString: getOuterHTMLSnippet.toString(),
getOuterHTMLSnippet: getOuterHTMLSnippet,
computeBenchmarkIndex: computeBenchmarkIndex,
getOuterHTMLSnippet,
computeBenchmarkIndex,
computeBenchmarkIndexString: computeBenchmarkIndex.toString(),
getMaxTextureSize,
getNodeDetailsString,
getNodeDetails,
getNodePathString: getNodePath.toString(),
getNodeSelectorString: getNodeSelector.toString(),
getNodePath,
getNodeSelector: getNodeSelector,
getNodeLabel: getNodeLabel,
getNodeSelector,
getNodeLabel,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive by changes... I was hoping these changes would be enough to pass Node's commonjs named exports heuristics, but it was not. Anyhow, these properties don't need to use renaming syntax.

getNodeLabelString: getNodeLabel.toString(),
isPositionFixedString: isPositionFixed.toString(),
wrapRequestIdleCallback,
Expand Down
27 changes: 27 additions & 0 deletions lighthouse-core/scripts/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @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.
*/
'use strict';

module.exports = {
env: {
browser: true,
},
rules: {
// TODO(esmodules): move to root eslint when all code is ESM
// or when this is resolved: https://github.com/import-js/eslint-plugin-import/issues/2214
'import/order': [2, {
'groups': [
'builtin',
'external',
['sibling', 'parent'],
'index',
'object',
'type',
],
'newlines-between': 'always',
}],
},
};
10 changes: 5 additions & 5 deletions lighthouse-core/scripts/benchmark-plus-extras.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
*/
'use strict';

/* global document */

/**
* @fileoverview This script computes the BenchmarkIndex and a few other related browser benchmarks.
* node lighthouse-core/scripts/benchmark-plus-extras.js
*/

const puppeteer = require('puppeteer');
const {computeBenchmarkIndex} = require('../lib/page-functions.js');
import puppeteer from 'puppeteer';

import pageFunctions from '../lib/page-functions.js';

/** @param {import('puppeteer').Page} page */
async function runOctane(page) {
Expand Down Expand Up @@ -80,7 +80,7 @@ async function main() {

process.stdout.write(`Running BenchmarkIndex...\n`);
for (let i = 0; i < 10; i++) {
const BenchmarkIndex = await page.evaluate(computeBenchmarkIndex);
const BenchmarkIndex = await page.evaluate(pageFunctions.computeBenchmarkIndex);
process.stdout.write(` ${i + 1}: BenchmarkIndex=${BenchmarkIndex}\n`);
}

Expand Down
6 changes: 4 additions & 2 deletions lighthouse-core/scripts/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@

/* eslint-disable no-console */

const computeBenchmarkIndex = require('../lib/page-functions.js').computeBenchmarkIndex;
// node lighthouse-core/scripts/benchmark.js

import pageFunctions from '../lib/page-functions.js';

console.log('Computing BenchmarkIndex 10 times...');

let total = 0;
for (let i = 0; i < 10; i++) {
const result = computeBenchmarkIndex();
const result = pageFunctions.computeBenchmarkIndex();
console.log(`Result ${i + 1}: ${result.toFixed(0)}`);
total += result;
}
Expand Down
23 changes: 13 additions & 10 deletions lighthouse-core/scripts/build-test-flow-report.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@
*/
'use strict';

const fs = require('fs');
const open = require('open');
const {execFileSync} = require('child_process');
// node lighthouse-core/scripts/build-test-flow-report.js

import fs from 'fs';
import {execFileSync} from 'child_process';

import open from 'open';

import reportGenerator from '../../report/generator/report-generator.js';
import {LH_ROOT, readJson} from '../../root.js';

execFileSync(`yarn`, ['build-report', '--standalone']);
const reportGenerator = require('../../report/generator/report-generator.js');

const flow = JSON.parse(fs.readFileSync(
`${__dirname}/../test/fixtures/fraggle-rock/reports/sample-lhrs.json`,
'utf-8')
);
const flow = readJson(
`${LH_ROOT}/lighthouse-core/test/fixtures/fraggle-rock/reports/sample-lhrs.json`);

const htmlReport = reportGenerator.generateFlowReportHtml(flow);

fs.writeFileSync(`${__dirname}/../../flow.report.html`, htmlReport);
open(`${__dirname}/../../flow.report.html`);
fs.writeFileSync(`${LH_ROOT}/flow.report.html`, htmlReport);
open(`${LH_ROOT}/flow.report.html`);
2 changes: 1 addition & 1 deletion lighthouse-core/scripts/cleanup-LHR-for-diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

/** @fileoverview Read in a LHR JSON file, remove whatever shouldn't be compared, write it back. */

const {readFileSync, writeFileSync} = require('fs');
import {readFileSync, writeFileSync} from 'fs';

const filename = process.argv[2];
const extraFlag = process.argv[3];
Expand Down
11 changes: 8 additions & 3 deletions lighthouse-core/scripts/cleanup-vuln-snapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@

/** @fileoverview Read in the snyk snapshot, remove whatever we don't need, write it back */

const {readFileSync, writeFileSync} = require('fs');
const prettyJSONStringify = require('pretty-json-stringify');
const libDetectorSource = readFileSync(require.resolve('js-library-detector/library/libraries.js'),
import {readFileSync, writeFileSync} from 'fs';

import prettyJSONStringify from 'pretty-json-stringify';

import {resolveModulePath} from './esm-utils.js';

const libDetectorSource = readFileSync(
resolveModulePath('js-library-detector/library/libraries.js'),
'utf8'
);

Expand Down
25 changes: 15 additions & 10 deletions lighthouse-core/scripts/compare-runs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,24 @@
// The script will report both timings and perf metric results. View just one of them by using --filter:
// node lighthouse-core/scripts/compare-runs.js --summarize --name pr --filter=metric

const fs = require('fs');
const mkdir = fs.promises.mkdir;
const glob = require('glob');
const util = require('util');
const execFile = util.promisify(require('child_process').execFile);
const yargs = require('yargs');
const {LH_ROOT} = require('../../root.js');
import fs from 'fs';
import util from 'util';
import childProcess from 'child_process';

import glob from 'glob';
import yargs from 'yargs';
import * as yargsHelpers from 'yargs/helpers';

const {ProgressLogger} = require('./lantern/collect/common.js');
import {LH_ROOT} from '../../root.js';
import {ProgressLogger} from './lantern/collect/common.js';

const mkdir = fs.promises.mkdir;
const execFile = util.promisify(childProcess.execFile);

const ROOT_OUTPUT_DIR = `${LH_ROOT}/timings-data`;

const rawArgv = yargs
const y = yargs(yargsHelpers.hideBin(process.argv));
const rawArgv = y
.help('help')
.describe({
// common flags
Expand Down Expand Up @@ -64,7 +69,7 @@ const rawArgv = yargs
.default('sort-by-absolute-value', false)
.default('lh-flags', '')
.strict() // fail on unknown commands
.wrap(yargs.terminalWidth())
.wrap(y.terminalWidth())
.argv;

// Augmenting yargs type with auto-camelCasing breaks in tsc@4.1.2 and @types/yargs@15.0.11,
Expand Down
18 changes: 18 additions & 0 deletions lighthouse-core/scripts/esm-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file may later find its way into lighthouse-core, depending on if it also needs require.resolve (I think it will?)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely will though I imagine might need more context/bundle awareness

* @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.
*/
'use strict';

import module from 'module';

const require = module.createRequire(import.meta.url);

/**
* Commonjs equivalent of `require.resolve`.
* @param {string} packageName
*/
export function resolveModulePath(packageName) {
return require.resolve(packageName);
}
4 changes: 2 additions & 2 deletions lighthouse-core/scripts/gcp-collection/analyze-lhr-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/

/* eslint-disable no-console */
const {readdirSync, readFileSync} = require('fs');
const {join} = require('path');
import {readdirSync, readFileSync} from 'fs';
import {join} from 'path';

const directory = process.argv[2];
const audit = process.argv[3];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
* USAGE: node lighthouse-core/scripts/gcp-collection/fleet-create-directories.js [<url list file>]
*/

const fs = require('fs');
const path = require('path');
const {LH_ROOT} = require('../../../root.js');
import fs from 'fs';
import path from 'path';

import {LH_ROOT} from '../../../root.js';

const TMP_DIR = path.join(LH_ROOT, '.tmp/gcp-instances');
const URLS_LIST = process.argv[2]
? path.resolve(process.cwd(), process.argv[2])
: path.join(__dirname, 'urls.txt');
: path.join(LH_ROOT, 'lighthouse-core/scripts/gcp-collection/urls.txt');

fs.rmdirSync(TMP_DIR, {recursive: true});
fs.mkdirSync(TMP_DIR);
Expand All @@ -44,9 +45,10 @@ instanceUrls.forEach((urls, i) => {

const dir = path.join(TMP_DIR, `instance${MACHINE_BASE_INDEX + i}`);
fs.mkdirSync(dir);
const files = fs.readdirSync(__dirname).filter(f => f.endsWith('.sh'));
const scriptDir = `${LH_ROOT}/lighthouse-core/scripts/gcp-collection`;
const files = fs.readdirSync(scriptDir).filter(f => f.endsWith('.sh'));
files.forEach(f =>
fs.copyFileSync(path.join(__dirname, f), path.join(dir, f))
fs.copyFileSync(path.join(scriptDir, f), path.join(dir, f))
);
fs.writeFileSync(path.join(dir, 'urls.txt'), urls.join('\n'));
});
18 changes: 11 additions & 7 deletions lighthouse-core/scripts/generate-timing-trace.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
*/
'use strict';

const fs = require('fs');
const path = require('path');
const {createTraceString} = require('../lib/timing-trace-saver.js');

/**
* @fileoverview This script takes the timing entries saved during a Lighthouse run and generates
* a trace file that's readable in DevTools perf panel or chrome://tracing.
*
* input = LHR.json
* output = LHR.timing.trace.json
* node lighthouse-core/scripts/generate-timing-trace.js latest-run/lhr.report.json
*
* input = lhr.json
* output = lhr.timing.trace.json
*/

import fs from 'fs';
import path from 'path';

import {createTraceString} from '../lib/timing-trace-saver.js';

/**
* @param {string} msg
*/
Expand Down Expand Up @@ -45,7 +48,8 @@ function saveTraceFromCLI() {
const lhrObject = JSON.parse(fs.readFileSync(filename, 'utf8'));
const jsonStr = createTraceString(lhrObject);

const traceFilePath = `${filename}.timing.trace.json`;
const pathObj = path.parse(filename);
const traceFilePath = path.join(pathObj.dir, `${pathObj.name}.timing.trace.json`);
fs.writeFileSync(traceFilePath, jsonStr, 'utf8');
// eslint-disable-next-line no-console
console.log(`
Expand Down
13 changes: 6 additions & 7 deletions lighthouse-core/scripts/i18n/bake-ctc-to-lhl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

/* eslint-disable no-console */

const fs = require('fs');
const path = require('path');
const {LH_ROOT} = require('../../../root.js');
import fs from 'fs';
import path from 'path';

import {LH_ROOT, readJson} from '../../../root.js';

/**
* @typedef CtcMessage
Expand Down Expand Up @@ -102,9 +103,7 @@ function bakePlaceholders(messages) {
function loadCtcStrings(file) {
if (!file.endsWith('.ctc.json')) throw new Error('Can only load ctc files');

const rawdata = fs.readFileSync(file, 'utf8');
const messages = JSON.parse(rawdata);
return messages;
return readJson(file);
}

/**
Expand Down Expand Up @@ -137,7 +136,7 @@ function collectAndBakeCtcStrings(dir) {
return lhlFilenames;
}

module.exports = {
export {
collectAndBakeCtcStrings,
bakePlaceholders,
};
32 changes: 18 additions & 14 deletions lighthouse-core/scripts/i18n/collect-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,21 @@

/* eslint-disable no-console, max-len */

const fs = require('fs');
const glob = require('glob');
const path = require('path');
const expect = require('expect');
const tsc = require('typescript');
const MessageParser = require('intl-messageformat-parser').default;
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');
const {LH_ROOT} = require('../../../root.js');
import fs from 'fs';
import path from 'path';

import glob from 'glob';
import expect from 'expect';
import tsc from 'typescript';
import MessageParser from 'intl-messageformat-parser';
import esMain from 'es-main';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to add this to our dev deps

Copy link
Collaborator Author

@connorjclark connorjclark Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

es-main is already there tho

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

es-main is already there tho

haha oh right


import Util from '../../../lighthouse-core/util-commonjs.js';
import {collectAndBakeCtcStrings} from './bake-ctc-to-lhl.js';
import {pruneObsoleteLhlMessages} from './prune-obsolete-lhl-messages.js';
import {countTranslatedMessages} from './count-translated.js';
import {LH_ROOT} from '../../../root.js';
import {resolveModulePath} from '../esm-utils.js';

const UISTRINGS_REGEX = /UIStrings = .*?\};\n/s;

Expand All @@ -30,7 +34,7 @@ const foldersWithStrings = [
`${LH_ROOT}/lighthouse-core`,
`${LH_ROOT}/report/renderer`,
`${LH_ROOT}/lighthouse-treemap`,
path.dirname(require.resolve('lighthouse-stack-packs')) + '/packs',
path.dirname(resolveModulePath('lighthouse-stack-packs')) + '/packs',
];

const ignoredPathComponents = [
Expand Down Expand Up @@ -722,14 +726,14 @@ async function main() {
}

// Test if called from the CLI or as a module.
if (require.main === module) {
if (esMain(import.meta)) {
main().catch(err => {
console.error(err.stack);
process.exit(1);
});
}

module.exports = {
export {
parseUIStrings,
createPsuedoLocaleStrings,
convertMessageToCtc,
Expand Down
Loading