11import type { App } from '@vuepress/core'
22import { fs , sanitizeFileName } from '@vuepress/utils'
33import autoprefixer from 'autoprefixer'
4- import history from 'connect-history-api-fallback'
54import type { AcceptedPlugin } from 'postcss'
65import postcssrc from 'postcss-load-config'
7- import type { AliasOptions , Connect , Plugin , UserConfig } from 'vite'
6+ import type { AliasOptions , Plugin , UserConfig } from 'vite'
87
98/**
109 * Resolve vite config `resolve.alias`
@@ -93,9 +92,9 @@ const resolveDefine = async ({
9392}
9493
9594/**
96- * The main plugin to compat vuepress with vite
95+ * Resolve and setup vite config
9796 */
98- export const vuepressMainPlugin = ( {
97+ export const vuepressConfigPlugin = ( {
9998 app,
10099 isBuild,
101100 isServer,
@@ -104,9 +103,11 @@ export const vuepressMainPlugin = ({
104103 isBuild : boolean
105104 isServer : boolean
106105} ) : Plugin => ( {
107- name : 'vuepress:main ' ,
106+ name : 'vuepress:config ' ,
108107
109- config : async ( ) => {
108+ enforce : 'pre' ,
109+
110+ async config ( ) {
110111 // vuepress related packages that include pure esm client code,
111112 // which should not be optimized in dev mode, and should not be
112113 // externalized in build ssr mode
@@ -188,58 +189,4 @@ export const vuepressMainPlugin = ({
188189 } ,
189190 }
190191 } ,
191-
192- generateBundle ( _ , bundle ) {
193- // delete all asset outputs in server build
194- if ( isServer ) {
195- Object . keys ( bundle ) . forEach ( ( key ) => {
196- if ( bundle [ key ] . type === 'asset' ) {
197- delete bundle [ key ]
198- }
199- } )
200- }
201- } ,
202-
203- configureServer ( server ) {
204- return ( ) => {
205- // fallback all `.html` requests to `/index.html`
206- server . middlewares . use (
207- history ( {
208- rewrites : [
209- {
210- from : / \. h t m l $ / ,
211- to : '/index.html' ,
212- } ,
213- ] ,
214- } ) as Connect . NextHandleFunction ,
215- )
216-
217- // serve the dev template as `/index.html`
218- server . middlewares . use ( ( req , res , next ) => {
219- if ( ! req . url ?. endsWith ( '.html' ) ) {
220- next ( )
221- return
222- }
223-
224- res . statusCode = 200
225- res . setHeader ( 'Content-Type' , 'text/html' )
226- const indexHtml = fs
227- . readFileSync ( app . options . templateDev )
228- . toString ( )
229- . replace (
230- / < \/ b o d y > / ,
231- `\
232- <script type="module">
233- import 'vuepress/client-app'
234- </script>
235- </body>` ,
236- )
237- void server
238- . transformIndexHtml ( req . url , indexHtml , req . originalUrl )
239- . then ( ( result ) => {
240- res . end ( result )
241- } )
242- } )
243- }
244- } ,
245192} )
0 commit comments