forked from betaflight/betaflight-configurator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.js
118 lines (113 loc) · 3.71 KB
/
vite.config.js
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
/// <reference types="vitest" />
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue2";
import path from "node:path";
import { readFileSync } from "node:fs";
import copy from "rollup-plugin-copy";
import pkg from './package.json';
import * as child from 'child_process';
import { VitePWA } from "vite-plugin-pwa";
const commitHash = child.execSync('git rev-parse --short HEAD').toString();
function serveFileFromDirectory(directory) {
return (req, res, next) => {
const filePath = req.url.replace(new RegExp(`^/${directory}/`), "");
const absolutePath = path.resolve(process.cwd(), directory, filePath);
try {
const fileContents = readFileSync(absolutePath, "utf-8");
res.end(fileContents);
} catch (e) {
// If file not found or any other error, pass to the next middleware
next();
}
};
}
/**
* This is plugin to work around the file structure required nwjs.
* In future this can be dropped if we restructure folder structure
* to be more web friendly.
* @returns {import("vite").Plugin}
*/
function serveLocalesPlugin() {
return {
name: "serve-locales",
configureServer(server) {
return () => {
server.middlewares.use((req, res, next) => {
if (req.url.startsWith("/locales/")) {
serveFileFromDirectory("locales")(req, res, next);
} else if (req.url.startsWith("/resources/")) {
serveFileFromDirectory("resources")(req, res, next);
} else {
next();
}
});
};
},
};
}
export default defineConfig({
define: {
'__APP_VERSION__': JSON.stringify(pkg.version),
'__APP_PRODUCTNAME__': JSON.stringify(pkg.productName),
'__APP_REVISION__': JSON.stringify(commitHash),
},
test: {
// NOTE: this is a replacement location for karma tests.
// moving forward we should colocate tests with the
// code they test.
include: ["test/**/*.test.{js,mjs,cjs}"],
environment: "jsdom",
setupFiles: ["test/setup.js"],
root: ".",
},
plugins: [
vue(),
serveLocalesPlugin(),
copy({
targets: [
{ src: ["locales", "resources", "src/tabs", "src/images"], dest: "src/dist" },
],
hook: "writeBundle",
}),
VitePWA({
registerType: 'autoUpdate',
workbox: {
globPatterns: ['**/*.{js,css,html,ico,png,svg,json,mcm}'],
// 5MB
maximumFileSizeToCacheInBytes: 5 * 1024 * 1024,
},
includeAssets: ['favicon.ico', 'apple-touch-icon.png'],
manifest: {
name: pkg.productName,
short_name: pkg.productName,
description: pkg.description,
theme_color: '#ffffff',
icons: [
{
src: '/images/pwa/pwa-192-192.png',
sizes: '192x192',
type: 'image/png',
},
{
src: '/images/pwa/pwa-512-512.png',
sizes: '512x512',
type: 'image/png',
},
],
},
}),
],
root: "./src",
resolve: {
alias: {
"/src": path.resolve(process.cwd(), "src"),
"vue": path.resolve(__dirname, "node_modules/vue/dist/vue.esm.js"),
},
},
server: {
port: 8000,
},
preview: {
port: 8080,
},
});