Skip to content

Commit

Permalink
chore: vite.config添加获取env的能力
Browse files Browse the repository at this point in the history
  • Loading branch information
sdmu-gaoqi committed Jan 18, 2024
1 parent 2c05a0b commit 57656df
Showing 1 changed file with 55 additions and 51 deletions.
106 changes: 55 additions & 51 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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的参数
}
}
})
)

0 comments on commit 57656df

Please sign in to comment.