Skip to content

Commit 66848a0

Browse files
committed
feat(entries): support prettify or minify html
1 parent 24b464d commit 66848a0

File tree

5 files changed

+231
-25
lines changed

5 files changed

+231
-25
lines changed

packages/entries/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
"devDependencies": {
3232
"@types/connect-history-api-fallback": "^1.3.5",
3333
"@types/fs-extra": "^9.0.13",
34+
"@types/html-minifier-terser": "^6.1.0",
3435
"@types/lodash": "^4.14.182",
3536
"@types/micromatch": "^4.0.2",
37+
"@types/prettier": "^2.6.3",
3638
"@types/resolve": "^1.20.2"
3739
},
3840
"dependencies": {
@@ -41,9 +43,11 @@
4143
"fast-glob": "^3.2.11",
4244
"fs-extra": "^10.1.0",
4345
"gray-matter": "^4.0.3",
46+
"html-minifier-terser": "7.0.0-beta.0",
4447
"jest-docblock": "^28.0.2",
4548
"lodash": "^4.17.21",
4649
"micromatch": "^4.0.5",
50+
"prettier": "^2.6.2",
4751
"resolve": "^1.22.0",
4852
"upath": "^2.0.1"
4953
}

packages/entries/src/html.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ import {
99
ViteDevServer,
1010
} from 'vite';
1111
import history, { Rewrite } from 'connect-history-api-fallback';
12-
import { Entry } from './types';
12+
import { format } from 'prettier';
13+
import { minify } from 'html-minifier-terser';
14+
import { Entry, UserConfig } from './types';
1315
import {
1416
BODY_INJECT_RE,
1517
BODY_PREPEND_INJECT_RE,
@@ -249,3 +251,40 @@ export function transformHtml(html: string, entry: Entry): string {
249251

250252
return html;
251253
}
254+
255+
export function prettifyHtml(
256+
html: string,
257+
options: NonNullable<UserConfig['prettifyHtml']>
258+
) {
259+
if (options === false) {
260+
return html;
261+
}
262+
return format(html, {
263+
parser: 'html',
264+
...(options === true ? {} : options),
265+
});
266+
}
267+
268+
export async function minifyHtml(
269+
html: string,
270+
options: NonNullable<UserConfig['minifyHtml']>
271+
) {
272+
if (options === false) {
273+
return html;
274+
}
275+
return minify(
276+
html,
277+
options === true
278+
? {
279+
collapseWhitespace: true,
280+
keepClosingSlash: true,
281+
removeComments: true,
282+
removeRedundantAttributes: true,
283+
removeScriptTypeAttributes: true,
284+
removeStyleLinkTypeAttributes: true,
285+
useShortDoctype: true,
286+
minifyCSS: true,
287+
}
288+
: options
289+
);
290+
}

packages/entries/src/index.ts

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import mm from 'micromatch';
66
import { Entry, UserConfig } from './types';
77
import { flattenPath, normalizeRoutePath, toArray } from './utils';
88
import { DEFAULT_ENTRY_MODULE_ID, DEFAULT_HTML_PATH } from './constants';
9-
import { spaFallbackMiddleware, transformHtml } from './html';
9+
import {
10+
minifyHtml,
11+
prettifyHtml,
12+
spaFallbackMiddleware,
13+
transformHtml,
14+
} from './html';
1015

1116
export * from './types';
1217

@@ -100,7 +105,12 @@ function ensureLinkHtmlPath(root: string, entry: Entry) {
100105
}
101106

102107
export function conventionalEntries(userConfig: UserConfig = {}): Plugin[] {
103-
const { pattern = '**/main.{js,jsx,ts,tsx}', basePath = '/' } = userConfig;
108+
const {
109+
pattern = '**/main.{js,jsx,ts,tsx}',
110+
basePath = '/',
111+
prettifyHtml: userPrettifyHtml = false,
112+
minifyHtml: userMinifyHtml = true,
113+
} = userConfig;
104114

105115
let src: string;
106116
let entries: Entry[];
@@ -216,30 +226,39 @@ if (rootEl) {
216226
return code;
217227
}
218228
},
219-
// expose entries
220-
getEntries() {
221-
return entries;
229+
api: {
230+
// expose entries
231+
getEntries() {
232+
return entries;
233+
},
222234
},
223-
} as Plugin,
224-
// vite will emit html with fileName which is relative(root, id),
225-
// for example: 'dist/node_modules/.conventional-entries/index.html'.
226-
// In order to have a clearer directory structure, we should rewrite html fileName here.
227-
// see also: https://github.com/vitejs/vite/blob/1878f465d26d1c61a29ede72882d54d7e95c1042/packages/vite/src/node/plugins/html.ts#L672
235+
},
228236
{
229-
name: 'vite-plugin-conventional-entries:transform-html-path',
237+
name: 'vite-plugin-conventional-entries:html',
230238
enforce: 'post',
231-
generateBundle(_options, bundle) {
232-
Object.values(bundle).forEach(chunkOrAsset => {
239+
async generateBundle(_options, bundle) {
240+
for (const chunk of Object.values(bundle)) {
233241
if (
234-
chunkOrAsset.type === 'asset' &&
235-
chunkOrAsset.fileName.endsWith('.html')
242+
chunk.type === 'asset' &&
243+
chunk.fileName.endsWith('.html') &&
244+
typeof chunk.source === 'string'
236245
) {
237-
chunkOrAsset.fileName = chunkOrAsset.fileName.replace(
246+
// vite will emit html with fileName which is relative(root, id),
247+
// for example: 'dist/node_modules/.conventional-entries/index.html'.
248+
// In order to have a clearer directory structure, we should rewrite html fileName here.
249+
// see also: https://github.com/vitejs/vite/blob/1878f465d26d1c61a29ede72882d54d7e95c1042/packages/vite/src/node/plugins/html.ts#L672
250+
chunk.fileName = chunk.fileName.replace(
238251
'node_modules/.conventional-entries/',
239252
''
240253
);
254+
255+
if (userPrettifyHtml) {
256+
chunk.source = prettifyHtml(chunk.source, userPrettifyHtml);
257+
} else if (userMinifyHtml) {
258+
chunk.source = await minifyHtml(chunk.source, userMinifyHtml);
259+
}
241260
}
242-
});
261+
}
243262
},
244263
},
245264
];

packages/entries/src/types.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import { Options as PrettierOptions } from 'prettier';
2+
import { Options as HtmlMinifierOptions } from 'html-minifier-terser';
3+
14
export interface Entry {
25
entryPath: string;
36
routePath: string;
@@ -21,4 +24,12 @@ export interface UserConfig {
2124
* @default '/'
2225
*/
2326
basePath?: string;
27+
/**
28+
* use prettier to format html
29+
*/
30+
prettifyHtml?: boolean | PrettierOptions;
31+
/**
32+
* minify html
33+
*/
34+
minifyHtml?: boolean | HtmlMinifierOptions;
2435
}

0 commit comments

Comments
 (0)