@@ -3,10 +3,10 @@ import type { InjectOptions, PageOption, Pages, UserOptions } from './typing'
33import { render } from 'ejs'
44import { cleanUrl , isDirEmpty , loadEnv } from './utils'
55import { htmlFilter } from './utils/createHtmlFilter'
6- import { mergeConfig , normalizePath } from 'vite'
6+ import { normalizePath } from 'vite'
77import { parse } from 'node-html-parser'
8- import fse from 'fs-extra'
9- import { resolve , dirname , basename } from 'pathe'
8+ import fs from 'fs-extra'
9+ import path from 'pathe'
1010import fg from 'fast-glob'
1111import consola from 'consola'
1212import { dim } from 'colorette'
@@ -37,6 +37,7 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
3737 } ,
3838 config ( conf ) {
3939 const input = createInput ( userOptions , conf as unknown as ResolvedConfig )
40+
4041 if ( input ) {
4142 return {
4243 build : {
@@ -69,16 +70,20 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
6970 excludeBaseUrl === '/' ? DEFAULT_TEMPLATE : url . replace ( '/' , '' )
7071
7172 const page = getPage ( userOptions , htmlName , viteConfig )
73+
7274 const { injectOptions = { } } = page
7375 let html = await getHtmlInPages ( page , viteConfig . root )
76+
7477 html = await renderHtml ( html , {
7578 injectOptions,
7679 viteConfig,
7780 env,
7881 entry : page . entry || entry ,
7982 verbose,
8083 } )
81- html = await server . transformIndexHtml ?.( url , html , req . originalUrl )
84+ if ( server . transformIndexHtml ) {
85+ html = await server . transformIndexHtml ( url , html , req . originalUrl )
86+ }
8287 res . end ( html )
8388 } catch ( e ) {
8489 consola . log ( e )
@@ -87,9 +92,12 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
8792 } ,
8893 transform ( code , id ) : Promise < TransformResult > | TransformResult {
8994 if ( viteConfig . command === 'build' && htmlFilter ( id ) ) {
90- const htmlName = id . match ( '[^/]+(?!.*/)' ) ?. [ 0 ] ?? DEFAULT_TEMPLATE
95+ const htmlName = id
96+ . replace ( path . join ( process . cwd ( ) , viteConfig . base ) , '/' )
97+ . replace ( / \/ p u b l i c / , '' )
9198
9299 const page = getPage ( userOptions , htmlName , viteConfig )
100+
93101 const { injectOptions = { } } = page
94102 return getHtmlInPages ( page , viteConfig . root ) . then ( ( html ) => {
95103 return renderHtml ( html , {
@@ -117,38 +125,40 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
117125
118126 if ( isMpa ( viteConfig ) || pages . length ) {
119127 for ( const page of pages ) {
120- const dir = dirname ( page . template )
128+ const dir = path . dirname ( page . template )
121129 if ( ! ignoreDirs . includes ( dir ) ) {
122130 outputDirs . push ( dir )
123131 }
124132 }
125133 } else {
126- const dir = dirname ( template )
134+ const dir = path . dirname ( template )
127135 if ( ! ignoreDirs . includes ( dir ) ) {
128136 outputDirs . push ( dir )
129137 }
130138 }
131- const cwd = resolve ( viteConfig . root , viteConfig . build . outDir )
139+ const cwd = path . resolve ( viteConfig . root , viteConfig . build . outDir )
132140 const htmlFiles = await fg (
133141 outputDirs . map ( ( dir ) => `${ dir } /*.html` ) ,
134- { cwd : resolve ( cwd ) , absolute : true } ,
142+ { cwd : path . resolve ( cwd ) , absolute : true } ,
135143 )
136144
137145 await Promise . all (
138146 htmlFiles . map ( ( file ) =>
139- fse . move ( file , resolve ( cwd , basename ( file ) ) , { overwrite : true } ) ,
147+ fs . move ( file , path . resolve ( cwd , path . basename ( file ) ) , {
148+ overwrite : true ,
149+ } ) ,
140150 ) ,
141151 )
142152
143153 const htmlDirs = await fg (
144154 outputDirs . map ( ( dir ) => dir ) ,
145- { cwd : resolve ( cwd ) , onlyDirectories : true , absolute : true } ,
155+ { cwd : path . resolve ( cwd ) , onlyDirectories : true , absolute : true } ,
146156 )
147157 await Promise . all (
148158 htmlDirs . map ( async ( item ) => {
149159 const isEmpty = await isDirEmpty ( item )
150160 if ( isEmpty ) {
151- return fse . remove ( item )
161+ return fs . remove ( item )
152162 }
153163 } ) ,
154164 )
@@ -164,20 +174,28 @@ export function createInput(
164174 if ( isMpa ( viteConfig ) || pages ?. length ) {
165175 const templates = pages . map ( ( page ) => page . template )
166176 templates . forEach ( ( temp ) => {
167- const file = basename ( temp )
168- const key = file . replace ( / \. h t m l / , '' )
169- input [ key ] = resolve ( viteConfig . root , temp )
177+ let dirName = path . dirname ( temp )
178+ const file = path . basename ( temp )
179+
180+ dirName = dirName . replace ( / \s + / g, '' ) . replace ( / \/ / g, '-' )
181+
182+ const key =
183+ dirName === '.' || dirName === 'public' || ! dirName
184+ ? file . replace ( / \. h t m l / , '' )
185+ : dirName
186+ input [ key ] = path . resolve ( viteConfig . root , temp )
170187 } )
188+
171189 return input
172190 } else {
173- const dir = dirname ( template )
191+ const dir = path . dirname ( template )
174192 if ( ignoreDirs . includes ( dir ) ) {
175193 return undefined
176194 } else {
177- const file = basename ( template )
195+ const file = path . basename ( template )
178196 const key = file . replace ( / \. h t m l / , '' )
179197 return {
180- [ key ] : resolve ( viteConfig . root , template ) ,
198+ [ key ] : path . resolve ( viteConfig . root , template ) ,
181199 }
182200 }
183201 }
@@ -278,24 +296,28 @@ export function getPageConfig(
278296 filename : defaultPage ,
279297 template : `./${ defaultPage } ` ,
280298 }
281- const page = pages . filter ( ( page ) => page . filename === htmlName ) ?. [ 0 ]
299+
300+ const page = pages . filter ( ( page ) => {
301+ return path . resolve ( '/' + page . filename ) === path . resolve ( '/' + htmlName )
302+ } ) ?. [ 0 ]
282303 return page ?? defaultPageOption ?? undefined
283304}
284305
285306export function getHtmlInPages ( page : PageOption , root : string ) {
286307 const htmlPath = getHtmlPath ( page , root )
308+
287309 return readHtml ( htmlPath )
288310}
289311
290312export function getHtmlPath ( page : PageOption , root : string ) {
291313 const { template } = page
292314 const templatePath = template . startsWith ( '.' ) ? template : `./${ template } `
293- return resolve ( root , templatePath )
315+ return path . resolve ( root , templatePath )
294316}
295317
296318export async function readHtml ( path : string ) {
297- if ( ! fse . pathExistsSync ( path ) ) {
319+ if ( ! fs . pathExistsSync ( path ) ) {
298320 throw new Error ( `html is not exist in ${ path } ` )
299321 }
300- return await fse . readFile ( path ) . then ( ( buffer ) => buffer . toString ( ) )
322+ return await fs . readFile ( path ) . then ( ( buffer ) => buffer . toString ( ) )
301323}
0 commit comments