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

Function handler

packages/vite/src/node/plugins/workerImportMetaUrl.ts:208–291  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

206 transform: {
207 filter: { code: workerImportMetaUrlRE },
208 async handler(code, id) {
209 const isBundled = this.environment.config.isBundled
210 let s: MagicString | undefined
211 const cleanString = stripLiteral(code)
212 const re = new RegExp(workerImportMetaUrlRE)
213
214 let match: RegExpExecArray | null
215 while ((match = re.exec(cleanString))) {
216 const [[, endIndex], [expStart, expEnd], [urlStart, urlEnd]] =
217 match.indices as Array<[number, number]>
218
219 const rawUrl = code.slice(urlStart, urlEnd)
220
221 // potential dynamic template string
222 if (rawUrl[0] === '`' && rawUrl.includes('${')) {
223 this.error(
224 `\`new URL(url, import.meta.url)\` is not supported in dynamic template string.`,
225 expStart,
226 )
227 }
228
229 s ||= new MagicString(code)
230 const workerType = await getWorkerType(code, cleanString, endIndex)
231 const url = rawUrl.slice(1, -1)
232 let file: string | undefined
233 if (url[0] === '.') {
234 file = path.resolve(path.dirname(id), url)
235 file = slash(tryFsResolve(file, fsResolveOptions) ?? file)
236 } else {
237 workerResolver ??= createBackCompatIdResolver(config, {
238 extensions: [],
239 tryIndex: false,
240 preferRelative: true,
241 })
242 file = await workerResolver(this.environment, url, id)
243 file ??=
244 url[0] === '/'
245 ? slash(path.join(config.publicDir, url))
246 : slash(path.resolve(path.dirname(id), url))
247 }
248
249 if (
250 isBundled &&
251 config.isWorker &&
252 config.bundleChain.at(-1) === cleanUrl(file)
253 ) {
254 s.update(expStart, expEnd, 'self.location.href')
255 } else {
256 let builtUrl: string
257 if (isBundled) {
258 const result = await workerFileToUrl(config, file)
259 if (this.environment.config.command === 'serve') {
260 builtUrl = toOutputFilePathInJSForBundledDev(
261 this.environment,
262 result.entryFilename,
263 )
264 } else {
265 builtUrl = result.entryUrlPlaceholder

Callers

nothing calls this directly

Calls 13

slashFunction · 0.90
tryFsResolveFunction · 0.90
cleanUrlFunction · 0.90
workerFileToUrlFunction · 0.90
fileToUrlFunction · 0.90
injectQueryFunction · 0.90
transformStableResultFunction · 0.90
getWorkerTypeFunction · 0.85
errorMethod · 0.65
resolveMethod · 0.65

Tested by

no test coverage detected