Repository: vitejs/vite
File: packages/vite/src/node/optimizer/rolldownDepPlugin.ts (TypeScript)

Code before:
          let hasReplacements = false
          let match
          while ((match = workerRE.exec(code))) {
            hasReplacements = true
            const [fullMatch, prefix, _, url] = match
            const absolutePath = path.resolve(path.dirname(id), url)
            const relativePath = path.relative(bundleDir, absolutePath)
            const normalizedRelativePath = normalizePath(relativePath)

            // NOTE: add `'' +` to opt-out rolldown's transform: https://github.com/rolldown/rolldown/issues/2745
            const replacement = `${prefix}new URL('' + ${JSON.stringify(normalizedRelativePath)}, import.meta.url)`

            s.overwrite(
              match.index,
              match.index + fullMatch.length,
              replacement,
            )

            const assetDir = path.dirname(absolutePath)

            if (
              environment.config.server.fs.allow &&
              !environment.config.server.fs.allow.includes(assetDir)
            ) {
              environment.config.server.fs.allow.push(assetDir)
            }
          }

          if (!hasReplacements) return null

          return { code: s.toString(), map: s.generateMap({ hires: true }) }
        },
      },
    },
  ]
}

const matchesEntireLine = (text: string) => `^${escapeRegex(text)}$`

// rolldown (and esbuild) doesn't transpile `require('foo')` into `import` statements if 'foo' is externalized
// https://rolldown.rs/in-depth/bundling-cjs#require-external-modules
export function rolldownCjsExternalPlugin(
  externals: string[],
  platform: 'node' | 'browser' | 'neutral',
): Plugin | undefined {
  // Skip this plugin for `platform: 'node'` as `require` is available in Node
  // and that is more accurate than converting to `import`
  if (platform === 'node') {
    return undefined
  }


Review comment (suggestion):
I guess this part is not needed because it'll be handled by the other internal plugins.

Code after:

          let match: RegExpExecArray | null
          while ((match = assetImportMetaUrlRE.exec(cleanString))) {
            const [[startIndex, endIndex], [urlStart, urlEnd]] = match.indices!
            if (hasViteIgnoreRE.test(code.slice(startIndex, urlStart))) continue

            const rawUrl = code.slice(urlStart, urlEnd)

            if (rawUrl[0] === '`' && rawUrl.includes('${')) {
              // We skip dynamic template strings in the optimizer for now as they
              // require complex glob transformation that is handled by the main asset plugin.
              continue
            }

            const url = rawUrl.slice(1, -1)
            if (isDataUrl(url) || isExternalUrl(url) || url.startsWith('/')) {
              continue
            }

            if (!s) s = new MagicString(code)

            // we resolve the relative path from the original library file (id) and
            // then rewrite it relative to the bundle (deps) directory.
            const absolutePath = path.resolve(path.dirname(id), url)
            const relativePath = path.relative(bundleOutputDir, absolutePath)
            const normalizedRelativePath = normalizePath(relativePath)
            s.update(
              startIndex,
              endIndex,
              // NOTE: add `'' +` to opt-out rolldown's transform: https://github.com/rolldown/rolldown/issues/2745
              `new URL('' + ${JSON.stringify(
                normalizedRelativePath,
              )}, import.meta.url)`,
            )
          }

          if (s) {
            return {
              code: s.toString(),
              map: s.generateMap({ hires: 'boundary' }),
            }
          }
        },
      },
    },
  ]
}

const matchesEntireLine = (text: string) => `^${escapeRegex(text)}$`

// rolldown (and esbuild) doesn't transpile `require('foo')` into `import` statements if 'foo' is externalized