-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(bundler-vite): split vite plugins
- Loading branch information
Showing
7 changed files
with
111 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './vuepressMainPlugin.js' | ||
export * from './vuepressBuildPlugin.js' | ||
export * from './vuepressConfigPlugin.js' | ||
export * from './vuepressDevPlugin.js' | ||
export * from './vuepressUserConfigPlugin.js' | ||
export * from './vuepressVuePlugin.js' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Plugin } from 'vite' | ||
|
||
/** | ||
* Configure build command for vuepress | ||
*/ | ||
export const vuepressBuildPlugin = ({ | ||
isServer, | ||
}: { | ||
isServer: boolean | ||
}): Plugin => ({ | ||
name: 'vuepress:build', | ||
|
||
generateBundle(_, bundle) { | ||
// delete all asset outputs in server build | ||
if (isServer) { | ||
Object.keys(bundle).forEach((key) => { | ||
if (bundle[key].type === 'asset') { | ||
delete bundle[key] | ||
} | ||
}) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { App } from '@vuepress/core' | ||
import { fs } from '@vuepress/utils' | ||
import history from 'connect-history-api-fallback' | ||
import type { Connect, Plugin } from 'vite' | ||
|
||
/** | ||
* Configure dev command for vuepress | ||
*/ | ||
export const vuepressDevPlugin = ({ app }: { app: App }): Plugin => ({ | ||
name: 'vuepress:dev', | ||
|
||
async configureServer(server) { | ||
// inject vuepress client app script into dev template | ||
const templateDevContent = await fs.readFile(app.options.templateDev, { | ||
encoding: 'utf-8', | ||
}) | ||
const indexHtml = templateDevContent.replace( | ||
/<\/body>/, | ||
`\ | ||
<script type="module"> | ||
import 'vuepress/client-app' | ||
</script> | ||
</body>`, | ||
) | ||
|
||
return () => { | ||
server.middlewares | ||
// fallback all `.html` requests to `/index.html` | ||
.use( | ||
history({ | ||
rewrites: [ | ||
{ | ||
from: /\.html$/, | ||
to: '/index.html', | ||
}, | ||
], | ||
}) as Connect.NextHandleFunction, | ||
) | ||
// serve virtual `/index.html` | ||
.use((req, res, next) => { | ||
if (!req.url?.endsWith('.html')) { | ||
next() | ||
return | ||
} | ||
|
||
res.statusCode = 200 | ||
res.setHeader('Content-Type', 'text/html') | ||
|
||
void server | ||
.transformIndexHtml(req.url, indexHtml, req.originalUrl) | ||
.then((result) => { | ||
res.end(result) | ||
}) | ||
}) | ||
} | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters