Skip to content

Conversation

@isker
Copy link
Contributor

@isker isker commented May 24, 2025

As far as I can tell, import attribute syntax should work everywhere lighthouse is supported.

My intent behind doing this was to maybe get Lighthouse to the point where you can bundle it for NodeJS using something like esbuild or rollup. But it turns out that errors about these locales files were just the first ones to occur when executing a bundled lighthouse entrypoint :^). Audits and gatherers are still loaded completely dynamically from the filesystem. Maybe those could be dynamically imported instead, but that would be a more substantial change.

Anyway, perhaps it's still better to ship this than to not.

Fixes #14611.

As far as I can tell, import attribute syntax should work everywhere
lighthouse is supported.

My intent behind doing this was to maybe get Lighthouse to the point
where you can bundle it for NodeJS using something like esbuild or
rollup. But it turns out that errors about these locales files were just
the first ones to occur when executing a bundled lighthouse entrypoint
:^). Audits and gatherers are still loaded completely dynamically from
the filesystem. Maybe those could be dynamically imported instead, but
that would be a more substantial change.

Anyway, perhaps it's still better to ship this than to not.

Fixes GoogleChrome#14611.
@isker isker requested a review from a team as a code owner May 24, 2025 15:34
@isker isker requested review from connorjclark and removed request for a team May 24, 2025 15:34
import fs from 'fs';

import {getModuleDirectory} from '../esm-utils.js';
import ar from './locales/ar.json' with { type: 'json' };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a more generic way to do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generic in what way?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think @isker is referring to the ar import as a hardcoded locale (Arabic). To make this more generic, you could dynamically import the locale based on a variable like this:

const locale = 'ar'; // could be 'en', 'fr', etc. Or dynamically set this based on user preference
const messages = await import(`./locales/${locale}.json`, {
  with: { type: 'json' }
});

This way, you can support multiple languages without having to write a separate import for each one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect dynamically importing them breaks the ability of a bundler to statically analyze the imports, which was the motivation behind me doing this in the first place.

Beyond that, I don't really understand why dynamic imports here are an improvement. What's the difference between writing down a list of locales and expanding them into dynamic imports, compared to writing down a list of static imports?

@brendankenny
Copy link
Contributor

My intent behind doing this was to maybe get Lighthouse to the point where you can bundle it for NodeJS using something like esbuild or rollup.

independent of the contents of this PR (I was the one that filed #14611 :), FWIW you can do this using the build-bundle script, which uses esbuild to bundle Lighthouse for use in Chrome DevTools and other places. See e.g. the build-devtools npm script in package.json.

Copy link
Collaborator

@connorjclark connorjclark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

I'm certain there's still more issues regarding bundling out-the-box - but as Brendan said, we do have a script to assist in that. It's what we use to bundle for DevTools and for PSI.

@connorjclark connorjclark merged commit 4a84063 into GoogleChrome:main May 29, 2025
24 checks passed
@G-Rath
Copy link
Contributor

G-Rath commented Aug 1, 2025

@connorjclark
Copy link
Collaborator

hmm that does seem right ... need to figure out how our CI passed for 18.20 given that.

I'll revert this change in the next release. Thanks for pointing it out.

@isker
Copy link
Contributor Author

isker commented Aug 1, 2025

This says 18.20: https://nodejs.org/api/esm.html#import-attributes

Could have sworn I also tested that import attributes work with node 18.20.

@G-Rath
Copy link
Contributor

G-Rath commented Aug 1, 2025

I think you're right about the v18 version, but it's still a breaking change as you're saying you work with any version of Node v20.

I did get a bit confused in thinking the min. was 20.10 as that's what MDN has but if you click on the little ... they're got v18.20 there too:

image

The failing CI that brought this to my attention is using v20.9 so it's obvious why that failed - but in a perfect world engines should be something like ^18.20 || >= 20.10

@isker
Copy link
Contributor Author

isker commented Aug 1, 2025

I see. 20.9.0 was indeed the first release that the 20 line went LTS, so it is that exact single version that is broken, if the project disregards support for non-LTS versions (which is something I have just made up).

I think this is a niche situation but I’ll leave it to the maintainers’ judgment.

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

Successfully merging this pull request may close these issues.

Switch to JSON module imports

6 participants