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

Update packages to be browser-first and use separate export for Node.js #2211

Merged
merged 13 commits into from
Feb 23, 2024

Conversation

Mrtenz
Copy link
Member

@Mrtenz Mrtenz commented Feb 22, 2024

This refactors all packages to be browser-first: All APIs and packages used should be compatible with browsers. This means no use of Node.js builtins, or native packages. Node-specific APIs have been moved to a separate export /node (e.g., @metamask/snaps-controllers/node). For the snaps-controllers package I've also added a /react-native export, which exports the React Native webview service.

This also means we can remove the "browser" field from the packages that were using it.

Breaking changes

  • Anything that uses Node.js was removed from the default export, and needs to be imported from /node.
  • The React Native webview service was moved, and needs to be imported from /react-native.

@Mrtenz Mrtenz force-pushed the mrtenz/node-exports branch from 0f97130 to 6ba5a4a Compare February 22, 2024 11:08
Copy link

codecov bot commented Feb 22, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 96.60%. Comparing base (9617754) to head (0b48769).

Additional details and impacted files
@@           Coverage Diff           @@
##             tsup    #2211   +/-   ##
=======================================
  Coverage   96.60%   96.60%           
=======================================
  Files         335      337    +2     
  Lines        7597     7599    +2     
  Branches     1175     1175           
=======================================
+ Hits         7339     7341    +2     
  Misses        258      258           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

return undefined;
}
- return search.substr(star, search.length - part2.length);
+ return search.substr(star, search.length - part2.length - part1.length);
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if this is a bug or not, but the previous behaviour was unexpected (and I'm not sure how this worked before). Basically when checking a tsconfig.json path with a wildcard, this function returns the replacement for the path.

  • Pattern: @metamask/*/node
    Input: @metamask/snaps-utils/node
    Result: snaps-utils
  • Pattern: @metamask/*
    Input: @metamask/snaps-controllers
    Result: snaps-controllers

And so on. Before this change however, it would return:

  • Pattern: @metamask/*/node
    Input: @metamask/snaps-utils/node
    Result: snaps-utils/node

Meaning that it would resolve to packages/snaps-utils/node/src/... and some other invalid paths. This causes it to fall back to Node.js' default resolver, which would look at the package.json of snaps-utils and try to load packages/snaps-utils/dist/node.js, but we can't do that since the file may not be built yet.

@Mrtenz Mrtenz marked this pull request as ready for review February 22, 2024 13:35
@Mrtenz Mrtenz requested a review from a team as a code owner February 22, 2024 13:35
return undefined;
}
- return search.substr(star, search.length - part2.length);
+ return search.substr(star, search.length - part2.length - part1.length);
Copy link
Member

Choose a reason for hiding this comment

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

We need to try and upstream this. Also we need to determine if it is a bug before merging 😓

Copy link
Member Author

Choose a reason for hiding this comment

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

There's a ~3.5 year old issue and PR open in tsconfig-paths to fix this. It does appear to be a bug.

Copy link
Member

Choose a reason for hiding this comment

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

Can we try opening a well-reasoned PR with tests etc that is an easy merge for the maintainers?

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, but do you think that should block merging this?

Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to get confirmation that it's a bug, but probably not blocking

@@ -0,0 +1,10 @@
/* eslint-disable import/export */

export * from '.';
Copy link
Member

Choose a reason for hiding this comment

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

This is a nice pattern, we should let everyone in the team know about how exports will work now.

Copy link
Member Author

Choose a reason for hiding this comment

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

That was my plan after merging this yeah.

Copy link
Member

Choose a reason for hiding this comment

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

General question: Can we do something to prevent Node.js imports accidentally being included in browser builds?

Copy link
Member Author

Choose a reason for hiding this comment

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

We have an eslint-config for browser environments, but it would be quite tedious to only enable this for certain files. Do you have any good ideas for how to do this?

Copy link
Member

Choose a reason for hiding this comment

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

Not really, that's why I ask 😅

I guess we could see if we can reduce this list and use that build failing as heuristic? https://github.com/MetaMask/snaps/blob/main/packages/snaps-simulator/webpack.config.ts#L63-L81

But it's not great.

@Mrtenz Mrtenz merged commit 72cfc76 into tsup Feb 23, 2024
142 of 143 checks passed
@Mrtenz Mrtenz deleted the mrtenz/node-exports branch February 23, 2024 11:56
Mrtenz added a commit that referenced this pull request Feb 23, 2024
…js (#2211)

This refactors all packages to be browser-first: All APIs and packages
used should be compatible with browsers. This means no use of Node.js
builtins, or native packages. Node-specific APIs have been moved to a
separate export `/node` (e.g., `@metamask/snaps-controllers/node`). For
the `snaps-controllers` package I've also added a `/react-native`
export, which exports the React Native webview service.

This also means we can remove the "browser" field from the packages that
were using it.

## Breaking changes

- Anything that uses Node.js was removed from the default export, and
needs to be imported from `/node`.
- The React Native webview service was moved, and needs to be imported
from `/react-native`.
Mrtenz added a commit that referenced this pull request Feb 26, 2024
…js (#2211)

This refactors all packages to be browser-first: All APIs and packages
used should be compatible with browsers. This means no use of Node.js
builtins, or native packages. Node-specific APIs have been moved to a
separate export `/node` (e.g., `@metamask/snaps-controllers/node`). For
the `snaps-controllers` package I've also added a `/react-native`
export, which exports the React Native webview service.

This also means we can remove the "browser" field from the packages that
were using it.

## Breaking changes

- Anything that uses Node.js was removed from the default export, and
needs to be imported from `/node`.
- The React Native webview service was moved, and needs to be imported
from `/react-native`.
Mrtenz added a commit that referenced this pull request Feb 26, 2024
This swaps out the build system for `tsup`, and adds a proper ESM build
to each package. In addition to that, the following changes have been
made:

- All packages with Node.js-specific code have been refactored to make
them browser compatible. Node.js exports were moved to a separate
`/node` export, i.e., `@metamask/snaps-utils/node`.
- This also applies to the React Native webview service in
`snaps-controllers`, which can now be imported from
`@metamask/snaps-controllers/react-native`.
- The `snaps-simulator` package is no longer published to NPM. We were
previously using it for `snaps-jest`, but it's no longer used now.

---

This pull request consists of the following pull requests:

- #2120.
- #2211.
naugtur pushed a commit that referenced this pull request Mar 28, 2024
…js (#2211)

This refactors all packages to be browser-first: All APIs and packages
used should be compatible with browsers. This means no use of Node.js
builtins, or native packages. Node-specific APIs have been moved to a
separate export `/node` (e.g., `@metamask/snaps-controllers/node`). For
the `snaps-controllers` package I've also added a `/react-native`
export, which exports the React Native webview service.

This also means we can remove the "browser" field from the packages that
were using it.

## Breaking changes

- Anything that uses Node.js was removed from the default export, and
needs to be imported from `/node`.
- The React Native webview service was moved, and needs to be imported
from `/react-native`.
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.

2 participants