MCPcopy
hub / github.com/vitejs/vite / replacer

Function replacer

packages/vite/src/node/server/middlewares/indexHtml.ts:144–197  ·  view source on GitHub ↗
(url: string)

Source from the content-addressed store, hash-verified

142): string => {
143 // prefix with base (dev only, base is never relative)
144 const replacer = (url: string) => {
145 if (
146 (url[0] === '/' && url[1] !== '/') ||
147 // #3230 if some request url (localhost:3000/a/b) return to fallback html, the relative assets
148 // path will add `/a/` prefix, it will caused 404.
149 //
150 // skip if url contains `:` as it implies a url protocol or Windows path that we don't want to replace.
151 //
152 // rewrite `./index.js` -> `localhost:5173/a/index.js`.
153 // rewrite `../index.js` -> `localhost:5173/index.js`.
154 // rewrite `relative/index.js` -> `localhost:5173/a/relative/index.js`.
155 ((url[0] === '.' || isBareRelative(url)) &&
156 originalUrl &&
157 originalUrl !== '/' &&
158 htmlPath === '/index.html')
159 ) {
160 url = path.posix.join(config.base, url)
161 }
162
163 let preTransformUrl: string | undefined
164
165 if (!isClassicScriptLink && shouldPreTransform(url, config)) {
166 if (url[0] === '/' && url[1] !== '/') {
167 preTransformUrl = url
168 } else if (url[0] === '.' || isBareRelative(url)) {
169 preTransformUrl = path.posix.join(
170 config.base,
171 getHtmlDirnameForRelativeUrl(htmlPath),
172 url,
173 )
174 }
175 }
176
177 if (server) {
178 const mod = server.environments.client.moduleGraph.urlToModuleMap.get(
179 preTransformUrl || url,
180 )
181 if (mod && mod.lastHMRTimestamp > 0) {
182 url = injectQuery(url, `t=${mod.lastHMRTimestamp}`)
183 }
184 }
185
186 if (server && preTransformUrl) {
187 try {
188 preTransformUrl = decodeURI(preTransformUrl)
189 } catch {
190 // Malformed uri. Skip pre-transform.
191 return url
192 }
193 preTransformRequest(server, preTransformUrl, config.decodedBase)
194 }
195
196 return url
197 }
198
199 const processedUrl = useSrcSetReplacer
200 ? processSrcSetSync(url, ({ url }) => replacer(url))

Callers 8

asyncReplaceFunction · 0.85
processSrcSetFunction · 0.85
processSrcSetSyncFunction · 0.85
processNodeUrlFunction · 0.85
doUrlReplaceFunction · 0.85
doImportCSSReplaceFunction · 0.85
getHmrImplementationFunction · 0.85
css.spec.tsFile · 0.85

Calls 6

injectQueryFunction · 0.90
isBareRelativeFunction · 0.85
shouldPreTransformFunction · 0.85
preTransformRequestFunction · 0.85
getMethod · 0.45

Tested by

no test coverage detected