( raw: string, clean: string, i: number, )
| 133 | } |
| 134 | |
| 135 | async 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 | |
| 184 | export const workerImportMetaUrlRE: RegExp = |
| 185 | /\bnew\s+(?:Worker|SharedWorker)\s*\(\s*(new\s+URL\s*\(\s*('[^']+'|"[^"]+"|`[^`]+`)\s*,\s*import\.meta\.url\s*(?:,\s*)?\))/dg |
no test coverage detected