-
Notifications
You must be signed in to change notification settings - Fork 30
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
Convert plugin to ESM and split large files #533
Conversation
👷 Deploy Preview for plugin-lighthouse processing.
|
const { getSettings } = require('./settings'); | ||
const { getBrowserPath, runLighthouse } = require('./lighthouse'); | ||
const { formatResults } = require('./format'); | ||
import chalk from 'chalk'; |
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.
This is the main file where functions were cut from
jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
jest.mock('chalk', () => { | ||
jest.unstable_mockModule('chalk', () => { |
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.
Jest isn't quite there with ESM. Mocking a module being one outstanding pain point. Based on a few recommendations, we use this module-specific mock (available since Jest v27), then lazy import the file we want to test after the mock is in place.
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.
[dust] It might be nice to add this link in a wee comment here, in case anyone wonders in future!
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.
Woooo this is a really quality improvement! ✨
I've run this locally and with a test site (inc with some paths that should generate lighthouse failures) and all seems to be working as before 👍
jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
jest.mock('chalk', () => { | ||
jest.unstable_mockModule('chalk', () => { |
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.
[dust] It might be nice to add this link in a wee comment here, in case anyone wonders in future!
This PR starts modernising the plugin by switching to ES Modules, and splitting some of the larger files into smaller discrete function files. In theory, there are no functional changes, but a lot of files have been touched.
These changes should allow easier test authoring, and allows us to switch out
express
fornetlify dev
in a more controlled way.Using ESM is the recommended Netlify way: https://docs.netlify.com/integrations/build-plugins/create-plugins/#es-modules.
Example site deploy using modernized version:
netlify/netlify-plugin-lighthouse#jbr/modernizing
Summary of changes
index.js
to standalone directoriesrequire
toimport
/export
import
files directly e.g.src/directory/index.js
(to match the spec) rather than importingsrc/directory
directly and relying on Babels's file resolving. Happy to change this in future if we find additional uses for transpiling.Test plan
yarn local
BEGIN_COMMIT_OVERRIDE
chore: internal refactor to use ES modules #533
END_COMMIT_OVERRIDE