| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- 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 ({ mode }) => 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: {
- rollupOptions: {
- input: entryConfig(),
- output: { //静态资源分类打包
- chunkFileNames: 'js/[name]-[hash].js',
- entryFileNames: 'js/[name]-[hash].js',
- assetFileNames: 'static/[name]-[hash].[ext]',
- // 拆分node_modules包
- manualChunks: (id) => {
- if (id.includes("node_modules")) {
- return id
- .toString()
- .split("node_modules/")[1]
- .split("/")[0]
- .toString();
- }
- }
- }
- }
- },
- define: { //环境变量配置
- 'process.env': {
- 'SERVERURL': mode === 'development' ? '':''
- }
- }
- })
|