import { join } from 'path' ; import { createRequire } from 'module' ; import hljs from 'highlight.js' ; import vitePluginMd from 'vite-plugin-md' ; import vitePluginVue from '@vitejs/plugin-vue' ; import vitePluginJsx from '@vitejs/plugin-vue-jsx' ; import { setBuildTarget , getVantConfig , isDev } from '../common/index.js' ; import { SITE_DIST_DIR , SITE_MOBILE_SHARED_FILE , SITE_DESKTOP_SHARED_FILE , SITE_SRC_DIR , } from '../common/constant.js' ; import { injectHtml } from 'vite-plugin-html' ; import type { InlineConfig } from 'vite' ; import type MarkdownIt from 'markdown-it' ; function markdownHighlight ( str , lang ) { if ( lang && hljs . getLanguage ( lang ) ) { return hljs . highlight ( str , { language : lang , ignoreIllegals : true } ) . value ; } return '' ; } function markdownCardWrapper ( htmlCode ) { const group = htmlCode . replace ( /<h3/g , ':::<h3' ) . replace ( /<h2/g , ':::<h2' ) . split ( ':::' ) ; return group . map ( ( fragment ) => { if ( fragment . indexOf ( '<h3' ) !== - 1 ) { return `<div class="van-doc-card">${ fragment }</div>` ; } return fragment ; } ) . join ( '' ) ; } function markdownLinkOpen ( md ) { const defaultRender = md . renderer . rules . link_open ; md . renderer . rules . link_open = ( tokens , idx , options , env , self ) => { const aIndex = tokens [ idx ] . attrIndex ( 'target' ) ; if ( aIndex < 0 ) { tokens [ idx ] . attrPush ( [ 'target' , '_blank' ] ) ; } if ( defaultRender ) { return defaultRender ( tokens , idx , options , env , self ) ; } return self . renderToken ( tokens , idx , options ) ; } ; } function getSiteConfig ( vantConfig ) { const siteConfig = vantConfig . site ; if ( siteConfig . locales ) { return siteConfig . locales [ siteConfig . defaultLang || 'en-US' ] ; } return siteConfig ; } function getTitle ( config ) { let { title } = config ; if ( config . description ) { title += ` - ${ config . description }` ; } return title ; } function getHTMLMeta ( vantConfig ) { const meta = vantConfig . site ?. htmlMeta ; if ( meta ) { return Object . keys ( meta ) . map ( ( key ) => `<meta name="${ key }" content="${ meta [ key ] }">` ) . join ( '\n' ) ; } return '' ; } export function getViteConfigForSiteDev ( ) { setBuildTarget ( 'site' ) ; const vantConfig = getVantConfig ( ) ; const siteConfig = getSiteConfig ( vantConfig ) ; const title = getTitle ( siteConfig ) ; const baiduAnalytics = vantConfig . site ?. baiduAnalytics ; const enableVConsole = isDev ( ) && vantConfig . site ?. enableVConsole ; return { root : SITE_SRC_DIR , plugins : [ vitePluginVue ( { include : [ /\.vue$/ , /\.md$/ ] , } ) , vitePluginMd ( { wrapperClasses : 'van-doc-markdown-body' , transforms : { after : markdownCardWrapper , } , markdownItOptions : { typographer : false , highlight : markdownHighlight , } , markdownItSetup ( md ) { const require = createRequire ( import . meta . url ) ; const { slugify } = require ( 'transliteration' ) ; const markdownItAnchor = require ( 'markdown-it-anchor' ) ; markdownLinkOpen ( md ) ; md . use ( markdownItAnchor , { level : 2 , slugify , } ) ; } , } ) , vitePluginJsx ( ) , injectHtml ( { data : { ... siteConfig , title , description : siteConfig . description , baiduAnalytics , enableVConsole , meta : getHTMLMeta ( vantConfig ) , } , } ) , ] , resolve : { alias : { 'site-mobile-shared' : SITE_MOBILE_SHARED_FILE , 'site-desktop-shared' : SITE_DESKTOP_SHARED_FILE , } , } , server : { host : '0.0.0.0' , } , } ; } export function getViteConfigForSiteProd ( ) { const devConfig = getViteConfigForSiteDev ( ) ; const vantConfig = getVantConfig ( ) ; const outDir = vantConfig . build ?. site ?. outputDir || SITE_DIST_DIR ; const publicPath = vantConfig . build ?. site ?. publicPath || '/' ; return { ... devConfig , base : publicPath , build : { outDir , brotliSize : false , emptyOutDir : true , cssTarget : [ 'chrome53' ] , rollupOptions : { input : { main : join ( SITE_SRC_DIR , 'index.html' ) , mobile : join ( SITE_SRC_DIR , 'mobile.html' ) , } , output : { manualChunks : { 'vue-libs' : [ 'vue' , 'vue-router' ] , } , } , } , } , } ; }