| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { defineConfig } from 'vite'
- import { createVuePlugin } from 'vite-plugin-vue2'
- import viteCompression from 'vite-plugin-compression'
- import path from 'path'
- import glob from 'glob'
- import fs from 'fs'
- // 多页面入口配置
- const entryConfig = function () {
- const viteconfig = {}
- // 含有.ts的文件
- const entryFiles = glob.sync('./src/pages/*/*.js')
- // 获取模板
- const temp = fs.readFileSync('./index.html')
- // 模板增加ts入口
- entryFiles.forEach((entry) => {
- const projectName = entry.split('/')[3]
- try {
- fs.accessSync(`./${projectName}.html`)
- } catch (error) {
- // 重构模板
- const html = temp.toString().replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, `<script type="module" src="${entry}"></script>`)
- fs.writeFile(`./${projectName}.html`, html, (err) => {
- if (err) console.error(err)
- })
- }
- viteconfig[projectName] = path.resolve(__dirname, `./${projectName}.html`)
- })
- return viteconfig
- }
- export default defineConfig({
- resolve: {
- alias: {
- /*
- 路径别名
- 若为文件系统路径必须是绝对路径的形式,否则将以别名原样呈现,不会解析为文件系统路径路径
- */
- '@': path.resolve(__dirname, './src')
- }
- },
- plugins: [createVuePlugin(), viteCompression()],
- server: {
- host: '0.0.0.0',
- port: 5000,
- open: true,
- strictPort: false,
- https: false
- // 反向代理
- // proxy: {
- // '/admin': {
- // target: 'http://api.vite-admin.com',
- // changeOrigin: true,
- // rewrite: (path) => path.replace(/^\/admin/, '')
- // },
- // }
- },
- build: {
- // 生成环境移除console
- terserOptions: {
- compress: {
- drop_console: true,
- drop_debugger: true
- }
- },
- rollupOptions: {
- input: entryConfig()
- },
- reportCompressedSize: false,
- assetsDir: 'static'
- }
- })
|