11import process from 'node:process' ;
2- import { render } from 'ejs' ;
3- import { expand } from 'dotenv-expand' ;
2+ import { createFilter } from '@rollup/pluginutils' ;
3+ import { dim } from 'colorette' ;
4+ import consola from 'consola' ;
45import dotenv from 'dotenv' ;
5- import path , { dirname , join } from 'pathe' ;
6- import fse from 'fs-extra' ;
7- import { normalizePath } from 'vite' ;
8- import { parse } from 'node-html-parser' ;
6+ import { expand } from 'dotenv-expand' ;
7+ import { render } from 'ejs' ;
98import fg from 'fast-glob' ;
10- import consola from 'consola' ;
11- import { dim } from 'colorette' ;
9+ import fse from 'fs-extra' ;
1210import { minify } from 'html-minifier-terser' ;
13- import { createFilter } from '@rollup/pluginutils' ;
11+ import { parse } from 'node-html-parser' ;
12+ import path , { dirname , join } from 'pathe' ;
13+ import { normalizePath } from 'vite' ;
1414import history from './connectHistoryMiddleware' ;
1515
1616function lookupFile ( dir , formats , pathOnly = false ) {
1717 for ( const format of formats ) {
1818 const fullPath = join ( dir , format ) ;
19- if ( fse . pathExistsSync ( fullPath ) && fse . statSync ( fullPath ) . isFile ( ) )
19+ if ( fse . pathExistsSync ( fullPath ) && fse . statSync ( fullPath ) . isFile ( ) ) {
2020 return pathOnly ? fullPath : fse . readFileSync ( fullPath , 'utf-8' ) ;
21+ }
2122 }
2223 const parentDir = dirname ( dir ) ;
23- if ( parentDir !== dir )
24+ if ( parentDir !== dir ) {
2425 return lookupFile ( parentDir , formats , pathOnly ) ;
26+ }
2527}
2628
2729function loadEnv ( mode , envDir , prefix = '' ) {
28- if ( mode === 'local' )
30+ if ( mode === 'local' ) {
2931 throw new Error ( `"local" cannot be used as a mode name because it conflicts with the .local postfix for .env files.` ) ;
32+ }
3033
3134 const env = { } ;
3235 const envFiles = [ `.env.${ mode } .local` , `.env.${ mode } ` , `.env.local` , `.env` ] ;
@@ -39,11 +42,13 @@ function loadEnv(mode, envDir, prefix = '') {
3942 ignoreProcessEnv : true ,
4043 } ) ;
4144 for ( const [ key , value ] of Object . entries ( parsed ) ) {
42- if ( key . startsWith ( prefix ) && env [ key ] === undefined )
45+ if ( key . startsWith ( prefix ) && env [ key ] === undefined ) {
4346 env [ key ] = value ;
47+ }
4448
45- else if ( key === 'NODE_ENV' )
49+ else if ( key === 'NODE_ENV' ) {
4650 process . env . VITE_USER_NODE_ENV = value ;
51+ }
4752 }
4853 }
4954 }
@@ -103,14 +108,15 @@ function createPlugin(userOptions = {}) {
103108 const keys = Object . keys ( proxy ) ;
104109 let indexPage = null ;
105110 for ( const page of _pages ) {
106- if ( page . filename !== 'index.html' )
111+ if ( page . filename !== 'index.html' ) {
107112 rewrites . push ( createRewire ( page . template , page , baseUrl , keys ) ) ;
113+ }
108114
109- else
110- indexPage = page ;
115+ else { indexPage = page ; }
111116 }
112- if ( indexPage )
117+ if ( indexPage ) {
113118 rewrites . push ( createRewire ( '' , indexPage , baseUrl , keys ) ) ;
119+ }
114120
115121 server . middlewares . use (
116122 history ( viteConfig , {
@@ -148,14 +154,16 @@ function createPlugin(userOptions = {}) {
148154 if ( isMpa ( viteConfig ) || pages . length ) {
149155 for ( const page of pages ) {
150156 const dir = path . dirname ( page . template ) ;
151- if ( ! ignoreDirs . includes ( dir ) )
157+ if ( ! ignoreDirs . includes ( dir ) ) {
152158 outputDirs . push ( dir ) ;
159+ }
153160 }
154161 }
155162 else {
156163 const dir = path . dirname ( template ) ;
157- if ( ! ignoreDirs . includes ( dir ) )
164+ if ( ! ignoreDirs . includes ( dir ) ) {
158165 outputDirs . push ( dir ) ;
166+ }
159167 }
160168 const cwd = path . resolve ( viteConfig . root , viteConfig . build . outDir ) ;
161169 const htmlFiles = await fg (
@@ -176,8 +184,9 @@ function createPlugin(userOptions = {}) {
176184 await Promise . all (
177185 htmlDirs . map ( async ( item ) => {
178186 const isEmpty = await isDirEmpty ( item ) ;
179- if ( isEmpty )
187+ if ( isEmpty ) {
180188 return fse . remove ( item ) ;
189+ }
181190 } ) ,
182191 ) ;
183192 } ,
@@ -198,8 +207,9 @@ function createInput({ pages = [], template = DEFAULT_TEMPLATE }, viteConfig) {
198207 return input ;
199208 }
200209 const dir = path . dirname ( template ) ;
201- if ( ignoreDirs . includes ( dir ) )
210+ if ( ignoreDirs . includes ( dir ) ) {
202211 return undefined ;
212+ }
203213
204214 const file = path . basename ( template ) ;
205215 const key = file . replace ( / \. h t m l / , '' ) ;
@@ -227,11 +237,11 @@ async function renderHtml(html, config) {
227237
228238function getPage ( { pages = [ ] , entry, template = DEFAULT_TEMPLATE , inject = { } } , name , viteConfig ) {
229239 let page ;
230- if ( isMpa ( viteConfig ) || pages ?. length )
240+ if ( isMpa ( viteConfig ) || pages ?. length ) {
231241 page = getPageConfig ( name , pages , DEFAULT_TEMPLATE ) ;
242+ }
232243
233- else
234- page = createSpaPage ( entry , template , inject ) ;
244+ else { page = createSpaPage ( entry , template , inject ) ; }
235245
236246 return page ;
237247}
@@ -242,8 +252,9 @@ function isMpa(viteConfig) {
242252}
243253
244254function removeEntryScript ( html , verbose = false ) {
245- if ( ! html )
255+ if ( ! html ) {
246256 return html ;
257+ }
247258
248259 const root = parse ( html ) ;
249260 const scriptNodes = root . querySelectorAll ( 'script[type=module]' ) || [ ] ;
@@ -255,8 +266,8 @@ function removeEntryScript(html, verbose = false) {
255266 verbose
256267 && removedNode . length
257268 && consola . warn ( `vite-plugin-html: Since you have already configured entry, ${ dim (
258- removedNode . toString ( ) ,
259- ) } is deleted. You may also delete it from the index.html.
269+ removedNode . toString ( ) ,
270+ ) } is deleted. You may also delete it from the index.html.
260271 ` ) ;
261272 return root . toString ( ) ;
262273}
@@ -286,8 +297,9 @@ function createRewire(reg, page, baseUrl, proxyUrlKeys) {
286297 const pathname = parsedUrl . pathname ;
287298 const excludeBaseUrl = pathname . replace ( baseUrl , '/' ) ;
288299 const template = path . resolve ( baseUrl , page . template ) ;
289- if ( excludeBaseUrl === '/' )
300+ if ( excludeBaseUrl === '/' ) {
290301 return template ;
302+ }
291303
292304 const isApiUrl = proxyUrlKeys . some ( item => pathname . startsWith ( path . resolve ( baseUrl , item ) ) ) ;
293305 return isApiUrl ? parsedUrl . path : template ;
@@ -311,12 +323,14 @@ function getOptions(_minify) {
311323}
312324
313325async function minifyHtml ( html , minify$1 ) {
314- if ( typeof minify$1 === 'boolean' && ! minify$1 )
326+ if ( typeof minify$1 === 'boolean' && ! minify$1 ) {
315327 return html ;
328+ }
316329
317330 let minifyOptions = minify$1 ;
318- if ( typeof minify$1 === 'boolean' && minify$1 )
331+ if ( typeof minify$1 === 'boolean' && minify$1 ) {
319332 minifyOptions = getOptions ( minify$1 ) ;
333+ }
320334
321335 const res = await minify ( html , minifyOptions ) ;
322336 return res ;
@@ -329,8 +343,9 @@ function createMinifyHtmlPlugin({ _minify = true } = {}) {
329343 async generateBundle ( _ , outBundle ) {
330344 if ( _minify ) {
331345 for ( const bundle of Object . values ( outBundle ) ) {
332- if ( bundle . type === 'asset' && htmlFilter ( bundle . fileName ) && typeof bundle . source === 'string' )
346+ if ( bundle . type === 'asset' && htmlFilter ( bundle . fileName ) && typeof bundle . source === 'string' ) {
333347 bundle . source = await minifyHtml ( bundle . source , _minify ) ;
348+ }
334349 }
335350 }
336351 } ,
0 commit comments