Skip to content

Commit

Permalink
Fix broken import replacement when createAuth isn't the last import
Browse files Browse the repository at this point in the history
Fixes #11588
  • Loading branch information
Philzen authored Sep 19, 2024
1 parent cfabf64 commit 6ca7f9b
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/storybook/src/plugins/mock-auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ export function mockAuth(): PluginOption {
enforce: 'pre',
transform(code: string, id: string) {
if (id.includes('web/src/auth')) {
// remove any existing import of `createAuth` without affecting
// anything else.
// this regex defines 3 capture groups, where the second is
// `createAuth` — we want to remove that one.
// Remove any existing import of `createAuth` without affecting anything else.
// This regex defines 4 capture groups, where the second is `createAuth` and
// the third is an (optional) comma for subsequent named imports — we want to remove those two.
code = code.replace(
/(import\s*{\s*[^}]*)(\bcreateAuth\b)([^}]*})/,
'$1$3',
/(import\s*{\s*[^}]*)(\bcreateAuth\b)(,?)([^}]*})/,
'$1$4',
)
// Add import to mocked `createAuth` at the top of the file.
code =
Expand Down

0 comments on commit 6ca7f9b

Please sign in to comment.