Skip to content

Commit 44cf433

Browse files
authoredMar 7, 2022
clients: convert devtools and lightrider entries to ES modules (#13722)
1 parent 138541a commit 44cf433

File tree

7 files changed

+49
-36
lines changed

7 files changed

+49
-36
lines changed
 

‎clients/devtools-entry.js ‎clients/devtools/devtools-entry.js

+11-9
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77

88
/* global globalThis */
99

10-
const lighthouse = require('../lighthouse-core/index.js');
11-
const {navigation, startTimespan, snapshot} = require('../lighthouse-core/fraggle-rock/api.js');
12-
const RawProtocol = require('../lighthouse-core/gather/connections/raw.js');
13-
const log = require('lighthouse-logger');
14-
const {lookupLocale} = require('../lighthouse-core/lib/i18n/i18n.js');
15-
const {registerLocaleData, getCanonicalLocales} = require('../shared/localization/format.js');
16-
const constants = require('../lighthouse-core/config/constants.js');
10+
import {Buffer} from 'buffer';
1711

18-
/** @typedef {import('../lighthouse-core/gather/connections/connection.js')} Connection */
12+
import lighthouse from '../../lighthouse-core/index.js';
13+
import {navigation, startTimespan, snapshot} from '../../lighthouse-core/fraggle-rock/api.js';
14+
import RawProtocol from '../../lighthouse-core/gather/connections/raw.js';
15+
import log from 'lighthouse-logger';
16+
import {lookupLocale} from '../../lighthouse-core/lib/i18n/i18n.js';
17+
import {registerLocaleData, getCanonicalLocales} from '../../shared/localization/format.js';
18+
import constants from '../../lighthouse-core/config/constants.js';
19+
20+
/** @typedef {import('../../lighthouse-core/gather/connections/connection.js')} Connection */
1921

2022
// Rollup seems to overlook some references to `Buffer`, so it must be made explicit.
2123
// (`parseSourceMapFromDataUrl` breaks without this)
2224
/** @type {BufferConstructor} */
23-
globalThis.Buffer = require('buffer').Buffer;
25+
globalThis.Buffer = Buffer;
2426

2527
/**
2628
* Returns a config, which runs only certain categories.

‎clients/devtools/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "module",
3+
"//": "Any directory that uses `import ... from` or `export ...` must be type module. Temporary file until root package.json is type: module"
4+
}

‎clients/lightrider/lightrider-entry.js

+13-15
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,28 @@
77

88
/* global globalThis */
99

10-
const lighthouse = require('../../lighthouse-core/index.js');
10+
import {Buffer} from 'buffer';
1111

12-
const LHError = require('../../lighthouse-core/lib/lh-error.js');
13-
const preprocessor = require('../../lighthouse-core/lib/proto-preprocessor.js');
14-
const assetSaver = require('../../lighthouse-core/lib/asset-saver.js');
12+
import lighthouse from '../../lighthouse-core/index.js';
13+
import LHError from '../../lighthouse-core/lib/lh-error.js';
14+
import preprocessor from '../../lighthouse-core/lib/proto-preprocessor.js';
15+
import assetSaver from '../../lighthouse-core/lib/asset-saver.js';
16+
17+
import mobileConfig from '../../lighthouse-core/config/lr-mobile-config.js';
18+
import desktopConfig from '../../lighthouse-core/config/lr-desktop-config.js';
1519

1620
/** @type {Record<'mobile'|'desktop', LH.Config.Json>} */
1721
const LR_PRESETS = {
18-
mobile: require('../../lighthouse-core/config/lr-mobile-config.js'),
19-
desktop: require('../../lighthouse-core/config/lr-desktop-config.js'),
22+
mobile: mobileConfig,
23+
desktop: desktopConfig,
2024
};
2125

2226
/** @typedef {import('../../lighthouse-core/gather/connections/connection.js')} Connection */
2327

2428
// Rollup seems to overlook some references to `Buffer`, so it must be made explicit.
2529
// (`parseSourceMapFromDataUrl` breaks without this)
26-
globalThis.Buffer = require('buffer').Buffer;
30+
/** @type {BufferConstructor} */
31+
globalThis.Buffer = Buffer;
2732

2833
/**
2934
* Run lighthouse for connection and provide similar results as in CLI.
@@ -35,7 +40,7 @@ globalThis.Buffer = require('buffer').Buffer;
3540
* @param {{lrDevice?: 'desktop'|'mobile', categoryIDs?: Array<string>, logAssets: boolean, configOverride?: LH.Config.Json}} lrOpts Options coming from Lightrider
3641
* @return {Promise<string>}
3742
*/
38-
async function runLighthouseInLR(connection, url, flags, lrOpts) {
43+
export async function runLighthouseInLR(connection, url, flags, lrOpts) {
3944
const {lrDevice, categoryIDs, logAssets, configOverride} = lrOpts;
4045

4146
// Certain fixes need to kick in under LR, see https://github.com/GoogleChrome/lighthouse/issues/5839
@@ -98,13 +103,6 @@ async function runLighthouseInLR(connection, url, flags, lrOpts) {
98103
}
99104
}
100105

101-
if (typeof module !== 'undefined' && module.exports) {
102-
// Export for require()ing into unit tests.
103-
module.exports = {
104-
runLighthouseInLR,
105-
};
106-
}
107-
108106
// Expose on window for browser-residing consumers of file.
109107
if (typeof window !== 'undefined') {
110108
// @ts-expect-error - not worth typing a property on `window`.

‎clients/lightrider/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "module",
3+
"//": "Any directory that uses `import ... from` or `export ...` must be type module. Temporary file until root package.json is type: module"
4+
}

‎clients/test/lightrider-entry-test.js ‎clients/test/lightrider/lightrider-entry-test.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
*/
66
'use strict';
77

8-
const assert = require('assert').strict;
9-
const lhBackground = require('../lightrider/lightrider-entry.js');
10-
const Runner = require('../../lighthouse-core/runner.js');
11-
const LHError = require('../../lighthouse-core/lib/lh-error.js');
8+
import {jest} from '@jest/globals';
9+
import {strict as assert} from 'assert';
10+
import {runLighthouseInLR} from '../../lightrider/lightrider-entry.js';
11+
import Runner from '../../../lighthouse-core/runner.js';
12+
import LHError from '../../../lighthouse-core/lib/lh-error.js';
1213

1314
/* eslint-env jest */
1415

@@ -31,7 +32,7 @@ describe('lightrider-entry', () => {
3132
const url = 'https://example.com';
3233
const output = 'json';
3334

34-
const result = await lhBackground.runLighthouseInLR(mockConnection, url, {output}, {});
35+
const result = await runLighthouseInLR(mockConnection, url, {output}, {});
3536
const parsedResult = JSON.parse(result);
3637
assert.strictEqual(parsedResult.runtimeError.code, connectionError.code);
3738
assert.ok(parsedResult.runtimeError.message.includes(connectionError.friendlyMessage));
@@ -52,7 +53,7 @@ describe('lightrider-entry', () => {
5253
const url = 'https://example.com';
5354
const output = 'json';
5455

55-
const result = await lhBackground.runLighthouseInLR(mockConnection, url, {output}, {});
56+
const result = await runLighthouseInLR(mockConnection, url, {output}, {});
5657
const parsedResult = JSON.parse(result);
5758
assert.strictEqual(parsedResult.runtimeError.code, LHError.UNKNOWN_ERROR);
5859
assert.ok(parsedResult.runtimeError.message.includes(errorMsg));
@@ -64,7 +65,7 @@ describe('lightrider-entry', () => {
6465
const mockConnection = {};
6566
const url = 'https://example.com';
6667

67-
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {});
68+
await runLighthouseInLR(mockConnection, url, {}, {});
6869
const config = runStub.mock.calls[0][1].config;
6970
assert.equal(config.settings.channel, 'lr');
7071

@@ -78,7 +79,7 @@ describe('lightrider-entry', () => {
7879
const url = 'https://example.com';
7980

8081
const lrDevice = 'desktop';
81-
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {lrDevice});
82+
await runLighthouseInLR(mockConnection, url, {}, {lrDevice});
8283
const config = runStub.mock.calls[0][1].config;
8384
assert.equal(config.settings.formFactor, 'desktop');
8485

@@ -92,7 +93,7 @@ describe('lightrider-entry', () => {
9293
const url = 'https://example.com';
9394

9495
const lrDevice = 'mobile';
95-
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {lrDevice});
96+
await runLighthouseInLR(mockConnection, url, {}, {lrDevice});
9697
const config = runStub.mock.calls[0][1].config;
9798
assert.equal(config.settings.formFactor, 'mobile');
9899

@@ -111,7 +112,7 @@ describe('lightrider-entry', () => {
111112
onlyAudits: ['network-requests'],
112113
},
113114
};
114-
await lhBackground.runLighthouseInLR(mockConnection, url, {}, {configOverride});
115+
await runLighthouseInLR(mockConnection, url, {}, {configOverride});
115116
const config = runStub.mock.calls[0][1].config;
116117
assert.equal(config.settings.onlyAudits.length, 1);
117118
assert.equal(config.settings.onlyAudits[0], 'network-requests');
@@ -141,7 +142,7 @@ describe('lightrider-entry', () => {
141142
const lrFlags = {
142143
logAssets: true,
143144
};
144-
const resultJson = await lhBackground.runLighthouseInLR(mockConnection, url, {}, lrFlags);
145+
const resultJson = await runLighthouseInLR(mockConnection, url, {}, lrFlags);
145146
const result = JSON.parse(resultJson);
146147
expect(result.artifacts).toMatchObject({
147148
Artifact: {

‎clients/test/lightrider/package.json

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "module",
3+
"//": "Any directory that uses `import ... from` or `export ...` must be type module. Temporary file until root package.json is type: module"
4+
}

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"build-extension": "yarn build-extension-chrome && yarn build-extension-firefox",
2121
"build-extension-chrome": "node ./build/build-extension.js chrome",
2222
"build-extension-firefox": "node ./build/build-extension.js firefox",
23-
"build-devtools": "yarn reset-link && node ./build/build-bundle.js clients/devtools-entry.js dist/lighthouse-dt-bundle.js && node ./build/build-dt-report-resources.js",
23+
"build-devtools": "yarn reset-link && node ./build/build-bundle.js clients/devtools/devtools-entry.js dist/lighthouse-dt-bundle.js && node ./build/build-dt-report-resources.js",
2424
"build-smokehouse-bundle": "node ./build/build-smokehouse-bundle.js",
2525
"build-lr": "yarn reset-link && node ./build/build-lightrider-bundles.js",
2626
"build-pack": "bash build/build-pack.sh",

0 commit comments

Comments
 (0)
Please sign in to comment.