vite-plugin-strip-comments 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -5,10 +5,10 @@
5
5
  [![NPM Version](https://img.shields.io/npm/v/vite-plugin-strip-comments.svg)](https://www.npmjs.com/package/vite-plugin-strip-comments)
6
6
  [![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-strip-comments.svg)](http://npm-stat.com/charts.html?package=vite-plugin-strip-comments)
7
7
  [![typescript version](https://img.shields.io/badge/typescript-5.6.3-brightgreen)](https://www.typescriptlang.org/)
8
- [![vitest version](https://img.shields.io/badge/vitest-2.1.4-brightgreen)](https://vitest.dev/)
9
- [![vite version](https://img.shields.io/badge/vite-5.4.10-brightgreen)](https://github.com/vitejs)
8
+ [![vitest version](https://img.shields.io/badge/vitest-2.1.5-brightgreen)](https://vitest.dev/)
9
+ [![vite version](https://img.shields.io/badge/vite-5.4.11-brightgreen)](https://github.com/vitejs)
10
10
 
11
- A simple Vite plugin for stripping comments in your production code. Some comments just don't get removed no matter what minify options you set, especially `/* istanbul ignore */` flags. Also keep in mind this is experimental, the configuration doesn't work properly, it always removes ALL comments, so **use with caution**.
11
+ A very simple Vite plugin for stripping comments in your production code. Some comments just don't get removed no matter what minify options you set, especially `/* istanbul ignore */` flags. Keep in mind this is experimental, please **use with caution**.
12
12
 
13
13
  ## Install
14
14
 
@@ -25,7 +25,7 @@ npm install -D vite-plugin-strip-comments
25
25
  ```
26
26
 
27
27
  ```bash
28
- deno install -D npm:vite-plugin-strip-comments@latest
28
+ deno add -D npm:vite-plugin-strip-comments@latest
29
29
  ```
30
30
 
31
31
  ## Usage
@@ -48,6 +48,7 @@ export default defineConfig({
48
48
  * **none** removes all comments
49
49
  * **keep-legal** remove all commments except those which contain `@legal` or `@license`, a very good practice to allow open source to shine yes?
50
50
  * **istanbul** (default) only remove comments that target istanbul code coverage instrumentation (EG: `/* istanbul ignore else @preserve */`)
51
+ * enforce: "pre" (default) | "post" - determines where in the compilation pipeline the plugin should work;
51
52
 
52
53
  ## Contributions
53
54
  * Found a problem, [report](https://github.com/thednp/vite-plugin-strip-comments/issues) the problem. Thank you!
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";const l={none:/((?=\/\*).*\*\/)|((?=((?<!\:)\/\/)).*(?=\n))/gm,"keep-legal":/(((?=(\/\*[\s\S](?!(\@legal|\@license)))).*\*\/)|((?=((?<!\:)\/\/[\s\S](?!(\@legal|\@license)))).*(?=\n)))/gm,istanbul:/(((?=(\/\*[\s\S]istanbul)).*\*\/)|((?=(\/\/[\s\S]istanbul)).*(?=\n)))/gm},m=e=>({name:"vite-plugin-strip-comments",transform(n,s){if(n?.length&&(!s?.length||!s.includes("node_modules"))){const t=["none","legal","istanbul"].some(a=>e?.type===a)?l[e?.type]:l.istanbul;return{code:n.replace(t,""),map:null}}}});module.exports=m;
1
+ "use strict";const c={type:"istanbul",enforce:"pre"},p=(l={})=>{const s={type:["none","keep-legal","istanbul"].some(e=>e===l.type)?l.type:c.type,enforce:["pre","post"].some(e=>e===l.enforce)?l.enforce:c.enforce};return{name:"vite-plugin-strip-comments",enforce:s.enforce,transform(e,o){if(!o||o.includes("node_modules"))return e;let t=e,n;const a=Array.from(e.matchAll(/\/\*[\s\S]*?\*\/|\/\/.*/gm));for(let r=0;r<a.length;r+=1)switch([n]=a[r],s.type){case"keep-legal":["@legal","@license"].some(i=>n.includes(i))||(t=t.replaceAll(n,""));break;case"istanbul":n.includes("istanbul")&&(t=t.replaceAll(n,""));break;case"none":default:t=t.replaceAll(n,"")}return t}}};module.exports=p;
2
2
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["export type StripCommentsOutput = {\n name: string;\n transform: (\n text: string,\n id: string | undefined,\n ) => undefined | { code: string; map: string | null };\n};\n\n/**\n * @default 'istanbul'\n */\nexport type StripCommentsConfig = {\n type: \"none\" | \"keep-legal\" | \"istanbul\";\n};\n\nconst replacements: Record<\"none\" | \"keep-legal\" | \"istanbul\", RegExp> = {\n none: /((?=\\/\\*).*\\*\\/)|((?=((?<!\\:)\\/\\/)).*(?=\\n))/gm,\n \"keep-legal\":\n /(((?=(\\/\\*[\\s\\S](?!(\\@legal|\\@license)))).*\\*\\/)|((?=((?<!\\:)\\/\\/[\\s\\S](?!(\\@legal|\\@license)))).*(?=\\n)))/gm,\n istanbul:\n /(((?=(\\/\\*[\\s\\S]istanbul)).*\\*\\/)|((?=(\\/\\/[\\s\\S]istanbul)).*(?=\\n)))/gm,\n};\n\nconst stripComments = (cfg?: Partial<StripCommentsConfig>) => {\n return {\n name: \"vite-plugin-strip-comments\",\n /* @ts-expect-error - it's just how Vite plugins work */\n transform(code: string, id?: string) {\n /* istanbul ignore else @preserve */\n if (code?.length && (!id?.length || !id.includes(\"node_modules\"))) {\n const replacement = [\"none\", \"legal\", \"istanbul\"].some((x) =>\n cfg?.type === x\n )\n ? replacements[cfg?.type as StripCommentsConfig[\"type\"]]\n : replacements[\"istanbul\"];\n return {\n code: code.replace(replacement, \"\"),\n map: null,\n };\n }\n },\n } satisfies StripCommentsOutput;\n};\n\nexport default stripComments;\n"],"names":["replacements","stripComments","cfg","code","id","replacement","x"],"mappings":"aAeA,MAAMA,EAAmE,CACvE,KAAM,iDACN,aACE,+GACF,SACE,yEACJ,EAEMC,EAAiBC,IACd,CACL,KAAM,6BAEN,UAAUC,EAAcC,EAAa,CAE/B,GAAAD,GAAM,SAAW,CAACC,GAAI,QAAU,CAACA,EAAG,SAAS,cAAc,GAAI,CACjE,MAAMC,EAAc,CAAC,OAAQ,QAAS,UAAU,EAAE,KAAMC,GACpDJ,GAAK,OAASI,GAEdN,EAAaE,GAAK,IAAmC,EACrDF,EAAa,SACV,MAAA,CACL,KAAMG,EAAK,QAAQE,EAAa,EAAE,EAClC,IAAK,IACP,CAAA,CACF,CAEJ"}
1
+ {"version":3,"file":"index.cjs","sources":["../src/index.ts"],"sourcesContent":["import { type Plugin } from \"vite\";\n\nexport type StripCommentsPlugin = Plugin & {\n name: string;\n enforce: \"pre\" | \"post\" | undefined;\n transform: (\n code: string,\n id?: string,\n ) => string;\n};\n\nexport type StripCommentsConfig = {\n type: \"none\" | \"keep-legal\" | \"istanbul\";\n enforce: \"pre\" | \"post\" | undefined;\n};\n\nconst StripCommentsDefaultConfig: StripCommentsConfig = {\n type: \"istanbul\",\n enforce: \"pre\",\n};\n\nconst stripComments = (cfg: Partial<StripCommentsConfig> = {}) => {\n const config: Partial<StripCommentsConfig> = {\n type:\n ([\"none\", \"keep-legal\", \"istanbul\"].some((x) => x === cfg.type)\n ? cfg.type\n : StripCommentsDefaultConfig.type) as StripCommentsConfig[\"type\"],\n enforce:\n ([\"pre\", \"post\"].some((x) => x === cfg.enforce)\n ? cfg.enforce\n : StripCommentsDefaultConfig.enforce) as StripCommentsConfig[\"enforce\"],\n };\n\n return {\n name: \"vite-plugin-strip-comments\",\n enforce: config.enforce,\n transform(code: string, id?: string) {\n /* istanbul ignore if @preserve */\n if (!id || id.includes(\"node_modules\")) return code;\n let result = code;\n let match: string;\n\n const matchesArray = Array.from(code.matchAll(\n /\\/\\*[\\s\\S]*?\\*\\/|\\/\\/.*/gm,\n ));\n\n for (let i = 0; i < matchesArray.length; i += 1) {\n // capture 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.replaceAll(match, \"\");\n }\n break;\n case \"istanbul\":\n if (match.includes(\"istanbul\")) {\n /* istanbul ignore next @preserve */\n result = result.replaceAll(match, \"\");\n }\n break;\n case \"none\":\n default:\n result = result.replaceAll(match, \"\");\n }\n }\n\n return result;\n },\n } satisfies StripCommentsPlugin;\n};\n\nexport default stripComments;\n"],"names":["StripCommentsDefaultConfig","stripComments","cfg","config","x","code","id","result","match","matchesArray","i"],"mappings":"aAgBA,MAAMA,EAAkD,CACtD,KAAM,WACN,QAAS,KACX,EAEMC,EAAgB,CAACC,EAAoC,KAAO,CAChE,MAAMC,EAAuC,CAC3C,KACG,CAAC,OAAQ,aAAc,UAAU,EAAE,KAAMC,GAAMA,IAAMF,EAAI,IAAI,EAC1DA,EAAI,KACJF,EAA2B,KACjC,QACG,CAAC,MAAO,MAAM,EAAE,KAAMI,GAAMA,IAAMF,EAAI,OAAO,EAC1CA,EAAI,QACJF,EAA2B,OACnC,EAEO,MAAA,CACL,KAAM,6BACN,QAASG,EAAO,QAChB,UAAUE,EAAcC,EAAa,CAEnC,GAAI,CAACA,GAAMA,EAAG,SAAS,cAAc,EAAU,OAAAD,EAC/C,IAAIE,EAASF,EACTG,EAEE,MAAAC,EAAe,MAAM,KAAKJ,EAAK,SACnC,2BAAA,CACD,EAED,QAASK,EAAI,EAAGA,EAAID,EAAa,OAAQC,GAAK,EAI5C,OAFC,CAAAF,CAAK,EAAIC,EAAaC,CAAC,EAEhBP,EAAO,KAAM,CACnB,IAAK,aACE,CAAC,SAAU,UAAU,EAAE,KAAMC,GAAMI,EAAM,SAASJ,CAAC,CAAC,IAC9CG,EAAAA,EAAO,WAAWC,EAAO,EAAE,GAEtC,MACF,IAAK,WACCA,EAAM,SAAS,UAAU,IAElBD,EAAAA,EAAO,WAAWC,EAAO,EAAE,GAEtC,MACF,IAAK,OACL,QACWD,EAAAA,EAAO,WAAWC,EAAO,EAAE,CAAA,CAInC,OAAAD,CAAA,CAEX,CACF"}
package/dist/index.d.ts CHANGED
@@ -1,17 +1,13 @@
1
1
  declare const stripComments: (cfg?: Partial<StripCommentsConfig>) => {
2
2
  name: string;
3
- transform(code: string, id?: string): {
4
- code: string;
5
- map: null;
6
- } | undefined;
3
+ enforce: "pre" | "post" | undefined;
4
+ transform(code: string, id?: string): string;
7
5
  };
8
6
  export default stripComments;
9
7
 
10
- /**
11
- * @default 'istanbul'
12
- */
13
8
  declare type StripCommentsConfig = {
14
9
  type: "none" | "keep-legal" | "istanbul";
10
+ enforce: "pre" | "post" | undefined;
15
11
  };
16
12
 
17
13
  export { }
package/dist/index.mjs CHANGED
@@ -1,23 +1,37 @@
1
- const s = {
2
- none: /((?=\/\*).*\*\/)|((?=((?<!\:)\/\/)).*(?=\n))/gm,
3
- "keep-legal": /(((?=(\/\*[\s\S](?!(\@legal|\@license)))).*\*\/)|((?=((?<!\:)\/\/[\s\S](?!(\@legal|\@license)))).*(?=\n)))/gm,
4
- istanbul: /(((?=(\/\*[\s\S]istanbul)).*\*\/)|((?=(\/\/[\s\S]istanbul)).*(?=\n)))/gm
5
- }, m = (e) => ({
6
- name: "vite-plugin-strip-comments",
7
- /* @ts-expect-error - it's just how Vite plugins work */
8
- transform(n, l) {
9
- if (n?.length && (!l?.length || !l.includes("node_modules"))) {
10
- const t = ["none", "legal", "istanbul"].some(
11
- (a) => e?.type === a
12
- ) ? s[e?.type] : s.istanbul;
13
- return {
14
- code: n.replace(t, ""),
15
- map: null
16
- };
1
+ const c = {
2
+ type: "istanbul",
3
+ enforce: "pre"
4
+ }, p = (l = {}) => {
5
+ const s = {
6
+ type: ["none", "keep-legal", "istanbul"].some((e) => e === l.type) ? l.type : c.type,
7
+ enforce: ["pre", "post"].some((e) => e === l.enforce) ? l.enforce : c.enforce
8
+ };
9
+ return {
10
+ name: "vite-plugin-strip-comments",
11
+ enforce: s.enforce,
12
+ transform(e, a) {
13
+ if (!a || a.includes("node_modules")) return e;
14
+ let t = e, n;
15
+ const o = Array.from(e.matchAll(
16
+ /\/\*[\s\S]*?\*\/|\/\/.*/gm
17
+ ));
18
+ for (let r = 0; r < o.length; r += 1)
19
+ switch ([n] = o[r], s.type) {
20
+ case "keep-legal":
21
+ ["@legal", "@license"].some((i) => n.includes(i)) || (t = t.replaceAll(n, ""));
22
+ break;
23
+ case "istanbul":
24
+ n.includes("istanbul") && (t = t.replaceAll(n, ""));
25
+ break;
26
+ case "none":
27
+ default:
28
+ t = t.replaceAll(n, "");
29
+ }
30
+ return t;
17
31
  }
18
- }
19
- });
32
+ };
33
+ };
20
34
  export {
21
- m as default
35
+ p as default
22
36
  };
23
37
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["export type StripCommentsOutput = {\n name: string;\n transform: (\n text: string,\n id: string | undefined,\n ) => undefined | { code: string; map: string | null };\n};\n\n/**\n * @default 'istanbul'\n */\nexport type StripCommentsConfig = {\n type: \"none\" | \"keep-legal\" | \"istanbul\";\n};\n\nconst replacements: Record<\"none\" | \"keep-legal\" | \"istanbul\", RegExp> = {\n none: /((?=\\/\\*).*\\*\\/)|((?=((?<!\\:)\\/\\/)).*(?=\\n))/gm,\n \"keep-legal\":\n /(((?=(\\/\\*[\\s\\S](?!(\\@legal|\\@license)))).*\\*\\/)|((?=((?<!\\:)\\/\\/[\\s\\S](?!(\\@legal|\\@license)))).*(?=\\n)))/gm,\n istanbul:\n /(((?=(\\/\\*[\\s\\S]istanbul)).*\\*\\/)|((?=(\\/\\/[\\s\\S]istanbul)).*(?=\\n)))/gm,\n};\n\nconst stripComments = (cfg?: Partial<StripCommentsConfig>) => {\n return {\n name: \"vite-plugin-strip-comments\",\n /* @ts-expect-error - it's just how Vite plugins work */\n transform(code: string, id?: string) {\n /* istanbul ignore else @preserve */\n if (code?.length && (!id?.length || !id.includes(\"node_modules\"))) {\n const replacement = [\"none\", \"legal\", \"istanbul\"].some((x) =>\n cfg?.type === x\n )\n ? replacements[cfg?.type as StripCommentsConfig[\"type\"]]\n : replacements[\"istanbul\"];\n return {\n code: code.replace(replacement, \"\"),\n map: null,\n };\n }\n },\n } satisfies StripCommentsOutput;\n};\n\nexport default stripComments;\n"],"names":["replacements","stripComments","cfg","code","id","replacement","x"],"mappings":"AAeA,MAAMA,IAAmE;AAAA,EACvE,MAAM;AAAA,EACN,cACE;AAAA,EACF,UACE;AACJ,GAEMC,IAAgB,CAACC,OACd;AAAA,EACL,MAAM;AAAA;AAAA,EAEN,UAAUC,GAAcC,GAAa;AAE/B,QAAAD,GAAM,WAAW,CAACC,GAAI,UAAU,CAACA,EAAG,SAAS,cAAc,IAAI;AACjE,YAAMC,IAAc,CAAC,QAAQ,SAAS,UAAU,EAAE;AAAA,QAAK,CAACC,MACpDJ,GAAK,SAASI;AAAA,UAEdN,EAAaE,GAAK,IAAmC,IACrDF,EAAa;AACV,aAAA;AAAA,QACL,MAAMG,EAAK,QAAQE,GAAa,EAAE;AAAA,QAClC,KAAK;AAAA,MACP;AAAA,IAAA;AAAA,EACF;AAEJ;"}
1
+ {"version":3,"file":"index.mjs","sources":["../src/index.ts"],"sourcesContent":["import { type Plugin } from \"vite\";\n\nexport type StripCommentsPlugin = Plugin & {\n name: string;\n enforce: \"pre\" | \"post\" | undefined;\n transform: (\n code: string,\n id?: string,\n ) => string;\n};\n\nexport type StripCommentsConfig = {\n type: \"none\" | \"keep-legal\" | \"istanbul\";\n enforce: \"pre\" | \"post\" | undefined;\n};\n\nconst StripCommentsDefaultConfig: StripCommentsConfig = {\n type: \"istanbul\",\n enforce: \"pre\",\n};\n\nconst stripComments = (cfg: Partial<StripCommentsConfig> = {}) => {\n const config: Partial<StripCommentsConfig> = {\n type:\n ([\"none\", \"keep-legal\", \"istanbul\"].some((x) => x === cfg.type)\n ? cfg.type\n : StripCommentsDefaultConfig.type) as StripCommentsConfig[\"type\"],\n enforce:\n ([\"pre\", \"post\"].some((x) => x === cfg.enforce)\n ? cfg.enforce\n : StripCommentsDefaultConfig.enforce) as StripCommentsConfig[\"enforce\"],\n };\n\n return {\n name: \"vite-plugin-strip-comments\",\n enforce: config.enforce,\n transform(code: string, id?: string) {\n /* istanbul ignore if @preserve */\n if (!id || id.includes(\"node_modules\")) return code;\n let result = code;\n let match: string;\n\n const matchesArray = Array.from(code.matchAll(\n /\\/\\*[\\s\\S]*?\\*\\/|\\/\\/.*/gm,\n ));\n\n for (let i = 0; i < matchesArray.length; i += 1) {\n // capture 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.replaceAll(match, \"\");\n }\n break;\n case \"istanbul\":\n if (match.includes(\"istanbul\")) {\n /* istanbul ignore next @preserve */\n result = result.replaceAll(match, \"\");\n }\n break;\n case \"none\":\n default:\n result = result.replaceAll(match, \"\");\n }\n }\n\n return result;\n },\n } satisfies StripCommentsPlugin;\n};\n\nexport default stripComments;\n"],"names":["StripCommentsDefaultConfig","stripComments","cfg","config","x","code","id","result","match","matchesArray","i"],"mappings":"AAgBA,MAAMA,IAAkD;AAAA,EACtD,MAAM;AAAA,EACN,SAAS;AACX,GAEMC,IAAgB,CAACC,IAAoC,OAAO;AAChE,QAAMC,IAAuC;AAAA,IAC3C,MACG,CAAC,QAAQ,cAAc,UAAU,EAAE,KAAK,CAACC,MAAMA,MAAMF,EAAI,IAAI,IAC1DA,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,UAAUE,GAAcC,GAAa;AAEnC,UAAI,CAACA,KAAMA,EAAG,SAAS,cAAc,EAAU,QAAAD;AAC/C,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,WAAWC,GAAO,EAAE;AAEtC;AAAA,UACF,KAAK;AACC,YAAAA,EAAM,SAAS,UAAU,MAElBD,IAAAA,EAAO,WAAWC,GAAO,EAAE;AAEtC;AAAA,UACF,KAAK;AAAA,UACL;AACW,YAAAD,IAAAA,EAAO,WAAWC,GAAO,EAAE;AAAA,QAAA;AAInC,aAAAD;AAAA,IAAA;AAAA,EAEX;AACF;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-strip-comments",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "🗃️ Vite plugin for stripping comments in production builds",
5
5
  "main": "dist/index.mjs",
6
6
  "module": "./dist/index.mjs",
@@ -28,13 +28,12 @@
28
28
  },
29
29
  "homepage": "https://github.com/thednp/vite-plugin-strip-comments#readme",
30
30
  "devDependencies": {
31
- "@types/node": "^20.17.3",
32
- "@vitest/coverage-istanbul": "^2.1.4",
33
- "playwright": "^1.48.2",
31
+ "@types/node": "^20.17.6",
32
+ "@vitest/coverage-istanbul": "^2.1.5",
34
33
  "typescript": "^5.6.3",
35
- "vite": "^5.4.10",
34
+ "vite": "^5.4.11",
36
35
  "vite-plugin-dts": "^4.3.0",
37
- "vitest": "^2.1.4"
36
+ "vitest": "^2.1.5"
38
37
  },
39
38
  "engines": {
40
39
  "node": ">=20",
package/tsconfig.json CHANGED
@@ -31,6 +31,6 @@
31
31
  ],
32
32
  }
33
33
  },
34
- "include": ["src/*", "src/**/*"],
34
+ "include": ["src/*"],
35
35
  "exclude": ["node_modules", "experiments", "coverage", "dist"],
36
36
  }