| 416 | * @param optimizeExport for dynamicImportVar plugin don't need to optimize export. |
| 417 | */ |
| 418 | export async function transformGlobImport( |
| 419 | code: string, |
| 420 | id: string, |
| 421 | root: string, |
| 422 | resolveId: IdResolver, |
| 423 | restoreQueryExtension = false, |
| 424 | logger?: Logger, |
| 425 | ): Promise<TransformGlobImportResult | null> { |
| 426 | id = slash(id) |
| 427 | root = slash(root) |
| 428 | const isVirtual = isVirtualModule(id) |
| 429 | const dir = isVirtual ? undefined : dirname(id) |
| 430 | const matches = await parseImportGlob( |
| 431 | code, |
| 432 | isVirtual ? undefined : id, |
| 433 | root, |
| 434 | resolveId, |
| 435 | logger, |
| 436 | ) |
| 437 | const matchedFiles = new Set<string>() |
| 438 | |
| 439 | if (!matches.length) return null |
| 440 | |
| 441 | const s = new MagicString(code) |
| 442 | |
| 443 | const staticImports = ( |
| 444 | await Promise.all( |
| 445 | matches.map( |
| 446 | async ({ |
| 447 | globsResolved, |
| 448 | isRelative, |
| 449 | options, |
| 450 | index, |
| 451 | start, |
| 452 | end, |
| 453 | onlyKeys, |
| 454 | onlyValues, |
| 455 | }) => { |
| 456 | if (!dir && !options.base && isRelative) { |
| 457 | throw new Error("In virtual modules, all globs must start with '/'") |
| 458 | } |
| 459 | |
| 460 | const cwd = getCommonBase(globsResolved) ?? root |
| 461 | const files = ( |
| 462 | await glob(globsResolved, { |
| 463 | absolute: true, |
| 464 | cwd, |
| 465 | dot: !!options.exhaustive, |
| 466 | expandDirectories: false, |
| 467 | caseSensitiveMatch: options.caseSensitive ?? true, |
| 468 | ignore: options.exhaustive ? [] : ['**/node_modules/**'], |
| 469 | extglob: false, |
| 470 | }) |
| 471 | ) |
| 472 | .filter((file) => file !== id) |
| 473 | .sort() |
| 474 | |
| 475 | const objectProps: string[] = [] |