@@ -6,7 +6,12 @@ import mm from 'micromatch';
66import { Entry , UserConfig } from './types' ;
77import { flattenPath , normalizeRoutePath , toArray } from './utils' ;
88import { 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
1116export * from './types' ;
1217
@@ -100,7 +105,12 @@ function ensureLinkHtmlPath(root: string, entry: Entry) {
100105}
101106
102107export 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 ] ;
0 commit comments