From ac6321660838a4069863c3fa3a054ee227ef9671 Mon Sep 17 00:00:00 2001 From: bddjr Date: Fri, 17 Jan 2025 20:39:54 +0800 Subject: [PATCH] 1.4.0 + Options.rename --- README.md | 9 +++++++-- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 16 ++++++++++++++-- src/options.ts | 9 +++++++++ 5 files changed, 33 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ca958b5..3b4f5c2 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,11 @@ See [src/options.ts](src/options.ts) ```ts export interface Options { + /** + * Rename index.html + */ + rename?: string + /** * https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference * @default defaultHtmlMinifierTerserOptions @@ -96,7 +101,7 @@ vite v6.0.7 building for production... ✓ 45 modules transformed. rendering chunks (1)... -vite-plugin-singlefile-compression 1.3.4 building... +vite-plugin-singlefile-compression 1.4.0 building... file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html 101.56 KiB -> 46.32 KiB @@ -104,7 +109,7 @@ vite-plugin-singlefile-compression 1.3.4 building... Finish. dist/index.html 47.42 kB -✓ built in 696ms +✓ built in 698ms ``` ![](effect.jpg) diff --git a/package-lock.json b/package-lock.json index fffafaa..5006f7b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vite-plugin-singlefile-compression", - "version": "1.3.4", + "version": "1.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vite-plugin-singlefile-compression", - "version": "1.3.4", + "version": "1.4.0", "license": "MIT", "dependencies": { "@types/html-minifier-terser": "^7.0.2", diff --git a/package.json b/package.json index e45b6fc..d4515fa 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vite-plugin-singlefile-compression", - "version": "1.3.4", + "version": "1.4.0", "main": "dist/index.js", "typings": "dist/index.d.ts", "files": [ diff --git a/src/index.ts b/src/index.ts index ab60db2..f66f985 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,7 +60,18 @@ function setConfig(config: UserConfig) { } async function generateBundle(bundle: OutputBundle, config: ResolvedConfig, options: innerOptions) { - console.log(pc.cyan('\n\nvite-plugin-singlefile-compression ' + version) + pc.green(' building...')) + console.log(pc.reset('') + pc.cyan('\n\nvite-plugin-singlefile-compression ' + version) + pc.green(' building...')) + + // rename + if (options.rename + && options.rename !== "index.html" + && ("index.html" in bundle) + && !(options.rename in bundle) + ) { + bundle[options.rename] = bundle["index.html"] + bundle[options.rename].fileName = options.rename + delete bundle["index.html"] + } const distURL = pathToFileURL(config.build.outDir).href + '/' @@ -248,7 +259,7 @@ async function generateBundle(bundle: OutputBundle, config: ResolvedConfig, opti htmlChunk.source = newHtml console.log( "\n" - + " " + pc.underline(pc.cyan(distURL) + pc.greenBright(htmlFileName)) + '\n' + + " " + pc.underline(pc.cyan(distURL) + pc.greenBright(bundle[htmlFileName].fileName)) + '\n' + " " + pc.gray(KiB(oldSize) + " -> ") + pc.cyanBright(KiB(newHtml.length)) + '\n' ) @@ -276,5 +287,6 @@ async function generateBundle(bundle: OutputBundle, config: ResolvedConfig, opti } } } + console.log(pc.green('Finish.\n')) } diff --git a/src/options.ts b/src/options.ts index 9e57808..f9a2011 100644 --- a/src/options.ts +++ b/src/options.ts @@ -2,6 +2,11 @@ import { Options as htmlMinifierOptions } from 'html-minifier-terser' import { compressFormat } from './compress.js' export interface Options { + /** + * Rename index.html + */ + rename?: string + /** * https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference * @default defaultHtmlMinifierTerserOptions @@ -57,6 +62,7 @@ export const defaultHtmlMinifierTerserOptions: htmlMinifierOptions = { } export interface innerOptions { + rename?: string htmlMinifierTerser: htmlMinifierOptions | false tryInlineHtmlAssets: boolean removeInlinedAssetFiles: boolean @@ -69,6 +75,9 @@ export interface innerOptions { export function getInnerOptions(opt?: Options): innerOptions { opt ||= {} return { + rename: + opt.rename && opt.rename.replace(/(\.(html?)?)?$/, '.html'), + htmlMinifierTerser: opt.htmlMinifierTerser == null || opt.htmlMinifierTerser === true ? defaultHtmlMinifierTerserOptions