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

ESM support (incl. lighthouserc.mjs + lighthouserc.cjs) #973

Open
karlhorky opened this issue Nov 15, 2023 · 2 comments
Open

ESM support (incl. lighthouserc.mjs + lighthouserc.cjs) #973

karlhorky opened this issue Nov 15, 2023 · 2 comments

Comments

@karlhorky
Copy link

karlhorky commented Nov 15, 2023

Is your feature request related to a problem? Please describe.

Currently, running lhci autorun in an ESM project causes ERR_REQUIRE_ESM failures, because the configuration file is being require()d:

$ lhci autorun
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/runner/work/here-for-you-codealong/here-for-you-codealong/lighthouserc.js from /home/runner/.config/yarn/global/node_modules/@lhci/utils/src/lighthouserc.js not supported.
/home/runner/work/here-for-you-codealong/here-for-you-codealong/lighthouserc.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename /home/runner/work/here-for-you-codealong/here-for-you-codealong/lighthouserc.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /home/runner/work/here-for-you-codealong/here-for-you-codealong/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

Describe the solution you'd like

It would be nice to be able to use Lighthouse CI in ESM projects (eg. projects specifying "type": "module" in package.json):

  1. ESM support in lighthouserc.js when "type": "module" specified in package.json
  2. lighthouserc.mjs for CommonJS projects who want to opt in to ESM in this one file
  3. lighthouserc.cjs for ESM projects who want to stay with the current CommonJS config done in feat: support cjs config file extension #650 and docs: add *.cjs to supported config extensions #975

The currently supported configuration files:

  • .lighthouserc.js
  • lighthouserc.js
  • .lighthouserc.json
  • lighthouserc.json
  • .lighthouserc.yml
  • lighthouserc.yml
  • .lighthouserc.yaml
  • lighthouserc.yaml

Describe alternatives you've considered

Not having ESM support (having to use JSON config files in ESM projects)

Additional context

Lighthouse itself switched to ESM in this umbrella issue managed by @connorjclark

karlhorky added a commit to upleveled/here-for-you-codealong that referenced this issue Nov 15, 2023
karlhorky added a commit to upleveled/here-for-you-codealong that referenced this issue Nov 15, 2023
karlhorky added a commit to upleveled/here-for-you-codealong that referenced this issue Nov 15, 2023
@adamraine
Copy link
Member

lighthouserc.cjs is actually supported (#650), but looks like the docs need to be updated :/. This at least covers case 3 so you don't need to use a JSON file, but the other cases would still be nice to have.

@moos
Copy link

moos commented Jan 12, 2024

+1 -- I have a custom Lighthouse config in my lighthouserc.js:

module.exports = {
  ci: {
     // [snip]
      settings: {
        configPath: './lighthouse-config.js',
      }
    },
};

Trying const constants = require('lighthouse/core/config/constants.js') in lighthouse-config.js doesn't work because those are ESM modules. import isn't supported (this issue), and haven't been able to get dynamic import() to work with CJS! 😖

Update: Renaming to lighthouse-config.mjs and using regular import/export does work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants