-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvite.config.ts
149 lines (136 loc) · 3.74 KB
/
vite.config.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import {ConfigEnv, defineConfig} from 'vite';
import vue from '@vitejs/plugin-vue';
import basicSsl from '@vitejs/plugin-basic-ssl';
import {nodePolyfills} from 'vite-plugin-node-polyfills';
import {createHtmlPlugin} from "vite-plugin-html";
import {resolve} from 'path';
import components from 'unplugin-vue-components/vite';
import autoImport from 'unplugin-auto-import/vite';
import dotenv from 'dotenv';
import { accessSync, readFileSync } from 'fs';
const getPublicBase = () => {
let base = process.env.PUBLIC_HOST_BASE ?? '/';
base = (base.charAt(0) != '/') ? `/${base}` : base;
if (base.charAt(base.length - 1) != '/') {
base = base + '/';
}
return base;
}
const getApiBase = (url: string) => {
if (url == null) {
return 'localhost';
}
const portIndex = url.lastIndexOf(':');
return portIndex > 0 ? url.substring(0, portIndex) : url;
}
const getEnvFile = (mode: string) => {
if (mode != '' && mode !== 'production') {
try {
/// check if mode file exists.
const file = `.env.${mode}`;
accessSync(file);
return file;
} catch (err) {
//
}
}
return '.env';
}
// https://vitejs.dev/config/
export default async function ({ mode }: ConfigEnv) {
const isProduction = mode === 'production';
console.log(`running in mode: ${mode}`);
dotenv.config({ path: getEnvFile(mode) });
const api_base = getApiBase(process.env.VITE_API_BASE as string);
let securityPolicies = [
"default-src 'self'",
"connect-src 'self' " +
[
api_base,
].join(" "),
"font-src 'self' data:",
isProduction ? "style-src 'self'" : "style-src 'self' 'unsafe-inline' 'unsafe-eval'",
];
const publicBasePath = getPublicBase();
console.log(`Deploying to base: ${publicBasePath}`);
return defineConfig({
base: publicBasePath,
resolve: {
alias: [
{ find: /^@\//, replacement: `${resolve(__dirname, './src')}/` },
{ find: /^assets\//, replacement: `${resolve(__dirname, './assets')}/` },
]
},
define: {
"import.meta.env.blade_api_version": '"' + findPackageVersion('@bladelabs/blade-web3.js') + '"',
"import.meta.env.blade_demo_version": '"' + process.env.npm_package_version + '"'
},
plugins: [
nodePolyfills({
include: ["buffer"],
globals: {
Buffer: true
}
}),
createHtmlPlugin({
inject: {
data: {
contentSecurityPolicy: securityPolicies.join("; "),
},
},
}),
vue(),
basicSsl(),
// auto-import listed packages
autoImport({
dts: resolve(__dirname, 'src/auto-imports.d.ts'),
imports: ["vue"]
}),
// auto-import components by naming in template
components({
dirs: [resolve(__dirname, 'src/components'), resolve(__dirname, 'src/views')],
dts: resolve(__dirname, 'src/components.d.ts'),
}),
],
optimizeDeps: {
esbuildOptions: {
define: {
global: "globalThis"
},
},
include: [
"vue",
"@fontsource/montserrat",
"@vueuse/core",
"@bladelabs/blade-web3.js"
]
},
preview: {
port: 5000,
cors: true,
https: true
},
server: {
port: 3001,
cors: true,
https: true,
hmr: {
host: 'localhost',
},
watch: {
ignored: [
resolve(__dirname, "src/auto-imports.d.ts"),
resolve(__dirname, "src/components.d.ts")
],
},
}
});
}
function findPackageVersion(name: string) {
try {
const contents = JSON.parse(readFileSync(process.env.npm_package_json as string).toString());
return contents.dependencies[name];
} catch (err) {
console.warn(`error reading blade version: ${err}`);
}
}