-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
cli: convert to ES modules #13045
cli: convert to ES modules #13045
Changes from 23 commits
0b0fbc4
dc10e88
f4b6656
2af3d97
03e996f
705a9d5
10558a2
22daf0e
6eadebf
453571d
d97ad94
2f11d0d
76c906a
d11f410
3829720
4306743
3427307
ff317a8
3256ac4
5a9dd20
5e8aa8c
852ae22
89726e6
593a629
98aa222
2f8b7da
dcca468
0800684
f6cdebe
662a727
a9bb355
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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', | ||
}], | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,19 +18,23 @@ | |
* cli-flags lh-core/index | ||
*/ | ||
|
||
const fs = require('fs'); | ||
const path = require('path'); | ||
const commands = require('./commands/commands.js'); | ||
const printer = require('./printer.js'); | ||
const {getFlags} = require('./cli-flags.js'); | ||
const {runLighthouse} = require('./run.js'); | ||
const {generateConfig} = require('../lighthouse-core/index.js'); | ||
const log = require('lighthouse-logger'); | ||
const pkg = require('../package.json'); | ||
const Sentry = require('../lighthouse-core/lib/sentry.js'); | ||
const updateNotifier = require('update-notifier'); | ||
const {askPermission} = require('./sentry-prompt.js'); | ||
const {LH_ROOT} = require('../root.js'); | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
import * as url from 'url'; | ||
|
||
import log from 'lighthouse-logger'; | ||
import updateNotifier from 'update-notifier'; | ||
|
||
import * as commands from './commands/commands.js'; | ||
import * as Printer from './printer.js'; | ||
import {getFlags} from './cli-flags.js'; | ||
import {runLighthouse} from './run.js'; | ||
import lighthouse from '../lighthouse-core/index.js'; | ||
import * as Sentry from '../lighthouse-core/lib/sentry.js'; | ||
import {askPermission} from './sentry-prompt.js'; | ||
import {LH_ROOT} from '../root.js'; | ||
|
||
const pkg = JSON.parse(fs.readFileSync(LH_ROOT + '/package.json', 'utf-8')); | ||
|
||
/** | ||
* @return {boolean} | ||
|
@@ -58,16 +62,22 @@ async function begin() { | |
commands.listTraceCategories(); | ||
} | ||
|
||
const url = cliFlags._[0]; | ||
const urlUnderTest = cliFlags._[0]; | ||
|
||
/** @type {LH.Config.Json|undefined} */ | ||
let configJson; | ||
if (cliFlags.configPath) { | ||
// Resolve the config file path relative to where cli was called. | ||
cliFlags.configPath = path.resolve(process.cwd(), cliFlags.configPath); | ||
configJson = require(cliFlags.configPath); | ||
|
||
if (cliFlags.configPath.endsWith('.json')) { | ||
configJson = JSON.parse(fs.readFileSync(cliFlags.configPath, 'utf-8')); | ||
} else { | ||
const configModuleUrl = url.pathToFileURL(cliFlags.configPath).href; | ||
configJson = (await import(configModuleUrl)).default; | ||
} | ||
} else if (cliFlags.preset) { | ||
configJson = require(`../lighthouse-core/config/${cliFlags.preset}-config.js`); | ||
configJson = (await import(`../lighthouse-core/config/${cliFlags.preset}-config.js`)).default; | ||
Comment on lines
+73
to
+80
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems like a place where a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't that fail for es modules? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah, good point :( |
||
} | ||
|
||
if (cliFlags.budgetPath) { | ||
|
@@ -88,7 +98,7 @@ async function begin() { | |
|
||
if ( | ||
cliFlags.output.length === 1 && | ||
cliFlags.output[0] === printer.OutputMode.json && | ||
cliFlags.output[0] === Printer.OutputMode.json && | ||
!cliFlags.outputPath | ||
) { | ||
cliFlags.outputPath = 'stdout'; | ||
|
@@ -106,7 +116,7 @@ async function begin() { | |
} | ||
|
||
if (cliFlags.printConfig) { | ||
const config = generateConfig(configJson, cliFlags); | ||
const config = lighthouse.generateConfig(configJson, cliFlags); | ||
process.stdout.write(config.getPrintString()); | ||
return; | ||
} | ||
|
@@ -119,7 +129,7 @@ async function begin() { | |
} | ||
if (cliFlags.enableErrorReporting) { | ||
Sentry.init({ | ||
url, | ||
url: urlUnderTest, | ||
flags: cliFlags, | ||
environmentData: { | ||
name: 'redacted', // prevent sentry from using hostname | ||
|
@@ -132,9 +142,9 @@ async function begin() { | |
}); | ||
} | ||
|
||
return runLighthouse(url, cliFlags, configJson); | ||
return runLighthouse(urlUnderTest, cliFlags, configJson); | ||
} | ||
|
||
module.exports = { | ||
begin, | ||
export { | ||
begin | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not an emergency, but it feels like we probably want 'comma-dangle': [2, {
imports: 'always-multiline',
exports: 'always-multiline',
}] seems weird not to have one There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. agreed, will put up a separate PR |
||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,18 +7,23 @@ | |
|
||
/* eslint-disable max-len */ | ||
|
||
const yargs = require('yargs'); | ||
const fs = require('fs'); | ||
const {isObjectOfUnknownValues} = require('../lighthouse-core/lib/type-verifiers.js'); | ||
import * as fs from 'fs'; | ||
connorjclark marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import yargs from 'yargs'; | ||
import * as yargsHelpers from 'yargs/helpers'; | ||
|
||
import {isObjectOfUnknownValues} from '../lighthouse-core/lib/type-verifiers.js'; | ||
|
||
/** | ||
* @param {string=} manualArgv | ||
* @param {{noExitOnFailure?: boolean}=} options | ||
* @return {LH.CliFlags} | ||
*/ | ||
function getFlags(manualArgv, options = {}) { | ||
// @ts-expect-error - undocumented, but yargs() supports parsing a single `string`. | ||
const y = manualArgv ? yargs(manualArgv) : yargs; | ||
const y = manualArgv ? | ||
// @ts-expect-error - undocumented, but yargs() supports parsing a single `string`. | ||
yargs(manualArgv) : | ||
yargs(yargsHelpers.hideBin(process.argv)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why the change? Seems way worse than having There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh. at one point I may have tried updating yargs and found this to be necessary. I can probably revert. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm... not sure what changed, as there is no yargs upgrade, but the old code results in this error:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
must be some change to the esmodules export. Here's a similar case: yargs/yargs#1774 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shurg. this is exactly what their demo code does https://github.com/yargs/yargs#esm tweaking the import to be |
||
|
||
let parser = y.help('help') | ||
.showHelpOnFail(false, 'Specify --help for available options') | ||
|
@@ -318,7 +323,7 @@ function getFlags(manualArgv, options = {}) { | |
throw new Error('Please provide a url'); | ||
}) | ||
.epilogue('For more information on Lighthouse, see https://developers.google.com/web/tools/lighthouse/.') | ||
.wrap(yargs.terminalWidth()); | ||
.wrap(y.terminalWidth()); | ||
|
||
if (options.noExitOnFailure) { | ||
// Silence console.error() logging and don't process.exit(). | ||
|
@@ -499,6 +504,6 @@ function coerceScreenEmulation(value) { | |
return screenEmulationSettings; | ||
} | ||
|
||
module.exports = { | ||
getFlags, | ||
export { | ||
getFlags | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,10 +5,5 @@ | |
*/ | ||
'use strict'; | ||
|
||
const listAudits = require('./list-audits.js'); | ||
const listTraceCategories = require('./list-trace-categories.js'); | ||
|
||
module.exports = { | ||
listAudits, | ||
listTraceCategories, | ||
}; | ||
export {listAudits} from './list-audits.js'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. complete nit, but not sure about this style. Maybe re-exporting is awkward no matter what, but I do like the explicitness of the other way |
||
export {listTraceCategories} from './list-trace-categories.js'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,22 +7,22 @@ | |
|
||
/* eslint-disable no-console */ | ||
|
||
const path = require('path'); | ||
const psList = require('ps-list'); | ||
/** @typedef {Error & {code: string, friendlyMessage?: string}} ExitError */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what did you think about my There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer local types at the top of the file, but not enough to discuss it much. I'm reverting the changes that were just moving things around. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ha, damn, I thought I had convinced you, but oh well :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the short of it is that exactly what is being imported isn't too informing (and sometimes the import list is so long you glaze over all of it like it's a Java file); but the types are quite useful as to the data structures involved in the file. so for the same reason that fileoverview would be at top, I like local types to be too There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For all the same reasons I think they should be closer to the code that uses them :) Definitions, just in the type domain instead of the value domain, so should be right next to the value definitions. |
||
|
||
const Printer = require('./printer.js'); | ||
const ChromeLauncher = require('chrome-launcher'); | ||
import * as path from 'path'; | ||
|
||
const yargsParser = require('yargs-parser'); | ||
const lighthouse = require('../lighthouse-core/index.js'); | ||
const log = require('lighthouse-logger'); | ||
const getFilenamePrefix = require('../report/generator/file-namer.js').getFilenamePrefix; | ||
const assetSaver = require('../lighthouse-core/lib/asset-saver.js'); | ||
const URL = require('../lighthouse-core/lib/url-shim.js'); | ||
import psList from 'ps-list'; | ||
import * as ChromeLauncher from 'chrome-launcher'; | ||
import yargsParser from 'yargs-parser'; | ||
import log from 'lighthouse-logger'; | ||
import open from 'open'; | ||
|
||
const open = require('open'); | ||
import * as Printer from './printer.js'; | ||
import lighthouse from '../lighthouse-core/index.js'; | ||
import {getFilenamePrefix} from '../report/generator/file-namer.js'; | ||
import * as assetSaver from '../lighthouse-core/lib/asset-saver.js'; | ||
import URL from '../lighthouse-core/lib/url-shim.js'; | ||
|
||
/** @typedef {Error & {code: string, friendlyMessage?: string}} ExitError */ | ||
|
||
const _RUNTIME_ERROR_CODE = 1; | ||
const _PROTOCOL_TIMEOUT_EXIT_CODE = 67; | ||
|
@@ -203,8 +203,8 @@ async function potentiallyKillChrome(launchedChrome) { | |
* @return {Promise<LH.RunnerResult|undefined>} | ||
*/ | ||
async function runLighthouseWithFraggleRock(url, flags, config, launchedChrome) { | ||
const fraggleRock = require('../lighthouse-core/fraggle-rock/api.js'); | ||
const puppeteer = require('puppeteer'); | ||
const fraggleRock = (await import('../lighthouse-core/fraggle-rock/api.js')).default; | ||
const puppeteer = (await import('puppeteer')).default; | ||
const browser = await puppeteer.connect({browserURL: `http://localhost:${launchedChrome.port}`}); | ||
const page = await browser.newPage(); | ||
flags.channel = 'fraggle-rock-cli'; | ||
|
@@ -272,8 +272,8 @@ async function runLighthouse(url, flags, config) { | |
} | ||
} | ||
|
||
module.exports = { | ||
export { | ||
parseChromeFlags, | ||
saveResults, | ||
runLighthouse, | ||
runLighthouse | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
of note.