Skip to content

fix: Narrow the Dev Server Middleware type #10522

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

n0th1ng-else
Copy link
Contributor

@n0th1ng-else n0th1ng-else commented May 29, 2025

Summary

It will be convenient to have the Dev Server Middleware types exported from the rspack. All our middleware is written in separate modules and annotated with JSDoc types.
By exporting the type, we can ge rid of webpack-dev-server mentions in the annotation and use import('@rspack/core').DevServerMiddleware to type the middleware we have.

I have also made an effort to narrow the Middleware type so it is aligned with the one in the webpack-dev-middleware.

(see https://github.com/webpack/webpack-dev-middleware/blob/master/types/index.d.ts#L370)

Example

/**
 * @return {import('@rspack/core').DevServerMiddleware}
 */
module.exports = function DevServerPreflightRequestsMiddleware() {
	return {
		name: 'DevServerPreflightRequestsMiddleware',
		middleware: (req, res, next) => {
			if (req.method === 'OPTIONS') {
				res.statusCode = 204
				res.setHeader('Content-Length', '0')
				res.end()
				return
			}
			next()
		},
	}
}
image

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

It will be convenient to have the Dev Server Middleware types exported from
the rspack. All our middleware is written in separate modules and annotated
with JSDoc types.
By exporting the type, we can ge rid of webpack-dev-server mentions in the
annotation and use `import('@rspack/core').DevServerMiddleware` to type
the middleware we have.

I have also made an effort to narrow the Middleware type so it is aligned
with the one in the webpack-dev-middleware.

(see https://github.com/webpack/webpack-dev-middleware/blob/master/types/index.d.ts#L370)
Copy link

netlify bot commented May 29, 2025

Deploy Preview for rspack canceled.

Built without sensitive environment variables

Name Link
🔨 Latest commit 6adc201
🔍 Latest deploy log https://app.netlify.com/projects/rspack/deploys/6838ead988406e0008a58104

@github-actions github-actions bot added the release: bug fix release: bug related release(mr only) label May 29, 2025
@n0th1ng-else n0th1ng-else marked this pull request as ready for review May 29, 2025 23:18
Copy link

codspeed-hq bot commented May 29, 2025

CodSpeed Performance Report

Merging #10522 will not alter performance

Comparing n0th1ng-else:middleware-type (6adc201) with main (8450da9)

Summary

✅ 12 untouched benchmarks

@chenjiahan chenjiahan requested a review from LingyuCoder May 30, 2025 02:21
@chenjiahan
Copy link
Member

@LingyuCoder cc~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release: bug fix release: bug related release(mr only)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants