From 57656df008930a5c06c14afad78d759f5a67aeee Mon Sep 17 00:00:00 2001 From: gaoqi <1224362143@qq.com> Date: Thu, 18 Jan 2024 16:32:13 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20vite.config=E6=B7=BB=E5=8A=A0=E8=8E=B7?= =?UTF-8?q?=E5=8F=96env=E7=9A=84=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- vite.config.ts | 106 +++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 5018c70..0a8668e 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -2,7 +2,7 @@ import vue from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' import path, { dirname } from 'path' import { fileURLToPath } from 'url' -import { ConfigEnv, UserConfigExport, defineConfig } from 'vite' +import { ConfigEnv, UserConfigExport, defineConfig, loadEnv } from 'vite' import mkcert from 'vite-plugin-mkcert' import postCssPxToRem from 'wa-postcss-pxtorem' import tailwindCss from 'tailwindcss' @@ -12,56 +12,60 @@ const __filename = fileURLToPath(import.meta.url) const __dirname = dirname(__filename) // https://vitejs.dev/config/ -// @ts-ignore -export default defineConfig(({ command }: ConfigEnv): UserConfigExport => { - return { - plugins: [ - vue(), - vueJsx(), - mkcert(), - viteMockServe({ - mockPath: 'mock', - injectFile: - command === 'serve' - ? path.resolve(process.cwd(), 'mock/test/*.ts') - : path.resolve(process.cwd(), 'mock/prod/*.ts'), - localEnabled: command === 'serve', - prodEnabled: true - }) - ], - resolve: { - alias: { - '@': path.resolve(__dirname, 'src'), - hooks: path.resolve(__dirname, 'src/hooks'), - styles: path.resolve(__dirname, 'src/styles'), - pages: path.resolve(__dirname, 'src/pages'), - components: path.resolve(__dirname, 'src/components'), - mocks: path.resolve(__dirname, 'mocks') - } - }, - server: { - https: false, - port: 5500 - }, - css: { - postcss: { - plugins: [ - postCssPxToRem({ - rootValue: 75, - propList: ['*'], - selectorBlackList: ['./to', 'html'], // to开头的不进行转换, - exclude: '/node_modules', - unit: 'wx' - }), - tailwindCss() - ] +export default defineConfig( + // @ts-ignore + ({ command, mode }: ConfigEnv): UserConfigExport => { + const envData = loadEnv(mode, path.resolve(process.cwd(), 'env')) + console.log(envData, 'envData') + return { + plugins: [ + vue(), + vueJsx(), + mkcert(), + viteMockServe({ + mockPath: 'mock', + injectFile: + command === 'serve' + ? path.resolve(process.cwd(), 'mock/test/*.ts') + : path.resolve(process.cwd(), 'mock/prod/*.ts'), + localEnabled: command === 'serve', + prodEnabled: true + }) + ], + resolve: { + alias: { + '@': path.resolve(__dirname, 'src'), + hooks: path.resolve(__dirname, 'src/hooks'), + styles: path.resolve(__dirname, 'src/styles'), + pages: path.resolve(__dirname, 'src/pages'), + components: path.resolve(__dirname, 'src/components'), + mocks: path.resolve(__dirname, 'mocks') + } + }, + server: { + https: false, + port: 5500 }, - preprocessorOptions: { - scss: { - additionalData: '@import "./src/styles/main.scss";' + css: { + postcss: { + plugins: [ + postCssPxToRem({ + rootValue: 75, + propList: ['*'], + selectorBlackList: ['./to', 'html'], // to开头的不进行转换, + exclude: '/node_modules', + unit: 'wx' + }), + tailwindCss() + ] + }, + preprocessorOptions: { + scss: { + additionalData: '@import "./src/styles/main.scss";' + } } - } - }, - envDir: 'env' // 注册进vite的参数 + }, + envDir: 'env' // 注册进vite的参数 + } } -}) +)