Skip to content

Commit 4946b04

Browse files
committed
refactor(bundler-vite): use temp index.html as entry point
1 parent 802b7b5 commit 4946b04

File tree

4 files changed

+133
-175
lines changed

4 files changed

+133
-175
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import type { Plugin } from 'vite'
2+
import type { App } from '@vuepress/core'
3+
import { fs } from '@vuepress/utils'
4+
import type { ViteBundlerOptions } from '../types'
5+
import { resolveAlias } from './resolveAlias'
6+
import { resolveDefine } from './resolveDefine'
7+
8+
// packages that include client code, which should not
9+
// be optimized nor externalized
10+
const clientPackages = [
11+
'@vuepress/client',
12+
'@vuepress/plugin-active-header-links',
13+
'@vuepress/plugin-back-to-top',
14+
'@vuepress/plugin-container',
15+
'@vuepress/plugin-debug',
16+
'@vuepress/plugin-docsearch',
17+
'@vuepress/plugin-git',
18+
'@vuepress/plugin-google-analytics',
19+
'@vuepress/plugin-medium-zoom',
20+
'@vuepress/plugin-nprogress',
21+
'@vuepress/plugin-palette',
22+
'@vuepress/plugin-prismjs',
23+
'@vuepress/plugin-pwa',
24+
'@vuepress/plugin-pwa-popup',
25+
'@vuepress/plugin-register-components',
26+
'@vuepress/plugin-search',
27+
'@vuepress/plugin-shiki',
28+
'@vuepress/plugin-theme-data',
29+
'@vuepress/plugin-toc',
30+
'@vuepress/theme-default',
31+
]
32+
33+
export const createMainPlugin = ({
34+
app,
35+
options,
36+
isServer,
37+
isBuild,
38+
}: {
39+
app: App
40+
options: ViteBundlerOptions
41+
isServer: boolean
42+
isBuild: boolean
43+
}): Plugin => ({
44+
name: 'vuepress:main',
45+
46+
config: async () => {
47+
// create a temp index.html as dev entry point
48+
if (!isBuild) {
49+
await app.writeTemp(
50+
'vite-root/index.html',
51+
fs
52+
.readFileSync(app.options.templateDev)
53+
.toString()
54+
.replace(
55+
/<\/body>/,
56+
`\
57+
<script type="module">
58+
import '@vuepress/client/lib/app.js'
59+
</script>
60+
</body>`
61+
)
62+
)
63+
}
64+
65+
return {
66+
root: app.dir.temp('vite-root'),
67+
base: app.options.base,
68+
mode: isBuild ? 'production' : 'development',
69+
define: await resolveDefine({ app, isServer }),
70+
publicDir: app.dir.public(),
71+
cacheDir: app.dir.cache(),
72+
resolve: {
73+
alias: await resolveAlias({ app }),
74+
},
75+
css: {
76+
postcss: {
77+
plugins: isServer
78+
? []
79+
: [
80+
require('autoprefixer'),
81+
...(isBuild ? [require('postcss-csso')] : []),
82+
],
83+
},
84+
},
85+
server: {
86+
host: app.options.host,
87+
port: app.options.port,
88+
open: app.options.open,
89+
// TODO: remove after https://github.com/vitejs/vite/issues/3373 is solved
90+
fsServe: {
91+
root: process.cwd(),
92+
},
93+
},
94+
build: {
95+
ssr: isServer,
96+
outDir: isServer ? app.dir.dest('.server') : app.dir.dest(),
97+
cssCodeSplit: false,
98+
rollupOptions: {
99+
input: app.dir.client('lib/app.js'),
100+
preserveEntrySignatures: 'allow-extension',
101+
},
102+
minify: isServer ? false : !app.env.isDebug,
103+
},
104+
optimizeDeps: {
105+
include: ['@vuepress/shared'],
106+
exclude: clientPackages,
107+
},
108+
ssr: {
109+
noExternal: clientPackages,
110+
},
111+
}
112+
},
113+
114+
generateBundle(_, bundle) {
115+
// delete all asset outputs in server build
116+
if (isServer) {
117+
Object.keys(bundle).forEach((key) => {
118+
if (bundle[key].type === 'asset') {
119+
delete bundle[key]
120+
}
121+
})
122+
}
123+
},
124+
})

packages/@vuepress/bundler-vite/src/plugin/createPlugin.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import createVuePlugin from '@vitejs/plugin-vue'
33
import type { App } from '@vuepress/core'
44
import type { ViteBundlerOptions } from '../types'
55
import { createConstantsReplacementPlugin } from './createConstantsReplacementPlugin'
6-
import { createVuepressPlugin } from './createVuepressPlugin'
6+
import { createMainPlugin } from './createMainPlugin'
77
import { createWorkaroundPlugin } from './createWorkaroundPlugin'
88

99
export const createPlugin = ({
@@ -17,9 +17,12 @@ export const createPlugin = ({
1717
isServer: boolean
1818
isBuild: boolean
1919
}): Plugin[] => [
20-
createConstantsReplacementPlugin(app),
20+
// official vue plugin
2121
createVuePlugin(options.vuePluginOptions),
22-
createVuepressPlugin({
22+
23+
// vuepress custom plugin
24+
createConstantsReplacementPlugin(app),
25+
createMainPlugin({
2326
app,
2427
options,
2528
isServer,

packages/@vuepress/bundler-vite/src/plugin/createVuepressPlugin.ts

Lines changed: 0 additions & 169 deletions
This file was deleted.

packages/@vuepress/bundler-vite/src/plugin/createWorkaroundPlugin.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { Plugin, ViteDevServer, DepOptimizationMetadata } from 'vite'
22

33
/**
4-
* Workaround for https://github.com/vitejs/vite/issues/2503
4+
* Vite will inject version hash into file queries, which does not work
5+
* well with VuePress.
56
*
6-
* Although the issue was closed in vite 2.3.0, there are still
7-
* some problems with the version hash
7+
* As a workaround, we remove the version hash to avoid the injection.
88
*/
99
export const createWorkaroundPlugin = (): Plugin => {
1010
let server:

0 commit comments

Comments
 (0)