electron-vite-vue/vite.config.ts

72 lines
1.9 KiB
TypeScript
Raw Normal View History

2021-02-17 22:27:40 +08:00
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
2020-11-08 18:43:37 +08:00
import { join, sep } from 'path'
2020-08-16 20:42:52 +08:00
import dotenv from 'dotenv'
dotenv.config({ path: join(__dirname, '.env') })
2020-08-31 09:50:57 +08:00
const root = join(__dirname, 'src/render')
2020-08-16 20:42:52 +08:00
2021-02-17 22:27:40 +08:00
// https://vitejs.dev/config/
export default defineConfig({
plugins: [vue()],
2020-08-31 09:50:57 +08:00
root,
2021-02-17 22:27:40 +08:00
// base: './',
server: {
port: +process.env.PORT,
},
resolve: {
alias: {
'@root': __dirname,
'@assets': join(__dirname, 'src/render/assets'),
'@components': join(__dirname, 'src/render/components'),
'@lib': join(__dirname, 'src/render/lib'),
'@utils': join(__dirname, 'src/render/utils'),
'@views': join(__dirname, 'src/render/views'),
},
2020-08-31 09:50:57 +08:00
},
optimizeDeps: {
2020-09-02 09:19:07 +08:00
// 这里不加也没事,用 require 的形式就能避开 import 被编译成 /@modules/fs?import
// allowNodeBuiltins: ['electron-is-dev', 'electron-store', 'electron']
2020-08-31 09:50:57 +08:00
},
2021-02-17 22:27:40 +08:00
build: {
outDir: join(__dirname, 'dist/render'),
rollupOptions: {
output: {
// format: 'commonjs',
},
external: [
'crypto',
'assert',
'fs',
'util',
'os',
'events',
'child_process',
'http',
'https',
'path',
'electron',
],
plugins: [
{
name: '@rollup/plugin-cjs2esm',
transform(code, filename) {
if (filename.includes(`${sep}node_modules${sep}`)) {
return code
}
2020-10-05 21:34:33 +08:00
2021-02-17 22:27:40 +08:00
const cjsRegexp = /(const|let|var)[\n\s]+(\w+)[\n\s]*=[\n\s]*require\(["|'](.+)["|']\)/g
const res = code.match(cjsRegexp)
if (res) {
// const Store = require('electron-store') -> import Store from 'electron-store'
code = code.replace(cjsRegexp, `import $2 from '$3'`)
}
return code
},
}
],
},
2020-09-25 11:39:39 +08:00
},
2021-02-17 22:27:40 +08:00
})
2020-08-16 20:42:52 +08:00