From 6ca7f9bbbf581edff9f2459e717059006177f264 Mon Sep 17 00:00:00 2001 From: Philzen Date: Fri, 20 Sep 2024 01:11:18 +0200 Subject: [PATCH] Fix broken import replacement when createAuth isn't the last import Fixes #11588 --- packages/storybook/src/plugins/mock-auth.ts | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/storybook/src/plugins/mock-auth.ts b/packages/storybook/src/plugins/mock-auth.ts index 5d90393caf64..0eaa634cd1a0 100644 --- a/packages/storybook/src/plugins/mock-auth.ts +++ b/packages/storybook/src/plugins/mock-auth.ts @@ -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 =