Repository: vitejs/vite
File: packages/vite/src/module-runner/evaluatedModules.ts (TypeScript)

Code before:
  /**
   * Extracts the inlined source map from the module code and returns the decoded
   * source map. If the source map is not inlined, it will return null.
   * @param id Resolved module ID
   */
  getModuleSourceMapById(id: string): DecodedMap | null {
    const mod = this.getModuleById(id)
    if (!mod) return null
    if (mod.map) return mod.map
    if (!mod.meta || !('code' in mod.meta)) return null

    // Find the last occurrence of sourceMappingURL by finding the last line that contains it
    const pattern = `//# ${SOURCEMAPPING_URL}=data:application/json;base64,`
    const lastIndex = mod.meta.code.lastIndexOf(pattern)
    if (lastIndex === -1) return null

    // Extract the line containing the last occurrence
    const codeFromLastMatch = mod.meta.code.substring(lastIndex)
    const firstLineBreak = codeFromLastMatch.indexOf('\n')
    const line =
      firstLineBreak === -1
        ? codeFromLastMatch
        : codeFromLastMatch.substring(0, firstLineBreak)

    // Apply the original regex to just this line
    const mapString = MODULE_RUNNER_SOURCEMAPPING_REGEXP.exec(line)?.[1]
    if (!mapString) return null

    try {
      mod.map = new DecodedMap(JSON.parse(decodeBase64(mapString)), mod.file)
      return mod.map
    } catch {
      // Invalid base64 or malformed JSON - return null gracefully
      return null
    }
  }

  public clear(): void {
    this.idToModuleMap.clear()
    this.fileToModulesMap.clear()
    this.urlToIdModuleMap.clear()
  }
}

// unique id that is not available as "$bare_import" like "test"
// https://nodejs.org/api/modules.html#built-in-modules-with-mandatory-node-prefix
const prefixedBuiltins = new Set([
  'node:sea',
  'node:sqlite',
  'node:test',
  'node:test/reporters',

Review comment (suggestion):
```suggestion
    const mapString = MODULE_RUNNER_SOURCEMAPPING_REGEXP.exec(mod.meta.code.slice(lastIndex))?.[1]
```
@copilot actually this should be fine


Code after:
  /**
   * Extracts the inlined source map from the module code and returns the decoded
   * source map. If the source map is not inlined, it will return null.
   * @param id Resolved module ID
   */
  getModuleSourceMapById(id: string): DecodedMap | null {
    const mod = this.getModuleById(id)
    if (!mod) return null
    if (mod.map) return mod.map
    if (!mod.meta || !('code' in mod.meta)) return null

    const pattern = `//# ${SOURCEMAPPING_URL}=data:application/json;base64,`
    const lastIndex = mod.meta.code.lastIndexOf(pattern)
    if (lastIndex === -1) return null

    const mapString = MODULE_RUNNER_SOURCEMAPPING_REGEXP.exec(
      mod.meta.code.slice(lastIndex),
    )?.[1]
    if (!mapString) return null
    mod.map = new DecodedMap(JSON.parse(decodeBase64(mapString)), mod.file)
    return mod.map
  }

  public clear(): void {
    this.idToModuleMap.clear()
    this.fileToModulesMap.clear()
    this.urlToIdModuleMap.clear()
  }
}

// unique id that is not available as "$bare_import" like "test"
// https://nodejs.org/api/modules.html#built-in-modules-with-mandatory-node-prefix
const prefixedBuiltins = new Set([
  'node:sea',
  'node:sqlite',
  'node:test',
  'node:test/reporters',
])

// transform file url to id
// virtual:custom -> virtual:custom
// \0custom -> \0custom
// node:fs -> fs
// /@fs/C:/root/id.js => C:/root/id.js
// file:///C:/root/id.js -> C:/root/id.js
export function normalizeModuleId(file: string): string {
  if (prefixedBuiltins.has(file)) return file

  // unix style, but Windows path still starts with the drive letter to check the root
  const unixFile = slash(file)
    .replace(/^\/@fs\//, isWindows ? '' : '/')