{"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { type Plugin } from \"vite\";\n\nexport type StripCommentsPlugin = Plugin<StripCommentsConfig> & {\n  name: string;\n  enforce: \"pre\" | \"post\" | undefined;\n  apply: \"build\";\n  transform: (\n    code: string,\n    id?: string,\n  ) => {\n    code: string;\n    map: string | null;\n  };\n};\n\nexport type StripCommentsConfig = {\n  type: \"none\" | \"keep-legal\";\n  enforce: \"pre\" | \"post\" | undefined;\n};\n\nconst StripCommentsDefaultConfig: StripCommentsConfig = {\n  type: \"keep-legal\",\n  enforce: \"pre\",\n};\n\nconst stripComments = (cfg: Partial<StripCommentsConfig> = {}) => {\n  const config: Partial<StripCommentsConfig> = {\n    type:\n      ([\"none\", \"keep-legal\"].some((x) => x === cfg.type)\n        ? cfg.type\n        : StripCommentsDefaultConfig.type),\n    enforce:\n      ([\"pre\", \"post\"].some((x) => x === cfg.enforce)\n        ? cfg.enforce\n        : StripCommentsDefaultConfig.enforce),\n  };\n\n  return {\n    name: \"vite-plugin-strip-comments\",\n    enforce: config.enforce,\n    apply: \"build\",\n    transform(code: string, id?: string): { code: string; map: null } {\n      /* istanbul ignore if @preserve */\n      if (!id || id.includes(\"node_modules\") || !/\\.([jt]sx?)$/.test(id)) {\n        return { code, map: null };\n      }\n      let result = code;\n      let match: string;\n\n      const matchesArray = Array.from(code.matchAll(\n        /\\/\\*[\\s\\S]*?\\*\\/|\\/\\*\\*.*?\\*\\/|(?<!https?:)\\/\\/.*(?=\\n)?/gm,\n      ));\n\n      for (let i = 0; i < matchesArray.length; i += 1) {\n        // first match\n        [match] = matchesArray[i];\n\n        switch (config.type) {\n          case \"keep-legal\":\n            if (![\"@legal\", \"@license\"].some((x) => match.includes(x))) {\n              result = result.replace(match, \"\");\n            }\n            break;\n          case \"none\":\n          default:\n            result = result.replace(match, \"\");\n        }\n      }\n\n      return { code: result, map: null };\n    },\n  } satisfies StripCommentsPlugin;\n};\n\nexport default stripComments;\n"],"names":["StripCommentsDefaultConfig","stripComments","cfg","config","x","code","id","result","match","matchesArray","i"],"mappings":"AAoBA,MAAMA,IAAkD;AAAA,EACtD,MAAM;AAAA,EACN,SAAS;AACX,GAEMC,IAAgB,CAACC,IAAoC,OAAO;AAChE,QAAMC,IAAuC;AAAA,IAC3C,MACG,CAAC,QAAQ,YAAY,EAAE,KAAK,CAACC,MAAMA,MAAMF,EAAI,IAAI,IAC9CA,EAAI,OACJF,EAA2B;AAAA,IACjC,SACG,CAAC,OAAO,MAAM,EAAE,KAAK,CAACI,MAAMA,MAAMF,EAAI,OAAO,IAC1CA,EAAI,UACJF,EAA2B;AAAA,EACnC;AAEO,SAAA;AAAA,IACL,MAAM;AAAA,IACN,SAASG,EAAO;AAAA,IAChB,OAAO;AAAA,IACP,UAAUE,GAAcC,GAA0C;AAE5D,UAAA,CAACA,KAAMA,EAAG,SAAS,cAAc,KAAK,CAAC,eAAe,KAAKA,CAAE;AACxD,eAAA,EAAE,MAAAD,GAAM,KAAK,KAAK;AAE3B,UAAIE,IAASF,GACTG;AAEE,YAAAC,IAAe,MAAM,KAAKJ,EAAK;AAAA,QACnC;AAAA,MAAA,CACD;AAED,eAASK,IAAI,GAAGA,IAAID,EAAa,QAAQC,KAAK;AAI5C,gBAFC,CAAAF,CAAK,IAAIC,EAAaC,CAAC,GAEhBP,EAAO,MAAM;AAAA,UACnB,KAAK;AACH,YAAK,CAAC,UAAU,UAAU,EAAE,KAAK,CAACC,MAAMI,EAAM,SAASJ,CAAC,CAAC,MAC9CG,IAAAA,EAAO,QAAQC,GAAO,EAAE;AAEnC;AAAA,UACF,KAAK;AAAA,UACL;AACW,YAAAD,IAAAA,EAAO,QAAQC,GAAO,EAAE;AAAA,QAAA;AAIvC,aAAO,EAAE,MAAMD,GAAQ,KAAK,KAAK;AAAA,IAAA;AAAA,EAErC;AACF;"}