1- import type { ResolvedConfig , PluginOption , TransformResult } from 'vite'
1+ import type { ResolvedConfig , PluginOption } from 'vite'
22import type { InjectOptions , PageOption , Pages , UserOptions } from './typing'
33import { render } from 'ejs'
4- import { cleanUrl , isDirEmpty , loadEnv } from './utils'
5- import { htmlFilter } from './utils/createHtmlFilter'
4+ import { isDirEmpty , loadEnv } from './utils'
65import { normalizePath } from 'vite'
76import { parse } from 'node-html-parser'
87import fs from 'fs-extra'
@@ -50,35 +49,48 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
5049 } ,
5150
5251 configureServer ( server ) {
52+ let _pages : { filename : string ; template : string } [ ] = [ ]
53+ const rewrites : { from : RegExp ; to : any } [ ] = [ ]
54+ if ( ! isMpa ( viteConfig ) ) {
55+ const template = userOptions . template || DEFAULT_TEMPLATE
56+ const filename = DEFAULT_TEMPLATE
57+ _pages . push ( {
58+ filename,
59+ template,
60+ } )
61+ } else {
62+ _pages = pages . map ( ( page ) => {
63+ return {
64+ filename : page . filename || DEFAULT_TEMPLATE ,
65+ template : page . template || DEFAULT_TEMPLATE ,
66+ }
67+ } )
68+ }
69+ const proxy = viteConfig . server ?. proxy ?? { }
70+ const baseUrl = viteConfig . base ?? '/'
71+ const keys = Object . keys ( proxy )
72+
73+ let indexPage : any = null
74+ for ( const page of _pages ) {
75+ if ( page . filename !== 'index.html' ) {
76+ rewrites . push ( createRewire ( page . template , page , baseUrl , keys ) )
77+ } else {
78+ indexPage = page
79+ }
80+ }
81+
82+ // ensure order
83+ if ( indexPage ) {
84+ rewrites . push ( createRewire ( '' , indexPage , baseUrl , keys ) )
85+ }
86+
5387 server . middlewares . use (
5488 history ( {
5589 disableDotRule : undefined ,
5690 htmlAcceptHeaders : [ 'text/html' , 'application/xhtml+xml' ] ,
91+ rewrites : rewrites ,
5792 } ) ,
5893 )
59- server . middlewares . use ( async ( req , res , next ) => {
60- const url = cleanUrl ( req . url || '' )
61- const base = viteConfig . base
62- const excludeBaseUrl = url . replace ( base , '/' )
63- if ( ! htmlFilter ( url ) && excludeBaseUrl !== '/' ) {
64- return next ( )
65- }
66- try {
67- const htmlName =
68- excludeBaseUrl === '/' ? DEFAULT_TEMPLATE : url . replace ( '/' , '' )
69-
70- const page = getPage ( userOptions , htmlName , viteConfig )
71-
72- let html = await getHtmlInPages ( page , viteConfig . root )
73-
74- if ( server . transformIndexHtml ) {
75- html = await server . transformIndexHtml ( url , html , req . originalUrl )
76- }
77- res . end ( html )
78- } catch ( e ) {
79- consola . log ( e )
80- }
81- } )
8294 } ,
8395
8496 transformIndexHtml : {
@@ -88,6 +100,7 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
88100 const base = viteConfig . base
89101 const excludeBaseUrl = url . replace ( base , '/' )
90102 const htmlName = path . relative ( process . cwd ( ) , excludeBaseUrl )
103+
91104 const page = getPage ( userOptions , htmlName , viteConfig )
92105 const { injectOptions = { } } = page
93106 const _html = await renderHtml ( html , {
@@ -104,36 +117,6 @@ export function createPlugin(userOptions: UserOptions = {}): PluginOption {
104117 }
105118 } ,
106119 } ,
107- transform ( code , id ) : Promise < TransformResult > | TransformResult {
108- if ( viteConfig . command === 'build' && htmlFilter ( id ) ) {
109- const htmlName = id
110- . replace ( path . join ( process . cwd ( ) , viteConfig . base ) , '/' )
111- . replace ( / \/ p u b l i c / , '' )
112-
113- const page = getPage ( userOptions , htmlName , viteConfig )
114-
115- const { injectOptions = { } } = page
116- return getHtmlInPages ( page , viteConfig . root ) . then ( ( html ) => {
117- return renderHtml ( html , {
118- injectOptions,
119- viteConfig,
120- env,
121- entry : page . entry || entry ,
122- verbose,
123- } ) . then ( ( resultHtml ) => {
124- return {
125- code : resultHtml ,
126- map : null ,
127- }
128- } )
129- } )
130- }
131-
132- return {
133- code,
134- map : null ,
135- }
136- } ,
137120 async closeBundle ( ) {
138121 const outputDirs : string [ ] = [ ]
139122
@@ -312,7 +295,7 @@ export function getPageConfig(
312295 }
313296
314297 const page = pages . filter ( ( page ) => {
315- return path . resolve ( '/' + page . filename ) === path . resolve ( '/' + htmlName )
298+ return path . resolve ( '/' + page . template ) === path . resolve ( '/' + htmlName )
316299 } ) ?. [ 0 ]
317300 return page ?? defaultPageOption ?? undefined
318301}
@@ -335,3 +318,29 @@ export async function readHtml(path: string) {
335318 }
336319 return await fs . readFile ( path ) . then ( ( buffer ) => buffer . toString ( ) )
337320}
321+
322+ function createRewire (
323+ reg : string ,
324+ page : any ,
325+ baseUrl : string ,
326+ proxyUrlKeys : string [ ] ,
327+ ) {
328+ return {
329+ from : new RegExp ( `^/${ reg } *` ) ,
330+ to ( { parsedUrl } : any ) {
331+ const pathname : string = parsedUrl . pathname
332+
333+ const excludeBaseUrl = pathname . replace ( baseUrl , '/' )
334+
335+ const template = path . resolve ( baseUrl , page . template )
336+
337+ if ( excludeBaseUrl === '/' ) {
338+ return template
339+ }
340+ const isApiUrl = proxyUrlKeys . some ( ( item ) =>
341+ pathname . startsWith ( path . resolve ( baseUrl , item ) ) ,
342+ )
343+ return isApiUrl ? excludeBaseUrl : template
344+ } ,
345+ }
346+ }
0 commit comments