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

Function handler

packages/vite/src/node/plugins/assetImportMetaUrl.ts:64–177  ·  view source on GitHub ↗
(code, id)

Source from the content-addressed store, hash-verified

62 code: assetImportMetaUrlRE,
63 },
64 async handler(code, id) {
65 let s: MagicString | undefined
66 const re = new RegExp(assetImportMetaUrlRE)
67 const cleanString = stripLiteral(code)
68
69 let match: RegExpExecArray | null
70 while ((match = re.exec(cleanString))) {
71 const [[startIndex, endIndex], [urlStart, urlEnd]] =
72 match.indices as Array<[number, number]>
73 if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue
74
75 const rawUrl = code.slice(urlStart, urlEnd)
76
77 if (!s) s = new MagicString(code)
78
79 // potential dynamic template string
80 if (rawUrl[0] === '`' && rawUrl.includes('${')) {
81 const queryDelimiterIndex = getQueryDelimiterIndex(rawUrl)
82 const hasQueryDelimiter = queryDelimiterIndex !== -1
83 const pureUrl = hasQueryDelimiter
84 ? rawUrl.slice(0, queryDelimiterIndex) + '`'
85 : rawUrl
86 const queryString = hasQueryDelimiter
87 ? rawUrl.slice(queryDelimiterIndex, -1)
88 : ''
89 const ast = this.parse(pureUrl)
90 const templateLiteral = (ast as any).body[0].expression
91 if (templateLiteral.expressions.length) {
92 const pattern = buildGlobPattern(templateLiteral)
93 if (pattern[0] === '*') {
94 // don't transform for patterns like this
95 // because users won't intend to do that in most cases
96 continue
97 }
98
99 const globOptions = {
100 eager: true,
101 import: 'default',
102 // A hack to allow 'as' & 'query' exist at the same time
103 query: injectQuery(queryString, 'url'),
104 }
105 s.update(
106 startIndex,
107 endIndex,
108 `new URL((import.meta.glob(${JSON.stringify(
109 pattern,
110 )}, ${JSON.stringify(
111 globOptions,
112 )}))[${pureUrl}], import.meta.url)`,
113 )
114 continue
115 }
116 }
117
118 const url = rawUrl.slice(1, -1)
119 if (isDataUrl(url)) {
120 continue
121 }

Callers

nothing calls this directly

Calls 15

injectQueryFunction · 0.90
isDataUrlFunction · 0.90
slashFunction · 0.90
tryFsResolveFunction · 0.90
isParentDirectoryFunction · 0.90
fileToUrlFunction · 0.90
tryStatSyncFunction · 0.90
transformStableResultFunction · 0.90
getQueryDelimiterIndexFunction · 0.85
buildGlobPatternFunction · 0.85
parseMethod · 0.80

Tested by

no test coverage detected