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

[WIP] Create subpackages for internal-ish code #2351

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

steveluscher
Copy link
Collaborator

@steveluscher steveluscher commented Mar 20, 2024

Summary

The entire problem that this PR tries to solve is this.

  1. Retain the simplicity of being able to export * from '@solana/errors' in packages/library/
  2. Prevent things like safeCaptureStackTrace() from being exported from packages/library/

The steps taken to that end were:

  1. Change moduleResolution everywhere to NodeNext. This enables subpackage exports
  2. Add .js extensions everywhere to satisfy that resolution regime
  3. Create a subpackage export in @solana/errors for all of the stuff that we want to share internally but not to export from packages/library/

Now any package in the @solana scope can use @solana/errors/internal but we can still enjoy the simplicity of export * from '@solana/errors' in packages/library/

Copy link

changeset-bot bot commented Mar 20, 2024

⚠️ No Changeset found

Latest commit: 39c0653

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Collaborator Author

This stack of pull requests is managed by Graphite. Learn more about stacking.

Join @steveluscher and the rest of your teammates on Graphite Graphite

@@ -3,7 +3,6 @@
"version": "0.0.0",
"private": true,
"files": [
"add-js-extension-to-types.mjs",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Nuked this, in favour of actually adding the .js extension everywhere.

@@ -25,7 +25,7 @@ export function getBaseConfig(platform: Platform, formats: Format[], _options: O
__REACTNATIVE__: `${platform === 'native'}`,
__VERSION__: `"${env.npm_package_version}"`,
},
entry: [`./src/index.ts`],
entry: ['./src/index.ts', './src/__internal.ts'],
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This makes the build pick up this __internal.ts entrypoint if it exists.

@@ -70,6 +70,7 @@ export function getBaseConfig(platform: Platform, formats: Format[], _options: O
platform: platform === 'node' ? 'node' : 'browser',
pure: ['process'],
sourcemap: format !== 'iife' || isDebugBuild,
splitting: false,
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This prevents tsup from doing fancy bundle splitting between entrypoints.

@@ -0,0 +1 @@
export * from './stack-trace.js';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here's that new ‘internal stuff’ entrypoint.

@@ -1,4 +1,5 @@
import { safeCaptureStackTrace, SOLANA_ERROR__RPC__INTEGER_OVERFLOW, SolanaError } from '@solana/errors';
import { SOLANA_ERROR__RPC__INTEGER_OVERFLOW, SolanaError } from '@solana/errors';
import { safeCaptureStackTrace } from '@solana/errors/internal';
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Here's an example of making use of the internal subpackage.

Comment on lines +12 to +13
"module": "NodeNext",
"moduleResolution": "NodeNext",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is the fancy new resolution strategy that allows all of this to work.

Comment on lines +5 to +9
moduleNameMapper: {
// Strip `.js` from imports
// https://github.com/swc-project/jest/issues/64#issuecomment-1029753225
'^(\\.{1,2}/.*)\\.js$': '$1',
},
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Of course Jest doesn't know any of this, so we have to re-strip the .js extensions.

@lorisleiva
Copy link
Collaborator

Ugh it's so sad because I do love the end result but I cannot bear having to use .js extensions on .ts files. And the fact that we then have to strip them for Jest is the icing on the cake.

Is there no way we can do this as a build step instead?

@steveluscher
Copy link
Collaborator Author

Is there no way we can do this as a build step instead?

Do what; add the extensions? No, because once you change to "moduleResolution": "NodeNext" it's gg.

@steveluscher steveluscher force-pushed the 03-20-_wip_create_subpackages_for_internal-ish_code branch from 2f890a3 to e057dc3 Compare March 20, 2024 21:58
@lorisleiva
Copy link
Collaborator

Well, adding "moduleResolution": "NodeNext" to our tsconfig changes how things are compiled, right?

What I'm saying is, instead of using the module resolution, can we not replicate the changes that this does at compile time to make subpackages work properly?

What exactly is NodeNext changing in our compiled code that makes subpackages possible?

Copy link
Collaborator Author

What exactly is NodeNext changing in our compiled code that makes subpackages possible?

I think subpackage imports just straight up don't work unless moduleResolution is node16 or higher.

With moduleResolution: NodeNext:

image.png

With moduleResolution: Node:

image.png

@lorisleiva
Copy link
Collaborator

Oh I see, as a consumer of our own API, we need NodeNext to import sub-packages. No matter how we export them. I'm happy to disagree and commit if everyone agrees that the benefits are worth it. .js extensions on .ts files do make me want to cry though.

@steveluscher steveluscher force-pushed the 03-20-_wip_create_subpackages_for_internal-ish_code branch from e057dc3 to 39c0653 Compare March 21, 2024 18:43
@steveluscher steveluscher added the do-not-close Add this tag to exempt an issue/PR from being closed by the stalebot label Apr 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
do-not-close Add this tag to exempt an issue/PR from being closed by the stalebot
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants