|
| 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 | +}) |
0 commit comments