Skip to content

feat!: migrate to esm only, unify exports, use pkg-utils for build, switch tests to Vitest#84

Merged
rdunk merged 4 commits intomainfrom
08-29-refactor_migrate_to_esm_use_pkg-utils_for_build_switch_tests_to_vitest
Nov 27, 2025
Merged

feat!: migrate to esm only, unify exports, use pkg-utils for build, switch tests to Vitest#84
rdunk merged 4 commits intomainfrom
08-29-refactor_migrate_to_esm_use_pkg-utils_for_build_switch_tests_to_vitest

Conversation

@rdunk
Copy link
Copy Markdown
Member

@rdunk rdunk commented Aug 29, 2025

To support signed URLs (the next PR upstack), I decided to add a specific signed version of the url builder available from a different export path /signed. This was to avoid bundling the crypto dependencies we rely on for signing, as this functionality will likely only be used by a very small subset of users.

I thought it would be a good time to "modernize" the repo a little to support that work. That included:

  • Replacing jest​ with vitest​ for tests
  • Replacing microbundle with @sanity/pkg-utils for building
  • Updating the TypeScript configuration to extend @sanity/pkg-utils strictest
  • Exporting all types defined in src/types.ts​ from the default export path

I’ve also added in a postbuild​/prepack​ step which outputs some files for backwards compatibility. This is to support users who are currently importing types from paths like @sanity/image-url/lib/types/types​.

@changeset-bot
Copy link
Copy Markdown

changeset-bot bot commented Aug 29, 2025

🦋 Changeset detected

Latest commit: 826e441

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sanity/image-url Major

Not sure what this means? Click here to learn what changesets are.

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

Copy link
Copy Markdown
Member Author

rdunk commented Aug 29, 2025

@rdunk rdunk mentioned this pull request Aug 29, 2025
@socket-security
Copy link
Copy Markdown

socket-security bot commented Aug 29, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updated@​types/​node@​17.0.45 ⏵ 24.3.0100 +110081 +196100
Updatedtypescript@​4.9.5 ⏵ 5.9.2100 +110090100100

View full report

@rdunk rdunk force-pushed the 08-29-refactor_migrate_to_esm_use_pkg-utils_for_build_switch_tests_to_vitest branch from 05bed0c to e0fdb42 Compare August 29, 2025 14:43
@rdunk rdunk force-pushed the 08-29-refactor_migrate_to_esm_use_pkg-utils_for_build_switch_tests_to_vitest branch from e0fdb42 to d2aec21 Compare August 29, 2025 16:06
@rdunk rdunk marked this pull request as ready for review August 29, 2025 16:12
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For a second I thought that the red & were them being removed on the updated version as was concerned 😅

Copy link
Copy Markdown
Contributor

@RitaDias RitaDias left a comment

Choose a reason for hiding this comment

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

This looks good to me though I would just want to have someone else that is a bit more knowledgeable on the config side of things double check that everything is good to go :)

Copy link
Copy Markdown
Member

@rexxars rexxars left a comment

Choose a reason for hiding this comment

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

Thanks, this is great. I am wondering if it's time to just do a major version bump and break things?

  • There are imports to things like @sanity/image-url/lib/browser/builder on the internet, which will break with this change
  • Tempting to go ESM-only, Node.js >= 20 at this point. We list node >= 10 in the package, but also use @sanity/browserslist-config which says maintained node versions. These are not compatible with each other. Additionally, the dependencies introduced in the signed URL PR requires Node v20.19, so it would make sense to align here anyway.
  • Would love to use a named export instead of default export

package.json Outdated
"index.js",
"urlForImage.js"
"src",
"build/compat-shims.mjs"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Don't think we need to ship this in the published npm module?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ah thank you, good catch, build/compat-shims.mjs was a holdover from my first attempt at backwards compatibility (it doesn't actually exist anymore).

Would you prefer to remove src too?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would you prefer to remove src too?

If the sourcemaps reference src I tend to include it, but also don't feel strongly about it.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Cool, they do, I'll include it.

@rdunk
Copy link
Copy Markdown
Member Author

rdunk commented Sep 4, 2025

Thanks for taking a look @rexxars and for catching the node mismatches, I glossed over that.

I was trying to weigh up whether or not a major would be “worth it” without new features, but if you’re comfortable with that then it would definitely simplify things. Feels like it could be a bit of a pain to maintain full backwards compatibility, evidenced by my attempt to cover the types but not accounting for those browser imports.

Would we introduce a named export and keep/soft deprecate the default export to make things slightly less painful?

@rexxars
Copy link
Copy Markdown
Member

rexxars commented Sep 4, 2025

Would we introduce a named export and keep/soft deprecate the default export to make things slightly less painful?

We could do that! Up to you :)

@rdunk
Copy link
Copy Markdown
Member Author

rdunk commented Sep 9, 2025

I’ve added a follow up commit that makes breaking changes:

  • All default exports are replaced with named exports. I opted not to go for a a soft deprecation as it's unlikely we'll do another major of this package anytime soon, better to make the change cleanly I think. Is urlBuilder what we want to go with?
  • The CJS exports have been removed so the package will now build ESM only.

@rdunk rdunk requested a review from rexxars September 12, 2025 08:39
@rexxars
Copy link
Copy Markdown
Member

rexxars commented Sep 17, 2025

Is urlBuilder what we want to go with?

I think createUrlBuilder or createImageUrlBuilder might be more descriptive? (mirrors createClient etc)

@rdunk
Copy link
Copy Markdown
Member Author

rdunk commented Sep 17, 2025

I think createUrlBuilder or createImageUrlBuilder might be more descriptive? (mirrors createClient etc)

I very much agree. I prefer the former for terseness but the latter seems sensibly unambiguous and probably the safer option, so I've added a commit to refactor to that.

@rexxars
Copy link
Copy Markdown
Member

rexxars commented Sep 19, 2025

Should likely add a MIGRATION.md or at least a note in the readme about how to migrate from the previous version, but we can do that as a separate PR 👍

Copy link
Copy Markdown
Member Author

rdunk commented Sep 19, 2025

Absolutely, I assume I also need to add a changeset before merging.

I was planning on speaking to docs about this in advance of releasing too as there are a lot of references to @sanity/image-url around.

Thanks for the review 🙇.

@rdunk rdunk changed the title refactor: migrate to esm, use pkg-utils for build, switch tests to Vitest feat!: migrate to esm only, unify exports, use pkg-utils for build, switch tests to Vitest Sep 23, 2025
@rdunk rdunk force-pushed the 08-29-refactor_migrate_to_esm_use_pkg-utils_for_build_switch_tests_to_vitest branch from f80b5ba to 826e441 Compare September 23, 2025 13:11
Copy link
Copy Markdown
Member Author

rdunk commented Nov 27, 2025

Merge activity

  • Nov 27, 12:45 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Nov 27, 12:46 PM UTC: @rdunk merged this pull request with Graphite.

@rdunk rdunk merged commit 440e082 into main Nov 27, 2025
7 checks passed
@rdunk rdunk deleted the 08-29-refactor_migrate_to_esm_use_pkg-utils_for_build_switch_tests_to_vitest branch November 27, 2025 12:46
@squiggler-legacy squiggler-legacy bot mentioned this pull request Nov 27, 2025
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.

4 participants