/@fs/ 相关请求处理

    1. export function serveRawFsMiddleware(): Connect.NextHandleFunction {
    2. const isWin = os.platform() === 'win32'
    3. const serveFromRoot = sirv('/', sirvOptions)
    4. return (req, res, next) => {
    5. let url = req.url!
    6. // In some cases (e.g. linked monorepos) files outside of root will
    7. // reference assets that are also out of served root. In such cases
    8. // the paths are rewritten to `/@fs/` prefixed paths and must be served by
    9. // searching based from fs root.
    10. // 如果 url 以 /@fs/ 为前缀,则视为静态资源获取,
    11. // 同时,如果是 windows 环境,而且 url 类似于 /@fs/D:asdfasdfasdf 这种带绝对路径的,直接抹除盘符号,强制视为以项目根文件夹为起始路径
    12. // 通过 sirv 这个库获取静态文件并返回
    13. if (url.startsWith(FS_PREFIX)) {
    14. url = url.slice(FS_PREFIX.length)
    15. if (isWin) url = url.replace(/^[A-Z]:/i, '')
    16. req.url = url
    17. serveFromRoot(req, res, next)
    18. } else {
    19. next()
    20. }
    21. }
    22. }