├── .npmignore ├── .gitignore ├── .prettierignore ├── prettier.config.js ├── tsconfig.json ├── tsup.config.ts ├── dist ├── index.js ├── index.d.cts ├── index.d.ts ├── index.cjs ├── index.js.map └── index.cjs.map ├── README.md ├── LICENSE ├── package.json └── src └── index.ts /.npmignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store 3 | .idea 4 | -------------------------------------------------------------------------------- /.prettierignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | dist 4 | package-lock.json -------------------------------------------------------------------------------- /prettier.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import("prettier").Config} */ 2 | export default { 3 | tabWidth: 2, 4 | singleQuote: true, 5 | semi: false, 6 | } 7 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "ESNext", 4 | "module": "ESNext", 5 | "moduleResolution": "node", 6 | "lib": ["ESNext"], 7 | "allowSyntheticDefaultImports": true 8 | }, 9 | "include": ["src"] 10 | } 11 | -------------------------------------------------------------------------------- /tsup.config.ts: -------------------------------------------------------------------------------- 1 | import { defineConfig } from 'tsup' 2 | 3 | export default defineConfig({ 4 | entry: ['src/index.ts'], 5 | format: ['esm', 'cjs'], 6 | splitting: false, 7 | sourcemap: true, 8 | clean: true, 9 | minify: true, 10 | dts: true, 11 | }) 12 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | import{minify as i}from"html-minifier-terser";function r(e){return{name:"vite-plugin-minify",apply:"build",transformIndexHtml:{order:"post",handler(t){return i(t,{removeComments:!0,collapseWhitespace:!0,collapseBooleanAttributes:!0,removeAttributeQuotes:!1,removeEmptyAttributes:!0,minifyCSS:!0,minifyJS:!0,minifyURLs:!0,...e})}}}}var u=r;export{r as ViteMinifyPlugin,u as default}; 2 | //# sourceMappingURL=index.js.map -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vite-plugin-minify 2 | 3 | Minify html in production 4 | 5 | ## Usage 6 | 7 | ```bash 8 | npm install vite-plugin-minify -D 9 | ``` 10 | 11 | ```js 12 | // vite.config.js 13 | import { defineConfig } from 'vite' 14 | import { ViteMinifyPlugin } from 'vite-plugin-minify' 15 | 16 | export default defineConfig({ 17 | plugins: [ 18 | // input https://www.npmjs.com/package/html-minifier-terser options 19 | ViteMinifyPlugin({}), 20 | ], 21 | }) 22 | ``` 23 | -------------------------------------------------------------------------------- /dist/index.d.cts: -------------------------------------------------------------------------------- 1 | import { Plugin } from 'vite'; 2 | import { Options } from 'html-minifier-terser'; 3 | 4 | /** 5 | * @param options [html-minifier-terser options](https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference) 6 | * 7 | * @default 8 | * 9 | * { 10 | * removeComments: true, 11 | * collapseWhitespace: true, 12 | * collapseBooleanAttributes: true, 13 | * removeAttributeQuotes: false, 14 | * removeEmptyAttributes: true, 15 | * minifyCSS: true, 16 | * minifyJS: true, 17 | * minifyURLs: true, 18 | * ...options, // user provided options 19 | * } 20 | * 21 | * @returns vite-plugin-minify 22 | */ 23 | declare function ViteMinifyPlugin(options?: Options): Plugin; 24 | 25 | export { ViteMinifyPlugin, ViteMinifyPlugin as default }; 26 | -------------------------------------------------------------------------------- /dist/index.d.ts: -------------------------------------------------------------------------------- 1 | import { Plugin } from 'vite'; 2 | import { Options } from 'html-minifier-terser'; 3 | 4 | /** 5 | * @param options [html-minifier-terser options](https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference) 6 | * 7 | * @default 8 | * 9 | * { 10 | * removeComments: true, 11 | * collapseWhitespace: true, 12 | * collapseBooleanAttributes: true, 13 | * removeAttributeQuotes: false, 14 | * removeEmptyAttributes: true, 15 | * minifyCSS: true, 16 | * minifyJS: true, 17 | * minifyURLs: true, 18 | * ...options, // user provided options 19 | * } 20 | * 21 | * @returns vite-plugin-minify 22 | */ 23 | declare function ViteMinifyPlugin(options?: Options): Plugin; 24 | 25 | export { ViteMinifyPlugin, ViteMinifyPlugin as default }; 26 | -------------------------------------------------------------------------------- /dist/index.cjs: -------------------------------------------------------------------------------- 1 | var n=Object.defineProperty;var l=Object.getOwnPropertyDescriptor;var p=Object.getOwnPropertyNames;var s=Object.prototype.hasOwnProperty;var f=(t,e)=>{for(var r in e)n(t,r,{get:e[r],enumerable:!0})},a=(t,e,r,o)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of p(e))!s.call(t,i)&&i!==r&&n(t,i,{get:()=>e[i],enumerable:!(o=l(e,i))||o.enumerable});return t};var y=t=>a(n({},"__esModule",{value:!0}),t);var b={};f(b,{ViteMinifyPlugin:()=>m,default:()=>d});module.exports=y(b);var u=require("html-minifier-terser");function m(t){return{name:"vite-plugin-minify",apply:"build",transformIndexHtml:{order:"post",handler(e){return(0,u.minify)(e,{removeComments:!0,collapseWhitespace:!0,collapseBooleanAttributes:!0,removeAttributeQuotes:!1,removeEmptyAttributes:!0,minifyCSS:!0,minifyJS:!0,minifyURLs:!0,...t})}}}}var d=m;0&&(module.exports={ViteMinifyPlugin}); 2 | //# sourceMappingURL=index.cjs.map -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 zhuweiyou 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. 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "vite-plugin-minify", 3 | "version": "2.1.0", 4 | "description": "Minify html in production", 5 | "type": "module", 6 | "main": "dist/index.cjs", 7 | "module": "dist/index.js", 8 | "types": "dist/index.d.ts", 9 | "scripts": { 10 | "format": "prettier --write .", 11 | "build": "npm run format && tsup" 12 | }, 13 | "author": "zhuweiyou ", 14 | "license": "MIT", 15 | "keywords": [ 16 | "vite", 17 | "minify", 18 | "minify-html", 19 | "vite-plugin", 20 | "vite-plugin-minify" 21 | ], 22 | "homepage": "https://github.com/zhuweiyou/vite-plugin-minify#readme", 23 | "repository": { 24 | "type": "git", 25 | "url": "git+https://github.com/zhuweiyou/vite-plugin-minify.git" 26 | }, 27 | "dependencies": { 28 | "@types/html-minifier-terser": "^7.0.2", 29 | "html-minifier-terser": "^7.2.0" 30 | }, 31 | "devDependencies": { 32 | "@types/node": "^22.1.0", 33 | "prettier": "^3.3.3", 34 | "tsup": "^8.2.4", 35 | "typescript": "^5.5.4", 36 | "vite": ">=5" 37 | }, 38 | "peerDependencies": { 39 | "vite": ">=5" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import { Plugin } from 'vite' 2 | import { minify, Options } from 'html-minifier-terser' 3 | 4 | /** 5 | * @param options [html-minifier-terser options](https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference) 6 | * 7 | * @default 8 | * 9 | * { 10 | * removeComments: true, 11 | * collapseWhitespace: true, 12 | * collapseBooleanAttributes: true, 13 | * removeAttributeQuotes: false, 14 | * removeEmptyAttributes: true, 15 | * minifyCSS: true, 16 | * minifyJS: true, 17 | * minifyURLs: true, 18 | * ...options, // user provided options 19 | * } 20 | * 21 | * @returns vite-plugin-minify 22 | */ 23 | export function ViteMinifyPlugin(options?: Options): Plugin { 24 | return { 25 | name: 'vite-plugin-minify', 26 | apply: 'build', 27 | transformIndexHtml: { 28 | order: 'post', 29 | handler(html) { 30 | return minify(html, { 31 | removeComments: true, 32 | collapseWhitespace: true, 33 | collapseBooleanAttributes: true, 34 | removeAttributeQuotes: false, 35 | removeEmptyAttributes: true, 36 | minifyCSS: true, 37 | minifyJS: true, 38 | minifyURLs: true, 39 | ...options, 40 | }) 41 | }, 42 | }, 43 | } 44 | } 45 | 46 | export default ViteMinifyPlugin 47 | -------------------------------------------------------------------------------- /dist/index.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Plugin } from 'vite'\nimport { minify, Options } from 'html-minifier-terser'\n\n/**\n * @param options [html-minifier-terser options](https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference)\n *\n * @default\n *\n * {\n * \tremoveComments: true,\n * \tcollapseWhitespace: true,\n * \tcollapseBooleanAttributes: true,\n * \tremoveAttributeQuotes: false,\n * \tremoveEmptyAttributes: true,\n * \tminifyCSS: true,\n * \tminifyJS: true,\n * \tminifyURLs: true,\n * \t...options, // user provided options\n * }\n *\n * @returns vite-plugin-minify\n */\nexport function ViteMinifyPlugin(options?: Options): Plugin {\n return {\n name: 'vite-plugin-minify',\n apply: 'build',\n transformIndexHtml: {\n order: 'post',\n handler(html) {\n return minify(html, {\n removeComments: true,\n collapseWhitespace: true,\n collapseBooleanAttributes: true,\n removeAttributeQuotes: false,\n removeEmptyAttributes: true,\n minifyCSS: true,\n minifyJS: true,\n minifyURLs: true,\n ...options,\n })\n },\n },\n }\n}\n\nexport default ViteMinifyPlugin\n"],"mappings":"AACA,OAAS,UAAAA,MAAuB,uBAqBzB,SAASC,EAAiBC,EAA2B,CAC1D,MAAO,CACL,KAAM,qBACN,MAAO,QACP,mBAAoB,CAClB,MAAO,OACP,QAAQC,EAAM,CACZ,OAAOH,EAAOG,EAAM,CAClB,eAAgB,GAChB,mBAAoB,GACpB,0BAA2B,GAC3B,sBAAuB,GACvB,sBAAuB,GACvB,UAAW,GACX,SAAU,GACV,WAAY,GACZ,GAAGD,CACL,CAAC,CACH,CACF,CACF,CACF,CAEA,IAAOE,EAAQH","names":["minify","ViteMinifyPlugin","options","html","src_default"]} -------------------------------------------------------------------------------- /dist/index.cjs.map: -------------------------------------------------------------------------------- 1 | {"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Plugin } from 'vite'\nimport { minify, Options } from 'html-minifier-terser'\n\n/**\n * @param options [html-minifier-terser options](https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference)\n *\n * @default\n *\n * {\n * \tremoveComments: true,\n * \tcollapseWhitespace: true,\n * \tcollapseBooleanAttributes: true,\n * \tremoveAttributeQuotes: false,\n * \tremoveEmptyAttributes: true,\n * \tminifyCSS: true,\n * \tminifyJS: true,\n * \tminifyURLs: true,\n * \t...options, // user provided options\n * }\n *\n * @returns vite-plugin-minify\n */\nexport function ViteMinifyPlugin(options?: Options): Plugin {\n return {\n name: 'vite-plugin-minify',\n apply: 'build',\n transformIndexHtml: {\n order: 'post',\n handler(html) {\n return minify(html, {\n removeComments: true,\n collapseWhitespace: true,\n collapseBooleanAttributes: true,\n removeAttributeQuotes: false,\n removeEmptyAttributes: true,\n minifyCSS: true,\n minifyJS: true,\n minifyURLs: true,\n ...options,\n })\n },\n },\n }\n}\n\nexport default ViteMinifyPlugin\n"],"mappings":"4ZAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,sBAAAE,EAAA,YAAAC,IAAA,eAAAC,EAAAJ,GACA,IAAAK,EAAgC,gCAqBzB,SAASH,EAAiBI,EAA2B,CAC1D,MAAO,CACL,KAAM,qBACN,MAAO,QACP,mBAAoB,CAClB,MAAO,OACP,QAAQC,EAAM,CACZ,SAAO,UAAOA,EAAM,CAClB,eAAgB,GAChB,mBAAoB,GACpB,0BAA2B,GAC3B,sBAAuB,GACvB,sBAAuB,GACvB,UAAW,GACX,SAAU,GACV,WAAY,GACZ,GAAGD,CACL,CAAC,CACH,CACF,CACF,CACF,CAEA,IAAOH,EAAQD","names":["src_exports","__export","ViteMinifyPlugin","src_default","__toCommonJS","import_html_minifier_terser","options","html"]} --------------------------------------------------------------------------------