Skip to content

Commit

Permalink
1.3.0 options add compressFormat, default deflate-raw
Browse files Browse the repository at this point in the history
  • Loading branch information
bddjr committed Jan 11, 2025
1 parent b9077a1 commit 6239e05
Show file tree
Hide file tree
Showing 17 changed files with 259 additions and 227 deletions.
3 changes: 2 additions & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
The MIT License (MIT)
Copyright © 2024-2025 bddjr
Copyright © 2024-present bddjr
Adapted from https://www.npmjs.com/package/vite-plugin-singlefile

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
60 changes: 7 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ export default defineConfig({
// Add singleFileCompression
singleFileCompression(),
],

build: {
// Not use old syntax, make file smaller.
target: 'esnext',
// Disable reporting compressed chunk sizes, slightly improve build speed.
reportCompressedSize: false
},
```
Then modify [src/router/index.ts](test/src/router/index.ts#L5)
Expand All @@ -44,47 +37,7 @@ const router = createRouter({
## Options
```ts
export interface Options {
/**
* https://github.com/terser/html-minifier-terser?tab=readme-ov-file#options-quick-reference
* @default defaultHtmlMinifierTerserOptions
*/
htmlMinifierTerser?: htmlMinifierOptions | boolean;

/**
* Try inline html used assets, if inlined or not used in JS.
* @default true
*/
tryInlineHtmlAssets?: boolean;

/**
* Remove inlined asset files.
* @default true
*/
removeInlinedAssetFiles?: boolean;

/**
* Try inline html icon, if icon is in public dir.
* @default true
*/
tryInlineHtmlPublicIcon?: boolean;

/**
* Remove inlined html icon files.
* @default true
*/
removeInlinedPublicIconFiles?: boolean;

/**
* Use Base128 to encode gzipped script.
* If false, use Base64.
* https://www.npmjs.com/package/base128-ascii
* @default true
*/
useBase128?: boolean;
}
```
See [src/options.ts](src/options.ts)
## Effect
Expand All @@ -95,15 +48,15 @@ vite v6.0.7 building for production...
✓ 45 modules transformed.
rendering chunks (1)...

vite-plugin-singlefile-compression 1.2.6 building...
vite-plugin-singlefile-compression 1.3.0 building...

file:///D:/bddjr/Desktop/code/js/vite-plugin-singlefile-compression/test/dist/index.html
101.56 KiB -> 46.84 KiB
101.56 KiB -> 46.76 KiB

Finish.

dist/index.html 47.96 kB
built in 686ms
dist/index.html 47.88 kB
built in 677ms
```
## Clone
Expand All @@ -120,6 +73,7 @@ npm run build
## License
[MIT](LICENSE.txt)
Using [MIT License](LICENSE.txt).
[src/template](src/template) using [Unlicense](src/template/LICENSE.txt).
Adapted from [vite-plugin-singlefile](https://www.npmjs.com/package/vite-plugin-singlefile).
19 changes: 15 additions & 4 deletions build.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import esbuild from 'esbuild'
import fs from 'fs'
import path from 'path'

const inDir = "src/template"
const outdir = "dist/template"

let result = esbuild.buildSync({
entryPoints: [
"src/template.js",
"src/template-assets.js",
"src/template-base128.js",
path.join(inDir, "base128.js"),
path.join(inDir, "base64.js"),
path.join(inDir, "assets.js"),
],
outdir: "dist",
outdir: outdir,
minify: true,
bundle: true,
format: 'esm',
Expand All @@ -18,6 +22,13 @@ let result = esbuild.buildSync({
if (result.errors.length)
throw result.errors

fs.mkdirSync(outdir)

for (const i of result.outputFiles) {
fs.writeFileSync(i.path, i.text.replace(/;?\n?$/, ''))
}

fs.copyFileSync(
path.join(inDir, "LICENSE.txt"),
path.join(outdir, "LICENSE.txt")
)
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.2.6",
"version": "1.3.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
Expand Down
3 changes: 3 additions & 0 deletions src/KiB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function KiB(size: number) {
return `${Math.ceil(size / 10.24) / 100} KiB`
}
27 changes: 27 additions & 0 deletions src/compress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import base128 from "base128-ascii"
import zlib from 'zlib'

export type compressFormat = "deflate-raw" | "deflate" | "gzip"

export function compress(format: compressFormat, buf: zlib.InputType, useBase128: boolean) {
const options = {
level: zlib.constants.Z_BEST_COMPRESSION,
}
let outBuf: Buffer
switch (format) {
case "deflate":
outBuf = zlib.deflateSync(buf, options)
break
case "deflate-raw":
outBuf = zlib.deflateRawSync(buf, options)
break
case "gzip":
outBuf = zlib.gzipSync(buf, options)
break
default:
throw `unknown compress format ${format}`
}
return useBase128
? base128.encode(Uint8Array.from(outBuf)).toJSTemplateLiterals()
: outBuf.toString('base64')
}
8 changes: 8 additions & 0 deletions src/dataurl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import svgToTinyDataUri from "mini-svg-data-uri";
import mime from 'mime'

export function bufferToDataURL(name: string, b: Buffer) {
return name.endsWith('.svg')
? svgToTinyDataUri(b.toString())
: `data:${mime.getType(name)};base64,${b.toString('base64')}`
}
32 changes: 32 additions & 0 deletions src/getTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import path from 'path'
import fs from 'fs'

const files = {
base64: fs.readFileSync(
path.join(import.meta.dirname, "template/base64.js")
).toString(),

base128: fs.readFileSync(
path.join(import.meta.dirname, "template/base128.js")
).toString(),

assets: fs.readFileSync(
path.join(import.meta.dirname, "template/assets.js")
).toString().split('{"":""}', 2),
}

export const template = {
base(script: string, format: string, useBase128: boolean) {
if (useBase128) {
return files.base128
.replace("<format>", format)
.split('"<script>"', 2).join(script)
}
return files.base64
.replace("<format>", format)
.split("<script>", 2).join(script)
},
assets(assetsJSON: string) {
return files.assets.join(assetsJSON)
},
}
8 changes: 8 additions & 0 deletions src/getVersion.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import fs from 'fs'
import path from 'path'

export const { version } = JSON.parse(
fs.readFileSync(
path.join(import.meta.dirname, "../package.json")
).toString()
) as { version: string }
Loading

0 comments on commit 6239e05

Please sign in to comment.