vite-plugin-strip-comments 0.0.1
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/LICENSE +21 -0
- package/README.md +58 -0
- package/dist/index.cjs +2 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.mjs +23 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +56 -0
- package/tsconfig.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 thednp
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# vite-plugin-strip-comments
|
|
2
|
+
|
|
3
|
+
[](https://coveralls.io/github/thednp/vite-plugin-strip-comments)
|
|
4
|
+
[](https://github.com/thednp/vite-plugin-strip-comments/actions/workflows/ci.yml)
|
|
5
|
+
[](https://www.npmjs.com/package/vite-plugin-strip-comments)
|
|
6
|
+
[](http://npm-stat.com/charts.html?package=vite-plugin-strip-comments)
|
|
7
|
+
[](https://www.typescriptlang.org/)
|
|
8
|
+
[](https://vitest.dev/)
|
|
9
|
+
[](https://github.com/vitejs)
|
|
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**.
|
|
12
|
+
|
|
13
|
+
## Install
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pnpm install -D vite-plugin-strip-comments
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
```bash
|
|
20
|
+
yarn add -D vite-plugin-strip-comments
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install -D vite-plugin-strip-comments
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
deno install -D npm:vite-plugin-strip-comments@latest
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Usage
|
|
32
|
+
|
|
33
|
+
```ts
|
|
34
|
+
// vite.config.mts
|
|
35
|
+
import stripComments from 'vite-plugin-strip-comments';
|
|
36
|
+
|
|
37
|
+
export default defineConfig({
|
|
38
|
+
plugins: [
|
|
39
|
+
// ... other plugins
|
|
40
|
+
stripComments({ type: 'istanbul' }),
|
|
41
|
+
],
|
|
42
|
+
});
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Options**
|
|
46
|
+
|
|
47
|
+
* type: "none" | "keep-legal" | "istanbul" (default) - changes the behavior of the transform function
|
|
48
|
+
* **none** removes all comments
|
|
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
|
+
* **istanbul** (default) only remove comments that target istanbul code coverage instrumentation (EG: `/* istanbul ignore else @preserve */`)
|
|
51
|
+
|
|
52
|
+
## Contributions
|
|
53
|
+
* Found a problem, [report](https://github.com/thednp/vite-plugin-strip-comments/issues) the problem. Thank you!
|
|
54
|
+
* Found a fix? Clone, install, apply fix and commit. Thank you!
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
## License
|
|
58
|
+
**vite-plugin-strip-comments** is released under the [MIT License](https://github.com/thednp/vite-plugin-strip-comments/blob/master/LICENSE).
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +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;
|
|
2
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +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"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
declare const stripComments: (cfg?: Partial<StripCommentsConfig>) => {
|
|
2
|
+
name: string;
|
|
3
|
+
transform(code: string, id?: string): {
|
|
4
|
+
code: string;
|
|
5
|
+
map: null;
|
|
6
|
+
} | undefined;
|
|
7
|
+
};
|
|
8
|
+
export default stripComments;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @default 'istanbul'
|
|
12
|
+
*/
|
|
13
|
+
declare type StripCommentsConfig = {
|
|
14
|
+
type: "none" | "keep-legal" | "istanbul";
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export { }
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
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
|
+
};
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
export {
|
|
21
|
+
m as default
|
|
22
|
+
};
|
|
23
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +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;"}
|
package/package.json
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "vite-plugin-strip-comments",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"description": "🗃️ Vite plugin for stripping comments in production builds",
|
|
5
|
+
"main": "dist/index.mjs",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"source": "./src/index.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"require": "./dist/index.cjs",
|
|
12
|
+
"import": "./dist/index.mjs"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/thednp/vite-plugin-strip-comments.git"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"vite",
|
|
21
|
+
"strip",
|
|
22
|
+
"comments"
|
|
23
|
+
],
|
|
24
|
+
"author": "thednp",
|
|
25
|
+
"license": "MIT",
|
|
26
|
+
"bugs": {
|
|
27
|
+
"url": "https://github.com/thednp/vite-plugin-strip-comments/issues"
|
|
28
|
+
},
|
|
29
|
+
"homepage": "https://github.com/thednp/vite-plugin-strip-comments#readme",
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/node": "^20.17.3",
|
|
32
|
+
"@vitest/coverage-istanbul": "^2.1.4",
|
|
33
|
+
"playwright": "^1.48.2",
|
|
34
|
+
"typescript": "^5.6.3",
|
|
35
|
+
"vite": "^5.4.10",
|
|
36
|
+
"vite-plugin-dts": "^4.3.0",
|
|
37
|
+
"vitest": "^2.1.4"
|
|
38
|
+
},
|
|
39
|
+
"engines": {
|
|
40
|
+
"node": ">=20",
|
|
41
|
+
"pnpm": ">=9.10.0",
|
|
42
|
+
"deno": ">=2.0.4"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"pre-test": "pnpm clean-coverage",
|
|
46
|
+
"badges": "npx -p dependency-version-badge update-badge typescript vitest vite",
|
|
47
|
+
"test": "pnpm pre-test && vitest --config vitest.config.mts",
|
|
48
|
+
"clean-coverage": "rm -rf coverage .nyc_output",
|
|
49
|
+
"format": "deno fmt src",
|
|
50
|
+
"lint": "pnpm lint:ts && pnpm check:ts",
|
|
51
|
+
"lint:ts": "deno lint src",
|
|
52
|
+
"fix:ts": "deno lint src --fix",
|
|
53
|
+
"check:ts": "tsc -noEmit",
|
|
54
|
+
"build": "vite build"
|
|
55
|
+
}
|
|
56
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
// https://janessagarrow.com/blog/typescript-and-esbuild/
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"lib": ["DOM", "ESNext", "DOM.Iterable"],
|
|
5
|
+
// "types": ["vite", "vite/client"],
|
|
6
|
+
"rootDir": "./",
|
|
7
|
+
"baseUrl": "./",
|
|
8
|
+
"module": "ESNext",
|
|
9
|
+
"target": "ESNext",
|
|
10
|
+
"moduleResolution": "Bundler",
|
|
11
|
+
"allowJs": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"resolvePackageJsonImports": true,
|
|
14
|
+
"useDefineForClassFields": true,
|
|
15
|
+
"strict": true,
|
|
16
|
+
"sourceMap": true,
|
|
17
|
+
"resolveJsonModule": true,
|
|
18
|
+
"esModuleInterop": true,
|
|
19
|
+
"isolatedModules": true,
|
|
20
|
+
"noUnusedLocals": true,
|
|
21
|
+
"noUnusedParameters": true,
|
|
22
|
+
"noImplicitReturns": true,
|
|
23
|
+
"removeComments": false,
|
|
24
|
+
"allowSyntheticDefaultImports": true,
|
|
25
|
+
"noEmit": true,
|
|
26
|
+
"checkJs": true,
|
|
27
|
+
"skipLibCheck": true, // allows dts-bundle-generator to import from package.json
|
|
28
|
+
"paths": {
|
|
29
|
+
"~/*": [
|
|
30
|
+
"./src/*"
|
|
31
|
+
],
|
|
32
|
+
}
|
|
33
|
+
},
|
|
34
|
+
"include": ["src/*", "src/**/*"],
|
|
35
|
+
"exclude": ["node_modules", "experiments", "coverage", "dist"],
|
|
36
|
+
}
|