-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make sure MSW loader only resolves when mocking is enabled
- Loading branch information
Showing
10 changed files
with
84 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,57 @@ | ||
import { InitializeOptions } from "./initialize.js"; | ||
import { InitializeOptions } from './initialize.js' | ||
|
||
const fileExtensionPattern = /\.(js|jsx|ts|tsx|mjs|woff|woff2|ttf|otf|eot)$/; | ||
const fileExtensionPattern = /\.(js|jsx|ts|tsx|mjs|woff|woff2|ttf|otf|eot)$/ | ||
const filteredURLSubstrings = [ | ||
"sb-common-assets", | ||
"node_modules", | ||
"node-modules", | ||
"hot-update.json", | ||
"__webpack_hmr", | ||
"sb-vite", | ||
]; | ||
'sb-common-assets', | ||
'node_modules', | ||
'node-modules', | ||
'hot-update.json', | ||
'__webpack_hmr', | ||
'sb-vite', | ||
'/virtual:', | ||
'.stories.', | ||
'.mdx', | ||
] | ||
|
||
const shouldFilterUrl = (url: string) => { | ||
// files which are mostly noise from webpack/vite builders + font files | ||
if (fileExtensionPattern.test(url)) { | ||
return true; | ||
return true | ||
} | ||
|
||
const isStorybookRequest = filteredURLSubstrings.some((substring) => | ||
url.includes(substring) | ||
); | ||
) | ||
|
||
if (isStorybookRequest) { | ||
return true; | ||
return true | ||
} | ||
|
||
return false; | ||
}; | ||
return false | ||
} | ||
|
||
export const augmentInitializeOptions = (options: InitializeOptions) => { | ||
if (typeof options?.onUnhandledRequest === "string") { | ||
return options; | ||
if (typeof options?.onUnhandledRequest === 'string') { | ||
return options | ||
} | ||
|
||
return { | ||
...options, | ||
// Filter requests that we know are not relevant to the user e.g. HMR, builder requests, statics assets, etc. | ||
onUnhandledRequest: (...args) => { | ||
const [{ url }, print] = args; | ||
const [{ url }, print] = args | ||
if (shouldFilterUrl(url)) { | ||
return; | ||
return | ||
} | ||
|
||
if (!options?.onUnhandledRequest) { | ||
print.warning(); | ||
return; | ||
print.warning() | ||
return | ||
} | ||
|
||
if (typeof options?.onUnhandledRequest === "function") { | ||
options.onUnhandledRequest(...args); | ||
if (typeof options?.onUnhandledRequest === 'function') { | ||
options.onUnhandledRequest(...args) | ||
} | ||
}, | ||
} as InitializeOptions; | ||
}; | ||
} as InitializeOptions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,10 @@ | ||
import { waitForMswReady } from '@build-time/initialize' | ||
import type { Context } from './decorator.js' | ||
import { applyRequestHandlers } from './applyRequestHandlers.js' | ||
|
||
export const mswLoader = async (context: Context) => { | ||
await waitForMswReady() | ||
applyRequestHandlers(context.parameters.msw) | ||
|
||
if ( | ||
typeof window !== 'undefined' && | ||
'navigator' in window && | ||
navigator.serviceWorker?.controller | ||
) { | ||
// No need to rely on the MSW Promise exactly | ||
// since only 1 worker can control 1 scope at a time. | ||
await navigator.serviceWorker.ready | ||
} | ||
|
||
return {} | ||
} |