-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
581 additions
and
583 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
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
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
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,107 @@ | ||
import path from 'path' | ||
import { Plugin } from 'vite' | ||
import { SiteConfig, resolveSiteData } from './config' | ||
import { createMarkdownToVueRenderFn } from './markdownToVue' | ||
import { APP_PATH, SITE_DATA_REQUEST_PATH } from './resolver' | ||
import createVuePlugin from '@vitejs/plugin-vue' | ||
import slash from 'slash' | ||
|
||
export function createVitePressPlugin( | ||
root: string, | ||
{ configPath, aliases, markdown, site: initialSiteData }: SiteConfig | ||
): Plugin[] { | ||
const markdownToVue = createMarkdownToVueRenderFn(root, markdown) | ||
|
||
const vuePlugin = createVuePlugin({ | ||
include: [/\.vue$/, /\.md$/] | ||
}) | ||
|
||
let siteData = initialSiteData | ||
let stringifiedData = JSON.stringify(JSON.stringify(initialSiteData)) | ||
|
||
const vitePressPlugin: Plugin = { | ||
name: 'vitepress', | ||
|
||
config() { | ||
return { | ||
alias: aliases, | ||
transformInclude: /\.md$/ | ||
} | ||
}, | ||
|
||
resolveId(id) { | ||
if (id === SITE_DATA_REQUEST_PATH) { | ||
return SITE_DATA_REQUEST_PATH | ||
} | ||
}, | ||
|
||
load(id) { | ||
if (id === SITE_DATA_REQUEST_PATH) { | ||
return `export default ${stringifiedData}` | ||
} | ||
}, | ||
|
||
transform(code, id) { | ||
if (id.endsWith('.md')) { | ||
// transform .md files into vueSrc so plugin-vue can handle it | ||
return markdownToVue(code, id).vueSrc | ||
} | ||
}, | ||
|
||
configureServer(server) { | ||
// serve our index.html after vite history fallback | ||
const indexPath = `/@fs/${path.join(APP_PATH, 'index.html')}` | ||
return () => { | ||
server.app.use((req, _, next) => { | ||
if (req.url!.endsWith('.html')) { | ||
req.url = indexPath | ||
} | ||
next() | ||
}) | ||
} | ||
}, | ||
|
||
async handleHotUpdate(file, mods, read, server) { | ||
if (file === configPath) { | ||
const newData = await resolveSiteData(root) | ||
stringifiedData = JSON.stringify(JSON.stringify(newData)) | ||
if (newData.base !== siteData.base) { | ||
console.warn( | ||
`[vitepress]: config.base has changed. Please restart the dev server.` | ||
) | ||
} | ||
siteData = newData | ||
return | ||
} | ||
|
||
// hot reload .md files as .vue files | ||
if (file.endsWith('.md')) { | ||
const content = await read() | ||
const { pageData, vueSrc } = markdownToVue( | ||
content.toString(), | ||
file, | ||
// do not inject pageData on HMR | ||
// it leads to plugin-vue to think <script> has changed and reloads | ||
// the component instead of re-rendering. | ||
// pageData needs separate HMR logic anyway (see below) | ||
false | ||
) | ||
|
||
// notify the client to update page data | ||
server.ws.send({ | ||
type: 'custom', | ||
event: 'vitepress:pageData', | ||
data: { | ||
path: `/${slash(path.relative(root, file))}`, | ||
pageData | ||
} | ||
}) | ||
|
||
// reload the content component | ||
return vuePlugin.handleHotUpdate!(file, mods, () => vueSrc, server) | ||
} | ||
} | ||
} | ||
|
||
return [vitePressPlugin, vuePlugin] | ||
} |
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
Oops, something went wrong.