{"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Plugin } from \"vite\";\n\nexport type StripCommentsPlugin = Plugin<StripCommentsConfig> & {\n  name: \"vite-plugin-strip-comments\";\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\" | \"keep-jsdoc\";\n  enforce: \"pre\" | \"post\" | undefined;\n};\n\nconst StripCommentsDefaultConfig: StripCommentsConfig = {\n  type: \"keep-legal\",\n  enforce: \"pre\",\n};\n\n/**\n * Strip comments plugin for Vite gives you more control over which comments\n * are removed and which comments stay in your bundle.\n *\n * NOTES:\n * - the plugin should be compatible with rollup, rolldown, tsup, tsdown;\n * - make sure to disable other plugins or options to strip comment to prevent\n * overprocessing and long build times.\n * @param config The plugin configuration\n * @property config.type The type of strip to perform (default is \"keep-legal\")\n * @property config.enforce The step in which the strip should happen (default is \"pre\")\n * @returns The vite plugin itself\n * @see https://github.com/thednp/vite-plugin-strip-comments#readme\n *\n * @example\n * ```ts\n * import { defineConfig } from \"vite\"\n * import strip from \"vite-plugin-strip-comments\"\n *\n * export default defineConfig({\n *   // using default configuration\n *   plugins:[strip()]\n * })\n * ```\n */\nconst stripComments = (config: Partial<StripCommentsConfig> = {}) => {\n  const cfg: Partial<StripCommentsConfig> = {\n    type: [\"none\", \"keep-legal\", \"keep-jsdoc\"].some((x) => x === config.type)\n      ? config.type\n      : StripCommentsDefaultConfig.type,\n    enforce: [\"pre\", \"post\"].some((x) => x === config.enforce)\n      ? config.enforce\n      : StripCommentsDefaultConfig.enforce,\n  };\n\n  return {\n    name: \"vite-plugin-strip-comments\",\n    enforce: cfg.enforce,\n    apply: \"build\",\n    transform(code: string, id?: string): { code: string; map: null } {\n      /* istanbul ignore if @preserve */\n      if (\n        !id || id.includes(\"node_modules\") || !/\\.([jt]sx?)$/.test(id) ||\n        id === \"\\x00rolldown/runtime.js\"\n      ) {\n        return { code, map: null };\n      }\n      const LEGAL_REGEX = /\\/\\*!\\s*|\\/\\/!\\s*|@(?:legal|license|copyright)\\b/;\n      const COMMENT_REGEX =\n        /\\/\\*[\\s\\S]*?\\*\\/|\\/\\*\\*.*?\\*\\/|(?<!https?:)\\/\\/.*(?=\\n)?/gm;\n      let result = code;\n      const matches = code.matchAll(COMMENT_REGEX);\n      const toRemove: string[] = [];\n\n      for (const match of matches) {\n        const comment = match[0];\n        let shouldRemove = false;\n\n        switch (cfg.type) {\n          case \"keep-jsdoc\":\n            shouldRemove = !comment.startsWith(\"/**\") &&\n              !LEGAL_REGEX.test(comment);\n            break;\n          case \"keep-legal\":\n            shouldRemove = !LEGAL_REGEX.test(comment);\n            break;\n          case \"none\":\n          default:\n            shouldRemove = true;\n        }\n        if (shouldRemove) {\n          toRemove.push(comment);\n        }\n      }\n\n      for (const comment of toRemove) {\n        result = result.replace(comment, \"\");\n      }\n\n      return { code: result, map: null };\n    },\n  } satisfies StripCommentsPlugin;\n};\n\nexport default stripComments;\n"],"mappings":";;;;;;;;AAoBA,MAAM,6BAAkD;CACtD,MAAM;CACN,SAAS;CACV;;;;;;;;;;;;;;;;;;;;;;;;;;AA2BD,MAAM,iBAAiB,SAAuC,EAAE,KAAK;CACnE,MAAM,MAAoC;EACxC,MAAM;GAAC;GAAQ;GAAc;GAAa,CAAC,MAAM,MAAM,MAAM,OAAO,KAAI,GACpE,OAAO,OACP,2BAA2B;EAC/B,SAAS,CAAC,OAAO,OAAO,CAAC,MAAM,MAAM,MAAM,OAAO,QAAO,GACrD,OAAO,UACP,2BAA2B;EAChC;AAED,QAAO;EACL,MAAM;EACN,SAAS,IAAI;EACb,OAAO;EACP,UAAU,MAAc,IAA0C;AAEhE,OACE,CAAC,MAAM,GAAG,SAAS,eAAe,IAAI,CAAC,eAAe,KAAK,GAAG,IAC9D,OAAO,wBAEP,QAAO;IAAE;IAAM,KAAK;IAAM;GAE5B,MAAM,cAAc;GACpB,MAAM,gBACJ;GACF,IAAI,SAAS;GACb,MAAM,UAAU,KAAK,SAAS,cAAc;GAC5C,MAAM,WAAqB,EAAE;AAE7B,QAAK,MAAM,SAAS,SAAS;IAC3B,MAAM,UAAU,MAAM;IACtB,IAAI,eAAe;AAEnB,YAAQ,IAAI,MAAZ;KACE,KAAK;AACH,qBAAe,CAAC,QAAQ,WAAW,MAAM,IACvC,CAAC,YAAY,KAAK,QAAQ;AAC5B;KACF,KAAK;AACH,qBAAe,CAAC,YAAY,KAAK,QAAQ;AACzC;KAEF,QACE,gBAAe;;AAEnB,QAAI,aACF,UAAS,KAAK,QAAQ;;AAI1B,QAAK,MAAM,WAAW,SACpB,UAAS,OAAO,QAAQ,SAAS,GAAG;AAGtC,UAAO;IAAE,MAAM;IAAQ,KAAK;IAAM;;EAErC"}