Skip to content

Commit a17bcd1

Browse files
committed
use webpack alias hack to suppress "msw/browser" imports
1 parent f45cce8 commit a17bcd1

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

examples/with-next/app/mockProvider.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
'use client'
22
import { useEffect, useState } from 'react'
33

4-
if (typeof window === 'undefined') {
5-
throw new Error('Is not this a browser-only module?')
6-
}
4+
// if (typeof window === 'undefined') {
5+
// throw new Error('Is not this a browser-only module?')
6+
// }
77

8-
export async function MockProvider({
8+
export function MockProvider({
99
children,
1010
}: Readonly<{
1111
children: React.ReactNode

examples/with-next/app/movieList.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ export function MovieList() {
2727
}),
2828
})
2929
.then((response) => response.json())
30-
.then((movies) => setMovies(movies))
30+
.then((response) => {
31+
setMovies(response.data.movies)
32+
})
3133
.catch(() => setMovies([]))
3234
}
3335

@@ -37,7 +39,7 @@ export function MovieList() {
3739
{movies.length > 0 ? (
3840
<ul>
3941
{movies.map((movie) => (
40-
<li id={movie.id}>{movie.title}</li>
42+
<li key={movie.id}>{movie.title}</li>
4143
))}
4244
</ul>
4345
) : null}

examples/with-next/mocks/handlers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { User } from '../app/page'
44
export const handlers = [
55
http.get<never, never, User>('https://api.example.com/user', () => {
66
return HttpResponse.json({
7-
firstName: 'John',
7+
firstName: 'Kate',
88
lastName: 'Maverick',
99
})
1010
}),

examples/with-next/next.config.mjs

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,29 @@
22
const nextConfig = {
33
experimental: {
44
instrumentationHook: true
5+
},
6+
webpack(config, { isServer }) {
7+
/**
8+
* @fixme This is completely redundant. webpack should understand
9+
* export conditions and don't try to import "msw/browser" code
10+
* that's clearly marked as client-side only in the app.
11+
*/
12+
if (isServer) {
13+
if (Array.isArray(config.resolve.alias)) {
14+
config.resolve.alias.push({ name: "msw/browser", alias: false })
15+
} else {
16+
config.resolve.alias["msw/browser"] = false
17+
}
18+
} else {
19+
if (Array.isArray(config.resolve.alias)) {
20+
config.resolve.alias.push({ name: "msw/node", alias: false })
21+
} else {
22+
config.resolve.alias["msw/node"] = false
23+
}
24+
}
25+
26+
return config;
527
}
6-
};
28+
}
729

830
export default nextConfig;

0 commit comments

Comments
 (0)