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: export browser integration from "msw/browser" #1591

Merged
merged 3 commits into from
Apr 11, 2023

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Apr 11, 2023

Changes

  • Browser integration (setupWorker and its types) is now exported under msw/browser.
  • The msw root-level export only exports primitives and utilities. No integrations there.

Why this change?

I want to ship proper ESM support and CJS support for older bundlers. With ESM it's pretty clear, and it's been addressed in #1584. The problem is TypeScript. With conditional exports, TS will not know what environment is the module you're writing:

// src/pages/index.tsx
import { setupWorker } from 'msw'
// No exported thingie "setupWorker" in "msw"!

Because TS will assume Node and will resolve msw under Node module resolution, respecting its exports, where no setupWorker types are exported in lib/node/index.d.ts (rightfully).

To ship this CJS/ESM support, we must provide the export pattern that will not make TypeScript confused. Instead of diving into another half a year of work to restructure this into a monorepo, I'm falling back to a /browser directory export:

import { rest } from 'msw'
import { setupWorker } from 'msw/browser'
  • Explicit intention.
  • CJS-compatible.
  • Old bundler-compatible (via the root-level /browser directory).
  • TypeScript is happy (non-ambiguous import).

@kettanaito kettanaito changed the base branch from main to feat/standard-api April 11, 2023 20:30
@codesandbox-ci
Copy link

codesandbox-ci bot commented Apr 11, 2023

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit f6ea87d:

Sandbox Source
MSW React Configuration

@kettanaito kettanaito merged commit 651e1a8 into feat/standard-api Apr 11, 2023
@kettanaito kettanaito deleted the feat/browser-dir branch April 11, 2023 22:05
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.

None yet

1 participant