Skip to content

Commit 2231283

Browse files
committed
feat: add sveltekit html parser
- add sveltekit html parser - add npm package - add README.md
0 parents  commit 2231283

File tree

7 files changed

+2157
-0
lines changed

7 files changed

+2157
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/node_modules/

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Sveltekit HTML Minifier
2+
3+
Sveltekit Adapter to Minify the preload HTML page in case using CSR/Preload by [html-minifier-terser](https://github.com/terser/html-minifier-terser).
4+
5+
## Installation
6+
7+
`npm i -D sveltekit-html-minifier`
8+
9+
## Usage
10+
11+
Add the adapter to your `svelte.config.js` file. Place your default adapter as the first parameter. This will run after the default adapter has finished rendering.
12+
13+
```js
14+
import adapter from "@sveltejs/adapter-static";
15+
import htmlMinifierAdaptor from "sveltekit-html-minifier";
16+
17+
export default {
18+
kit: {
19+
adapter: htmlMinifierAdaptor(adapter()),
20+
},
21+
};
22+
```
23+
24+
### Options
25+
26+
You can pass additional options to the adapter. For example
27+
28+
```js
29+
import adapter from "@sveltejs/adapter-static";
30+
import htmlMinifierAdaptor from "sveltekit-html-minifier";
31+
32+
export default {
33+
kit: {
34+
adapter: htmlMinifierAdaptor(
35+
adapter({
36+
pages: "build",
37+
}),
38+
{
39+
// your build path (same as adapter static pages)
40+
pages: "build",
41+
// custom html-minifier-terser options
42+
// https://github.com/terser/html-minifier-terser#options-quick-reference
43+
minifierOptions: {},
44+
}
45+
),
46+
},
47+
};
48+
```
49+
50+
- `pages` (string): Specifies the build path. This should be the same as the adapter static pages.
51+
- `minifierOptions` (object): Custom options for [html-minifier-terser](https://github.com/terser/html-minifier-terser#options-quick-reference).

index.d.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Adapter } from "@sveltejs/kit";
2+
import { type Options as minifierOptions } from "html-minifier-terser";
3+
4+
interface Options {
5+
pages?: string;
6+
minifierOptions?: minifierOptions;
7+
}
8+
9+
export default function adapter(
10+
insideAdapter?: Adapter,
11+
options?: Options
12+
): Adapter;

index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import path from "node:path";
2+
import { readFileSync, writeFileSync } from "node:fs";
3+
import minifier from "html-minifier-terser";
4+
5+
/** @type {import('.').default} */
6+
export default function (insideAdapter, options) {
7+
const name = "sveltekit-html-minifier";
8+
return {
9+
name: insideAdapter ? `${name}, ${insideAdapter.name}` : name,
10+
async adapt(builder) {
11+
if (insideAdapter) {
12+
await insideAdapter.adapt(builder);
13+
}
14+
15+
/** @type {import('.').Options} */
16+
const mergedOptions = {
17+
pages: "build",
18+
...options,
19+
minifierOptions: {
20+
removeAttributeQuotes: true,
21+
minifyJS: true,
22+
minifyCSS: true,
23+
collapseWhitespace: true,
24+
useShortDoctype: true,
25+
...options?.minifierOptions,
26+
},
27+
};
28+
29+
builder.prerendered.pages.forEach(async (page) => {
30+
const htmlPath = path.join(mergedOptions.pages, page.file);
31+
32+
builder.log.info(`Minify HTML ${htmlPath}`);
33+
34+
// read HTML file
35+
const html = readFileSync(htmlPath).toString();
36+
37+
// minify HTML
38+
const minHTML = await minifier.minify(
39+
html,
40+
mergedOptions.minifierOptions
41+
);
42+
43+
// write minified HTML
44+
await writeFileSync(htmlPath, minHTML);
45+
});
46+
},
47+
};
48+
}

0 commit comments

Comments
 (0)