Skip to content

Commit

Permalink
Fix storybook-vite not starting when auth.ts contains named imports…
Browse files Browse the repository at this point in the history
… after `createAuth` (#11593)

Fixes #11588
  • Loading branch information
Philzen authored and Josh-Walker-GM committed Sep 24, 2024
1 parent 05f301d commit ce9c723
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 7 deletions.
19 changes: 19 additions & 0 deletions .changesets/11593.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- Fix storybook-vite not starting when `auth.ts` contains named imports after `createAuth` (#11593) by @Philzen

This change fixes an issue with our internal storybook mocks. Previous to this you might find that your storybook would fail with an error like:

```
3:49:51 PM [vite] Internal server error: Transform failed with 1 error:
…/web/src/auth.ts:2:9: ERROR: Expected identifier but found ","
Plugin: vite:esbuild
File: …/web/src/auth.ts:3:6
Expected identifier but found ","
1 | import { createAuthentication as createAuth } from '@redwoodjs/testing/dist/web/mockAuth.js'
2 | import { , createDbAuthClient } from '@redwoodjs/auth-dbauth-web';
| ^
3 | const dbAuthClient = createDbAuthClient();
4 | export const {
```

This was due to a bug in our handling of imports which this change fixes.
2 changes: 1 addition & 1 deletion __fixtures__/test-project/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"postcss": "^8.4.47",
"postcss-loader": "^8.1.1",
"prettier-plugin-tailwindcss": "^0.5.12",
"tailwindcss": "^3.4.12"
"tailwindcss": "^3.4.13"
}
}
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 ce9c723

Please sign in to comment.