Skip to content

Commit

Permalink
1.4.0 + Options.rename
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Jan 17, 2025
1 parent dbe3511 commit ac63216
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 7 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -96,15 +101,15 @@ 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

Finish.

dist/index.html 47.42 kB
built in 696ms
built in 698ms
```
![](effect.jpg)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
16 changes: 14 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 + '/'

Expand Down Expand Up @@ -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'
)

Expand Down Expand Up @@ -276,5 +287,6 @@ async function generateBundle(bundle: OutputBundle, config: ResolvedConfig, opti
}
}
}

console.log(pc.green('Finish.\n'))
}
9 changes: 9 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -57,6 +62,7 @@ export const defaultHtmlMinifierTerserOptions: htmlMinifierOptions = {
}

export interface innerOptions {
rename?: string
htmlMinifierTerser: htmlMinifierOptions | false
tryInlineHtmlAssets: boolean
removeInlinedAssetFiles: boolean
Expand All @@ -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
Expand Down

0 comments on commit ac63216

Please sign in to comment.