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: makes the library esm-compatible #1399

Merged
merged 6 commits into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -141,6 +141,7 @@
"regenerator-runtime": "^0.13.9",
"rimraf": "^3.0.2",
"simple-git-hooks": "^2.8.0",
"statuses": "^2.0.0",
"ts-jest": "26",
"ts-loader": "^9.2.6",
"ts-node": "^10.1.0",
Expand Down
7 changes: 6 additions & 1 deletion src/context/fetch.ts
Expand Up @@ -3,7 +3,12 @@ import { Headers } from 'headers-polyfill'
import { MockedRequest } from '../utils/request/MockedRequest'

const useFetch: (input: RequestInfo, init?: RequestInit) => Promise<Response> =
isNodeProcess() ? require('node-fetch') : window.fetch
isNodeProcess()
? (input, init) =>
import('node-fetch').then(({ default: nodeFetch }) =>
(nodeFetch as unknown as typeof window.fetch)(input, init),
)
: window.fetch

export const augmentRequestInit = (requestInit: RequestInit): RequestInit => {
const headers = new Headers(requestInit.headers)
Expand Down
3 changes: 2 additions & 1 deletion src/node/createSetupServer.ts
@@ -1,4 +1,5 @@
import { bold } from 'chalk'
import chalk from 'chalk'
const { bold } = chalk
import { isNodeProcess } from 'is-node-process'
import { StrictEventEmitter } from 'strict-event-emitter'
import {
Expand Down
4 changes: 2 additions & 2 deletions src/node/setupServer.ts
@@ -1,5 +1,5 @@
import { ClientRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/ClientRequest'
import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/XMLHttpRequest'
import { ClientRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/ClientRequest/index.js'
import { XMLHttpRequestInterceptor } from '@mswjs/interceptors/lib/interceptors/XMLHttpRequest/index.js'
Copy link
Contributor

Choose a reason for hiding this comment

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

we should really just update interceptors to expose an esm build.

I started on that over the weekend, and have some thoughts on how we could do that, but I think we'll need to probably remove debug as a dependency there - since it relies on tty and os, and i'm having issues getting those properly bundled to a browser compat version, that multiple build tools like.

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 look into some debug recipes perhaps. It has browser support, so it should run in the browser. Perhaps we can do some import map for this dependency to ensure that in the browser it always imports the browser dist.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, it mostly relates to the fact that debug doesn't have an esm build, so loading in a browser directly from @web/dev-server seems problematic.

Maybe that just means users in those environments have to do some extra setup, and that's enough. Will keep playing with configurations there, because it's also possible we don't need to be as explicit there.

I was testing using tsup to bundle, however using tsc to generate cjs and esm transpilations might help avoid issues related to that (since it won't try to handle debug directly)

import { createSetupServer } from './createSetupServer'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/utils/matching/matchRequestUrl.ts
@@ -1,5 +1,5 @@
import { match } from 'path-to-regexp'
import { getCleanUrl } from '@mswjs/interceptors/lib/utils/getCleanUrl'
import { getCleanUrl } from '@mswjs/interceptors/lib/utils/getCleanUrl.js'
import { normalizePath } from './normalizePath'

export type Path = string | RegExp
Expand Down