1. export function serveStaticMiddleware(
    2. dir: string,
    3. config: ResolvedConfig
    4. ): Connect.NextHandleFunction {
    5. const serve = sirv(dir, sirvOptions)
    6. return (req, res, next) => {
    7. const url = req.url!
    8. // only serve the file if it's not an html request
    9. // so that html requests can fallthrough to our html middleware for
    10. // special processing
    11. if (path.extname(cleanUrl(url)) === '.html') {
    12. return next()
    13. }
    14. // apply aliases to static requests as well
    15. let redirected: string | undefined
    16. for (const { find, replacement } of config.resolve.alias) {
    17. const matches =
    18. typeof find === 'string' ? url.startsWith(find) : find.test(url)
    19. if (matches) {
    20. redirected = url.replace(find, replacement)
    21. break
    22. }
    23. }
    24. if (redirected) {
    25. // dir is pre-normalized to posix style
    26. if (redirected.startsWith(dir)) {
    27. redirected = redirected.slice(dir.length)
    28. }
    29. req.url = redirected
    30. }
    31. serve(req, res, next)
    32. }
    33. }