1
1
import type { App } from '@vuepress/core'
2
2
import { fs , sanitizeFileName } from '@vuepress/utils'
3
3
import autoprefixer from 'autoprefixer'
4
- import history from 'connect-history-api-fallback'
5
4
import type { AcceptedPlugin } from 'postcss'
6
5
import postcssrc from 'postcss-load-config'
7
- import type { AliasOptions , Connect , Plugin , UserConfig } from 'vite'
6
+ import type { AliasOptions , Plugin , UserConfig } from 'vite'
8
7
9
8
/**
10
9
* Resolve vite config `resolve.alias`
@@ -93,9 +92,9 @@ const resolveDefine = async ({
93
92
}
94
93
95
94
/**
96
- * The main plugin to compat vuepress with vite
95
+ * Resolve and setup vite config
97
96
*/
98
- export const vuepressMainPlugin = ( {
97
+ export const vuepressConfigPlugin = ( {
99
98
app,
100
99
isBuild,
101
100
isServer,
@@ -104,9 +103,11 @@ export const vuepressMainPlugin = ({
104
103
isBuild : boolean
105
104
isServer : boolean
106
105
} ) : Plugin => ( {
107
- name : 'vuepress:main ' ,
106
+ name : 'vuepress:config ' ,
108
107
109
- config : async ( ) => {
108
+ enforce : 'pre' ,
109
+
110
+ async config ( ) {
110
111
// vuepress related packages that include pure esm client code,
111
112
// which should not be optimized in dev mode, and should not be
112
113
// externalized in build ssr mode
@@ -188,58 +189,4 @@ export const vuepressMainPlugin = ({
188
189
} ,
189
190
}
190
191
} ,
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
- } ,
245
192
} )
0 commit comments