@jsenv/lighthouse-impact 2.1.1 → 2.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -24,7 +24,7 @@ The first thing you need is a script capable to generate a lighthouse report.
24
24
  npm install --save-dev @jsenv/lighthouse-impact
25
25
  ```
26
26
 
27
- _generate_lighthouse_report.mjs_
27
+ _lighthouse.mjs_
28
28
 
29
29
  ```js
30
30
  /*
@@ -69,7 +69,7 @@ _index.html_
69
69
  </html>
70
70
  ```
71
71
 
72
- At this stage, you could generate a lighthouse report on your machine. For an example, see [package.json#L36](./package.json#L36) and [script/lighthouse/generate_lighthouse_report.mjs#L37](./script/lighthouse/generate_lighthouse_report.mjs#L37).
72
+ At this stage, you could generate a lighthouse report on your machine. For an example, see [package.json#L37](./package.json#L37) and [script/lighthouse/lighthouse.mjs#L19](./script/lighthouse/lighthouse.mjs#L19).
73
73
 
74
74
  Now it's time to configure a workflow to compare lighthouse reports before and after merging a pull request.
75
75
 
@@ -99,10 +99,10 @@ jobs:
99
99
  - name: Setup git
100
100
  uses: actions/checkout@v2
101
101
  - name: Setup node
102
- uses: actions/setup-node@v1
102
+ uses: actions/setup-node@v2
103
103
  with:
104
- node-version: "16.x"
105
- - name: Setup npm
104
+ node-version: "16.13.0"
105
+ - name: Install node modules
106
106
  run: npm install
107
107
  - name: Report lighthouse impact
108
108
  run: node ./report_lighthouse_impact.mjs
@@ -123,9 +123,9 @@ import {
123
123
  readGitHubWorkflowEnv,
124
124
  } from "@jsenv/lighthouse-impact"
125
125
 
126
- reportLighthouseImpact({
126
+ await reportLighthouseImpact({
127
127
  ...readGitHubWorkflowEnv(),
128
- lighthouseReportPath: "./generate_lighthouse_report.mjs#lighthouseReport",
128
+ lighthouseReportPath: "./lighthouse.mjs#lighthouseReport",
129
129
  })
130
130
  ```
131
131
 
@@ -167,7 +167,7 @@ reportLighthouseImpact({
167
167
  + repositoryName: process.env.TRAVIS_REPO_SLUG.split("/")[1],
168
168
  + pullRequestNumber: process.env.TRAVIS_PULL_REQUEST,
169
169
  + githubToken: process.env.GITHUB_TOKEN, // see next step
170
- lighthouseReportPath: "./generate_lighthouse_report.mjs#lighthouseReport",
170
+ lighthouseReportPath: "./lighthouse.mjs#lighthouseReport",
171
171
  })
172
172
  ```
173
173
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jsenv/lighthouse-impact",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
4
4
  "description": "Package description",
5
5
  "license": "MIT",
6
6
  "author": {
@@ -42,13 +42,13 @@
42
42
  "prettier": "prettier --write ."
43
43
  },
44
44
  "dependencies": {
45
- "@jsenv/cancellation": "3.0.0",
45
+ "@jsenv/abort": "4.1.2",
46
46
  "@jsenv/dynamic-import-worker": "1.0.0",
47
- "@jsenv/filesystem": "2.3.1",
47
+ "@jsenv/filesystem": "2.5.1",
48
48
  "@jsenv/github-pull-request-impact": "1.6.4",
49
49
  "@jsenv/logger": "4.0.1",
50
- "chrome-launcher": "0.14.0",
51
- "lighthouse": "8.2.0"
50
+ "chrome-launcher": "0.15.0",
51
+ "lighthouse": "9.1.0"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@jsenv/assert": "2.4.0",
@@ -61,8 +61,8 @@
61
61
  "@jsenv/performance-impact": "2.2.1",
62
62
  "@jsenv/server": "12.2.0",
63
63
  "eslint": "8.4.1",
64
- "eslint-plugin-import": "2.25.3",
65
64
  "eslint-plugin-html": "6.2.0",
65
+ "eslint-plugin-import": "2.25.3",
66
66
  "prettier": "2.5.1"
67
67
  }
68
68
  }
@@ -3,13 +3,8 @@
3
3
  import { createRequire } from "node:module"
4
4
 
5
5
  import { createLogger } from "@jsenv/logger"
6
- import {
7
- createCancellationToken,
8
- createCancellationSource,
9
- composeCancellationToken,
10
- createOperation,
11
- executeAsyncFunction,
12
- } from "@jsenv/cancellation"
6
+ import { Abort, raceProcessTeardownEvents } from "@jsenv/abort"
7
+
13
8
  import {
14
9
  writeFile,
15
10
  resolveUrl,
@@ -23,8 +18,8 @@ const require = createRequire(import.meta.url)
23
18
  export const generateLighthouseReport = async (
24
19
  url,
25
20
  {
26
- cancellationToken = createCancellationToken(),
27
- cancelOnSIGINT = true,
21
+ signal = new AbortController().signal,
22
+ handleSIGINT = true,
28
23
  logLevel,
29
24
 
30
25
  headless = true,
@@ -47,31 +42,23 @@ export const generateLighthouseReport = async (
47
42
  htmlFileLog = true,
48
43
  } = {},
49
44
  ) => {
50
- // eslint-disable-next-line import/no-unresolved
51
- const ReportGenerator = require("lighthouse/report/report-generator")
45
+ const ReportGenerator = require("lighthouse/report/generator/report-generator.js")
52
46
  const {
53
47
  computeMedianRun,
54
48
  } = require("lighthouse/lighthouse-core/lib/median-run.js")
55
49
  const chromeLauncher = require("chrome-launcher")
56
50
 
57
- let cleanup = () => {}
58
-
59
- if (cancelOnSIGINT) {
60
- const processCancellationSource = createCancellationSource()
61
- const processCancellationToken = processCancellationSource.token
62
- cancellationToken = composeCancellationToken(
63
- cancellationToken,
64
- processCancellationToken,
65
- )
66
- const SIGINTCallback = () => {
67
- processCancellationSource.cancel("process SIGINT")
68
- }
69
- process.once("SIGINT", SIGINTCallback)
70
- // beware if someday something do things on cleanup
71
- // to call the old cleanup function
72
- cleanup = () => {
73
- process.removeListener("SIGINT", SIGINTCallback)
74
- }
51
+ const generateReportOperation = Abort.startOperation()
52
+ generateReportOperation.addAbortSignal(signal)
53
+ if (handleSIGINT) {
54
+ generateReportOperation.addAbortSource((abort) => {
55
+ return raceProcessTeardownEvents(
56
+ {
57
+ SIGINT: true,
58
+ },
59
+ abort,
60
+ )
61
+ })
75
62
  }
76
63
 
77
64
  const jsenvGenerateLighthouseReport = async () => {
@@ -86,32 +73,42 @@ export const generateLighthouseReport = async (
86
73
  "--disk-cache-size=1",
87
74
  // "--disk-cache-dir=/dev/null",
88
75
  ]
89
- const chrome = await createOperation({
90
- cancellationToken,
91
- start: () => chromeLauncher.launch({ chromeFlags }),
92
- })
76
+ const chrome = await chromeLauncher.launch({ chromeFlags })
77
+ if (generateReportOperation.signal.aborted) {
78
+ return { aborted: true }
79
+ }
80
+
93
81
  const lighthouseOptions = {
94
82
  chromeFlags,
95
83
  port: chrome.port,
96
84
  }
97
85
 
98
86
  const reports = []
99
- await Array(runCount)
100
- .fill()
101
- .reduce(async (previous, _, index) => {
102
- await previous
103
- if (index > 0 && delayBetweenEachRunInSeconds) {
104
- await new Promise((resolve) =>
105
- setTimeout(resolve, delayBetweenEachRunInSeconds * 1000),
106
- )
107
- }
108
- const report = await generateOneLighthouseReport(url, {
109
- cancellationToken,
110
- lighthouseOptions,
111
- config,
112
- })
113
- reports.push(report)
114
- }, Promise.resolve())
87
+ try {
88
+ await Array(runCount)
89
+ .fill()
90
+ .reduce(async (previous, _, index) => {
91
+ generateReportOperation.throwIfAborted()
92
+ await previous
93
+
94
+ if (index > 0 && delayBetweenEachRunInSeconds) {
95
+ await new Promise((resolve) =>
96
+ setTimeout(resolve, delayBetweenEachRunInSeconds * 1000),
97
+ )
98
+ }
99
+ generateReportOperation.throwIfAborted()
100
+ const report = await generateOneLighthouseReport(url, {
101
+ lighthouseOptions,
102
+ config,
103
+ })
104
+ reports.push(report)
105
+ }, Promise.resolve())
106
+ } catch (e) {
107
+ if (Abort.isAbortError(e)) {
108
+ return { aborted: true }
109
+ }
110
+ throw e
111
+ }
115
112
 
116
113
  const lighthouseReport = computeMedianRun(reports)
117
114
 
@@ -161,30 +158,19 @@ export const generateLighthouseReport = async (
161
158
  return lighthouseReport
162
159
  }
163
160
 
164
- return executeAsyncFunction(
165
- async () => {
166
- try {
167
- return await jsenvGenerateLighthouseReport()
168
- } finally {
169
- cleanup()
170
- }
171
- },
172
- {
173
- catchCancellation: true,
174
- considerUnhandledRejectionsAsExceptions: true,
175
- },
176
- )
161
+ try {
162
+ return await jsenvGenerateLighthouseReport()
163
+ } finally {
164
+ await generateReportOperation.end()
165
+ }
177
166
  }
178
167
 
179
168
  const generateOneLighthouseReport = async (
180
169
  url,
181
- { cancellationToken, lighthouseOptions, config },
170
+ { lighthouseOptions, config },
182
171
  ) => {
183
172
  const lighthouse = require("lighthouse")
184
- const results = await createOperation({
185
- cancellationToken,
186
- start: () => lighthouse(url, lighthouseOptions, config),
187
- })
173
+ const results = await lighthouse(url, lighthouseOptions, config)
188
174
 
189
175
  // use results.lhr for the JS-consumeable output
190
176
  // https://github.com/GoogleChrome/lighthouse/blob/master/types/lhr.d.ts
@@ -8,6 +8,8 @@ import { createLighthouseImpactComment } from "./internal/createLighthouseImpact
8
8
  export const reportLighthouseImpact = async ({
9
9
  logLevel,
10
10
  commandLogs = false,
11
+ // TODO: update { cancellationToken, cancelOnSIGINT } in "@jsenv/github-pull-request-impact"
12
+ // to use { signal, handleSIGINT } instead
11
13
  cancellationToken,
12
14
  cancelOnSIGINT,
13
15
  projectDirectoryUrl,