( server: ViteDevServer, )
| 110 | } |
| 111 | |
| 112 | export function transformMiddleware( |
| 113 | server: ViteDevServer, |
| 114 | ): Connect.NextHandleFunction { |
| 115 | // Keep the named function. The name is visible in debug logs via `DEBUG=connect:dispatcher ...` |
| 116 | |
| 117 | // check if public dir is inside root dir |
| 118 | const { root, publicDir } = server.config |
| 119 | const publicDirInRoot = publicDir.startsWith(withTrailingSlash(root)) |
| 120 | const publicPath = `${publicDir.slice(root.length)}/` |
| 121 | |
| 122 | return async function viteTransformMiddleware(req, res, next) { |
| 123 | const environment = server.environments.client |
| 124 | |
| 125 | if ( |
| 126 | (req.method !== 'GET' && req.method !== 'HEAD') || |
| 127 | knownIgnoreList.has(req.url!) || |
| 128 | isDocumentFetchDest(req) |
| 129 | ) { |
| 130 | return next() |
| 131 | } |
| 132 | |
| 133 | let url: string |
| 134 | try { |
| 135 | url = decodeURI(removeTimestampQuery(req.url!)).replace( |
| 136 | NULL_BYTE_PLACEHOLDER, |
| 137 | '\0', |
| 138 | ) |
| 139 | } catch (e) { |
| 140 | if (e instanceof URIError) { |
| 141 | server.config.logger.warn( |
| 142 | colors.yellow( |
| 143 | `Malformed URI sequence in request URL: ${removeTimestampQuery(req.url!)}`, |
| 144 | ), |
| 145 | ) |
| 146 | return next() |
| 147 | } |
| 148 | return next(e) |
| 149 | } |
| 150 | |
| 151 | const withoutQuery = cleanUrl(url) |
| 152 | |
| 153 | try { |
| 154 | const isSourceMap = withoutQuery.endsWith('.map') |
| 155 | // since we generate source map references, handle those requests here |
| 156 | if (isSourceMap) { |
| 157 | const depsOptimizer = environment.depsOptimizer |
| 158 | if (depsOptimizer?.isOptimizedDepUrl(url)) { |
| 159 | // If the browser is requesting a source map for an optimized dep, it |
| 160 | // means that the dependency has already been pre-bundled and loaded |
| 161 | const sourcemapPath = url.startsWith(FS_PREFIX) |
| 162 | ? fsPathFromId(url) |
| 163 | : normalizePath(path.resolve(server.config.root, url.slice(1))) |
| 164 | // url may contain relative path that may resolve outside of the optimized deps directory |
| 165 | if (!depsOptimizer.isOptimizedDepFile(sourcemapPath)) { |
| 166 | return next() |
| 167 | } |
| 168 | try { |
| 169 | const map = JSON.parse( |
no test coverage detected