{"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: Buffer | string) => void): void;\n}\n\nexport default function sourcemaps(\n  // eslint-disable-next-line @typescript-eslint/unbound-method\n  { include, exclude, readFile = fs.readFile }: SourcemapsPluginOptions = {},\n): Plugin {\n  // Create a filter function based on the include and exclude options\n  const filter = createFilter(include, exclude);\n\n  // Promisify the readFile function\n  const promisifiedReadFile = promisify(readFile);\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)).toString();\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","fs","filter","promisifiedReadFile","promisify","name","load","id","code","toString","addWatchFile","cleanId","replace","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,QAAAA,GAAWC,GAAGD,QAAQ,EAA2B,GAAG,EAAE,EAAA;;IAG1E,MAAME,MAAAA,GAASP,aAAaG,OAASC,EAAAA,OAAAA,CAAAA;;AAGrC,IAAA,MAAMI,sBAAsBC,mBAAUJ,CAAAA,QAAAA,CAAAA;IAEtC,OAAO;QACLK,IAAM,EAAA,YAAA;AAENC,QAAAA,IAAAA,EAAM,eAAqCC,EAAU,EAAA;YACnD,IAAIC,IAAAA;;YAEJ,IAAI,CAACN,OAAOK,EAAK,CAAA,EAAA;gBACf,OAAO,IAAA;AACT;YAEA,IAAI;;AAEFC,gBAAAA,IAAAA,GAAO,CAAC,MAAML,mBAAoBI,CAAAA,EAAAA,CAAE,EAAGE,QAAQ,EAAA;;gBAE/C,IAAI,CAACC,YAAY,CAACH,EAAAA,CAAAA;AACpB,aAAA,CAAE,OAAM;gBACN,IAAI;;AAEF,oBAAA,MAAMI,OAAUJ,GAAAA,EAAAA,CAAGK,OAAO,CAAC,OAAS,EAAA,EAAA,CAAA;AACpCJ,oBAAAA,IAAAA,GAAO,CAAC,MAAML,mBAAoBQ,CAAAA,OAAAA,CAAO,EAAGF,QAAQ,EAAA;;oBAEpD,IA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