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

feat: Add new export for initializeInpageProvider #391

Merged
merged 2 commits into from
Nov 27, 2024

Conversation

Gudahtt
Copy link
Member

@Gudahtt Gudahtt commented Nov 27, 2024

Add a new top-level export map entry for initializeInpageProvider that maps correctly onto CJS and ESM builds, and add a fallback JavaScript file at the root of the repository for build systems that don't support export maps.

This gives the best of both worlds: build systems that support exports will behave correctly, and those that don't will get the CJS fallback.

The metamask-extension project uses two build systems: one which obeys export maps, one which ignores them (Webpack and browserify respectively). The existing initializeInpageProvider export map entry doesn't work for browserify because it's looking for initializeInpageProvider.js. The file extension is wrong. This fixes that problem.

This strategy was copied from https://github.com/MetaMask/snaps/blob/cbe55deb75e640c03add1ebf9235d4abd513e9d5/packages/snaps-sdk/jsx.js

@Gudahtt

This comment was marked as resolved.

@Gudahtt Gudahtt force-pushed the add-cjs-subpath-export branch 2 times, most recently from 74a85e4 to 8344e64 Compare November 27, 2024 20:20
@Gudahtt Gudahtt marked this pull request as ready for review November 27, 2024 20:20
@Gudahtt Gudahtt requested a review from a team as a code owner November 27, 2024 20:20
@Gudahtt Gudahtt force-pushed the add-cjs-subpath-export branch 2 times, most recently from e8d09c2 to 3e58d90 Compare November 27, 2024 20:21
Add a subpath export for the `initializeInpageProvider.cjs` file. This
allows projects with legacy build systems to import this file directly,
which can be useful for reducing bundle size (especially for build
systems without tree shaking).

The `metamask-extension` project uses two build systems: one which
obeys export maps, one which ignores them (Webpack and browserify
respectively). The existing `initializeInpageProvider` export map entry
doesn't work for browserify because it's looking for
`initializeInpageProvider.js`. The file extension is wrong.
Add a new top-level export map entry for `initializeProvider` that maps
correctly onto CJS and ESM builds, and add a fallback JavaScript file
at the root of the repository for build systems that don't support
export maps.

This gives the best of both worlds: build systems that support exports
will behave correctly, and those that don't will get the CJS fallback.
@Gudahtt Gudahtt force-pushed the add-cjs-subpath-export branch from 3e58d90 to 8d85f6b Compare November 27, 2024 20:22
@@ -49,6 +49,16 @@
"default": "./dist/initializeInpageProvider.cjs"
}
},
"./initializeInpageProvider": {
Copy link
Member Author

@Gudahtt Gudahtt Nov 27, 2024

Choose a reason for hiding this comment

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

We can remove the other export entries that are no longer needed in a later PR. That would be a breaking change.

Tracked here: #393

@Gudahtt Gudahtt changed the title feat: Add subpath export for initializeInpageProvider.cjs feat: Add new export for initializeInpageProvider Nov 27, 2024
@Gudahtt Gudahtt merged commit 1c4015d into main Nov 27, 2024
18 checks passed
@Gudahtt Gudahtt deleted the add-cjs-subpath-export branch November 27, 2024 20:28
github-merge-queue bot pushed a commit to MetaMask/metamask-extension that referenced this pull request Nov 27, 2024
## **Description**

The `@metamask/providers` package has been updated from v14 to v18. The
breaking changes do not require any changes to the extension. They
include:
* Removal of certain provider features, which were later restored
* Updates to JSON-RPC packages that used to impact error serialization
in a breaking way, but no longer do (as of rpc-errors v7.0.1)
* `webextension-polyfill` added as a peer dependency
* Sub-path exports no longer work due to addition of exports and ESM
build.

Altogether this should result in no functional changes, aside from
eliminating some redundant older copies of libraries (reducing bundle
size).

This update was difficult because of the need for a subpath export in
`inpage.js`. We want to import just the `initializeInpageProvider`
module without the rest of the package, which means we need to use a
subpath export (because browserify doesn't support tree shaking). But
this was challenging because the export maps added in v17 prevent the
use of any subpath exports not defined in the export map. There was no
entry that worked correctly across webpack and browserify. This was
resolved by a new entry added in
MetaMask/providers#391 (as part of v18.2.0)

Changelog:
https://github.com/MetaMask/providers/blob/main/CHANGELOG.md#1820

[![Open in GitHub
Codespaces](https://github.com/codespaces/badge.svg)](https://codespaces.new/MetaMask/metamask-extension/pull/28757?quickstart=1)

## **Related issues**

N/A

## **Manual testing steps**

N/A

## **Screenshots/Recordings**

N/A

## **Pre-merge author checklist**

- [x] I've followed [MetaMask Contributor
Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask
Extension Coding
Standards](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/CODING_GUIDELINES.md).
- [x] I've completed the PR template to the best of my ability
- [x] I’ve included tests if applicable
- [x] I’ve documented my code using [JSDoc](https://jsdoc.app/) format
if applicable
- [x] I’ve applied the right labels on the PR (see [labeling
guidelines](https://github.com/MetaMask/metamask-extension/blob/develop/.github/guidelines/LABELING_GUIDELINES.md)).
Not required for external contributors.

## **Pre-merge reviewer checklist**

- [ ] I've manually tested the PR (e.g. pull and build branch, run the
app, test code being changed).
- [ ] I confirm that this PR addresses all acceptance criteria described
in the ticket it closes and includes the necessary testing evidence such
as recordings and or screenshots.
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.

3 participants