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

Function getWorkerType

packages/vite/src/node/plugins/workerImportMetaUrl.ts:135–182  ·  view source on GitHub ↗
(
  raw: string,
  clean: string,
  i: number,
)

Source from the content-addressed store, hash-verified

133}
134
135async function getWorkerType(
136 raw: string,
137 clean: string,
138 i: number,
139): Promise<WorkerType> {
140 const commaIndex = clean.indexOf(',', i)
141 if (commaIndex === -1) {
142 return 'classic'
143 }
144 const endIndex = findClosingParen(clean, i)
145
146 // case: ') ... ,' mean no worker options params
147 if (commaIndex > endIndex) {
148 return 'classic'
149 }
150
151 // need to find in comment code
152 let workerOptString = raw.substring(commaIndex + 1, endIndex)
153 const hasViteIgnore = hasViteIgnoreRE.test(workerOptString)
154 if (hasViteIgnore) {
155 return 'ignore'
156 }
157
158 // need to find in no comment code
159 const cleanWorkerOptString = clean.substring(commaIndex + 1, endIndex)
160 const trimmedCleanWorkerOptString = cleanWorkerOptString.trim()
161 if (!trimmedCleanWorkerOptString.length) {
162 return 'classic'
163 }
164
165 // strip trailing comma for evalValue
166 if (trimmedCleanWorkerOptString.endsWith(',')) {
167 workerOptString = workerOptString.slice(
168 0,
169 cleanWorkerOptString.lastIndexOf(','),
170 )
171 }
172
173 const workerOpts = await parseWorkerOptions(workerOptString, commaIndex + 1)
174 if (
175 workerOpts.type &&
176 (workerOpts.type === 'module' || workerOpts.type === 'classic')
177 ) {
178 return workerOpts.type
179 }
180
181 return 'classic'
182}
183
184export const workerImportMetaUrlRE: RegExp =
185 /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\))/dg

Callers 1

handlerFunction · 0.85

Calls 2

findClosingParenFunction · 0.85
parseWorkerOptionsFunction · 0.85

Tested by

no test coverage detected