{"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import fs from 'node:fs';\nimport { promisify } from 'node:util';\nimport pluginUtils, { type CreateFilter } from '@rollup/pluginutils';\nimport type { ExistingRawSourceMap, Plugin, PluginContext } from 'rollup';\nimport { resolveSourceMap, resolveSources } from './source-map-resolve.js';\n\nconst { createFilter } = pluginUtils;\n\nexport interface SourcemapsPluginOptions {\n  include?: Parameters<CreateFilter>[0];\n  exclude?: Parameters<CreateFilter>[1];\n  readFile?(path: string, callback: (error: Error | null, data: string) => void): void;\n}\n\nexport default function sourcemaps(\n  // eslint-disable-next-line @typescript-eslint/unbound-method\n  { include, exclude, readFile }: SourcemapsPluginOptions = {},\n): Plugin {\n  // Create a filter function based on the include and exclude options\n  const filter = createFilter(include, exclude);\n\n  // Default readFile that always reads as UTF-8 string\n  const defaultReadFile = (\n    path: string,\n    cb: (err: NodeJS.ErrnoException | null, data: string) => void,\n  ) => {\n    fs.readFile(path, 'utf8', cb);\n  };\n\n  // Use the provided readFile or the default one\n  const effectiveReadFile = readFile || defaultReadFile;\n\n  // Promisify the readFile function\n  const promisifiedReadFile = promisify(effectiveReadFile);\n\n  return {\n    name: 'sourcemaps',\n\n    load: async function (this: PluginContext, id: string) {\n      let code: string;\n      // If the id does not pass the filter, return null\n      if (!filter(id)) {\n        return null;\n      }\n\n      try {\n        // Try to read the file with the given id\n        code = await promisifiedReadFile(id);\n        // Add the file to the watch list\n        this.addWatchFile(id);\n      } catch {\n        try {\n          // If reading fails, try again without a query suffix that some plugins use\n          const cleanId = id.replace(/\\?.*$/, '');\n          code = (await promisifiedReadFile(cleanId)).toString();\n          // Add the file to the watch list\n          this.addWatchFile(cleanId);\n        } catch {\n          // If reading still fails, warn and return null\n          this.warn('Failed reading file');\n          return null;\n        }\n      }\n\n      let map: ExistingRawSourceMap;\n      try {\n        // Try to resolve the source map for the code\n        const result = await resolveSourceMap(code, id, promisifiedReadFile);\n\n        // If the code contained no sourceMappingURL, return the code\n        if (result === null) {\n          return code;\n        }\n\n        // If the source map was resolved, assign it to map\n        map = result.map;\n      } catch {\n        // If resolving the source map fails, warn and return the code\n        this.warn('Failed resolving source map');\n        return code;\n      }\n\n      // If the sources are not included in the map, try to resolve them\n      if (map.sourcesContent === undefined) {\n        try {\n          const { sourcesContent } = await resolveSources(map, id, promisifiedReadFile);\n          // If all sources are strings, assign them to map.sourcesContent\n          if (sourcesContent.every(item => typeof item === 'string')) {\n            map.sourcesContent = sourcesContent;\n          }\n        } catch {\n          // If resolving the sources fails, warn\n          this.warn('Failed resolving sources for source map');\n        }\n      }\n\n      // Return the code and the map\n      return { code, map };\n    },\n  };\n}\n"],"names":["createFilter","pluginUtils","sourcemaps","include","exclude","readFile","filter","defaultReadFile","path","cb","fs","effectiveReadFile","promisifiedReadFile","promisify","name","load","id","code","addWatchFile","cleanId","replace","toString","warn","map","result","resolveSourceMap","sourcesContent","undefined","resolveSources","every","item"],"mappings":";;;;;;;AAMA,MAAM,EAAEA,YAAY,EAAE,GAAGC,WAAAA;AAQV,SAASC;AAEtB,EAAEC,OAAO,EAAEC,OAAO,EAAEC,QAAQ,EAA2B,GAAG,EAAE,EAAA;;IAG5D,MAAMC,MAAAA,GAASN,aAAaG,OAAAA,EAASC,OAAAA,CAAAA;;IAGrC,MAAMG,eAAAA,GAAkB,CACtBC,IAAAA,EACAC,EAAAA,GAAAA;QAEAC,EAAAA,CAAGL,QAAQ,CAACG,IAAAA,EAAM,MAAA,EAAQC,EAAAA,CAAAA;AAC5B,IAAA,CAAA;;AAGA,IAAA,MAAME,oBAAoBN,QAAAA,IAAYE,eAAAA;;AAGtC,IAAA,MAAMK,sBAAsBC,mBAAAA,CAAUF,iBAAAA,CAAAA;IAEtC,OAAO;QACLG,IAAAA,EAAM,YAAA;AAENC,QAAAA,IAAAA,EAAM,eAAqCC,EAAU,EAAA;YACnD,IAAIC,IAAAA;;YAEJ,IAAI,CAACX,OAAOU,EAAAA,CAAAA,EAAK;gBACf,OAAO,IAAA;AAC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