| 228 | id: new RegExp(`(?:${Object.keys(deps).join('|')})$`), |
| 229 | }, |
| 230 | handler(code, id) { |
| 231 | const file = Object.keys(deps).find((file) => |
| 232 | id.replace(/\\/g, '/').endsWith(file), |
| 233 | ) |
| 234 | if (!file) return |
| 235 | |
| 236 | for (const { src, replacement, pattern } of deps[file]) { |
| 237 | const magicString = new MagicString(code) |
| 238 | |
| 239 | if (src) { |
| 240 | const pos = code.indexOf(src) |
| 241 | if (pos < 0) { |
| 242 | this.error( |
| 243 | `Could not find expected src "${src}" in file "${file}"`, |
| 244 | ) |
| 245 | } |
| 246 | transformed[file] = true |
| 247 | magicString.overwrite(pos, pos + src.length, replacement) |
| 248 | } |
| 249 | |
| 250 | if (pattern) { |
| 251 | let match |
| 252 | while ((match = pattern.exec(code))) { |
| 253 | transformed[file] = true |
| 254 | const start = match.index |
| 255 | const end = start + match[0].length |
| 256 | let _replacement = replacement |
| 257 | for (let i = 1; i <= match.length; i++) { |
| 258 | _replacement = _replacement.replace(`$${i}`, match[i] || '') |
| 259 | } |
| 260 | magicString.overwrite(start, end, _replacement) |
| 261 | } |
| 262 | if (!transformed[file]) { |
| 263 | this.error( |
| 264 | `Could not find expected pattern "${pattern}" in file "${file}"`, |
| 265 | ) |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | code = magicString.toString() |
| 270 | } |
| 271 | |
| 272 | console.log(`shimmed: ${file}`) |
| 273 | |
| 274 | return code |
| 275 | }, |
| 276 | }, |
| 277 | buildEnd(err) { |
| 278 | if (this.meta.watchMode) return |