Skip to content

Use "exports" field in package.json #13359

Description

@connorjclark

We first tried to introduce this via #13349 for the v9 release. The idea was to define the exports in a non-breaking way, such that all modules under ./lighthouse-core/* / ./lighthouse-cli/* / ./types/* would be exposed + a new export for the renderer api at ./renderer. However, we quickly found that clients (specifically pubads, but if you search GitHb there are tons) tend to import modules with and without a .js extension.

Defining exports like this seemed like it would support optional extension in the import. The first entry that matches the key is used, and the * matches as much as the pattern as possible (includes slashes).

"./lighthouse-core/*.js": "./lighthouse-core/*.js",
"./lighthouse-core/*": "./lighthouse-core/*.js",

However, yarn build-devtools (exercising the pubads code) still failed to resolve all its imports. As this seemed like a huge blocker to adoption for this feature, I began to write a bug report, but first I found these issues in the nodejs project:

nodejs/node#39635

Turns out, "pattern trailers" was an overlooked feature. It was recently added AND backported to 14.

nodejs/Release#690 (comment)

It should land in the next releases across 14+, but IDK when that is. (Ex: 14.18.2 will have the fix, but the latest is 14.18.1. ditto for 17.1.1 see #13359 (comment)). But, it won't be in time for v9.


I can't test it yet, but I believe this will work:

"exports": {
  ".": "./lighthouse-core/index.js",
  "./renderer": "./dist/report/bundle.esm.js",
  "./lighthouse-core/*": "./lighthouse-core/*.js",
  "./lighthouse-core/*.js": "./lighthouse-core/*.js",
  "./lighthouse-cli/*": "./lighthouse-cli/*.js",
  "./lighthouse-cli/*.js": "./lighthouse-cli/*.js",
  "./types/*": "./types/*"
},

(note the order is swapped from the original intuition. not sure why, I'm just copying from the example in the PR)

Metadata

Metadata

Assignees

Labels

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions