init
This commit is contained in:
92
vite.config.js
Normal file
92
vite.config.js
Normal file
@@ -0,0 +1,92 @@
|
||||
const packageJson = require('./package.json');
|
||||
import vue from '@vitejs/plugin-vue'
|
||||
import vueJsx from '@vitejs/plugin-vue-jsx';
|
||||
import { defineConfig } from 'vite'
|
||||
import path from 'path'
|
||||
import fs from 'fs'
|
||||
|
||||
/** publicDir 复制完成后,从产物中移除 .md(Vite 本身不支持 publicDir ignore) */
|
||||
function excludePublicMarkdown({ publicRoot, outDir }) {
|
||||
function removeMarkdownFromDist() {
|
||||
if (!fs.existsSync(publicRoot)) return
|
||||
|
||||
const stack = [publicRoot]
|
||||
while (stack.length) {
|
||||
const dir = stack.pop()
|
||||
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
||||
const fullPath = path.join(dir, entry.name)
|
||||
if (entry.isDirectory()) {
|
||||
stack.push(fullPath)
|
||||
continue
|
||||
}
|
||||
if (!entry.name.endsWith('.md')) continue
|
||||
|
||||
const distPath = path.join(outDir, path.relative(publicRoot, fullPath))
|
||||
if (fs.existsSync(distPath)) fs.unlinkSync(distPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
name: 'exclude-public-markdown',
|
||||
closeBundle: removeMarkdownFromDist,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// https://vite.dev/config/
|
||||
export default defineConfig(() => {
|
||||
const projectRoot = __dirname;
|
||||
const pagesRoot = path.resolve(projectRoot, 'src/pages');
|
||||
const publicRoot = path.resolve(projectRoot, 'public');
|
||||
const outDir = path.resolve(projectRoot, 'dist');
|
||||
const buildDate = new Date()
|
||||
.toISOString()
|
||||
.slice(0, 10)
|
||||
.replace(/-/g, '');
|
||||
const buildVersion = ["v" + packageJson.version,
|
||||
buildDate].filter(item => !!item).join('-');
|
||||
|
||||
return {
|
||||
define: {
|
||||
__BUILD_VERSION__: JSON.stringify(buildVersion),
|
||||
},
|
||||
// root 为 src/pages 时,默认 public 目录为 src/pages/public;
|
||||
// 显式指向仓库根目录的 public,打包时会复制到 dist 根目录
|
||||
publicDir: publicRoot,
|
||||
plugins: [
|
||||
vue(),
|
||||
vueJsx(),
|
||||
excludePublicMarkdown({ publicRoot, outDir }),
|
||||
],
|
||||
envDir: projectRoot,
|
||||
build: {
|
||||
outDir,
|
||||
emptyOutDir: true,
|
||||
rollupOptions: {
|
||||
input: {
|
||||
index: path.resolve(pagesRoot, 'index.html'),
|
||||
},
|
||||
output: {
|
||||
manualChunks(id) {
|
||||
if (id.includes('node_modules')) {
|
||||
return 'vendor';
|
||||
}
|
||||
return "app";
|
||||
},
|
||||
},
|
||||
}
|
||||
},
|
||||
base: './',
|
||||
root: pagesRoot,
|
||||
server: {
|
||||
open: true,
|
||||
cors: true,
|
||||
},
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': path.resolve(__dirname, './src'),
|
||||
},
|
||||
},
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user