-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix: compile createMiddleware using start-compiler-plugin #5245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
File renamed without changes.
File renamed without changes.
File renamed without changes.
37 changes: 37 additions & 0 deletions
37
...s/start-plugin-core/tests/createMiddleware-start-compiler-plugin/createMiddleware.test.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| import { readFile, readdir } from 'node:fs/promises' | ||
| import path from 'node:path' | ||
| import { describe, expect, test } from 'vitest' | ||
| import { compileStartOutputFactory } from '../../src/start-compiler-plugin/compilers' | ||
|
|
||
| const compileStartOutput = compileStartOutputFactory('react') | ||
|
|
||
| async function getFilenames() { | ||
| return await readdir(path.resolve(import.meta.dirname, './test-files')) | ||
| } | ||
|
|
||
| describe('createMiddleware compiles correctly', async () => { | ||
| const filenames = await getFilenames() | ||
|
|
||
| describe.each(filenames)('should handle "%s"', async (filename) => { | ||
| const file = await readFile( | ||
| path.resolve(import.meta.dirname, `./test-files/${filename}`), | ||
| ) | ||
| const code = file.toString() | ||
|
|
||
| test.each(['client', 'server'] as const)( | ||
| `should compile for ${filename} %s`, | ||
| async (env) => { | ||
| const compiledResult = compileStartOutput({ | ||
| env, | ||
| code, | ||
| filename, | ||
| dce: false, | ||
| }) | ||
|
|
||
| await expect(compiledResult.code).toMatchFileSnapshot( | ||
| `./snapshots/${env}/${filename}`, | ||
| ) | ||
| }, | ||
| ) | ||
| }) | ||
| }) |
1 change: 1 addition & 0 deletions
1
...hots/client/create-function-middleware.ts → ...hots/client/create-function-middleware.ts
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
.../createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructured.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { createMiddleware } from '@tanstack/react-start'; | ||
| import { z } from 'zod'; | ||
| export const withUseServer = createMiddleware({ | ||
| id: 'test' | ||
| }); | ||
| export const withoutUseServer = createMiddleware({ | ||
| id: 'test' | ||
| }); | ||
| export const withVariable = createMiddleware({ | ||
| id: 'test' | ||
| }); | ||
| async function abstractedFunction() { | ||
| console.info('Fetching posts...'); | ||
| await new Promise(r => setTimeout(r, 500)); | ||
| return axios.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); | ||
| } | ||
| function zodValidator<TSchema extends z.ZodSchema, TResult>(schema: TSchema, fn: (input: z.output<TSchema>) => TResult) { | ||
| return async (input: unknown) => { | ||
| return fn(schema.parse(input)); | ||
| }; | ||
| } | ||
| export const withZodValidator = createMiddleware({ | ||
| id: 'test' | ||
| }); |
24 changes: 24 additions & 0 deletions
24
...eMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareDestructuredRename.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { createMiddleware as middlewareFn } from '@tanstack/react-start'; | ||
| import { z } from 'zod'; | ||
| export const withUseServer = middlewareFn({ | ||
| id: 'test' | ||
| }); | ||
| export const withoutUseServer = middlewareFn({ | ||
| id: 'test' | ||
| }); | ||
| export const withVariable = middlewareFn({ | ||
| id: 'test' | ||
| }); | ||
| async function abstractedFunction() { | ||
| console.info('Fetching posts...'); | ||
| await new Promise(r => setTimeout(r, 500)); | ||
| return axios.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); | ||
| } | ||
| function zodValidator<TSchema extends z.ZodSchema, TResult>(schema: TSchema, fn: (input: z.output<TSchema>) => TResult) { | ||
| return async (input: unknown) => { | ||
| return fn(schema.parse(input)); | ||
| }; | ||
| } | ||
| export const withZodValidator = middlewareFn({ | ||
| id: 'test' | ||
| }); |
24 changes: 24 additions & 0 deletions
24
...ts/createMiddleware-start-compiler-plugin/snapshots/client/createMiddlewareStarImport.tsx
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import * as TanStackStart from '@tanstack/react-start'; | ||
| import { z } from 'zod'; | ||
| export const withUseServer = TanStackStart.createMiddleware({ | ||
| id: 'test' | ||
| }); | ||
| export const withoutUseServer = TanStackStart.createMiddleware({ | ||
| id: 'test' | ||
| }); | ||
| export const withVariable = TanStackStart.createMiddleware({ | ||
| id: 'test' | ||
| }); | ||
| async function abstractedFunction() { | ||
| console.info('Fetching posts...'); | ||
| await new Promise(r => setTimeout(r, 500)); | ||
| return axios.get<Array<PostType>>('https://jsonplaceholder.typicode.com/posts').then(r => r.data.slice(0, 10)); | ||
| } | ||
| function zodValidator<TSchema extends z.ZodSchema, TResult>(schema: TSchema, fn: (input: z.output<TSchema>) => TResult) { | ||
| return async (input: unknown) => { | ||
| return fn(schema.parse(input)); | ||
| }; | ||
| } | ||
| export const withZodValidator = TanStackStart.createMiddleware({ | ||
| id: 'test' | ||
| }); |
1 change: 1 addition & 0 deletions
1
...hots/client/createMiddlewareValidator.tsx → ...hots/client/createMiddlewareValidator.tsx
This file contains hidden or 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,4 +1,5 @@ | ||
| import { createMiddleware } from '@tanstack/react-start'; | ||
| import { z } from 'zod'; | ||
| export const withUseServer = createMiddleware({ | ||
| id: 'test' | ||
| }); |
15 changes: 15 additions & 0 deletions
15
...tart-plugin-core/tests/createMiddleware-start-compiler-plugin/snapshots/client/factory.ts
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { createMiddleware } from '@tanstack/react-start'; | ||
| import { getCookie } from '@tanstack/react-start/server'; | ||
| interface AuthMiddlewareOptions { | ||
| allowUnauthenticated?: boolean; | ||
| } | ||
| interface AuthContext { | ||
| session: { | ||
| id: string; | ||
| } | null; | ||
| } | ||
| export const createAuthMiddleware = (opts: AuthMiddlewareOptions = { | ||
| allowUnauthenticated: false | ||
| }) => createMiddleware({ | ||
| type: 'function' | ||
| }); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix the
createMiddlewarefilter before shippingThe new
/.createMiddleware\(\)/filter only matches a literalcreateMiddleware()call with empty parentheses. Real code always has arguments (createMiddleware({...}),start.createMiddleware(...)), so the transform hook never receives those modules and the middleware rewrite never runs. Please broaden the regex so anycreateMiddleware(call is caught.📝 Committable suggestion
🤖 Prompt for AI Agents